package com.jeequan.jeepay.pay.ctrl.openapi;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.model.openapi.AccessReq;
import com.jeequan.jeepay.core.model.openapi.AccessResp;
import com.jeequan.jeepay.core.service.IOpenApiService;
import com.jeequan.jeepay.core.utils.JeepayKit;
import com.jeequan.jeepay.core.utils.SeqKit;
import com.jeequan.jeepay.pay.anno.OpenLabel;
import com.jeequan.jeepay.pay.ctrl.OpenApiController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * TODO
 * 交易Api控制器
 * @author crystal
 * @date 2024/4/7 15:03
 */
@Slf4j
@RestController
@RequestMapping("/api/open")
public class TradeOpenApiController extends OpenApiController {

    @Autowired
    @Qualifier("jsapiOpenApiService")
    private IOpenApiService jsapiService;

    @Autowired
    @Qualifier("microOpenApiService")
    private IOpenApiService microService;

    @Autowired
    @Qualifier("h5OpenApiService")
    private IOpenApiService h5Service;

    @Autowired
    @Qualifier("scanOpenApiService")
    private IOpenApiService scanService;

    @Autowired
    @Qualifier("appOpenApiService")
    private IOpenApiService appService;

    @Autowired
    @Qualifier("closeOpenApiService")
    private IOpenApiService closeService;


    @Autowired
    @Qualifier("cashierOpenApiService")
    private IOpenApiService cashierService;

    /**
     * 聚合收银台
     * @return
     */
    @OpenLabel(CS.MCH_APP_API_ENUM_V2.CASHIER_PAY)
    @RequestMapping(value = "/payment/chrpay")
    public AccessResp chrPay(){
        return cashierService.execute(getRQ());
    }

    /**
     * 公众号/生活号支付
     * @return
     */
    @OpenLabel(CS.MCH_APP_API_ENUM_V2.JSAPI_PAY)
    @RequestMapping(value = "/payment/jspay")
    public AccessResp jsPay(){
        return jsapiService.execute(getRQ());
    }

    /**
     * 聚合扫码支付
     * @return
     */
    @OpenLabel(CS.MCH_APP_API_ENUM_V2.SCAN_PAY)
    @RequestMapping(value = "/payment/scanpay")
    public AccessResp scanPay(){
        return scanService.execute(getRQ());
    }

    /**
     * 小程序支付入口
     * @return
     */
    @OpenLabel(CS.MCH_APP_API_ENUM_V2.APPLET_PAY)
    @RequestMapping(value = "/payment/ltpay")
    public AccessResp ltPay(){
        AccessReq rq = getRQ().setIsApplet(true);
        return jsapiService.execute(rq);
    }

    /**
     * 反扫支付
     * @return
     */
    @OpenLabel(CS.MCH_APP_API_ENUM_V2.MICRO_PAY)
    @RequestMapping(value = "/payment/micropay")
    public AccessResp microPay(){
        return microService.execute(getRQ());
    }

    /**
     * H5支付入口
     * @return
     */
    @OpenLabel(CS.MCH_APP_API_ENUM_V2.H5_PAY)
    @RequestMapping(value = {"/payment/h5pay"})
    public AccessResp h5Pay(){
        return h5Service.execute(getRQ());
    }

    /**
     * APP支付入口
     * @return
     */
    @OpenLabel(CS.MCH_APP_API_ENUM_V2.APP_PAY)
    @RequestMapping(value = "/payment/apppay")
    public AccessResp appPay(){
        return appService.execute(getRQ());
    }

    /**
     * 订单关闭
     * @return
     */
    @OpenLabel()
    @RequestMapping(value = "/order/close")
    public AccessResp close(){
        return closeService.execute(getRQ());
    }

    //        result.put("payType", "wx");
//        result.put("openid", "oY6Es6Y52jNTArOpt-CTDP4ScKqs");
//        result.put("payType", "alipay");
//        result.put("userid", "2088502904759300");


