package com.crm.manager.controller;

import com.alibaba.fastjson.JSON;
import com.crm.manager.service.SysConfigService;
import com.crm.rely.backend.core.constant.ConfigConstants;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.PrefixEnum;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.system.config.SysConfigSearchDto;
import com.crm.rely.backend.core.dto.system.config.SysConfigWithdrawDayLimitDto;
import com.crm.rely.backend.core.dto.system.config.amount.SysConfigFinanceMaxAndMinAmountDto;
import com.crm.rely.backend.core.dto.system.config.autolimit.SysConfigAutolimitDto;
import com.crm.rely.backend.core.dto.system.config.automatic.SysConfigAutomaticDto;
import com.crm.rely.backend.core.dto.system.config.domain.SysConfigDomainNameDto;
import com.crm.rely.backend.core.dto.system.config.file.limit.SysConfigFileLimitDto;
import com.crm.rely.backend.core.dto.system.config.notice.SysConfigNoticeEmailDto;
import com.crm.rely.backend.core.dto.system.config.trade.interest.SysConfigTradeInterestDto;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.system.config.*;
import com.crm.rely.backend.core.entity.system.config.amount.SysConfigFinanceMaxAndMinAmountByEmailCodeEntity;
import com.crm.rely.backend.core.entity.system.config.autolimit.SysConfigAutolimitByEmailCodeEntity;
import com.crm.rely.backend.core.entity.system.config.autolimit.SysConfigAutolimitEntity;
import com.crm.rely.backend.core.entity.system.config.automatic.SysConfigAutomaticByEmailCodeEntity;
import com.crm.rely.backend.core.entity.system.config.domain.SysConfigDomainNameEntity;
import com.crm.rely.backend.core.entity.system.config.file.limit.SysConfigFileLimitEntity;
import com.crm.rely.backend.core.entity.system.config.notice.SysConfigNoticeEmailByEmailCodeEntity;
import com.crm.rely.backend.core.entity.system.config.trade.interest.SysConfigTradeInterestByEmailCodeEntity;
import com.crm.rely.backend.util.GetIpAndMac;
import com.crm.rely.backend.util.GoogleAuthenticatorUtil;
import com.crm.rely.backend.util.MD5Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 配置
 *
 * @author houn
 */
@RestController
@RequestMapping("/config")
public class SysConfigController {

    @Autowired
    private SysConfigService sysConfigService;

    @PostMapping("/refresh")
    public BaseResultDto refresh() throws Exception {

        sysConfigService.refresh();
        return BaseResultDto.success();
    }

    @PostMapping("/cancel")
    public BaseResultDto cancel() throws Exception {

        sysConfigService.cancel();
        return BaseResultDto.success();
    }

    /**
     * 添加配置
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated SysConfigAddEntity entity) throws Exception {

        entity.setPwd(MD5Util.getMD5(entity.getPwd()));

        sysConfigService.add(entity);
        return BaseResultDto.success();
    }

    /**
     * 删除配置
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated SysConfigDeleteEntity entity, InfoEntity infoEntity) throws Exception {
        if (!PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
            return BaseResultDto.error(Constants.SYSTEM_ERROR);
        }
        sysConfigService.delete(entity);
        return BaseResultDto.success();
    }

    /**
     * 修改配置
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated SysConfigUpdateEntity entity, InfoEntity infoEntity) throws Exception {
//        entity.setEmail(infoEntity.getIbInfo().getEmail());
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(), entity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }
        sysConfigService.update(entity);

        return BaseResultDto.success();
    }

    @PostMapping("/update/send/code")
    public BaseResultDto updateManagerSendEmailCode(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    /**
     * 列表查询
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/list")
    public BaseResultDto list(@RequestBody @Validated SysConfigSearchEntity entity, InfoEntity infoEntity) throws Exception {


        ResultWithPagerDto<SysConfigSearchDto> result = sysConfigService.searchPageList(entity);

        return result;
    }

    /**
     * 单个查询
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/single")
    public BaseResultDto single(@RequestBody @Validated SysConfigSingleEntity entity) throws Exception {

        SysConfigSearchDto result = sysConfigService.searchSingle(entity);

        return BaseResultDto.success(result);
    }

    /**
     * 查看全局邮件配置
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/email/search")
    public BaseResultDto searchGlobal() throws Exception {

        SysConfigSearchDto dto = sysConfigService.searchSingleByCode(ConfigConstants.EMAIL_CONFIG_CONFIGURE);
        return BaseResultDto.success(dto);
    }

    /**
     * 修改全局邮件配置
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/email/update")
    public BaseResultDto updateGlobal(@RequestBody @Validated SysConfigUpdateEntity entity, InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(), entity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }
        entity.setCode(ConfigConstants.EMAIL_CONFIG_CONFIGURE);
//        entity.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/email/update/send/code")
    public BaseResultDto updateGlobalSendEmailCode(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    /**
     * 查询(查看)全局入金最大最小金额限制
     *
     * @return
     */
    @PostMapping("/deposit/amount/searcher")
    public BaseResultDto searcherGlobalMaxAndMinDepositAmount() throws Exception {
        List<String> codes = new ArrayList<>(2);

        codes.add(ConfigConstants.FINANCE_DEPOSIT_MAX);
        codes.add(ConfigConstants.FINANCE_DEPOSIT_MIN);

        List<SysConfigSearchDto> dtos = sysConfigService.searchSingleByCodes(codes);
        SysConfigFinanceMaxAndMinAmountDto sysConfigFinanceMaxAndMinAmountDto = new SysConfigFinanceMaxAndMinAmountDto();
        for (SysConfigSearchDto sysConfigSearchDto : dtos) {
            switch (sysConfigSearchDto.getCode()) {
                case ConfigConstants.FINANCE_DEPOSIT_MAX:
                    sysConfigFinanceMaxAndMinAmountDto.setMaxAmount(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.FINANCE_DEPOSIT_MIN:
                    sysConfigFinanceMaxAndMinAmountDto.setMinAmount(sysConfigSearchDto.getValue());
                    break;
                default:
                    break;
            }
        }
        return BaseResultDto.success(sysConfigFinanceMaxAndMinAmountDto);
    }

    /**
     * 修改全局最大最小金额限制
     *
     * @return
     */
    @PostMapping("/deposit/amount/update")
    public BaseResultDto updateGlobalMaxAndMinDepositAmount(@RequestBody @Validated SysConfigFinanceMaxAndMinAmountByEmailCodeEntity sysConfigFinanceMaxAndMinAmountEntity, InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(),
                sysConfigFinanceMaxAndMinAmountEntity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }
        SysConfigUpdateEntitys entitys = new SysConfigUpdateEntitys();
//        entitys.setEmail(sysConfigFinanceMaxAndMinAmountEntity.getEmail());
//        entitys.setEmailCode(sysConfigFinanceMaxAndMinAmountEntity.getEmailCode());
        List<BaseSysConfigUpdateEntity> updateData = new ArrayList<>(2);

        BaseSysConfigUpdateEntity sysConfigMaxUpdateEntity = new BaseSysConfigUpdateEntity();
        sysConfigMaxUpdateEntity.setCode(ConfigConstants.FINANCE_DEPOSIT_MAX);
        sysConfigMaxUpdateEntity.setValue(sysConfigFinanceMaxAndMinAmountEntity.getMaxAmount().toString());
        updateData.add(sysConfigMaxUpdateEntity);

        BaseSysConfigUpdateEntity sysConfigMinUpdateEntity = new BaseSysConfigUpdateEntity();
        sysConfigMinUpdateEntity.setCode(ConfigConstants.FINANCE_DEPOSIT_MIN);
        sysConfigMinUpdateEntity.setValue(sysConfigFinanceMaxAndMinAmountEntity.getMinAmount().toString());
        updateData.add(sysConfigMinUpdateEntity);
        entitys.setUpdateData(updateData);
//        entitys.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.updates(entitys);
        return BaseResultDto.success();
    }

