package cn.pluss.platform.controller.merchant.callback;

import cn.hutool.crypto.symmetric.DES;
import cn.pluss.platform.channel.MerchantAuditService;
import cn.pluss.platform.entity.RyxAccessModel;
import cn.pluss.platform.ryx.RyxService;
import cn.pluss.platform.ys.impl.v20210929.YsConfigV2;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @author DJH
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/auditCallback")
public class AuditCallbackController {

    @Setter(onMethod_ = {@Autowired})
    private MerchantAuditService sxfMerchantAuditService;

    @Setter(onMethod_ = {@Autowired})
    private MerchantAuditService ryxMerchantAuditService;

    @Setter(onMethod_ = {@Autowired})
    private MerchantAuditService ysAuditService;

    @Setter(onMethod_ = {@Autowired})
    private MerchantAuditService ysAuditServiceV2;

    @Setter(onMethod_ = {@Autowired})
    private MerchantAuditService ysAuditServiceExtensionV2;

    @Autowired
    private RyxService ryxService;

    @Autowired
    private YsConfigV2 ysConfig;

    @PostMapping("sxf")
    // TODO 后续需要接sxf的回调
    public Map<String, Object> sxfAuditCallback(@RequestBody JSONObject result) throws Exception {
        return sxfMerchantAuditService.merchantAuditCallback(result);
    }

    @PostMapping("ys")
    public String ysAuditCallback(@RequestParam Map<String, Object> result) throws Exception {
        result = (JSONObject) JSON.toJSON(result);
        log.info("data: {}", ((JSONObject) result).toJSONString());
        ysAuditService.merchantAuditCallback((JSONObject) result);
        return "success";
    }

    /**
     * 审核不通过的解密data数据
     * {"urm":{"sysFlowId":"27202110211044510914671","nogoReason":null,"nogoRemark":"小微缺手持身份证","noticeType":"审核拒绝"}}
     * 审核通过
     * {"notifyType":"00","status":"00","urm":{"agtMercId":"801521000001V0S","custId":"2022011320293776","mercAbbr":"华辉建材","mercCnm":"晋江市内坑镇威乐斯建材商行","mercId":"826701889990014","nogoRemark":"开户成功","noticeType":"开户成功","sysFlowId":"27202201131806151642525"}}
     */
    @PostMapping("ysV2")
    public String ysAuditCallbackV2(@RequestBody Map<String, Object> result) throws Exception {
        DES des = new DES(ysConfig.getEncryptPwd().getBytes(StandardCharsets.UTF_8));
        String resp = des.decryptStr((String) result.get("data"));
        JSONObject respJson = JSONObject.parseObject(resp);
        log.info(resp);
        ysAuditServiceV2.merchantAuditCallback(respJson);
        return "success";
    }

    /**
     * 审核不通过的解密data数据
     * {"urm":{"sysFlowId":"27202110211044510914671","nogoReason":null,"nogoRemark":"小微缺手持身份证","noticeType":"审核拒绝"}}
     * 审核通过
     * {"notifyType":"00","status":"00","urm":{"agtMercId":"801521000001V0S","custId":"2022011320293776","mercAbbr":"华辉建材","mercCnm":"晋江市内坑镇威乐斯建材商行","mercId":"826701889990014","nogoRemark":"开户成功","noticeType":"开户成功","sysFlowId":"27202201131806151642525"}}
     */
    @PostMapping("ysExtensionV2")
    public String ysExtensionAuditCallbackV2(@RequestBody Map<String, Object> result) throws Exception {
        DES des = new DES(ysConfig.getEncryptPwd().getBytes(StandardCharsets.UTF_8));
        String resp = des.decryptStr((String) result.get("data"));
        JSONObject respJson = JSONObject.parseObject(resp);
        log.info(resp);
        ysAuditServiceExtensionV2.merchantAuditCallback(respJson);
        return "success";
    }

    @PostMapping("sxf/edit")
    public Map<String, Object> sxfEditCallback(@RequestBody JSONObject result) throws Exception {
        return sxfMerchantAuditService.editMerchantCallback(result);
    }

    @PostMapping("ryx")
    public Map<String, Object> ryxAuditCallback(@RequestBody JSONObject result) throws Exception {
        RyxAccessModel ryxAccessModel = JSON.toJavaObject(result, RyxAccessModel.class);
        JSONObject respResult = ryxService.accessResultDecrypt(ryxAccessModel, JSONObject.class);
        return ryxMerchantAuditService.merchantAuditCallback(respResult);
    }

    @PostMapping("ryx/edit")
    public Map<String, Object> ryxEditCallback(@RequestBody JSONObject result) throws Exception {
        return ryxMerchantAuditService.editMerchantCallback(result);
    }


}