    public static void main(String[] args) {
        String md5Key = "3oloC61eOXyFYLJiYom6HpvAkxYEj2ZZhaNUljSsRGLyRTuw2m77EQM2ouaK7okh3gwiYakPYFOKP2HzBKB3xp86d9RrCgOnl6D4mRVNJKWXOy8OtQpvRaEXrRMYDLY7";
        String storeId = "S2404236859";
        String mercOrderNo = SeqKit.genMhoOrderId();
        JSONObject bizData = new JSONObject();
        bizData.put("subject","测试支付1");
        bizData.put("amount",3);
//        bizData.put("payType","ALIPAY");
//        bizData.put("isScreen",true);
//        bizData.put("subAppId","wx9646c9a52da17c40");
//        bizData.put("userId","oY6Es6Y52jNTArOpt-CTDP4ScKqs");
//        bizData.put("userId","2088502904759300");
        bizData.put("clientIp","127.0.0.1");
        bizData.put("mchOrderNo",mercOrderNo);
        bizData.put("body","测试支付2");
        bizData.put("storeId",storeId);
        bizData.put("currency","cny");
        JSONObject params = new JSONObject();
        params.put("reqId",System.currentTimeMillis() + "");
        params.put("reqTime", DateUtil.format(new DateTime(),DatePattern.PURE_DATETIME_FORMAT));
        params.put("version", "1.0");
        params.put("signType", "MD5");
        params.put("appId", "2081240416099412");
        params.put("bizData",bizData);
        String strSort = JeepayKit.getStrSort(params) + "&appSecret="+md5Key;
        String sign = JeepayKit.md5(strSort, "utf-8");
        params.put("sign", sign);
        System.out.println(params.toJSONString());
    }

    /**
     * 测试1
     * @param args
     */
//    public static void main(String[] args) {
//        String md5Key = "3oloC61eOXyFYLJiYom6HpvAkxYEj2ZZhaNUljSsRGLyRTuw2m77EQM2ouaK7okh3gwiYakPYFOKP2HzBKB3xp86d9RrCgOnl6D4mRVNJKWXOy8OtQpvRaEXrRMYDLY7";
//        String storeId = "S2404183032";
//        String mercOrderNo = SeqKit.genMhoOrderId();
//        JSONObject bizData = new JSONObject();
//        bizData.put("subject","测试支付1");
//        bizData.put("amount",3);
//        bizData.put("payType","ALIPAY");
////        bizData.put("isScreen",true);
////        bizData.put("subAppId","wx9646c9a52da17c40");
////        bizData.put("userId","oY6Es6Y52jNTArOpt-CTDP4ScKqs");
////        bizData.put("userId","2088502904759300");
//        bizData.put("clientIp","127.0.0.1");
//        bizData.put("mchOrderNo",mercOrderNo);
//        bizData.put("body","测试支付2");
//        bizData.put("storeId",storeId);
//        bizData.put("currency","cny");
//        JSONObject params = new JSONObject();
//        params.put("reqId",System.currentTimeMillis() + "");
//        params.put("reqTime", DateUtil.format(new DateTime(),DatePattern.PURE_DATETIME_FORMAT));
//        params.put("version", "1.0");
//        params.put("signType", "MD5");
//        params.put("appId", "2081240416099412");
//        params.put("bizData",bizData);
//        String strSort = JeepayKit.getStrSort(params) + "&appSecret="+md5Key;
//        String sign = JeepayKit.md5(strSort, "utf-8");
//        params.put("sign", sign);
//        System.out.println(params.toJSONString());
//    }


//    public static void main(String[] args) {
//        String md5Key = "O3kAhyaxYSbYhFg0CDiNflJ8eoNGpQ8Gn8m1vQKrcybE0tH8aB1WEggDT3NbRza6FItOoSDbj64WdxR0JG749c1jTTI4LQkKzZMAggPbhRB32Mf2lgnYDKJNs5sqlJLS";
//        String mercOrderNo = SeqKit.genMhoOrderId();
//        JSONObject bizData = new JSONObject();
//        bizData.put("mchOrderNo","1231231");
//        JSONObject params = new JSONObject();
//        params.put("reqId",System.currentTimeMillis() + "");
//        params.put("reqTime", DateUtil.format(new DateTime(),DatePattern.PURE_DATETIME_FORMAT));
//        params.put("version", "1.0");
//        params.put("signType", "MD5");
//        params.put("appId", "65d5abb9e4b0607c72ea5eaf");
//        params.put("bizData",bizData);
//        String strSort = JeepayKit.getStrSort(params) + "&appSecret="+md5Key;
//        String sign = JeepayKit.md5(strSort, "utf-8");
//        params.put("sign", sign);
//        System.out.println(params.toJSONString());
//    }



}