    @PostMapping("/deposit/amount/update/send/code")
    public BaseResultDto updateGlobalMaxAndMinDepositAmountSendEmailCode(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    @PostMapping("/domain/name/searcher")
    public BaseResultDto searcherGlobalDomainName() throws Exception {
        List<String> codes = new ArrayList<>(2);

        codes.add(ConfigConstants.WEB_MY_URE_CONFIG);
        codes.add(ConfigConstants.WEB_ADMIN_URE_CONFIG);

        List<SysConfigSearchDto> dtos = sysConfigService.searchSingleByCodes(codes);
        SysConfigDomainNameDto sysConfigNoticeEmailDto = new SysConfigDomainNameDto();
        for (SysConfigSearchDto sysConfigSearchDto : dtos) {
            switch (sysConfigSearchDto.getCode()) {
                case ConfigConstants.WEB_MY_URE_CONFIG:
                    sysConfigNoticeEmailDto.setMyDomainName(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.WEB_ADMIN_URE_CONFIG:
                    sysConfigNoticeEmailDto.setAdminDomainName(sysConfigSearchDto.getValue());
                    break;
                default:
                    break;
            }
        }
        return BaseResultDto.success(sysConfigNoticeEmailDto);
    }

    @PostMapping("/domain/name/update")
    public BaseResultDto updateGlobalDomainName(@RequestBody @Validated SysConfigDomainNameEntity sysConfigDomainNameEntity, InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(), sysConfigDomainNameEntity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }
        SysConfigUpdateEntitys entitys = new SysConfigUpdateEntitys();
//        entitys.setEmail(sysConfigDomainNameEntity.getEmail());
//        entitys.setEmailCode(sysConfigDomainNameEntity.getEmailCode());
        List<BaseSysConfigUpdateEntity> updateData = new ArrayList<>(2);

        BaseSysConfigUpdateEntity baseSysConfigUpdateEntity = new BaseSysConfigUpdateEntity();
        baseSysConfigUpdateEntity.setCode(ConfigConstants.WEB_MY_URE_CONFIG);
        baseSysConfigUpdateEntity.setValue(sysConfigDomainNameEntity.getMyDomainName());
        updateData.add(baseSysConfigUpdateEntity);

        BaseSysConfigUpdateEntity baseSysConfigUpdateEntity1 = new BaseSysConfigUpdateEntity();
        baseSysConfigUpdateEntity1.setCode(ConfigConstants.WEB_ADMIN_URE_CONFIG);
        baseSysConfigUpdateEntity1.setValue(sysConfigDomainNameEntity.getAdminDomainName());
        updateData.add(baseSysConfigUpdateEntity1);

        entitys.setUpdateData(updateData);
//        entitys.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.updates(entitys);
        return BaseResultDto.success();
    }

    @PostMapping("/domain/name/update/send/code")
    public BaseResultDto updateGlobalDomainName(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    @PostMapping("/autolimit/searcher")
    public BaseResultDto searcherGlobalAutolimit() throws Exception {

        SysConfigSearchDto dto = sysConfigService.searchSingleByCode(ConfigConstants.AUTOLIMIT_DEPOSIT_CONFIG);
        SysConfigAutolimitDto sysConfigAutolimitDto = JSON.parseObject(dto.getValue(), SysConfigAutolimitDto.class);

        return BaseResultDto.success(sysConfigAutolimitDto);
    }

    @PostMapping("/autolimit/update")
    public BaseResultDto updateGlobalAutolimit(@RequestBody @Validated SysConfigAutolimitByEmailCodeEntity sysConfigDomainNameEntity, InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(), sysConfigDomainNameEntity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }
        SysConfigUpdateEntitys entitys = new SysConfigUpdateEntitys();
//        entitys.setEmail(sysConfigDomainNameEntity.getEmail());
//        entitys.setEmailCode(sysConfigDomainNameEntity.getEmailCode());
        List<BaseSysConfigUpdateEntity> updateData = new ArrayList<>(1);

        BaseSysConfigUpdateEntity baseSysConfigUpdateEntity = new BaseSysConfigUpdateEntity();
        baseSysConfigUpdateEntity.setCode(ConfigConstants.AUTOLIMIT_DEPOSIT_CONFIG);
        SysConfigAutolimitEntity sysConfigAutolimitEntity = new SysConfigAutolimitEntity();
        BeanUtils.copyProperties(sysConfigDomainNameEntity, sysConfigAutolimitEntity);
        baseSysConfigUpdateEntity.setValue(JSON.toJSONString(sysConfigAutolimitEntity));
        updateData.add(baseSysConfigUpdateEntity);

        entitys.setUpdateData(updateData);
//        entitys.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.updates(entitys);
        return BaseResultDto.success();
    }

    @PostMapping("/autolimit/update/send/code")
    public BaseResultDto updateGlobalAutolimit(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    @PostMapping("/file/limit/searcher")
    public BaseResultDto searcherGlobalFileVideoAutolimit() throws Exception {

        List<String> codes = new ArrayList<>(4);

        codes.add(ConfigConstants.UPLOAD_SIZE_MAX);
        codes.add(ConfigConstants.UPLOAD_REGEX);
        codes.add(ConfigConstants.UPLOAD_VIDEO_SIZE_MAX);
        codes.add(ConfigConstants.UPLOAD_VIDEO_REGEX);

        List<SysConfigSearchDto> dtos = sysConfigService.searchSingleByCodes(codes);
        SysConfigFileLimitDto sysConfigFileLimitDto = new SysConfigFileLimitDto();
        for (SysConfigSearchDto sysConfigSearchDto : dtos) {
            switch (sysConfigSearchDto.getCode()) {
                case ConfigConstants.UPLOAD_SIZE_MAX:
                    sysConfigFileLimitDto.setSize(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.UPLOAD_REGEX:
                    sysConfigFileLimitDto.setRegex(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.UPLOAD_VIDEO_SIZE_MAX:
                    sysConfigFileLimitDto.setSizeVideo(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.UPLOAD_VIDEO_REGEX:
                    sysConfigFileLimitDto.setRegexVideo(sysConfigSearchDto.getValue());
                    break;
                default:
                    break;
            }
        }
        return BaseResultDto.success(sysConfigFileLimitDto);
    }

    @PostMapping("/file/limit/update")
    public BaseResultDto updateGlobalFileAutolimit(@RequestBody @Validated SysConfigFileLimitEntity sysConfigDomainNameEntity, InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(), sysConfigDomainNameEntity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }
        SysConfigUpdateEntitys entitys = new SysConfigUpdateEntitys();
//        entitys.setEmail(sysConfigDomainNameEntity.getEmail());
//        entitys.setEmailCode(sysConfigDomainNameEntity.getEmailCode());
        List<BaseSysConfigUpdateEntity> updateData = new ArrayList<>(1);

        BaseSysConfigUpdateEntity baseSysConfigUpdateEntity = new BaseSysConfigUpdateEntity();
        baseSysConfigUpdateEntity.setCode(ConfigConstants.UPLOAD_SIZE_MAX);
        baseSysConfigUpdateEntity.setValue(sysConfigDomainNameEntity.getSize());
        updateData.add(baseSysConfigUpdateEntity);

        BaseSysConfigUpdateEntity configUpdateEntity = new BaseSysConfigUpdateEntity();
        configUpdateEntity.setCode(ConfigConstants.UPLOAD_REGEX);
        configUpdateEntity.setValue(sysConfigDomainNameEntity.getRegex());
        updateData.add(configUpdateEntity);

        BaseSysConfigUpdateEntity updateVideoMax = new BaseSysConfigUpdateEntity();
        updateVideoMax.setCode(ConfigConstants.UPLOAD_VIDEO_SIZE_MAX);
        updateVideoMax.setValue(sysConfigDomainNameEntity.getSizeVideo());
        updateData.add(updateVideoMax);

        BaseSysConfigUpdateEntity updateVideoRegex = new BaseSysConfigUpdateEntity();
        updateVideoRegex.setCode(ConfigConstants.UPLOAD_VIDEO_REGEX);
        updateVideoRegex.setValue(sysConfigDomainNameEntity.getRegexVideo());
        updateData.add(updateVideoRegex);

        entitys.setUpdateData(updateData);
//        entitys.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.updates(entitys);
        return BaseResultDto.success();
    }

    @PostMapping("/file/limit/update/send/code")
    public BaseResultDto updateGlobalFileAutolimit(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    @PostMapping("/notice/email/searcher")
    public BaseResultDto searcherGlobalNoticeEmail() throws Exception {
        List<String> codes = new ArrayList<>(6);

        codes.add(ConfigConstants.REGISTER_EMAIL_NOTICE);
        codes.add(ConfigConstants.LEVERAGE_EMAIL_NOTICE);
        codes.add(ConfigConstants.APPLY_EMAIL_NOTICE);
        codes.add(ConfigConstants.FINANCE_DEPOSIT_EMAIL_NOTICE);
        codes.add(ConfigConstants.FINANCE_WITHDRAW_EMAIL_NOTICE);
        codes.add(ConfigConstants.FINANCE_TRANSFER_EMAIL_NOTICE);

        List<SysConfigSearchDto> dtos = sysConfigService.searchSingleByCodes(codes);
        SysConfigNoticeEmailDto sysConfigNoticeEmailDto = new SysConfigNoticeEmailDto();
        for (SysConfigSearchDto sysConfigSearchDto : dtos) {
            switch (sysConfigSearchDto.getCode()) {
                case ConfigConstants.REGISTER_EMAIL_NOTICE:
                    sysConfigNoticeEmailDto.setRegisterEmailNotice(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.LEVERAGE_EMAIL_NOTICE:
                    sysConfigNoticeEmailDto.setLeverageEmailNotice(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.APPLY_EMAIL_NOTICE:
                    sysConfigNoticeEmailDto.setApplyEmailNotice(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.FINANCE_DEPOSIT_EMAIL_NOTICE:
                    sysConfigNoticeEmailDto.setDepositEmailNotice(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.FINANCE_WITHDRAW_EMAIL_NOTICE:
                    sysConfigNoticeEmailDto.setWithdrawEmailNotice(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.FINANCE_TRANSFER_EMAIL_NOTICE:
                    sysConfigNoticeEmailDto.setTransferEmailNotice(sysConfigSearchDto.getValue());
                    break;
                default:
                    break;
            }
        }
        return BaseResultDto.success(sysConfigNoticeEmailDto);
    }

    @PostMapping("/notice/email/update")
    public BaseResultDto updateGlobalNoticeEmail(@RequestBody @Validated SysConfigNoticeEmailByEmailCodeEntity sysConfigNoticeEmailByEmailCodeEntity, InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(),
                sysConfigNoticeEmailByEmailCodeEntity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }
        SysConfigUpdateEntitys entitys = new SysConfigUpdateEntitys();
//        entitys.setEmail(sysConfigNoticeEmailByEmailCodeEntity.getEmail());
//        entitys.setEmailCode(sysConfigNoticeEmailByEmailCodeEntity.getEmailCode());
        List<BaseSysConfigUpdateEntity> updateData = new ArrayList<>(6);

        BaseSysConfigUpdateEntity sysConfigMaxUpdateEntityRegister = new BaseSysConfigUpdateEntity();
        sysConfigMaxUpdateEntityRegister.setCode(ConfigConstants.REGISTER_EMAIL_NOTICE);
        sysConfigMaxUpdateEntityRegister.setValue(sysConfigNoticeEmailByEmailCodeEntity.getRegisterEmailNotice());
        updateData.add(sysConfigMaxUpdateEntityRegister);

        BaseSysConfigUpdateEntity sysConfigMaxUpdateEntityLeverage = new BaseSysConfigUpdateEntity();
        sysConfigMaxUpdateEntityLeverage.setCode(ConfigConstants.LEVERAGE_EMAIL_NOTICE);
        sysConfigMaxUpdateEntityLeverage.setValue(sysConfigNoticeEmailByEmailCodeEntity.getLeverageEmailNotice());
        updateData.add(sysConfigMaxUpdateEntityLeverage);

        BaseSysConfigUpdateEntity sysConfigMaxUpdateEntityApply = new BaseSysConfigUpdateEntity();
        sysConfigMaxUpdateEntityApply.setCode(ConfigConstants.APPLY_EMAIL_NOTICE);
        sysConfigMaxUpdateEntityApply.setValue(sysConfigNoticeEmailByEmailCodeEntity.getApplyEmailNotice());
        updateData.add(sysConfigMaxUpdateEntityApply);

        BaseSysConfigUpdateEntity sysConfigMaxUpdateEntityDeposit = new BaseSysConfigUpdateEntity();
        sysConfigMaxUpdateEntityDeposit.setCode(ConfigConstants.FINANCE_DEPOSIT_EMAIL_NOTICE);
        sysConfigMaxUpdateEntityDeposit.setValue(sysConfigNoticeEmailByEmailCodeEntity.getDepositEmailNotice());
        updateData.add(sysConfigMaxUpdateEntityDeposit);

        BaseSysConfigUpdateEntity sysConfigMaxUpdateEntityWithdraw = new BaseSysConfigUpdateEntity();
        sysConfigMaxUpdateEntityWithdraw.setCode(ConfigConstants.FINANCE_WITHDRAW_EMAIL_NOTICE);
        sysConfigMaxUpdateEntityWithdraw.setValue(sysConfigNoticeEmailByEmailCodeEntity.getWithdrawEmailNotice());
        updateData.add(sysConfigMaxUpdateEntityWithdraw);

        BaseSysConfigUpdateEntity sysConfigMaxUpdateEntityTransfer = new BaseSysConfigUpdateEntity();
        sysConfigMaxUpdateEntityTransfer.setCode(ConfigConstants.FINANCE_TRANSFER_EMAIL_NOTICE);
        sysConfigMaxUpdateEntityTransfer.setValue(sysConfigNoticeEmailByEmailCodeEntity.getTransferEmailNotice());
        updateData.add(sysConfigMaxUpdateEntityTransfer);

        entitys.setUpdateData(updateData);
//        entitys.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.updates(entitys);
        return BaseResultDto.success();
    }

    @PostMapping("/notice/email/update/send/code")
    public BaseResultDto updateGlobalNoticeEmail(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    @PostMapping("/automatic/searcher")
    public BaseResultDto searcherGlobalAutomatic() throws Exception {
        List<String> codes = new ArrayList<>(6);

        codes.add(ConfigConstants.AUTOMATIC_DEPOSIT_CONFIG);
        codes.add(ConfigConstants.AUTOMATIC_LEVERAGE_CONFIG);
        codes.add(ConfigConstants.AUTOMATIC_TRANSFER_CONFIG);
        codes.add(ConfigConstants.AUTOMATIC_MASTER_PASSWORD_CONFIG);
        codes.add(ConfigConstants.AUTOMATIC_INVESTOR_PASSWORD_CONFIG);
        codes.add(ConfigConstants.AUTOMATIC_ASCRIPTION_CONFIG);
        codes.add(ConfigConstants.AUTOMATIC_CREATE_ACCOUNT_CONFIG);
        codes.add(ConfigConstants.AUTOMATIC_WITHDRAW_CONFIG);
        codes.add(ConfigConstants.AUTOMATIC_KYC_WITHDRAW);
        codes.add(ConfigConstants.AUTOMATIC_KYC_FAST_WITHDRAW);

        List<SysConfigSearchDto> dtos = sysConfigService.searchSingleByCodes(codes);
        SysConfigAutomaticDto sysConfigAutomaticDto = new SysConfigAutomaticDto();
        for (SysConfigSearchDto sysConfigSearchDto : dtos) {
            switch (sysConfigSearchDto.getCode()) {
                case ConfigConstants.AUTOMATIC_DEPOSIT_CONFIG:
                    sysConfigAutomaticDto.setDepositAutomatic(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.AUTOMATIC_LEVERAGE_CONFIG:
                    sysConfigAutomaticDto.setLeverageAutomatic(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.AUTOMATIC_TRANSFER_CONFIG:
                    sysConfigAutomaticDto.setTransferAutomatic(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.AUTOMATIC_MASTER_PASSWORD_CONFIG:
                    sysConfigAutomaticDto.setMasterAutomatic(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.AUTOMATIC_INVESTOR_PASSWORD_CONFIG:
                    sysConfigAutomaticDto.setInvestorAutomatic(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.AUTOMATIC_ASCRIPTION_CONFIG:
                    sysConfigAutomaticDto.setAscriptionAutomatic(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.AUTOMATIC_CREATE_ACCOUNT_CONFIG:
                    sysConfigAutomaticDto.setAccountAutomatic(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.AUTOMATIC_WITHDRAW_CONFIG:
                    sysConfigAutomaticDto.setWithdrawAutomatic(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.AUTOMATIC_KYC_WITHDRAW:
                    sysConfigAutomaticDto.setKycWithdrawAutomatic(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.AUTOMATIC_KYC_FAST_WITHDRAW:
                    sysConfigAutomaticDto.setKycFastWithdrawAutomatic(sysConfigSearchDto.getValue());
                    break;
                default:
                    break;
            }
        }
        return BaseResultDto.success(sysConfigAutomaticDto);
    }

    @PostMapping("/automatic/update")
    public BaseResultDto updateGlobalAutomatic(@RequestBody @Validated SysConfigAutomaticByEmailCodeEntity sysConfigAutomaticByEmailCodeEntity, InfoEntity infoEntity) throws Exception {
        /*if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(),
                sysConfigAutomaticByEmailCodeEntity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }*/
        SysConfigUpdateEntitys entitys = new SysConfigUpdateEntitys();
//        entitys.setEmail(sysConfigAutomaticByEmailCodeEntity.getEmail());
//        entitys.setEmailCode(sysConfigAutomaticByEmailCodeEntity.getEmailCode());
        List<BaseSysConfigUpdateEntity> updateData = new ArrayList<>(8);

        BaseSysConfigUpdateEntity sysConfigMaxUpdateEntityRegister = new BaseSysConfigUpdateEntity();
        sysConfigMaxUpdateEntityRegister.setCode(ConfigConstants.AUTOMATIC_DEPOSIT_CONFIG);
        sysConfigMaxUpdateEntityRegister.setValue(sysConfigAutomaticByEmailCodeEntity.getDepositAutomatic());
        updateData.add(sysConfigMaxUpdateEntityRegister);

        BaseSysConfigUpdateEntity sysConfigMaxUpdateEntityLeverage = new BaseSysConfigUpdateEntity();
        sysConfigMaxUpdateEntityLeverage.setCode(ConfigConstants.AUTOMATIC_LEVERAGE_CONFIG);
        sysConfigMaxUpdateEntityLeverage.setValue(sysConfigAutomaticByEmailCodeEntity.getLeverageAutomatic());
        updateData.add(sysConfigMaxUpdateEntityLeverage);

        BaseSysConfigUpdateEntity sysConfigMaxUpdateEntityApply = new BaseSysConfigUpdateEntity();
        sysConfigMaxUpdateEntityApply.setCode(ConfigConstants.AUTOMATIC_TRANSFER_CONFIG);
        sysConfigMaxUpdateEntityApply.setValue(sysConfigAutomaticByEmailCodeEntity.getTransferAutomatic());
        updateData.add(sysConfigMaxUpdateEntityApply);

        BaseSysConfigUpdateEntity sysConfigMaxUpdateEntityMaster = new BaseSysConfigUpdateEntity();
        sysConfigMaxUpdateEntityMaster.setCode(ConfigConstants.AUTOMATIC_MASTER_PASSWORD_CONFIG);
        sysConfigMaxUpdateEntityMaster.setValue(sysConfigAutomaticByEmailCodeEntity.getMasterAutomatic());
        updateData.add(sysConfigMaxUpdateEntityMaster);

        BaseSysConfigUpdateEntity sysConfigMaxUpdateEntityInvestor = new BaseSysConfigUpdateEntity();
        sysConfigMaxUpdateEntityInvestor.setCode(ConfigConstants.AUTOMATIC_INVESTOR_PASSWORD_CONFIG);
        sysConfigMaxUpdateEntityInvestor.setValue(sysConfigAutomaticByEmailCodeEntity.getInvestorAutomatic());
        updateData.add(sysConfigMaxUpdateEntityInvestor);

        BaseSysConfigUpdateEntity sysConfigMaxUpdateEntityTransfer = new BaseSysConfigUpdateEntity();
        sysConfigMaxUpdateEntityTransfer.setCode(ConfigConstants.AUTOMATIC_ASCRIPTION_CONFIG);
        sysConfigMaxUpdateEntityTransfer.setValue(sysConfigAutomaticByEmailCodeEntity.getAscriptionAutomatic());
        updateData.add(sysConfigMaxUpdateEntityTransfer);

        BaseSysConfigUpdateEntity sysConfigUpdateEntityAccount = new BaseSysConfigUpdateEntity();
        sysConfigUpdateEntityAccount.setCode(ConfigConstants.AUTOMATIC_CREATE_ACCOUNT_CONFIG);
        sysConfigUpdateEntityAccount.setValue(sysConfigAutomaticByEmailCodeEntity.getAccountAutomatic());
        updateData.add(sysConfigUpdateEntityAccount);

        BaseSysConfigUpdateEntity sysConfigUpdateEntityWithdraw = new BaseSysConfigUpdateEntity();
        sysConfigUpdateEntityWithdraw.setCode(ConfigConstants.AUTOMATIC_WITHDRAW_CONFIG);
        sysConfigUpdateEntityWithdraw.setValue(sysConfigAutomaticByEmailCodeEntity.getWithdrawAutomatic());
        updateData.add(sysConfigUpdateEntityWithdraw);

        BaseSysConfigUpdateEntity sysConfigUpdateEntityKycWithdraw = new BaseSysConfigUpdateEntity();
        sysConfigUpdateEntityKycWithdraw.setCode(ConfigConstants.AUTOMATIC_KYC_WITHDRAW);
        sysConfigUpdateEntityKycWithdraw.setValue(sysConfigAutomaticByEmailCodeEntity.getKycWithdrawAutomatic());
        updateData.add(sysConfigUpdateEntityKycWithdraw);

        BaseSysConfigUpdateEntity sysConfigUpdateEntityKycFastWithdraw = new BaseSysConfigUpdateEntity();
        sysConfigUpdateEntityKycFastWithdraw.setCode(ConfigConstants.AUTOMATIC_KYC_FAST_WITHDRAW);
        sysConfigUpdateEntityKycFastWithdraw.setValue(sysConfigAutomaticByEmailCodeEntity.getKycFastWithdrawAutomatic());
        updateData.add(sysConfigUpdateEntityKycFastWithdraw);

        entitys.setUpdateData(updateData);
//        entitys.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.updates(entitys);
        return BaseResultDto.success();
    }

    @PostMapping("/automatic/update/send/code")
    public BaseResultDto updateGlobalAutomatic(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    /**
     * 查看全局入金通知邮件
     *
     * @return
     */
    @PostMapping("/deposit/notice/email/searcher")
    public BaseResultDto searcherGlobalDepositNoticeEmail() throws Exception {
        SysConfigSearchDto dto = sysConfigService.searchSingleByCode(ConfigConstants.FINANCE_DEPOSIT_EMAIL_NOTICE);
        return BaseResultDto.success(dto);
    }

    /**
     * 修改全局入金通知邮件
     *
     * @return
     */
    @PostMapping("/deposit/notice/email/update")
    public BaseResultDto updateGlobalDepositNoticeEmail(@RequestBody @Validated SysConfigUpdateEntity entity,
                                                        InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(), entity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }
        entity.setCode(ConfigConstants.FINANCE_DEPOSIT_EMAIL_NOTICE);
//        entity.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/deposit/notice/email/update/send/code")
    public BaseResultDto updateGlobalDepositNoticeEmailSendEmailCode(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    /**
     * 查询(查看)全局出金最大最小金额限制
     *
     * @return
     */
    @PostMapping("/withdraw/amount/searcher")
    public BaseResultDto searcherGlobalMaxAndMinWithdrawAmount() throws Exception {
        List<String> codes = new ArrayList<>(2);

        codes.add(ConfigConstants.FINANCE_WITHDRAW_MAX);
        codes.add(ConfigConstants.FINANCE_WITHDRAW_MIN);

        List<SysConfigSearchDto> dtos = sysConfigService.searchSingleByCodes(codes);
        SysConfigFinanceMaxAndMinAmountDto sysConfigFinanceMaxAndMinAmountDto = new SysConfigFinanceMaxAndMinAmountDto();
        for (SysConfigSearchDto sysConfigSearchDto : dtos) {
            switch (sysConfigSearchDto.getCode()) {
                case ConfigConstants.FINANCE_WITHDRAW_MAX:
                    sysConfigFinanceMaxAndMinAmountDto.setMaxAmount(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.FINANCE_WITHDRAW_MIN:
                    sysConfigFinanceMaxAndMinAmountDto.setMinAmount(sysConfigSearchDto.getValue());
                    break;
                default:
                    break;
            }
        }
        return BaseResultDto.success(sysConfigFinanceMaxAndMinAmountDto);
    }

    /**
     * 修改全局出金最大最小金额限制
     *
     * @return
     */
    @PostMapping("/withdraw/amount/update")
    public BaseResultDto updateGlobalMaxAndMinWithdrawAmount(@RequestBody @Validated SysConfigFinanceMaxAndMinAmountByEmailCodeEntity sysConfigFinanceMaxAndMinAmountEntity, InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(),
                sysConfigFinanceMaxAndMinAmountEntity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }

        SysConfigUpdateEntitys entitys = new SysConfigUpdateEntitys();

//        entitys.setEmail(sysConfigFinanceMaxAndMinAmountEntity.getEmail());
//        entitys.setEmailCode(sysConfigFinanceMaxAndMinAmountEntity.getEmailCode());

        List<BaseSysConfigUpdateEntity> updateData = new ArrayList<>(2);

        BaseSysConfigUpdateEntity sysConfigMaxUpdateEntity = new BaseSysConfigUpdateEntity();
        sysConfigMaxUpdateEntity.setCode(ConfigConstants.FINANCE_WITHDRAW_MAX);
        sysConfigMaxUpdateEntity.setValue(sysConfigFinanceMaxAndMinAmountEntity.getMaxAmount().toString());
        updateData.add(sysConfigMaxUpdateEntity);

        BaseSysConfigUpdateEntity sysConfigMinUpdateEntity = new BaseSysConfigUpdateEntity();
        sysConfigMinUpdateEntity.setCode(ConfigConstants.FINANCE_WITHDRAW_MIN);
        sysConfigMinUpdateEntity.setValue(sysConfigFinanceMaxAndMinAmountEntity.getMinAmount().toString());
        updateData.add(sysConfigMinUpdateEntity);
        entitys.setUpdateData(updateData);
//        entitys.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.updates(entitys);
        return BaseResultDto.success();
    }

    @PostMapping("/withdraw/amount/update/send/code")
    public BaseResultDto updateGlobalMaxAndMinWithdrawAmountSendEmailCode(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    /**
     * 查看出金内转通知邮件
     *
     * @return
     */
    @PostMapping("/withdraw/notice/email/searcher")
    public BaseResultDto searcherGlobalWithdrawNoticeEmail() throws Exception {

        SysConfigSearchDto dto = sysConfigService.searchSingleByCode(ConfigConstants.FINANCE_WITHDRAW_EMAIL_NOTICE);
        return BaseResultDto.success(dto);
    }

    /**
     * 修改全局出金通知邮件
     *
     * @return
     */
    @PostMapping("/withdraw/notice/email/update")
    public BaseResultDto updateGlobalMaxAndMinWithdrawNoticeEmail(@RequestBody @Validated SysConfigUpdateEntity entity, InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(), entity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }
        entity.setCode(ConfigConstants.FINANCE_WITHDRAW_EMAIL_NOTICE);
//        entity.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/withdraw/notice/email/update/send/code")
    public BaseResultDto updateGlobalMaxAndMinWithdrawNoticeEmailSendEmailCode(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    /**
     * 查询(查看)全局内转最大最小金额限制
     *
     * @return
     */
    @PostMapping("/transfer/amount/searcher")
    public BaseResultDto searcherGlobalMaxAndMinTransferAmount() throws Exception {
        List<String> codes = new ArrayList<>(2);

        codes.add(ConfigConstants.FINANCE_TRANSFER_MAX);
        codes.add(ConfigConstants.FINANCE_TRANSFER_MIN);

        List<SysConfigSearchDto> dtos = sysConfigService.searchSingleByCodes(codes);

        SysConfigFinanceMaxAndMinAmountDto sysConfigFinanceMaxAndMinAmountDto = new SysConfigFinanceMaxAndMinAmountDto();
        for (SysConfigSearchDto sysConfigSearchDto : dtos) {
            switch (sysConfigSearchDto.getCode()) {
                case ConfigConstants.FINANCE_TRANSFER_MAX:
                    sysConfigFinanceMaxAndMinAmountDto.setMaxAmount(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.FINANCE_TRANSFER_MIN:
                    sysConfigFinanceMaxAndMinAmountDto.setMinAmount(sysConfigSearchDto.getValue());
                    break;
                default:
                    break;
            }
        }
        return BaseResultDto.success(sysConfigFinanceMaxAndMinAmountDto);
    }

    /**
     * 修改全局内转最大最小金额限制
     *
     * @return
     */
    @PostMapping("/transfer/amount/update")
    public BaseResultDto updateGlobalMaxAndMinTransferAmount(@RequestBody @Validated SysConfigFinanceMaxAndMinAmountByEmailCodeEntity sysConfigFinanceMaxAndMinAmountEntity, InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(),
                sysConfigFinanceMaxAndMinAmountEntity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }

        SysConfigUpdateEntitys entitys = new SysConfigUpdateEntitys();
//
//        entitys.setEmail(sysConfigFinanceMaxAndMinAmountEntity.getEmail());
//        entitys.setEmailCode(sysConfigFinanceMaxAndMinAmountEntity.getEmailCode());

        List<BaseSysConfigUpdateEntity> updateData = new ArrayList<>(2);

        BaseSysConfigUpdateEntity sysConfigMaxUpdateEntity = new BaseSysConfigUpdateEntity();
        sysConfigMaxUpdateEntity.setCode(ConfigConstants.FINANCE_TRANSFER_MAX);
        sysConfigMaxUpdateEntity.setValue(sysConfigFinanceMaxAndMinAmountEntity.getMaxAmount().toString());
        updateData.add(sysConfigMaxUpdateEntity);

        BaseSysConfigUpdateEntity sysConfigMinUpdateEntity = new BaseSysConfigUpdateEntity();
        sysConfigMinUpdateEntity.setCode(ConfigConstants.FINANCE_TRANSFER_MIN);
        sysConfigMinUpdateEntity.setValue(sysConfigFinanceMaxAndMinAmountEntity.getMinAmount().toString());
        updateData.add(sysConfigMinUpdateEntity);
        entitys.setUpdateData(updateData);
//        entitys.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.updates(entitys);
        return BaseResultDto.success();
    }

    @PostMapping("/transfer/amount/update/send/code")
    public BaseResultDto updateGlobalMaxAndMinTransferAmountSendEmailCode(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    /**
     * 查看全局内转通知邮件
     *
     * @return
     */
    @PostMapping("/transfer/notice/email/searcher")
    public BaseResultDto searcherGlobalTransferNoticeEmail() throws Exception {
        SysConfigSearchDto dto = sysConfigService.searchSingleByCode(ConfigConstants.FINANCE_TRANSFER_EMAIL_NOTICE);
        return BaseResultDto.success(dto);
    }

    /**
     * 修改全局内转通知邮件
     *
     * @return
     */
    @PostMapping("/transfer/notice/email/update")
    public BaseResultDto updateGlobalMaxAndMinTransferNoticeEmail(@RequestBody @Validated SysConfigUpdateEntity entity, InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(), entity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }

        entity.setCode(ConfigConstants.FINANCE_TRANSFER_EMAIL_NOTICE);
//        entity.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/transfer/notice/email/update/send/code")
    public BaseResultDto updateGlobalMaxAndMinTransferNoticeEmailSendEmailCode(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    /**
     * 查看全局个人中心地址
     *
     * @return
     */
    @PostMapping("/web/url/my/searcher")
    public BaseResultDto searcherGlobalWebUrlMy() throws Exception {
        SysConfigSearchDto dto = sysConfigService.searchSingleByCode(ConfigConstants.WEB_MY_URE_CONFIG);
        return BaseResultDto.success(dto);
    }

    /**
     * 修改全局个人中心地址
     *
     * @return
     */
    @PostMapping("/web/url/my/update")
    public BaseResultDto updateGlobalGlobalWebUrlMy(@RequestBody @Validated SysConfigUpdateEntity entity,
                                                    InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(), entity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }
        entity.setCode(ConfigConstants.WEB_MY_URE_CONFIG);
//        entity.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/web/url/my/update/send/code")
    public BaseResultDto updateGlobalGlobalWebUrlMySendEmailCode(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    /**
     * 查看全局个人中心手机上传图片地址
     *
     * @return
     */
    @PostMapping("/web/url/my/mobile/searcher")
    public BaseResultDto searcherGlobalWebUrlMyMobile() throws Exception {
        SysConfigSearchDto dto = sysConfigService.searchSingleByCode(ConfigConstants.WEB_MY_URL_MOBILE_UPLOAD_CONFIG);
        return BaseResultDto.success(dto);
    }

    /**
     * 修改全局个人中心手机上传图片地址
     *
     * @return
     */
    @PostMapping("/web/url/my/mobile/update")
    public BaseResultDto updateGlobalGlobalWebUrlMyMobile(@RequestBody @Validated SysConfigUpdateEntity entity,
                                                          InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(), entity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }
        entity.setCode(ConfigConstants.WEB_MY_URL_MOBILE_UPLOAD_CONFIG);
//        entity.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/web/url/my/mobile/update/send/code")
    public BaseResultDto updateGlobalGlobalWebUrlMyMobileSendEmailCode(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    /**
     * 查看上传文件大小限制
     *
     * @return
     */
    @PostMapping("/upload/size/searcher")
    public BaseResultDto searcherGlobalUploadSizeMax() throws Exception {
        SysConfigSearchDto dto = sysConfigService.searchSingleByCode(ConfigConstants.UPLOAD_SIZE_MAX);
        return BaseResultDto.success(dto);
    }

    /**
     * 修改上传文件大小限制
     *
     * @return
     */
    @PostMapping("/upload/size/update")
    public BaseResultDto updateGlobalUploadSizeMax(@RequestBody @Validated SysConfigUpdateEntity entity,
                                                   InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(), entity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }
        entity.setCode(ConfigConstants.UPLOAD_SIZE_MAX);
//        entity.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/upload/size/update/send/code")
    public BaseResultDto updateGlobalUploadSizeMaxSendEmailCode(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    @PostMapping("/upload/size/update/video")
    public BaseResultDto updateGlobalUploadVideoSizeMax(@RequestBody @Validated SysConfigUpdateEntity entity,
                                                        InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(), entity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }
        entity.setCode(ConfigConstants.UPLOAD_SIZE_MAX);
//        entity.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/upload/size/update/video/send/code")
    public BaseResultDto updateGlobalUploadVideoSizeMaxSendEmailCode(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    /**
     * 查看上传文件格式限制
     *
     * @return
     */
    @PostMapping("/upload/regex/searcher")
    public BaseResultDto searcherGlobalUploadRegex() throws Exception {
        SysConfigSearchDto dto = sysConfigService.searchSingleByCode(ConfigConstants.UPLOAD_REGEX);
        return BaseResultDto.success(dto);
    }

    /**
     * 修改上传文件格式限制
     *
     * @return
     */
    @PostMapping("/upload/regex/update")
    public BaseResultDto updateGlobalUploadRegex(@RequestBody @Validated SysConfigUpdateEntity entity,
                                                 InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(), entity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }
        entity.setCode(ConfigConstants.UPLOAD_REGEX);
//        entity.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/upload/regex/update/send/code")
    public BaseResultDto updateGlobalUploadRegexSendEmailCode(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    /**
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/upload/regex/update/video")
    public BaseResultDto updateGlobalUploadVideoRegex(@RequestBody @Validated SysConfigUpdateEntity entity,
                                                      InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(), entity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }
        entity.setCode(ConfigConstants.UPLOAD_REGEX);
//        entity.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/upload/regex/update/video/send/code")
    public BaseResultDto updateGlobalUploadVideoRegexSendEmailCode(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    @PostMapping("/trade/interest/searcher")
    public BaseResultDto searcherGlobalTradeInterest() throws Exception {
        List<String> codes = new ArrayList<>(2);

        codes.add(ConfigConstants.FINANCE_TRADE_INTEREST);
        codes.add(ConfigConstants.FINANCE_TRADE_INTEREST_GROUP_TYPE);

        List<SysConfigSearchDto> dtos = sysConfigService.searchSingleByCodes(codes);

        SysConfigTradeInterestDto dto = new SysConfigTradeInterestDto();
        for (SysConfigSearchDto sysConfigSearchDto : dtos) {
            switch (sysConfigSearchDto.getCode()) {
                case ConfigConstants.FINANCE_TRADE_INTEREST:
                    dto.setTradeInterest(sysConfigSearchDto.getValue());
                    break;
                case ConfigConstants.FINANCE_TRADE_INTEREST_GROUP_TYPE:
                    dto.setTradeInterestGroupTypes(Arrays.asList(sysConfigSearchDto.getValue().split(",")));
                    break;
                default:
                    break;
            }
        }
        return BaseResultDto.success(dto);
    }

    @PostMapping("/trade/interest/update")
    public BaseResultDto updateGlobalTradeInterest(@RequestBody @Validated SysConfigTradeInterestByEmailCodeEntity entity, InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(), entity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }
        SysConfigUpdateEntitys entitys = new SysConfigUpdateEntitys();

//        entitys.setEmail(entity.getEmail());
//        entitys.setEmailCode(entity.getEmailCode());

        List<BaseSysConfigUpdateEntity> updateData = new ArrayList<>(2);

        BaseSysConfigUpdateEntity sysConfigMaxUpdateEntity = new BaseSysConfigUpdateEntity();
        sysConfigMaxUpdateEntity.setCode(ConfigConstants.FINANCE_TRADE_INTEREST);
        sysConfigMaxUpdateEntity.setValue(entity.getTradeInterest().toString());
        updateData.add(sysConfigMaxUpdateEntity);

        BaseSysConfigUpdateEntity sysConfigMinUpdateEntity = new BaseSysConfigUpdateEntity();
        sysConfigMinUpdateEntity.setCode(ConfigConstants.FINANCE_TRADE_INTEREST_GROUP_TYPE);
        sysConfigMinUpdateEntity.setValue(String.join(",", entity.getTradeInterestGroupTypes()));
        updateData.add(sysConfigMinUpdateEntity);
        entitys.setUpdateData(updateData);
//        entitys.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.updates(entitys);
        return BaseResultDto.success();
    }

    @PostMapping("/trade/interest/update/send/code")
    public BaseResultDto updateGlobalTradeInterestSendEmailCode(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    @PostMapping("/withdraw/day/limit/searcher")
    public BaseResultDto searcherWithdrawDayLimit() throws Exception {

        SysConfigSearchDto dtos = sysConfigService.searchSingleByCode(ConfigConstants.FINANCE_WITHDRAW_DAY_LIMIT);

        SysConfigWithdrawDayLimitDto dto = new SysConfigWithdrawDayLimitDto();
        dto.setDayLimit(dtos.getValue());
        return BaseResultDto.success(dto);
    }

    @PostMapping("/withdraw/day/limit/update")
    public BaseResultDto updateWithdrawDayLimit(@RequestBody @Validated SysConfigWithdrawDayLimitByEmailCodeEntity entity, InfoEntity infoEntity) throws Exception {
        if (!GoogleAuthenticatorUtil.verifyCode(infoEntity.getSecretKey(), entity.getGaCode())) {
            return BaseResultDto.error(Constants.GOOGLE_CAPTCHA_ERROR);
        }
        SysConfigUpdateEntitys entitys = new SysConfigUpdateEntitys();

//        entitys.setEmail(entity.getEmail());
//        entitys.setEmailCode(entity.getEmailCode());

        List<BaseSysConfigUpdateEntity> updateData = new ArrayList<>(2);

        BaseSysConfigUpdateEntity sysConfigUpdateEntity = new BaseSysConfigUpdateEntity();
        sysConfigUpdateEntity.setCode(ConfigConstants.FINANCE_WITHDRAW_DAY_LIMIT);
        sysConfigUpdateEntity.setValue(entity.getDayLimit().toString());
        updateData.add(sysConfigUpdateEntity);
        entitys.setUpdateData(updateData);
//        entitys.setEmail(infoEntity.getIbInfo().getEmail());
        sysConfigService.updates(entitys);
        return BaseResultDto.success();
    }

    @PostMapping("/withdraw/day/limit/update/send/code")
    public BaseResultDto updateWithdrawDayLimitSendEmailCode(InfoEntity infoEntity) throws Exception {

        return updateSendEmailCode(infoEntity);
    }

    @PostMapping("/update/swapsRatio")
    public BaseResultDto<Object> updateSwapsRatio(@RequestBody BaseSysConfigUpdateEntity entity,
                                                  InfoEntity infoEntity) throws Exception {
//        if (!PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
//            return BaseResultDto.error(Constants.SYSTEM_ERROR);
//        }
        if (StringUtils.isEmpty(entity.getValue())) {
            entity.setValue("0");
        }
        float ratio = Float.parseFloat(entity.getValue());
        if (ratio < 0 || ratio > 100) {
            return BaseResultDto.error(Constants.SYSTEM_ERROR);
        }
        sysConfigService.updateSwapsRatioByCode(infoEntity, entity);
        return BaseResultDto.success();
    }

    @PostMapping("/search/swapsRatio")
    public BaseResultDto<SysConfigSearchDto> searchSwapsRatioByCode(@RequestBody SysConfigSingleEntity entity, InfoEntity infoEntity) throws Exception {
//        if (!PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
//            return BaseResultDto.error(Constants.SYSTEM_ERROR);
//        }
        SysConfigSearchDto dto = sysConfigService.searchSingleByCode(entity.getCode());
        dto.setValue(String.valueOf(Float.parseFloat(dto.getValue()) * 100F));
        return BaseResultDto.success(dto);
    }

    /**
     * 发送修改配置邮件code(当前人员邮箱)
     *
     * @param infoEntity 登录人信息
     * @return
     * @throws Exception
     */
    private BaseResultDto updateSendEmailCode(InfoEntity infoEntity) throws Exception {
        if (infoEntity == null || infoEntity.getIbInfo() == null) {
            return BaseResultDto.error(Constants.SYSTEM_ERROR);
        }
        SendUpdateEmailCodeEntity emailCodeEntity = new SendUpdateEmailCodeEntity();

        emailCodeEntity.setEmail(infoEntity.getIbInfo().getEmail());
        emailCodeEntity.setTime(new Date());
        emailCodeEntity.setUser(infoEntity.getIbInfo().getId());
        emailCodeEntity.setIp(GetIpAndMac.getIp());

        sysConfigService.sendUpdateEmailCode(emailCodeEntity);
        return BaseResultDto.success(Constants.SEND_SUCCESS);
    }
}
