package com.jeequan.jeepay.mch.ctrl.payconfig;

import com.jeequan.jeepay.components.mq.vender.IMQSender;
import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.constants.ApiCodeEnum;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.core.model.oauth2.Oauth2Params;
import com.jeequan.jeepay.core.utils.StringKit;
import com.jeequan.jeepay.db.entity.PayInterfaceConfig;
import com.jeequan.jeepay.db.entity.RateConfig;
import com.jeequan.jeepay.mch.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.MchAppService;
import com.jeequan.jeepay.service.impl.PayInterfaceConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/***
* oauth2配置
*
* @author terrfly
*
* @date 2022/11/9 17:01
*/
@RestController
@RequestMapping("/api/payOauth2Config")
public class PayOauth2ConfigController extends CommonCtrl {

    @Autowired private MchAppService mchAppService;
    @Autowired private PayInterfaceConfigService payInterfaceConfigService;
    @Autowired private IMQSender mqSender;


    /** 查询已经配置的参数信息
     *
     * **/
    @GetMapping(value="/savedConfigs")
    public ApiRes interfaceSavedConfigs() {

        String ifCode = getValStringRequired("ifCode"); // ifCode
        String infoId = getValStringRequired("infoId"); // infoId
        String configMode = getValStringRequired("configMode"); // 设置类型

        String infoType = null;

        // 普通商户的配置
        if(RateConfig.CONFIG_MODE_MCHSELFAPP1.equals(configMode)){
            infoType = CS.SYS_ROLE_TYPE.MCH_APP_OAUTH2;
        }

        // 查询已经配置的信息
        PayInterfaceConfig payInterfaceConfig = payInterfaceConfigService.getByInfoIdAndIfCode(infoType, infoId, ifCode);

        if(payInterfaceConfig == null){
            payInterfaceConfig = new PayInterfaceConfig();
        }

        // 处理脱敏数据
        if(StringUtils.isNotEmpty(payInterfaceConfig.getIfParams())){
            Oauth2Params isvParams = Oauth2Params.factory(ifCode, payInterfaceConfig.getIfParams());
            if (isvParams != null) {
                payInterfaceConfig.setIfParams(isvParams.deSenData());
            }
        }
        return ApiRes.ok(payInterfaceConfig);
    }


    /** 更新支付参数 **/
    @PostMapping("/configParams")
    @MethodLog(remark = "更新oauth2参数")
    public ApiRes saveOrUpdate() {

        PayInterfaceConfig payInterfaceConfig = getObject(PayInterfaceConfig.class);

        String infoType = CS.SYS_ROLE_TYPE.MCH_APP_OAUTH2;

        payInterfaceConfig.setInfoType(infoType);

        //添加更新者信息
        Long userId = getCurrentUser().getSysUser().getSysUserId();
        String realName = getCurrentUser().getSysUser().getRealname();
        payInterfaceConfig.setUpdatedUid(userId);
        payInterfaceConfig.setUpdatedBy(realName);

        //根据 服务商号、接口类型 获取商户参数配置
        PayInterfaceConfig dbRecord = payInterfaceConfigService.getByInfoIdAndIfCode(payInterfaceConfig.getInfoType(), payInterfaceConfig.getInfoId(), payInterfaceConfig.getIfCode());
        //若配置存在，为saveOrUpdate添加ID，第一次配置添加创建者
        if (dbRecord != null) {
            payInterfaceConfig.setId(dbRecord.getId());

            // 合并支付参数
            payInterfaceConfig.setIfParams(StringKit.marge(dbRecord.getIfParams(), payInterfaceConfig.getIfParams(), dbRecord.getIfCode()));
        }else {
            payInterfaceConfig.setCreatedUid(userId);
            payInterfaceConfig.setCreatedBy(realName);
        }

        boolean result = payInterfaceConfigService.saveOrUpdate(payInterfaceConfig);
        if (!result) {
            return ApiRes.fail(ApiCodeEnum.SYSTEM_ERROR, "配置失败");
        }

        return ApiRes.ok();
    }

}
