Skip to content

Commit

Permalink
- 增加本地(sqlite)存储方式
Browse files Browse the repository at this point in the history
- 对一些模组端的优化
  • Loading branch information
meteorOSS committed Dec 13, 2023
1 parent 8bc6fb0 commit 0b23c8a
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 9 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
# 关于

![bs](https://bstats.org/signatures/bukkit/AifadianPay.svg)
(实时使用统计)

将爱发电支付集成到MC服务器中,你可以轻松的创建商品让玩家捐赠服务器

注意: 3.0版本几乎重构,如果使用了这之前的插件版本.为了防止已处理订单污染数据库,需将shopitems中的商品名更换 (不要和之前的同名)
注意: 3.0版本几乎重构,如果使用了这之前的插件版本.为了防止已处理订单污染数据库,需将shopitems.yml中的商品名更换 (不要和之前的同名)
如要保留先前的记录,可以参考 [导入历史订单](https://github.com/meteorOSS/AifadianPay-plugin/wiki/Home/_edit)

在开始之前,请先注册一个 [爱发电](https://afdian.net) 账号
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.meteor</groupId>
<artifactId>AifadianPay</artifactId>
<version>3.1-SNAPSHOT</version>
<version>3.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>AifadianPay</name>
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/meteor/aifadianpay/AifadianPay.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.meteor.aifadianpay.listener.PlayerListener;
import com.meteor.aifadianpay.storage.IStorage;
import com.meteor.aifadianpay.storage.sub.MysqlStorage;
import com.meteor.aifadianpay.storage.sub.SqliteStorage;
import com.meteor.aifadianpay.util.BaseConfig;
import com.meteor.api.hook.PlaceholderHook;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -85,7 +86,9 @@ public void onDisable() {
* 初始化存储
*/
private void initStorage(){
if(getConfig().isBoolean("mysql-info.enable")) iStorage = new MysqlStorage(this);
if(getConfig().getBoolean("mysql-info.enable",false))
this.iStorage = new MysqlStorage(this);
else this.iStorage = new SqliteStorage(this);
QueryOrderTask.init(iStorage);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ public void success(QueryOrderResponse queryOrderResponse,boolean isSave) {

if(orders!=null){
if(AifadianPay.debug) {
boolean test = iStorage==null;
AifadianPay.INSTANCE.getLogger().info("storage "+test);
AifadianPay.INSTANCE.getLogger().info("本页订单数 " + orders.getOrderList().size());
}
orders.getOrderList().forEach(order -> {
if(AifadianPay.debug){
AifadianPay.INSTANCE.getLogger().info("handler"+order.getOutTradeNo());
}
iStorage.handeOrder(order,isSave);
});
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/meteor/aifadianpay/command/SubCmd.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.meteor.aifadianpay.command;

import com.google.common.collect.ImmutableMap;
import com.meteor.aifadianpay.util.BaseConfig;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class SubCmd {
protected JavaPlugin plugin;
Expand All @@ -32,12 +35,13 @@ public List<String> getTab(final Player p, final int i, final String[] args) {
public abstract void perform(final CommandSender p0, final String[] p1);
public void execute(CommandSender sender, String[] args){
if(this.playersOnly()&&!(sender instanceof Player)){
// sender.sendMessage(Store.state.getMessageBox().getMessage(null,"message.playerOnly"));
sender.sendMessage(BaseConfig.STORE.getMessageBox().getMessage(null,"message.only-player"));
return;
}
if (!hasPerm(sender)) {
// sender.sendMessage(Store.state.getMessageBox().getMessage(ImmutableMap.<String,String>builder().put("@perm@",getPermission()).build(),
// "message.no-perm"));
Map<String,String> prams = new HashMap<>();
prams.put("@permission@",getPermission());
sender.sendMessage(BaseConfig.STORE.getMessageBox().getMessage(prams,"message.no-perm"));
return;
}
this.perform(sender,args);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/meteor/aifadianpay/storage/IStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

public interface IStorage {



/**
* 处理订单
*/
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/meteor/aifadianpay/storage/sub/MysqlStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ public class MysqlStorage implements IStorage {


public MysqlStorage(AifadianPay plugin){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
plugin.getLogger().info("无法加载数据库驱动,请反馈给开发者");
return;
}
}
this.plugin = plugin;
this.fastMySQLStorage = new FastMySQLStorage(plugin,plugin.getConfig().getConfigurationSection("mysql-info"));
this.connect();
Expand Down Expand Up @@ -128,7 +139,6 @@ public void handeOrder(Order order,boolean isSave) {
});
}
}

}

}
Expand All @@ -137,7 +147,7 @@ public void handeOrder(Order order,boolean isSave) {
public int queryPlayerDonate(String p) {
PreparedStatement preparedStatement = null;
try {
String sql = "select sum(price) as count from "+ORDER_TABLE+" where remark = ?";
String sql = "select sum(cast(`price` as DECIMAL(10,2))) as count from "+ORDER_TABLE+" where remark = ?";
preparedStatement = fastMySQLStorage.getConnection().prepareStatement(sql);
preparedStatement.setString(1,p);
ResultSet resultSet = preparedStatement.executeQuery();
Expand Down
185 changes: 185 additions & 0 deletions src/main/java/com/meteor/aifadianpay/storage/sub/SqliteStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package com.meteor.aifadianpay.storage.sub;

import com.meteor.aifadianpay.AifadianPay;
import com.meteor.aifadianpay.afdian.response.Order;
import com.meteor.aifadianpay.afdian.response.SkuDetail;
import com.meteor.aifadianpay.api.event.SendOutGoodsEvent;
import com.meteor.aifadianpay.data.ShopItem;
import com.meteor.aifadianpay.filter.FilterManager;
import com.meteor.aifadianpay.mysql.data.KeyValue;
import com.meteor.aifadianpay.storage.IStorage;
import com.meteor.aifadianpay.util.BaseConfig;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.sql.*;
import java.util.Arrays;
import java.util.List;

public class SqliteStorage implements IStorage {


private final String ORDER_TABLE = "AIFADIAN_ORDERS_0";
private final String SKU_DETAIL_TABLE = "AIFADIAN_SKUDETAIL_0";

private AifadianPay plugin;
private Connection connection;


private void connect(){
try {
Class.forName("org.sqlite.JDBC");
this.connection = DriverManager.getConnection("jdbc:sqlite:"+plugin.getDataFolder().getPath()+"/data.db");

String orders_column = "CREATE TABLE IF NOT EXISTS AIFADIAN_ORDERS_0 ("
+ "out_trade_no varchar(40) PRIMARY KEY,"
+ "remark varchar(100),"
+ "user_id varchar(40),"
+ "plan_title varchar(40),"
+ "redeem_id varchar(40),"
+ "price varchar(40),"
+ "insert_time bigint"
+ ")";

Statement statement = connection.createStatement();

String skudetail_column = "CREATE TABLE IF NOT EXISTS AIFADIAN_SKUDETAIL_0 ("
+ "out_trade_no varchar(40),"
+ "sku_id varchar(40),"
+ "price varchar(40),"
+ "name varchar(40),"
+ "count int"
+ ")";
statement.execute(orders_column);
statement.execute(skudetail_column);
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}


public SqliteStorage(AifadianPay plugin){
this.plugin = plugin;
this.connect();;
}

public boolean isExistOrder(String no){
PreparedStatement preparedStatement = null;
ResultSet resultSet;
resultSet = null;
try {
preparedStatement = this.connection.prepareStatement("select * from " + ORDER_TABLE + " where out_trade_no = ?");
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) return true;
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
try {
preparedStatement.close();
resultSet.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return false;
}


public void insertOrder(Order order) {

String sql = "INSERT INTO AIFADIAN_ORDERS_0 (out_trade_no, remark, user_id, plan_title, redeem_id,price,insert_time) VALUES (?, ?, ?, ?,?,?,?)";

try (
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1,order.getOutTradeNo());
statement.setString(2,order.getRemark());
statement.setString(3,order.getUserId());
statement.setString(4,order.getPlanTitle());
statement.setString(5,order.getRedeemId());
statement.setString(6,order.getTotalAmount());
statement.setLong(7,System.currentTimeMillis());
statement.executeUpdate();
} catch (SQLException e) {
}
}


public void insertSkudetail(Order order,SkuDetail skuDetail) {

String sql = "INSERT INTO AIFADIAN_SKUDETAIL_0 (out_trade_no, sku_id, price, `name`, `count`) VALUES (?, ?, ?, ?, ?)";

try (
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1,order.getOutTradeNo());
statement.setString(2,skuDetail.getSkuId());
statement.setString(3,order.getTotalAmount());
statement.setString(4,skuDetail.getName());
statement.setInt(5,skuDetail.getCount());
statement.executeUpdate();
} catch (SQLException e) {
}
}

@Override
public void handeOrder(Order order, boolean b) {
List<Order> orders = FilterManager.meet(Arrays.asList(order));
if(!orders.isEmpty()){
Order handleOrder = orders.get(0);
Player playerExact = Bukkit.getPlayerExact(handleOrder.getRemark());

if(playerExact!=null&&b){


if(!isExistOrder(handleOrder.getOutTradeNo())){

/***
* 插入已处理订单表
*/
this.insertOrder(handleOrder);


/***
* 型号处理
*/
for (SkuDetail skuDetail : handleOrder.getSkuDetail()) {
this.insertSkudetail(handleOrder,skuDetail);
}

/**
* 发货
*/
ShopItem shopItem = BaseConfig.STORE.getShopItemMap().get(handleOrder.getPlanTitle());
Bukkit.getScheduler().runTask(plugin,()->{
SendOutGoodsEvent sendOutGoodsEvent = new SendOutGoodsEvent(playerExact,shopItem,handleOrder.getOutTradeNo(),handleOrder.getSkuDetail(),handleOrder);
Bukkit.getServer().getPluginManager().callEvent(sendOutGoodsEvent);
});
}
}
}
}

@Override
public int queryPlayerDonate(String p) {
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
String sql = "select sum(`price`) as count from "+ORDER_TABLE+" where remark = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,p);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) return resultSet.getInt("sum");
} catch (Throwable e) {
throw new RuntimeException(e);
}finally {
try {
preparedStatement.close();
resultSet.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return 0;
}
}
1 change: 1 addition & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ token: ''
# 数据库信息 (推荐启用)
mysql-info:
ip: '127.0.0.1'
enable: false
port: 3306
param: 'useSsl=false'
user: 'root'
Expand Down

0 comments on commit 0b23c8a

Please sign in to comment.