package com.hzs.system.config.controller.manage;


import cn.hutool.core.collection.CollUtil;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.system.config.BdTradeConfig;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.system.config.service.IBdTradeConfigService;
import com.hzs.system.config.vo.BdTradeConfigVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 交易配置表 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2023-08-18
 */
@Slf4j
@RestController
@RequestMapping("/manage/trade-config")
public class BdTradeConfigController extends BaseController {

    @Autowired
    private RedisService redisService;

    @Autowired

    private IBdTradeConfigService bdTradeConfigService;

    /**
     * 保存交易配置
     *
     * @param tradeConfigVO 入参
     * @return AjaxResult
     */
    @Log(module = EOperationModule.TRADE_CONFIG, business = EOperationBusiness.WITHDRAWAL_CONFIG, method = EOperationMethod.INSERT)
    @PostMapping("/add")
    public AjaxResult addTradeConfig(@RequestBody BdTradeConfigVO tradeConfigVO) {
        AjaxResult ajaxResult = parametersVerify(tradeConfigVO);
        if (ajaxResult.equals(AjaxResult.error())) {
            return ajaxResult;
        }
        tradeConfigVO.setPkMember(SecurityUtils.getUserId());
        tradeConfigVO.setPkCountry(SecurityUtils.getPkCountry());
        return toAjax(bdTradeConfigService.addTradeConfig(tradeConfigVO));
    }


    /**
     * 交易配置列表显示
     *
     * @param tradeConfigVO 入参
     * @return AjaxResult
     */
    @Log(module = EOperationModule.TRADE_CONFIG, business = EOperationBusiness.WITHDRAWAL_CONFIG, method = EOperationMethod.SELECT)
    @PostMapping("/list")
    public AjaxResult showTradeConfigList(@RequestBody BdTradeConfigVO tradeConfigVO) {
        tradeConfigVO.setPkCountry(SecurityUtils.getPkCountry());
        return AjaxResult.success(bdTradeConfigService.showTradeConfigList(tradeConfigVO));
    }

    /**
     * 更新交易配置信息
     *
     * @param tradeConfigVO 入参
     * @return AjaxResult
     */
    @Log(module = EOperationModule.TRADE_CONFIG, business = EOperationBusiness.WITHDRAWAL_CONFIG, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult updateTradeConfig(@RequestBody BdTradeConfigVO tradeConfigVO) {
        AjaxResult ajaxResult = parametersVerify(tradeConfigVO);
        if (ajaxResult.equals(AjaxResult.error())) {
            return ajaxResult;
        }
        tradeConfigVO.setPkMember(SecurityUtils.getUserId());
        tradeConfigVO.setPkCountry(SecurityUtils.getPkCountry());
        getCacheKey(tradeConfigVO.getPkId());
        return toAjax(bdTradeConfigService.updateTradeConfig(tradeConfigVO));
    }

    /**
     * 删除交易配置
     *
     * @param pkId 主键ID
     * @return AjaxResult
     */
    @Log(module = EOperationModule.TRADE_CONFIG, business = EOperationBusiness.WITHDRAWAL_CONFIG, method = EOperationMethod.DELETE)
    @GetMapping("/delete")
    public AjaxResult deleteTradeConfig(Integer pkId) {
        getCacheKey(pkId);
        return toAjax(bdTradeConfigService.deleteTradeConfig(pkId, SecurityUtils.getUserId()));
    }


    /**
     * 查看详情
     *
     * @param pkId 主键ID
     * @return AjaxResult
     */
    @Log(module = EOperationModule.TRADE_CONFIG, business = EOperationBusiness.WITHDRAWAL_CONFIG, method = EOperationMethod.DELETE)
    @GetMapping("/detail")
    public AjaxResult selectTradeConfigDetail(Integer pkId) {
        return AjaxResult.success(bdTradeConfigService.selectTradeConfigDetail(pkId, SecurityUtils.getPkCountry()));
    }


    /**
     * 公共参数校验
     *
     * @param tradeConfigVO 入参
     * @return AjaxResult
     */
    private AjaxResult parametersVerify(BdTradeConfigVO tradeConfigVO) {
        if (CollUtil.isEmpty(tradeConfigVO.getPkAwardsList())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.AWARDS_NAME_NOT_NULL));
        }
        if (null == tradeConfigVO.getLicenseState()) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.LICENSE_STATE_NOT_EMPTY));
        }

        if (null == tradeConfigVO.getBusinessModule()) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.BUSINESS_MODULE));
        }
        return AjaxResult.success();
    }

    private String[] getCacheKey(Integer pkId) {
        BdTradeConfig tradeConfig = bdTradeConfigService.getById(pkId);
        Integer pkCountry = tradeConfig.getPkCountry();
        Integer businessModule = tradeConfig.getBusinessModule();
        String pkAwards = tradeConfig.getPkAwards();
        String pkGrade = tradeConfig.getPkGrade();
        Integer realNameState = tradeConfig.getRealNameState();
        Integer licenseState = tradeConfig.getLicenseState();
        //删除奖衔缓存
        if (StringUtils.isNotEmpty(pkAwards)) {
            String[] pkAwardsStrs = pkAwards.split(",");
            for (String pkAwardsStr : pkAwardsStrs) {
                deleteCacheKey(pkCountry, businessModule, realNameState, licenseState, pkAwardsStr, null);
            }
        }
        //删除等级缓存
        if (StringUtils.isNotEmpty(pkGrade)) {
            String[] pkGradeStrs = pkGrade.split(",");
            for (String pkGradeStr : pkGradeStrs) {
                deleteCacheKey(pkCountry, businessModule, realNameState, licenseState, null, pkGradeStr);
            }
        }
        return null;
    }

    /**
     * 获取键值
     *
     * @param pkCountry      国家
     * @param businessModule 业务模块
     * @param realNameState  实名
     * @param licenseState   营业执照
     * @param pkAwards       奖衔
     * @param pkGrade        等级
     * @return
     */

    private String deleteCacheKey(Integer pkCountry, Integer businessModule, Integer realNameState, Integer licenseState, String pkAwards, String pkGrade) {
        String key = null;
        //提现、转账、奖衔
        if (null != businessModule && null != pkAwards && null != realNameState) {
            key = CacheConstants.TRADE_CONFIG + ":" + pkCountry + ":" + businessModule + pkAwards + EVerificationModule.REAL_NAME.getValue();
            redisService.deleteObjectByKey(key);
        }
        if (null != businessModule && null != pkAwards && null != licenseState) {
            key = CacheConstants.TRADE_CONFIG + ":" + pkCountry + ":" + businessModule + pkAwards + EVerificationModule.BUSINESS_LICENSE.getValue();
            redisService.deleteObjectByKey(key);
        }

        //提现等级
        if (null != businessModule && null != pkGrade && null != realNameState) {
            key = CacheConstants.TRADE_CONFIG + ":" + pkCountry + ":" + businessModule + pkGrade + EVerificationModule.REAL_NAME.getValue();
            redisService.deleteObjectByKey(key);
        }
        if (null != businessModule && null != pkGrade && null != licenseState) {
            key = CacheConstants.TRADE_CONFIG + ":" + pkCountry + ":" + businessModule + pkGrade + EVerificationModule.BUSINESS_LICENSE.getValue();
            redisService.deleteObjectByKey(key);
        }
        return key;
    }
}
