package reconciliation;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/***
* jeepay-plus  生成对账测试数据
*
* @author zx
*
* @date 2022/9/16 10:38
*/
public class BillTestData {

    static String ISV_NO = "V1648691605";
    static String MCH_NO = "M1648693513";
    static String MCH_APPID = "62451114500860516e34305c";
    static String IF_CODE = "shengpay";
    static String CHANNEL_MCH_NO = "39301087";

    public static void main(String[] args) throws Exception {

        // 订单sql集合
        List<String> sqlList = new ArrayList<>();

        // 渠道账单集合
        List<JSONObject> channelBillList = new ArrayList<>();

        Date billDate = DateUtil.beginOfDay(DateUtil.yesterday());

        String payOrderSqlModel = "INSERT INTO t_pay_order VALUES ('%s', '%s', '%s', NULL, NULL, '%s', '笑果', 2, 1001, '[默认]笑果', NULL, NULL, NULL, NULL, " +
                "'%s', '%s', 'WX_BAR', 'WECHAT', %d, '单笔费率：0.50', %d, 'CNY', %d, 1, '0:0:0:0:0:0:0:1', '接口调试[M1648693513商户联调]', '接口调试[M1648693513商户联调]', " +
                "NULL, 'ooIeqsynvcGLKQTomxRRX1plvSj4', " +
                "'%s', NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, 'http://192.168.0.113:9218/api/anon/paytestNotify/payOrder', NULL, NULL, NULL, 100034, " +
                "'2022-09-16 13:11:28', '2022-5-13 11:11:30', '%s', '%s')";

        Long feeAmount = 0L;

        // 平账数据
        for (int i = 1; i <= 10000; i++) {

            // 替换顺序：支付订单号，商户号，服务商号，商户应用appId，商户订单号，支付接口ifCode，订单金额，支付状态，渠道订单号
            sqlList.add(String.format(payOrderSqlModel, "PPPPPPPPPPPPPPPP" + i, MCH_NO, ISV_NO, MCH_APPID, "MMMMMMMMMMMMMMMM" + i,
                    IF_CODE, i, feeAmount, 2, "CCCCCCCCCCCCCCCC" + i, DateUtil.formatDateTime(billDate), DateUtil.formatDateTime(billDate)));

            JSONObject bill = new JSONObject();
            bill.put("billType", "pay");                                                  // pay：支付, refund：退款
            bill.put("ifCode", IF_CODE);                                              // 支付接口代码
            bill.put("channelMchNo", CHANNEL_MCH_NO);               // 渠道商户号
            bill.put("billDate", billDate);                                              // 对账日期
            bill.put("orderId", "PPPPPPPPPPPPPPPP" + i);                                         // 渠道商户订单号
            bill.put("channelAmount", i);                                            // 交易金额，单位分
            bill.put("channelFeeAmount", 0);                                     // 渠道手续费，单位分
            bill.put("channelOrderNo", "CCCCCCCCCCCCCCCC" + i);                  // 渠道订单号
            bill.put("channelState", 2);
            bill.put("channelSuccessAt", "2022-09-16 13:11:28");
            channelBillList.add(bill);
        }

        // 本地多帐类型
        // for (int i = 20001; i <= 20055; i++) {
        //     sqlList.add(String.format(payOrderSqlModel, "PPPPPPPPPPPPPPPP" + i, MCH_NO, ISV_NO, MCH_APPID, "MMMMMMMMMMMMMMMM" + i,
        //             IF_CODE, i, feeAmount, 2, "CCCCCCCCCCCCCCCC" + i, DateUtil.formatDateTime(billDate), DateUtil.formatDateTime(billDate)));
        // }

        // 渠道多帐类型
        // for (int i = 30001; i <= 30088; i++) {
        //     JSONObject bill = new JSONObject();
        //     bill.put("billType", "pay");                                                  // pay：支付, refund：退款
        //     bill.put("ifCode", IF_CODE);                                              // 支付接口代码
        //     bill.put("channelMchNo", CHANNEL_MCH_NO);               // 渠道商户号
        //     bill.put("billDate", billDate);                                              // 对账日期
        //     bill.put("orderId", "PPPPPPPPPPPPPPPP" + i);                                         // 渠道商户订单号
        //     bill.put("channelAmount", i);                                            // 交易金额，单位分
        //     bill.put("channelFeeAmount", 0);                                     // 渠道手续费，单位分
        //     bill.put("channelOrderNo", "CCCCCCCCCCCCCCCC" + i);                  // 渠道订单号
        //     bill.put("channelState", 2);
        //     bill.put("channelSuccessAt", "2022-09-16 13:11:28");
        //     channelBillList.add(bill);
        // }

        // 金额不一致
        for (int i = 0; i < channelBillList.size(); i++) {
            JSONObject jsonObject = channelBillList.get(i);
            if (jsonObject.getLong("channelAmount") <= 27) {
                jsonObject.put("channelAmount", i * 2);
            }
        }


        String projectPath = System.getProperty("user.dir");  //获取当前项目的 文件夹地址
        String resourcePath = projectPath + File.separator+ "jeepay-components/jeepay-components-3rd/src/main/resources/channel/shengpay/";

        File sqlFile = new File(resourcePath + "pay_order.sql");
        File billFile = new File(resourcePath + "bill.json");


        // 写入文件
        writeFile(sqlFile, "UTF-8", CollUtil.join(sqlList, ";\n"));
        writeFile(billFile, "UTF-8", JSON.toJSONString(channelBillList, false));

        System.out.println("完成！ ");

    }

    /** 将指定内容写入 到对应的文件中  **/
    public static void writeFile(File needWriteFile, String charset, String writeText) throws Exception {

        FileOutputStream fos = new FileOutputStream(needWriteFile);
        OutputStreamWriter osw = new OutputStreamWriter(fos, charset);
        BufferedWriter writer = new BufferedWriter (osw);

        try {
            writer.write(writeText);
            writer.flush();

        }catch (Exception e){
            throw e;
        }finally {
            if(writer != null) writer.close();
            if(osw != null) writer.close();
            if(fos != null) writer.close();
        }
    }

}
