package com.jeequan.jeepay.agent.ctrl.agentconfig;

import cn.hutool.core.codec.Base64;
import com.jeequan.jeepay.agent.ctrl.CommonCtrl;
import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.AgentConfig;
import com.jeequan.jeepay.db.entity.AgentInfo;
import com.jeequan.jeepay.db.entity.ThirdAuthDataEntity;
import com.jeequan.jeepay.service.impl.AgentConfigService;
import com.jeequan.jeepay.service.impl.AgentInfoService;
import com.jeequan.jeepay.service.impl.ThirdAuthDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/***
* 服务商配置
*
* @author terrfly
*
* @date 2022/5/24 11:12
*/
@RestController
@RequestMapping("/api/agentConfig")
public class AgentConfigController extends CommonCtrl {

    @Autowired private AgentInfoService agentInfoService;
    @Autowired private AgentConfigService agentConfigService;

    @Autowired
    private ThirdAuthDataService thirdAuthDataService;


    @MethodLog(remark = "更改支付密码")
    @PutMapping("/agentSipw")
    public ApiRes updateSipw() {

        String confirmPwd = Base64.decodeStr(getValStringRequired("confirmPwd"));

        // 验证原密码
        AgentInfo dbAgentInfo = agentInfoService.getById(getCurrentAgentNo());
        if(StringUtils.isNotEmpty(dbAgentInfo.getSipw())){
            //当前旧密码
            String originalPwd = Base64.decodeStr(getValStringRequired("originalPwd"));
            if(!new BCryptPasswordEncoder().matches(originalPwd, dbAgentInfo.getSipw())){
                throw new BizException("原支付验证失败！");
            }
        }

        AgentInfo agentInfo = new AgentInfo();
        agentInfo.setAgentNo(getCurrentAgentNo());
        agentInfo.setSipw(new BCryptPasswordEncoder().encode(confirmPwd));
        agentInfoService.updateById(agentInfo);
        return ApiRes.ok();
    }


    /** 是否设置了支付密码 **/
    @GetMapping("/hasSipwValidate")
    public ApiRes hasSipwValidate() {

        AgentInfo dbAgentInfo = agentInfoService.getById(getCurrentAgentNo());

        return ApiRes.ok(StringUtils.isNotEmpty(dbAgentInfo.getSipw()));
    }

    /** 验证支付密码是否正确 **/
    @PostMapping("/agentSipwValidate")
    public ApiRes agentSipwValidate() {

        //当前旧密码
        String originalPwd = Base64.decodeStr(getValStringRequired("originalPwd"));

        // 验证原密码
        AgentInfo dbAgentInfo = agentInfoService.getById(getCurrentAgentNo());


        return ApiRes.ok(StringUtils.isEmpty(dbAgentInfo.getSipw()) || new BCryptPasswordEncoder().matches(originalPwd, dbAgentInfo.getSipw()));

    }

    /**
     * 根据configKey查询服务商配置
     */
    @GetMapping("/{configKey}")
    public ApiRes get(@PathVariable("configKey") String configKey) {
        AgentConfig config = agentConfigService.getByAgentNoAndConfigKey(getCurrentAgentNo(), configKey);
        return ApiRes.ok(config);
    }


    @PostMapping("/authData")
    public ApiRes addAuthData() {
        ThirdAuthDataEntity entity = getObject(ThirdAuthDataEntity.class);
        Assert.notNull(entity.getType(), "授权渠道[type]不能为空");
        Assert.notNull(entity.getSubType(), "授权类型[subType]不能为空");
        Assert.notNull(entity.getAuthFileName(), "文件名称[authFileName]不能为空");
        Assert.notNull(entity.getValue(), "文件内容[value]不能为空");

        ThirdAuthDataEntity one = thirdAuthDataService.lambdaQuery()
                .eq(ThirdAuthDataEntity::getAuthFileName, entity.getAuthFileName())
                .one();

        if (one != null) {
            throw new BizException("数据已存在");
        }

        thirdAuthDataService.save(entity);

        return ApiRes.ok();
    }

    @PostMapping("/config")
    public ApiRes setConfig() {
        AgentConfig reqParam = getObject(AgentConfig.class);
        Assert.notNull(reqParam, "缺少参数");
        Assert.notNull(reqParam.getGroupKey(), "配置分组名称代码[groupKey]不能为空");
        Assert.notNull(reqParam.getConfigKey(), "配置名称代码[configKey]不能为空");
        Assert.notNull(reqParam.getAgentNo(), "服务商编号[agentNo]不能为空");
        Assert.notNull(reqParam.getConfigVal(), "配置项[configVal]未赋值");

        agentConfigService.saveOrUpdateConfig(reqParam);

        return ApiRes.ok();
    }
}
