package com.ruoyi.common.pay.yst.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.pay.Payable;
import com.ruoyi.common.pay.yst.config.YstConfig;
import com.ruoyi.common.pay.yst.constants.YstApi;
import com.ruoyi.common.pay.yst.constants.YstTransCode;
import com.ruoyi.common.pay.yst.domain.BizParameter;
import com.ruoyi.common.pay.yst.domain.YstResponse;
import com.ruoyi.common.pay.yst.domain.bizRes.comsumptionApply.ChannelParamInfo;
import com.ruoyi.common.pay.yst.domain.bizRes.comsumptionApply.ConsumptionApplyRes;
import com.ruoyi.common.pay.yst.service.YstPayService;
import com.ruoyi.common.pay.yst.util.SM4Utils;
import com.ruoyi.common.pay.yst.util.YstClient;
import com.ruoyi.common.utils.SnowFlakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 云商通支付
 * @author wjh
 * 2024/8/1
 */
@Service
@Slf4j
@RestController
@RequestMapping("/yst")
public class YstPayServiceImpl implements YstPayService {

    @Autowired
    private YstClient ystClient;

    @Autowired
    private YstConfig ystConfig;

    /**
     * 调起支付
     */
    @Override
    @GetMapping
    public ChannelParamInfo prepayWx(Payable payable) {
        try {
            // 支付模式
            String bankCardNo= SM4Utils.encryptEcb(ystConfig.getSecretKey(), "6212261001029054530");
            //微信正扫
            Map<String, Object> SCAN_WEIXIN = new HashMap<>();
            SCAN_WEIXIN.put("SCAN_WEIXIN","{\"limitPay\":\"no_credit\"}");
            SCAN_WEIXIN.put("SCAN_WEIXIN","{\"vspCusid\":\"55058404816VQJW\"}");
//        SCAN_WEIXIN.put("SCAN_WEIXIN","{\"extendParams\":\"渠道扩展参数\"}");
//        SCAN_WEIXIN.put("SCAN_WEIXIN","{\"idNo\":\"\"}");
//        SCAN_WEIXIN.put("SCAN_WEIXIN","{\"name\":\"\"}");
//        SCAN_WEIXIN.put("SCAN_WEIXIN","{\"cardThype\":\"\"}");


            //收银宝快捷
            Map<String, Object> QUICKPAY_VSP = new HashMap<>();

            System.out.println("{\"bankCardNo\":\""+bankCardNo+"\"}");
            QUICKPAY_VSP.put("QUICKPAY_VSP","{\"bankCardNo\":\""+bankCardNo+"\"}");

            //收付通快捷
            Map<String, Object> QUICKPAY_SFT = new HashMap<>();
            System.out.println("{\"bankCardNo\":\""+bankCardNo+"\"}");
            QUICKPAY_SFT.put("QUICKPAY_SFT","{\"bankCardNo\":\""+bankCardNo+"\"}");

            //银联正扫
            Map<String, Object> SCAN_UNIONPAY = new HashMap<>();
            SCAN_UNIONPAY.put("SCAN_UNIONPAY","{\"limitPay\":\"no_credit\"}");

            //支付宝正扫
            Map<String, Object> SCAN_ALIPAY = new HashMap<>();
            SCAN_ALIPAY.put("SCAN_ALIPAY","{\"limitPay\":\"no_credit\"}");

            //收银宝POS
            Map<String, Object> ORDER_VSPPAY = new HashMap<>();
            ORDER_VSPPAY.put("ORDER_VSPPAY","{\"vspCusid\":\"6602900601500JK\"}");

            //付款码支付
            Map<String, Object> CODEPAY_VSP = new HashMap<>();
            CODEPAY_VSP.put("CODEPAY_VSP","{\"vspCusid\":\"\"}");
            CODEPAY_VSP.put("CODEPAY_VSP","{\"authcode\":\"280310687633511560\"}");

            //分账规则
            List sepDetail = new ArrayList();
            Map<String, Object> map1 = new HashMap<>();
            map1.put("signNum", "2705wxl00002");
            map1.put("amount", 1);
            sepDetail.add(map1);


            Map<String, Object> map2 = new HashMap<>();
            map2.put("signNum", "9665wxl202400004");
            map2.put("amount", 1);
//        sepDetail.add(map2);


            //订单过期时间
            final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            final Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MINUTE, 720);
            final Date date = calendar.getTime();
            final String orderValidTime = sdf.format(date);

            // 组装参数
            BizParameter bizParameter = new BizParameter();
            bizParameter.addParam("signNum", "111111");//TESTWC10011
            bizParameter.addParam("receiverSignNum", "2705wxl00001");
            bizParameter.addParam("reqTraceNum", "wxl"+ SnowFlakeUtil.newId());//商户订单号
            bizParameter.addParam("orderAmount", 3);//订单金额
            bizParameter.addParam("payAmount", 2);//支付金额
            bizParameter.addParam("promotionAmount", 1);//营销金额
            bizParameter.addParam("couponAmount",1 );//平台抽佣金额
            bizParameter.addParam("verifyType", 1);//交易验证方式-0：无验证、1：短信验证码（默认-1：短信验证码）

            //支付模式
            bizParameter.addMapParam("payMode", SCAN_WEIXIN);//微信正扫
//        bizParameter.addMapParam("payMode", QUICKPAY_VSP);//收银宝快捷
//        bizParameter.addMapParam("payMode", QUICKPAY_SFT);//收付通快捷
//        bizParameter.addMapParam("payMode", SCAN_UNIONPAY);//银联正扫
//        bizParameter.addMapParam("payMode", SCAN_ALIPAY);//支付宝正扫
//        bizParameter.addMapParam("payMode", ORDER_VSPPAY);//收银宝POS
//        bizParameter.addMapParam("payMode", CODEPAY_VSP);//付款码支付

            bizParameter.addListParam("sepDetail", sepDetail);//分账规则
            bizParameter.addParam("reqsUrl", "http://www.baidu.com");//前台通知地址
            bizParameter.addParam("respUrl", "http://test.allinpay.com/open/testNotify");//后台通知地址
            bizParameter.addParam("orderValidTime", orderValidTime);//订单过期时间
//        bizParameter.addParam("goodsType", "goodsType");//商品类型
//        bizParameter.addParam("bizGoodsNo", "bizGoodsNo123456");//商户商品编号
            bizParameter.addParam("goodsName", "goodsName123456");//商品名称
            bizParameter.addParam("goodsDesc", "goodsDesc123456");//商品描述
            bizParameter.addParam("industryCode", "111111");//行业代码
            bizParameter.addParam("industryName", "222222");//行业名称
            bizParameter.addParam("summary", "摘要");
            bizParameter.addParam("extendParams", "{\"extend\":\"abcdefghiii\"}");

            // 发送请求
            YstResponse<ConsumptionApplyRes> res = ystClient.sendRequest(YstApi.TX, YstTransCode.CONSUMPTION_APPLY, bizParameter, ConsumptionApplyRes.class);
            return res.getBizData().getChannelParamInfo();
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }
}
