package com.izouma.nineth.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huifu.adapay.core.exception.BaseAdaPayException;
import com.huifu.adapay.model.Refund;
import com.izouma.nineth.ApplicationTests;
import com.izouma.nineth.domain.AdaTrade;
import com.izouma.nineth.repo.GiftOrderRepo;
import com.izouma.nineth.repo.OrderRepo;
import com.izouma.nineth.utils.SnowflakeIdWorker;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;

public class AdapayServiceTest extends ApplicationTests {
    @Autowired
    private OrderRepo         orderRepo;
    @Autowired
    private GiftOrderRepo     giftOrderRepo;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    private final String appId = "app_0e8d3acb-3d95-4ebb-8445-e470c378a787";

    @Test
    public void queryRefund() throws ExecutionException, InterruptedException {

        ForkJoinPool customThreadPool = new ForkJoinPool(100);
        customThreadPool.submit(() -> {

            List<AdaTrade> list = EasyExcel.read("/Users/drew/Downloads/merTransDetail_0284905900625472_20220212_20220212_1644980532.xlsx")
                    .head(AdaTrade.class).sheet().doReadSync();
            list = list.parallelStream().filter(adaTrade -> {
                return adaTrade.get交易金额().equals("19.80")
                        && orderRepo.findByTransactionId(adaTrade.get支付流水号()) == null
                        && giftOrderRepo.findByTransactionId(adaTrade.get支付流水号()) == null;
            }).collect(Collectors.toList());

            EasyExcel.write("/Users/drew/Downloads/1.xlsx", AdaTrade.class).sheet("模板").doWrite(list);
            System.out.println(list.size());


        }).get();

    }

    @Test
    public void refund() {
        List<AdaTrade> list = EasyExcel.read("/Users/drew/Downloads/1.xlsx")
                .head(AdaTrade.class).sheet().doReadSync();
        list.parallelStream().forEach(adaTrade -> {
            Map<String, Object> refundParams = new HashMap<>();
            refundParams.put("refund_amt", adaTrade.get交易金额());
            refundParams.put("refund_order_no", snowflakeIdWorker.nextId() + "");
            try {
                Map<String, Object> response = Refund.create(adaTrade.get支付流水号(), refundParams);
            } catch (BaseAdaPayException e) {
                e.printStackTrace();
            }
        });
    }

    @Test
    public void verifyRefund() throws BaseAdaPayException {
        List<AdaTrade> list = EasyExcel.read("/Users/drew/Downloads/1.xlsx")
                .head(AdaTrade.class).sheet().doReadSync();
        list = list.parallelStream().filter(adaTrade -> {
            boolean success = false;
            try {
                Map<String, Object> refundParams = new HashMap<>(2);
                refundParams.put("payment_id", adaTrade.get支付流水号());
                Map<String, Object> refund = Refund.query(refundParams);
                JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(refund));
                if ("S".equals(jsonObject.getJSONArray("refunds")
                        .getJSONObject(jsonObject.getJSONArray("refunds").size() - 1).getString("trans_status"))) {
                    success = true;
                }
            } catch (Exception e) {
            }
            return !success;
        }).collect(Collectors.toList());
        EasyExcel.write("/Users/drew/Downloads/2.xlsx", AdaTrade.class).sheet("模板").doWrite(list);
    }

}