package org.dromara.payment.sys.service.impl;

import cn.hutool.core.util.RandomUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.constant.Constants;
import org.dromara.common.core.constant.GlobalConstants;
import org.dromara.common.core.enums.BusRole;
import org.dromara.common.core.enums.SmsType;
import org.dromara.common.core.service.BaseService;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.payment.merchant.domain.BusMerchant;
import org.dromara.payment.merchant.domain.bo.BusMerchantBo;
import org.dromara.payment.merchant.domain.vo.BusMerchantVo;
import org.dromara.payment.merchant.mapper.BusMerchantMapper;
import org.dromara.payment.merchant.service.IBusMerchantService;
import org.dromara.payment.operator.domain.BusOperator;
import org.dromara.payment.operator.domain.bo.BusOperatorBo;
import org.dromara.payment.operator.domain.vo.BusOperatorVo;
import org.dromara.payment.operator.mapper.BusOperatorMapper;
import org.dromara.payment.operator.service.IBusOperatorService;
import org.dromara.payment.sys.domain.SysSyd;
import org.dromara.payment.sys.domain.bo.SysPayPwdBo;
import org.dromara.payment.sys.domain.vo.SysSydVo;
import org.dromara.payment.sys.mapper.SysPayPwdMapper;
import org.dromara.payment.sys.mapper.SysSydMapper;
import org.dromara.payment.sys.service.ISysPayPwdService;
import org.dromara.payment.sys.service.ISysSydService;
import org.dromara.resource.api.RemoteSmsService;
import org.dromara.resource.api.domain.RemoteSms;
import org.dromara.system.api.RemoteUserService;
import org.dromara.system.api.domain.bo.RemoteUserBo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.Date;
import java.util.LinkedHashMap;

/**
 * @author sunzexing
 * @version 1.0
 * @title SysPayPwdServiceImpl
 * @description
 * @create 2024-05-13 12:08
 */
@Service
public class SysPayPwdServiceImpl extends BaseService implements ISysPayPwdService {

    @Resource
    public IBusMerchantService busMerchantService;

    @Resource
    public SysPayPwdMapper sysPayPwdMapper;

    @Resource
    public IBusOperatorService busOperatorService;


    @Resource
    public ISysSydService sysSydService;


    @DubboReference
    public RemoteSmsService  remoteSmsService;


    @DubboReference
    public RemoteUserService remoteUserService;


    @Resource
    public BusOperatorMapper busOperatorMapper;


    @Resource
    public BusMerchantMapper busMerchantMapper;


    @Resource
    public SysSydMapper sysSydMapper;




    /****
     * 修改支付密码
     * @param bo
     * @return
     */
    @Override
    public String updPayPwd(SysPayPwdBo bo) {

        Integer busRole = LoginHelper.getBusRole();
        String phone = "";
        if(busRole == BusRole.MERCHANT.getId() ){
            //商户
            BusMerchantVo bmv =  this.busMerchantService.queryById(LoginHelper.getBusId());
            phone = bmv.getWtrSjh();

        }else if(busRole == BusRole.OPERATOR.getId()){
            //运营商
            BusOperatorVo bov =  this.busOperatorService.queryById(LoginHelper.getBusId());
            phone = bov.getWtrSjh();

        }else if(busRole ==  BusRole.SERVICE.getId()){
            //服务商
            SysSydVo sysVo =  this.sysSydService.queryById(LoginHelper.getBusId());
            phone = sysVo.getConPhone();

        }
        String key = GlobalConstants.CAPTCHA_PAY_PWD_PHONE_CODE_KEY+ SmsType.RESET_PAY_PWD.getIType() + phone;
        String cacheCode = RedisUtils.getCacheObject(key);
        if(StringUtils.isEmpty(cacheCode)){
            this.invalidationParamsException("验证码无效");
        }

        if(!cacheCode.equals(bo.getSmsCode())){
            this.invalidationParamsException("验证码无效");
        }

        if(busRole == BusRole.MERCHANT.getId() ){
            //商户
            BusMerchant busBo = new BusMerchant();
            busBo.setId(LoginHelper.getBusId());
            busBo.setPayPassword(bo.getPayPwd());
            busBo.setCreateTime(new Date());
            busBo.setUpdateBy(LoginHelper.getUserId());
            this.busMerchantMapper.updateById(busBo);
            updateUserResetPayTag();

        }else if(busRole == BusRole.OPERATOR.getId()){
            //运营商
            BusOperator  busOperatorBo = new BusOperator();
            busOperatorBo.setId(LoginHelper.getBusId());
            busOperatorBo.setPayPassword(bo.getPayPwd());
            busOperatorBo.setCreateTime(new Date());
            busOperatorBo.setUpdateBy(LoginHelper.getUserId());
            this.busOperatorMapper.updateById(busOperatorBo);
            updateUserResetPayTag();

        }else if(busRole == BusRole.SERVICE.getId()){
            //服务商
            SysSyd sysSydVo = new SysSyd();
            sysSydVo.setId(LoginHelper.getBusId());
            sysSydVo.setPayPassword(bo.getPayPwd());
            sysSydVo.setUpdateTime(new Date());
            sysSydVo.setUpdateBy(LoginHelper.getUserId());
            this.sysSydMapper.updateById(sysSydVo);
            updateUserResetPayTag();

        }else{
            this.invalidationParamsException("此用户不能修改支付密码");
        }
        return "1";
    }

    private void updateUserResetPayTag() {
        this.remoteUserService.updateUserResetPayTag(LoginHelper.getUserId());

    }


    @Override
    public boolean sendSmsCode(String domain) {
        Integer busRole = LoginHelper.getBusRole();
        String phone = null;
        if(busRole == BusRole.MERCHANT.getId() ){
            //商户
            BusMerchantVo bmv =  this.busMerchantService.queryById(LoginHelper.getBusId());
            phone = bmv.getWtrSjh();
        }else if(busRole == BusRole.OPERATOR.getId()){
            //运营商
            BusOperatorVo bov =  this.busOperatorService.queryById(LoginHelper.getBusId());
            phone = bov.getWtrSjh();
        }else if(busRole == BusRole.SERVICE.getId()){
            //服务商
            SysSydVo sov = this.sysSydService.queryById(LoginHelper.getBusId());
            phone = sov.getConPhone();
        }else{
            this.invalidationParamsException("此用户不能修改支付密码");
        }

        if(StringUtils.isEmpty(phone)){
            this.invalidationParamsException("经办人手机号不能为空");
        }
        String key = GlobalConstants.CAPTCHA_PAY_PWD_PHONE_CODE_KEY+ SmsType.RESET_PAY_PWD.getIType() + phone;
        String cacheCode = RedisUtils.getCacheObject(key);
        if(StringUtils.isNotEmpty(cacheCode)){
            this.invalidationParamsException("2分钟内不能重复发送");
        }
        String code = RandomUtil.randomNumbers(4);
        //        RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));

        LinkedHashMap<String, String> map = new LinkedHashMap<>(1);
        map.put("code", code);
        RemoteSms  sms = this.remoteSmsService.send(domain, phone, SmsType.RESET_PAY_PWD, map);
        if(sms != null){
            RedisUtils.setCacheObject(key, code, Duration.ofMinutes(2));
        }
        return true;
    }
}
