package com.cpop.oam.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cpop.api.cloudDb.core.constant.CloudDbUrl;
import com.cpop.core.utils.SpringUtils;
import com.cpop.pay.framewok.handler.wxPay.WxPayHandler;
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.mybatisflex.core.datasource.DataSourceKey;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author DB
 * @version 1.0.0
 * @since 2024-02-20 11:29
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles(profiles = {"prod","core","pay"})
public class CpopEasyLearnDataAnalyseTest{

    @Autowired
    private WxPayHandler wxPayHandler;

    /**
     * 查询先学后付数据
     * @author DB
     * @since 2024/2/20
     */
    @Test
    public void insertLearnNowPayLaterData() throws WxPayException {
        try {
            //查询旧表
            DataSourceKey.use("jambox");
            List<Row> list = DbChain.table("j_wx_pay_profit_sharing")
                    .select("sub_mch_id")
                    .select("transaction_id")
                    .select("rate")
                    .select("total_amount")
                    .select("sharing_amount")
                    .select("out_order_no")
                    .select("create_time")
                    .from("j_wx_pay_profit_sharing")
                    .where("create_time >= ?", "2024-04-08 19:00:00")
                    //.between("create_time", "2023-04-23 00:00:00","2023-11-13 22:00:00")
                    .list();
            List<Row> insertList = new ArrayList<>();
            list.forEach(item->{
                WxPayService wxPayService = wxPayHandler.getWxPayService(null, item.getString("subMchId"));
                try {
                    WxPayOrderQueryResult wxPayOrderQueryResult = wxPayService.queryOrder(item.getString("transactionId"), null);
                    String[] split = wxPayOrderQueryResult.getAttach().split("\\|");
                    if (split.length > 1) {
                        Row row = Row.ofKey(RowKey.AUTO);
                        row.set("amount",item.getInt("totalAmount"));
                        row.set("create_time",item.getLocalDateTime("createTime"));
                        row.set("rate",item.getDouble("rate"));
                        row.set("sharing_amount",item.getInt("sharingAmount"));
                        row.set("sharing_order_id",item.getString("outOrderNo"));
                        row.set("transaction_id",item.getString("transactionId"));
                        row.set("detail_order_id",split[2]);
                        JSONObject jsonBody = new JSONObject();
                        jsonBody.put("_type", "merchantPayInfo");
                        jsonBody.put("merchant_plan_detail_no", split[2]);
                        JSONObject result = SpringUtils.getBean(RestTemplate.class).postForObject(CloudDbUrl.COMMON_USE_URL, jsonBody, JSONObject.class);
                        if (result.getBoolean("success")) {
                            row.set("brand_name", result.getJSONObject("data").getString("brandName"));
                            row.set("store_name", result.getJSONObject("data").getString("storeName"));
                            row.set("customer_name", result.getJSONObject("data").getString("customerName"));
                            row.set("phone", result.getJSONObject("data").getString("customerPhone"));
                        }
                        insertList.add(row);
                    }
                } catch (WxPayException e) {
                    throw new RuntimeException(e);
                }
            });
            Db.insertBatch("data_analyse",insertList);
        } finally {
            DataSourceKey.clear();
        }
    }
}
