package com.cpop.mall.web;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.cpop.core.utils.uuid.IdUtils;
import com.cpop.pay.framewok.config.wxPay.WxPayProperties;
import com.github.binarywang.wxpay.bean.profitsharing.request.ProfitSharingQueryRequest;
import com.github.binarywang.wxpay.bean.profitsharing.request.ProfitSharingReceiverRequest;
import com.github.binarywang.wxpay.bean.profitsharing.request.ProfitSharingReturnRequest;
import com.github.binarywang.wxpay.bean.profitsharing.request.ProfitSharingUnfreezeRequest;
import com.github.binarywang.wxpay.bean.profitsharing.result.ProfitSharingQueryResult;
import com.github.binarywang.wxpay.bean.profitsharing.result.ProfitSharingV3Result;
import com.github.binarywang.wxpay.bean.request.WxPayOrderReverseRequest;
import com.github.binarywang.wxpay.bean.request.WxPayRefundV3Request;
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult;
import com.github.binarywang.wxpay.bean.result.WxPayRefundV3Result;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.Map;

/**
 * @author DB
 * @createTime 2023/11/01 18:15
 * @description 微信支付
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CpopWxPayTests {

    @Autowired
    private WxPayService wxPayService;

    @Autowired
    private WxPayProperties wxPayProperties;

    /**
     * @descriptions 微信支付退款
     * @author DB
     * @date 2023/11/01 18:20
     * @return: void
     */
    @Test
    public void refund() throws WxPayException {
        //Brand brand = SpringUtils.getBean(BrandService.class).getById("75140168047210496");
        //Order order = SpringUtils.getBean(OrderService.class).getById("78954327589658624");
        WxPayRefundV3Request request = new WxPayRefundV3Request();
        WxPayRefundV3Request.Amount amount = new WxPayRefundV3Request.Amount();
        //退款金额(单位分)
        //int refund = order.getTotalAmount().scaleByPowerOfTen(2).intValue();
        int refund = 1;
        amount.setRefund(refund)
                .setTotal(refund)
                .setCurrency("CNY");
        request.setTransactionId("4200002123202402194880609914")
                //.setOutTradeNo("1000000000202401121937112562856")
                //.setTransactionId(order.getOutOrderNo())
                //.setOutTradeNo(order.getId())
                .setNotifyUrl(wxPayProperties.getNotifyRefund())
                .setOutRefundNo(IdUtil.getSnowflakeNextIdStr())
                .setSubMchid("1618925571")
                .setReason("先学后付测试退款")
                .setAmount(amount);
        WxPayRefundV3Result result = wxPayService.refundV3(request);
        System.out.println(result);
    }

    /**
     * @descriptions 分账退款
     * @author DB
     * @date 2023/11/03 9:57
     * @return: void
     */
    @Test
    public void profitSharingRefund() throws WxPayException {
        ProfitSharingReturnRequest profitSharingReturnRequest = new ProfitSharingReturnRequest();
        //ProfitSharing profitSharing = SpringUtils.getBean(ProfitSharingService.class).getById("77860920238751744");
        //profitSharingReturnRequest.setOrderId(profitSharing.getOutProfitSharingId());
        //profitSharingReturnRequest.setOutReturnNo(profitSharing.getId());
        profitSharingReturnRequest.setOrderId("30001803972024020160326538828");
        profitSharingReturnRequest.setOutReturnNo(IdUtil.getSnowflakeNextIdStr());
        profitSharingReturnRequest.setDescription("分账退款");
        profitSharingReturnRequest.setSubMchId("1661323640");
        profitSharingReturnRequest.setReturnAccount("1618884922");
        profitSharingReturnRequest.setReturnAccountType("MERCHANT_ID");
        //profitSharingReturnRequest.setAmount(profitSharing.getAmount());
        profitSharingReturnRequest.setReturnAmount(504);
        wxPayService.getProfitSharingService().profitSharingReturn(profitSharingReturnRequest);
    }

    /**
     * 取消订单
     * @throws WxPayException
     */
    @Test
    public void reverse() throws WxPayException {
        WxPayOrderReverseRequest wxPayOrderReverseRequest = new WxPayOrderReverseRequest();
        //wxPayOrderReverseRequest.setOutTradeNo("97945902236385280");
        wxPayOrderReverseRequest.setOutTradeNo("1732752269107220540");
        wxPayOrderReverseRequest.setSubMchId("1661323640");
        wxPayService.reverseOrder(wxPayOrderReverseRequest);
    }

    /**
     * @descriptions 添加分账接收方
     * @author DB
     * @date 2023/11/03 10:17
     * @return: void
     */
    @Test
    public void addReceiver() throws WxPayException {
        //固定商户信息
        Map<String, Object> mapReceiver = new HashMap<>(4);
        mapReceiver.put("type", "MERCHANT_ID");
        mapReceiver.put("account", "1618884922");
        mapReceiver.put("relation_type", "SERVICE_PROVIDER");
        mapReceiver.put("name","果酱盒子");
        //添加分账接收方
        ProfitSharingReceiverRequest profitSharingReceiver = new ProfitSharingReceiverRequest();
        profitSharingReceiver.setReceiver(JSONObject.toJSONString(mapReceiver));
        WxPayConfig config = wxPayService.getConfig();
        config.setSubMchId("1618925571");
        wxPayService.getProfitSharingService().addReceiver(profitSharingReceiver);
    }

    /**
     * @descriptions 获取订单信息
     * @author DB
     * @date 2023/11/17 17:14
     * @return: void
     */
    @Test
    public void getOrderInfo() throws WxPayException {
        WxPayConfig config = wxPayService.getConfig();
        config.setSubMchId("1661323640");
        WxPayOrderQueryResult wxPayOrderQueryResult = wxPayService.queryOrder("4200002096202401188599394121", null);
        System.out.println(JSONObject.toJSONString(wxPayOrderQueryResult));
    }

    /**
     * @descriptions  查询分账结果
     * @author DB
     * @date 2023/11/20 12:35
     * @return: void
     */
    @Test
    public void getSharingInfo() throws WxPayException {
        ProfitSharingQueryRequest profitSharingQueryRequest = new ProfitSharingQueryRequest();
        profitSharingQueryRequest.setOutOrderNo("1736629608413073412").setTransactionId("4200002096202401188599394121");
        profitSharingQueryRequest.setSubMchId("1661323640");
        ProfitSharingQueryResult result = wxPayService.getProfitSharingService().profitSharingQuery(profitSharingQueryRequest);
        System.out.println(result.getResultCode());
    }

    /**
     * @descriptions 查询分账结果
     * @author DB
     * @date 2023/11/17 17:56
     * @return: void
     */
    @Test
    public void getProfitSharingResult() throws WxPayException {
        ProfitSharingV3Result profitSharingV3Result = wxPayService.getProfitSharingService().profitSharingQueryV3("1745800085756895232", "4200002127202401122097980590", "1661323640");
        System.out.println(profitSharingV3Result);
    }

    /**
     * 结束分账
     * @throws WxPayException
     */
    @Test
    public void profitSharingFinish() throws WxPayException {
        ProfitSharingUnfreezeRequest profitSharingFinishRequest = new ProfitSharingUnfreezeRequest();
        profitSharingFinishRequest.setTransactionId("4200002122202401129346519359");
        profitSharingFinishRequest.setOutOrderNo("1745820005743910912");
        profitSharingFinishRequest.setDescription("结束分账");
        profitSharingFinishRequest.setSubMchId("1661323640");
        wxPayService.getProfitSharingService().profitSharingFinish(profitSharingFinishRequest);
    }

    /**
     * 获取微信支付分用户签约计划
     * @author DB
     * @since 2023/12/15
     */
    @Test
    public void getPayScoreUserSignPlans() throws WxPayException {
        wxPayService.getConfig().setServiceId("00003053000000169450961228104460");
        wxPayService.getV3("https://api.mch.weixin.qq.com/v3/payscore/sign-plan/partner/user-sign-plans/merchant-sign-plan-no/1736679801829396540?sub_mchid=1661323640");
    }

}
