package com.crm.ucard.service.impl;

import com.crm.rely.backend.core.constant.*;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.wasabi.WasabiThird3dsTransactionDto;
import com.crm.rely.backend.core.entity.system.email.SysEmailSendEntity;
import com.crm.rely.backend.core.entity.ucard.CardQueryCvvVerifyEmailCodeEntity;
import com.crm.rely.backend.core.entity.ucard.CardSendEmailCodeEntity;
import com.crm.rely.backend.core.entity.ucard.CardVerifyGoogleCodeEntity;
import com.crm.rely.backend.core.entity.wasabi.WasabiThird3dsTransactionEntity;
import com.crm.rely.backend.core.enums.GlobalPayoutMethodEnum;
import com.crm.rely.backend.core.enums.GlobalTransferTypeEnum;
import com.crm.rely.backend.core.pojo.table.SysReasonsRefusalTable;
import com.crm.rely.backend.core.pojo.table.ucard.*;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.service.EmailService;
import com.crm.rely.backend.util.*;
import com.crm.ucard.dao.repository.CardMerchantUserRepository;
import com.crm.ucard.dao.repository.SysReasonsRefusalRepository;
import com.crm.ucard.dao.repository.UcardCardRepository;
import com.crm.ucard.service.SysUcardConfigService;
import com.crm.ucard.service.UcardCardService;
import com.crm.ucard.service.UcardEmailService;
import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class UcardEmailServiceImpl implements UcardEmailService {

    @Autowired
    private CardMerchantUserRepository merchantUserRepository;
    @Autowired
    private EmailService emailService;
    @Autowired
    private SysReasonsRefusalRepository sysReasonsRefusalRepository;
    @Autowired
    private UcardCardRepository cardRepository;
    @Autowired
    private Environment environment;
    @Autowired
    private UcardCardService ucardCardService;
    @Autowired
    private SysUcardConfigService sysUcardConfigService;

    /**
     * 查询cvv发送验证码邮件
     */
    @Override
    public SysEmailSendEntity createQueryCvvSendEmailCodeSysEmailEntity(CardSendEmailCodeEntity entity) {
        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();

        Map<String, String> map = new HashMap<>(4);
        String code = GetRandom.getRandom(6);
        map.put(MapConstants.CODE, code);
        Date time = DateUtil.operationMinute(new Date(), Constants.EMAIL_REGISTER_VALIDATECODE_TIME);
        map.put(MapConstants.EXPIRE_TIME, DateUtil.formatTime(time));
        map.put(MapConstants.DATE_TIME, DateUtil.formatTime());

        sysEmailSendEntity.setAddIp(entity.getIp());
        sysEmailSendEntity.setAddTime(entity.getTime());

        sysEmailSendEntity.setUsers(entity.getEmail());
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.QUERY_CVV_CODE);
        if (CountryUtil.isCN(entity.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.CARD_QUERY_CVV_SEND_EMAIL_CODE_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.CARD_QUERY_CVV_SEND_EMAIL_CODE_EN);
        }
        sysEmailSendEntity.setMap(map);

        return sysEmailSendEntity;
    }

    /**
     * 用户信息审核成功发送邮件
     */
    @Override
    public void sendUserInfoReviewSuccessEmail(CardMerchantUserTable userTable) throws ServiceException {
        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(2);

        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG USER INFO REVIEW SUCCESS");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.USER_INFO_REVIEW_SUCCESS);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.USER_INFO_REVIEW_SUCCESS_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.USER_INFO_REVIEW_SUCCESS_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 用户信息审核失败发送邮件
     */
    @Override
    public void sendUserInfoReviewFailEmail(CardMerchantUserTable userTable, Long reason) throws ServiceException {
        String reasonsRefusal = "";
        if (reason != null) {
            SysReasonsRefusalTable rejectTable = sysReasonsRefusalRepository.getFirstById(reason);
            if (rejectTable != null) {
                if (CountryUtil.isCN(userTable.getCountry())) {
                    reasonsRefusal = rejectTable.getContent();
                } else {
                    reasonsRefusal = rejectTable.getEnContent();
                }
            }
        }
        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(3);
        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.REASONS_REFUSAL, StringUtils.isEmpty(reasonsRefusal) ? "" : reasonsRefusal);

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG USER INFO REVIEW FAILURE");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.USER_INFO_REVIEW_FAIL);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.USER_INFO_REVIEW_FAIL_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.USER_INFO_REVIEW_FAIL_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 开卡申请发送邮件通知审核人员审核
     */
    @Override
    public void sendApplyCardReviewEmail(CardApplyTable applyTable) throws Exception {
        CardMerchantUserTable userTable = merchantUserRepository.getByUniqueId(applyTable.getUniqueId());

        SysUcardConfigTable sysUcardConfigTable = sysUcardConfigService.getByCode(ConfigConstants.APPLY_CARD_REVIEW_EMAIL);
        if (sysUcardConfigTable == null) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        //构建map 用于后面邮件模版替换
        Map<String, String> map = new HashMap<>(7);
        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.EMAIL, userTable.getEmail());
        map.put(MapConstants.V_FIRST_NAME_V, userTable.getFirstName());
        map.put(MapConstants.V_LAST_NAME_V, userTable.getLastName());
        map.put(MapConstants.C_ID, String.valueOf(userTable.getCId()));
        map.put(MapConstants.PHONE, userTable.getMobile());
        map.put(MapConstants.APPLY_TIME, DateUtil.formatTime(applyTable.getAddTime()));

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG CARD APPLY CARD REVIEW");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.APPLY_CARD_REVIEW);
        sysEmailSendEntity.setMap(map);
        sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.APPLY_CARD_REVIEW);
        sysEmailSendEntity.setUsers(sysUcardConfigTable.getValue());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 开卡成功发送邮件
     */
    @Override
    public void sendOpenCardSuccessEmail(UcardCardTable cardTable) throws Exception {
        CardMerchantUserTable userTable = merchantUserRepository.getByUniqueId(cardTable.getUniqueId());
        // 实体卡卡号脱敏
        String cardNumber = MaskUtil.mask(cardTable.getCardNumber(), 6, 4);

        // 查询激活码
        WasabiThird3dsTransactionEntity entity = new WasabiThird3dsTransactionEntity();
        entity.setCId(null);
        entity.setCardNo(cardTable.getCardNo());
        String activationCode = null;
        try {
            BaseResultDto<WasabiThird3dsTransactionDto> dto = ucardCardService.getActivationCode(entity);
            activationCode = dto.getData().getValues();
        } catch (Exception e) {
            log.error("开卡成功发送邮件 - 查询卡激活码失败", e);
        }
        if (activationCode == null) {
            return;
        }

        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(4);

        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.ACTIVATION_CODE, activationCode);
        map.put(MapConstants.CARD_NUMBER, cardNumber);

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG CARD OPEN CARD SUCCESS");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.OPEN_CARD_SUCCESS);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.OPEN_CARD_SUCCESS_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.OPEN_CARD_SUCCESS_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 开卡审批失败发送邮件
     */
    @Override
    public void sendOpenCardApprovalFailEmail(CardMerchantUserTable userTable, Long reason) throws ServiceException {
        String reasonsRefusal = "";
        if (reason != null) {
            SysReasonsRefusalTable rejectTable = sysReasonsRefusalRepository.getFirstById(reason);
            if (rejectTable != null) {
                if (CountryUtil.isCN(userTable.getCountry())) {
                    reasonsRefusal = rejectTable.getContent();
                } else {
                    reasonsRefusal = rejectTable.getEnContent();
                }
            }
        }
        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(3);
        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.REASONS_REFUSAL, StringUtils.isEmpty(reasonsRefusal) ? "" : reasonsRefusal);

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG CARD OPEN CARD APPROVAL FAIL");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.OPEN_CARD_FAIL);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.OPEN_CARD_FAIL_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.OPEN_CARD_FAIL_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 激活成功发送邮件
     */
    @Override
    public void sendActivateCardSuccessEmail(UcardCardTable cardTable) throws ServiceException {
        CardMerchantUserTable userTable = merchantUserRepository.getByUniqueId(cardTable.getUniqueId());
        String cardNumber = MaskUtil.mask(cardTable.getCardNumber(), 6, 4);
        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(3);

        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.CARD_NUMBER, cardNumber);

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG CARD ACTIVATE CARD SUCCESS");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.ACTIVATE_CARD_SUCCESS);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.ACTIVATE_CARD_SUCCESS_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.ACTIVATE_CARD_SUCCESS_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 充值申请给财务发邮件
     */
    @Override
    public void sendRechargeReviewEmail(CardMerchantUserTable userTable, String cardNumber, BigDecimal amount,
                                        BigDecimal fee, Date applyTime) throws Exception {
        SysUcardConfigTable sysUcardConfigTable = sysUcardConfigService.getByCode(ConfigConstants.RECHARGE_CARD_REVIEW_EMAIL);
        if (sysUcardConfigTable == null) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        //构建map 用于后面邮件模版替换
        Map<String, String> map = new HashMap<>(10);
        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.C_ID, String.valueOf(userTable.getCId()));
        map.put(MapConstants.V_FIRST_NAME_V, userTable.getFirstName());
        map.put(MapConstants.V_LAST_NAME_V, userTable.getLastName());
        map.put(MapConstants.EMAIL, userTable.getEmail());
        map.put(MapConstants.PHONE, userTable.getMobile());
        map.put(MapConstants.CARD_NUMBER, String.valueOf(cardNumber));
        map.put(MapConstants.AMOUNT, String.valueOf(amount));
        map.put(MapConstants.FEE, String.valueOf(fee));
        map.put(MapConstants.APPLY_TIME, DateUtil.formatTime(applyTime));

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG CARD RECHARGE REVIEW");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.RECHARGE_CARD_REVIEW);
        sysEmailSendEntity.setMap(map);
        sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.RECHARGE_CARD_REVIEW);
        sysEmailSendEntity.setUsers(sysUcardConfigTable.getValue());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 充值成功发送邮件
     */
    @Override
    public void sendCardRechargeSuccessEmail(CardMerchantUserTable userTable,
                                             CardRechargeOrderTable orderTable) throws ServiceException {
        String number = MaskUtil.mask(orderTable.getCardNumber(), 6, 4);

        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(9);

        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.CARD_NUMBER, number);
        map.put(MapConstants.AMOUNT, String.valueOf(orderTable.getAmount()));
        map.put(MapConstants.FEE, String.valueOf(orderTable.getFee()));
        map.put(MapConstants.CURRENCY_TYPE, orderTable.getCurrency());
        map.put(MapConstants.RECEIVED_AMOUNT, String.valueOf(orderTable.getReceivedAmount()));
        map.put(MapConstants.RECEIVED_CURRENCY, orderTable.getReceivedCurrency());
        map.put(MapConstants.SUCCESS_TIME, orderTable.getTransactionTime());

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG CARD RECHARGE SUCCESS");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.CARD_RECHARGE_SUCCESS);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.CARD_RECHARGE_SUCCESS_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.CARD_RECHARGE_SUCCESS_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 充值审核失败发送邮件
     */
    @Override
    public void sendCardRechargeReviewFailEmail(CardMerchantUserTable userTable,
                                                CardRechargeOrderTable orderTable,
                                                Long reason) throws ServiceException {
        String reasonsRefusal = "";
        if (reason != null) {
            SysReasonsRefusalTable rejectTable = sysReasonsRefusalRepository.getFirstById(reason);
            if (rejectTable != null) {
                if (CountryUtil.isCN(userTable.getCountry())) {
                    reasonsRefusal = rejectTable.getContent();
                } else {
                    reasonsRefusal = rejectTable.getEnContent();
                }
            }
        }
        String number = MaskUtil.mask(orderTable.getCardNumber(), 6, 4);

        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(8);
        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.CARD_NUMBER, number);
        map.put(MapConstants.AMOUNT, String.valueOf(orderTable.getAmount()));
        map.put(MapConstants.FEE, String.valueOf(orderTable.getFee()));
        map.put(MapConstants.CURRENCY_TYPE, orderTable.getCurrency());
        map.put(MapConstants.APPLY_TIME, DateUtil.formatTime(orderTable.getAddTime()));
        map.put(MapConstants.REASONS_REFUSAL, StringUtils.isEmpty(reasonsRefusal) ? "" : reasonsRefusal);

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG CARD RECHARGE REVIEW FAIL");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.CARD_RECHARGE_REVIEW_FAIL);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.CARD_RECHARGE_FAIL_REVIEW_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.CARD_RECHARGE_FAIL_REVIEW_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改密码成功发送邮件
     */
    @Override
    public void sendCardUpdatePasswordSuccessEmail(CardMerchantUserTable userTable, String cardNumber) throws ServiceException {
        String number = MaskUtil.mask(cardNumber, 6, 4);
        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(3);

        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.CARD_NUMBER, number);

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG CARD UPDATE PASSWORD SUCCESS");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.CARD_PASSWORD_UPDATE_SUCCESS);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.CARD_PASSWORD_UPDATE_SUCCESS_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.CARD_PASSWORD_UPDATE_SUCCESS_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 用户上传证件照片邮件
     */
    @Override
    public void sendUserUploadPhotoEmail(CardMerchantUserTable userTable) throws ServiceException {
        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(2);

        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG USER UPLOAD PHOTO");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.USER_UPLOAD_PHOTO);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.USER_UPLOAD_PHOTO_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.USER_UPLOAD_PHOTO_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 激活码通知邮件
     */
    @Override
    public void sendUserActivationCodeEmail(CardMerchantUserTable userTable, String cardNumber,
                                            String activationCode) throws ServiceException {
        String number = MaskUtil.mask(cardNumber, 6, 4);
        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(4);

        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.ACTIVATION_CODE, activationCode);
        map.put(MapConstants.CARD_NUMBER, number);

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG CARD ACTIVATION CODE");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.ACTIVATION_CODE);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.ACTIVATION_CODE_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.ACTIVATION_CODE_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 3ds通知邮件
     */
    @Override
    public void sendUser3dsEmail(CardMerchantUserTable userTable, String cardNumber, String activationCode,
                                 WasabiThird3dsTransactionDto dto) throws ServiceException {
        String number = MaskUtil.mask(cardNumber, 6, 4);
        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(6);

        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.ACTIVATION_CODE, activationCode);
        map.put(MapConstants.CARD_NUMBER, number);
        map.put(MapConstants.MERCHANT_NAME, dto.getMerchantName());
        map.put(MapConstants.EXPIRE_TIME, DateUtil.formatTimestamp(dto.getExpirationTime()));
        // 一次性验证码

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG CARD 3DS NOTICE");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.CARD_THIRD_3DS_OTP);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.CARD_THIRD_3DS_OTP_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.CARD_THIRD_3DS_OTP_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 交易授权url通知邮件
     */
    @Override
    public void sendUserAuthUrlEmail(CardMerchantUserTable userTable, String cardNumber, String activationCode,
                                     WasabiThird3dsTransactionDto dto) throws ServiceException {
        String number = MaskUtil.mask(cardNumber, 6, 4);
        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(6);

        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.ACTIVATION_CODE, activationCode);
        map.put(MapConstants.CARD_NUMBER, number);
        map.put(MapConstants.MERCHANT_NAME, dto.getMerchantName());
        map.put(MapConstants.EXPIRE_TIME, DateUtil.formatTimestamp(dto.getExpirationTime()));
        // 一次性验证码

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG CARD AUTH URL");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.CARD_AUTH_URL);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.CARD_AUTH_URL_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.CARD_AUTH_URL_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 速汇订单审核邮件
     */
    @Override
    public void sendGlobalOrderReviewEmail(CardMerchantUserTable userTable, CardGlobalOrderTable orderTable) throws Exception {
        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        SysUcardConfigTable sysUcardConfigTable = sysUcardConfigService.getByCode(ConfigConstants.GLOBAL_ORDER_REVIEW_EMAIL);
        if (sysUcardConfigTable == null) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        Map<String, String> map = new HashMap<>(9);

        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.EMAIL, userTable.getEmail());
        map.put(MapConstants.PHONE, userTable.getMobile());
        map.put(MapConstants.AMOUNT, String.valueOf(orderTable.getDeductionAmount()));
        map.put(MapConstants.C_ID, String.valueOf(userTable.getCId()));
        map.put(MapConstants.FEE, String.valueOf(orderTable.getDeductionFee()));
        map.put(MapConstants.APPLY_TIME, DateUtil.formatTime(orderTable.getAddTime()));
        map.put(MapConstants.ORDER_NUMBER, orderTable.getMerchantOrderNo());

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("GLOBAL FAST REMITTANCE ORDER REVIEW");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.GLOBAL_ORDER_REVIEW);
        sysEmailSendEntity.setMap(map);
        sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.GLOBAL_ORDER_REVIEW);
        sysEmailSendEntity.setUsers(sysUcardConfigTable.getValue());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 速汇订单审核失败发送邮件
     */
    @Override
    public void sendGlobalOrderReviewFailEmail(CardMerchantUserTable userTable, Long reason,
                                               CardGlobalOrderTable orderTable) throws ServiceException {
        String reasonsRefusal = "";
        if (reason != null) {
            SysReasonsRefusalTable rejectTable = sysReasonsRefusalRepository.getFirstById(reason);
            if (rejectTable != null) {
                if (CountryUtil.isCN(userTable.getCountry())) {
                    reasonsRefusal = rejectTable.getContent();
                } else {
                    reasonsRefusal = rejectTable.getEnContent();
                }
            }
        }
        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(10);
        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.REASONS_REFUSAL, StringUtils.isEmpty(reasonsRefusal) ? "" : reasonsRefusal);
        map.put(MapConstants.ORDER_NUMBER, orderTable.getMerchantOrderNo());
        map.put(MapConstants.PAYOUT_CURRENCY, orderTable.getPayoutCurrency());
        map.put(MapConstants.TRANSFER_TYPE, GlobalTransferTypeEnum.matchType(orderTable.getTransferType()));
        map.put(MapConstants.PAYOUT_METHOD, GlobalPayoutMethodEnum.matchCountry(orderTable.getPayoutMethod(), userTable.getCountry()));
        map.put(MapConstants.AMOUNT, String.valueOf(orderTable.getDeductionAmount()));
        map.put(MapConstants.FEE, String.valueOf(orderTable.getDeductionFee()));
        map.put(MapConstants.APPLY_TIME, DateUtil.formatTime(orderTable.getAddTime()));

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("GLOBAL FAST REMITTANCE ORDER REVIEW FAIL");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.GLOBAL_ORDER_REVIEW_FAIL);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.GLOBAL_ORDER_REVIEW_FAIL_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.GLOBAL_ORDER_REVIEW_FAIL_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 速汇订单补充资料发送邮件
     */
    @Override
    public void sendGlobalOrderSupplementaryDataEmail(CardMerchantUserTable userTable,
                                                      CardGlobalOrderTable orderTable) throws ServiceException {

        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(3);
        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.ORDER_NUMBER, orderTable.getMerchantOrderNo());

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("GLOBAL FAST REMITTANCE ORDER SUPPLEMENTARY DATA");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.GLOBAL_ORDER_SUPPLEMENTARY_DATA);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.GLOBAL_ORDER_SUPPLEMENTARY_DATA_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.GLOBAL_ORDER_SUPPLEMENTARY_DATA_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 速汇订单取消订单发送邮件
     */
    @Override
    public void sendGlobalOrderCancelEmail(CardMerchantUserTable userTable, CardGlobalOrderTable orderTable) throws ServiceException {

        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(3);
        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.ORDER_NUMBER, orderTable.getMerchantOrderNo());

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("GLOBAL FAST REMITTANCE ORDER CANCEL");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.GLOBAL_ORDER_CANCEL);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.GLOBAL_ORDER_CANCEL_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.GLOBAL_ORDER_CANCEL_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 速汇订单交易失败发送邮件
     */
    @Override
    public void sendGlobalOrderTransactionFailEmail(CardMerchantUserTable userTable,
                                                    CardGlobalOrderTable orderTable) throws ServiceException {

        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(10);
        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.REASONS_REFUSAL, StringUtils.isEmpty(orderTable.getError()) ? "" : orderTable.getError());
        map.put(MapConstants.ORDER_NUMBER, orderTable.getMerchantOrderNo());
        map.put(MapConstants.PAYOUT_CURRENCY, orderTable.getPayoutCurrency());
        map.put(MapConstants.TRANSFER_TYPE, GlobalTransferTypeEnum.matchType(orderTable.getTransferType()));
        map.put(MapConstants.PAYOUT_METHOD, GlobalPayoutMethodEnum.matchCountry(orderTable.getPayoutMethod(), userTable.getCountry()));
        map.put(MapConstants.AMOUNT, String.valueOf(orderTable.getDeductionAmount()));
        map.put(MapConstants.FEE, String.valueOf(orderTable.getDeductionFee()));
        map.put(MapConstants.APPLY_TIME, DateUtil.formatTime(orderTable.getAddTime()));

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("GLOBAL FAST REMITTANCE ORDER TRANSACTION FAIL");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.GLOBAL_ORDER_TRANSACTION_FAIL);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.GLOBAL_ORDER_TRANSACTION_FAIL_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.GLOBAL_ORDER_TRANSACTION_FAIL_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 速汇订单交易成功发送邮件
     */
    @Override
    public void sendGlobalOrderTransactionSuccessEmail(CardMerchantUserTable userTable,
                                                       CardGlobalOrderTable orderTable) throws ServiceException {

        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(10);
        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.REASONS_REFUSAL, orderTable.getError());
        map.put(MapConstants.ORDER_NUMBER, orderTable.getMerchantOrderNo());
        map.put(MapConstants.PAYOUT_CURRENCY, orderTable.getPayoutCurrency());
        map.put(MapConstants.TRANSFER_TYPE, GlobalTransferTypeEnum.matchType(orderTable.getTransferType()));
        map.put(MapConstants.PAYOUT_METHOD, GlobalPayoutMethodEnum.matchCountry(orderTable.getPayoutMethod(), userTable.getCountry()));
        map.put(MapConstants.AMOUNT, String.valueOf(orderTable.getDeductionAmount()));
        map.put(MapConstants.FEE, String.valueOf(orderTable.getDeductionFee()));
        map.put(MapConstants.APPLY_TIME, DateUtil.formatTime(orderTable.getAddTime()));

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("GLOBAL FAST REMITTANCE ORDER TRANSACTION SUCCESS");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.GLOBAL_ORDER_TRANSACTION_SUCCESS);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.GLOBAL_ORDER_TRANSACTION_SUCCESS_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.GLOBAL_ORDER_TRANSACTION_SUCCESS_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 加密钱包回调成功给客户发送邮件
     */
    @Override
    public void sendEncryptedWalletSuccessEmail(UcardVaultodyCallbackRecordTable table) throws ServiceException {
        CardMerchantUserTable userTable = merchantUserRepository.getByUniqueId(table.getUniqueId());

        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(8);

        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.WALLET_ADDRESS, table.getAddress());
        map.put(MapConstants.AMOUNT, table.getAmount());
        map.put(MapConstants.CURRENCY_TYPE, table.getUnit());
        map.put(MapConstants.DEPOSIT_TRANSFORM_CURRENCY, "USD");
        map.put(MapConstants.TRANSFORM_AMOUNT, String.valueOf(table.getReceivedAmount()));
        map.put(MapConstants.SUCCESS_TIME, DateUtil.formatTime(table.getTimestamp()));

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("ENCRYPTED WALLET TRANSACTION SUCCESS");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.ENCRYPTED_WALLET_TRANSACTION_SUCCESS);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.ENCRYPTED_WALLET_TRANSACTION_SUCCESS_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.ENCRYPTED_WALLET_TRANSACTION_SUCCESS_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 校验谷歌验证码查询cvv码
     */
    @Override
    public BaseResultDto cardQueryCvv(CardVerifyGoogleCodeEntity entity) throws ServiceException {
        validateGoogleAuthenticatorCode(entity);
        UcardCardTable table = cardRepository.findOne(entity.getId());
        if (table == null) {
            return BaseResultDto.error(Constants.INFO_NOT_FOUND);
        }
        return BaseResultDto.success(Constants.SUCCESS, table.getCvv());
    }

    /**
     * 根据环境校验
     */
    @Override
    public void validateGoogleAuthenticatorCode(CardVerifyGoogleCodeEntity entity) throws ServiceException {
        for (int i = 0; i < environment.getActiveProfiles().length; i++) {
            if (environment.getActiveProfiles()[i].equals("prod")) {
                if (entity.getVerified() == null || !entity.getVerified().equals(1)) {
                    throw ServiceException.exception(Constants.GOOGLE_SECRET_NOT_BIND_ERROR);
                }
                if (Strings.isNullOrEmpty(entity.getSecretKey())) {
                    throw ServiceException.exception(Constants.GOOGLE_SECRET_NOT_EXIST_ERROR);
                }

                if (entity.getGaCode() == null || !GoogleAuthenticatorUtil.verifyCode(entity.getSecretKey(),
                        entity.getGaCode())) {
                    throw ServiceException.exception(Constants.GOOGLE_CAPTCHA_ERROR);
                }
                break;
            }
        }
    }

    /**
     * 查询CVV发送验证码
     */
    @Override
    public void queryCvvSendCode(CardSendEmailCodeEntity entity) throws ServiceException {
        //验证邮箱是否存在
        existByEmail(entity.getEmail());

        //构建实体
        SysEmailSendEntity sysEmailSendEntity = createQueryCvvSendEmailCodeSysEmailEntity(entity);

        //发送收据
        emailService.sendCode(sysEmailSendEntity, sysEmailSendEntity.getMap().get(MapConstants.CODE),
                Constants.EMAIL_REGISTER_VALIDATECODE_TIME * 60 * 1000);
    }

    /**
     * 验证邮箱验证码查询cvv
     */
    @Override
    public BaseResultDto queryCvvVerifyCode(CardQueryCvvVerifyEmailCodeEntity entity) throws ServiceException {
        //验证邮箱是否存在
        existByEmail(entity.getEmail());
        emailService.validateCode(entity.getEmail(), entity.getEmailCode());
        UcardCardTable table = cardRepository.getByCardNumber(entity.getCardNumber());
        return BaseResultDto.success(Constants.SUCCESS, table.getCvv());
    }


    @Override
    public void existByEmail(String email) throws ServiceException {
        boolean existByEmail = merchantUserRepository.existsByEmail(email);
        if (!existByEmail) {
            throw new ServiceException(Constants.EMAIL_IS_EXIST_ERROR);
        }
    }

    /**
     * 钱包提款发送验证码
     */
    @Override
    public void sendEmailCode(CardSendEmailCodeEntity entity) throws ServiceException {
        //验证邮箱是否存在
        existByEmail(entity.getEmail());

        //构建实体
        SysEmailSendEntity sysEmailSendEntity = cardWalletWithdrawSendEmailCodeSysEmailEntity(entity);

        //发送收据
        emailService.sendCode(sysEmailSendEntity, sysEmailSendEntity.getMap().get(MapConstants.CODE),
                Constants.EMAIL_REGISTER_VALIDATECODE_TIME * 60 * 1000);
    }

    /**
     * 钱包提款发送验证码邮件
     */
    public SysEmailSendEntity cardWalletWithdrawSendEmailCodeSysEmailEntity(CardSendEmailCodeEntity entity) {
        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();

        Map<String, String> map = new HashMap<>(4);
        String code = GetRandom.getRandom(6);
        map.put(MapConstants.CODE, code);
        Date time = DateUtil.operationMinute(new Date(), Constants.EMAIL_REGISTER_VALIDATECODE_TIME);
        map.put(MapConstants.EXPIRE_TIME, DateUtil.formatTime(time));
        map.put(MapConstants.DATE_TIME, DateUtil.formatTime());

        sysEmailSendEntity.setAddIp(entity.getIp());
        sysEmailSendEntity.setAddTime(entity.getTime());

        sysEmailSendEntity.setUsers(entity.getEmail());
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.CARD_WALLET_WITHDRAW_CODE);
        if (CountryUtil.isCN(entity.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.CARD_WALLET_WITHDRAW_SEND_EMAIL_CODE_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.CARD_WALLET_WITHDRAW_SEND_EMAIL_CODE_EN);
        }

        sysEmailSendEntity.setMap(map);

        return sysEmailSendEntity;
    }

    /**
     * 钱包充值成功邮件
     */
    @Override
    public void sendWalletRechargeSuccessEmail(UcardWalletRecordTable recordTable, CardMerchantUserTable userTable) throws ServiceException {
        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(8);

        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.AMOUNT, String.valueOf(recordTable.getAmount()));
        map.put(MapConstants.FEE, String.valueOf(recordTable.getFee()));
        map.put(MapConstants.SUCCESS_TIME, DateUtil.formatTime(recordTable.getAddTime()));

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG WALLET RECHARGE SUCCESS");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.UCARD_WALLET_RECHARGE_SUCCESS);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.UCARD_WALLET_RECHARGE_SUCCESS_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.UCARD_WALLET_RECHARGE_SUCCESS_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 钱包提现成功邮件
     */
    @Override
    public void sendWalletWithdrawalSuccessEmail(BigDecimal amount, BigDecimal fee, Date addTime, String uniqueId) throws ServiceException {
        CardMerchantUserTable userTable = merchantUserRepository.getByUniqueId(uniqueId);
        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(8);
        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.AMOUNT, String.valueOf(amount));
        map.put(MapConstants.FEE, String.valueOf(fee));
        map.put(MapConstants.SUCCESS_TIME, DateUtil.formatTime(addTime));

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG WALLET WITHDRAWAL SUCCESS");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.UCARD_WALLET_WITHDRAWAL_SUCCESS);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.UCARD_WALLET_WITHDRAWAL_SUCCESS_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.UCARD_WALLET_WITHDRAWAL_SUCCESS_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 钱包提现失败邮件
     */
    @Override
    public void sendWalletWithdrawalFailedEmail(UcardWalletWithdrawTable table) throws ServiceException {
        // 钱包地址脱敏
        String address = MaskUtil.mask(table.getAddress(), 6, 4);

        CardMerchantUserTable userTable = merchantUserRepository.getByUniqueId(table.getUniqueId());
        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(8);
        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.WALLET_ADDRESS, address);
        map.put(MapConstants.CURRENCY_TYPE, table.getReceivedCurrency());
        map.put(MapConstants.AMOUNT, String.valueOf(table.getAmount()));
        map.put(MapConstants.FEE, String.valueOf(BigDecimal.ZERO));
        map.put(MapConstants.APPLY_TIME, DateUtil.formatTime(table.getAddTime()));

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG WALLET WITHDRAWAL FAILED");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.UCARD_WALLET_WITHDRAWAL_FAILED);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.UCARD_WALLET_WITHDRAWAL_FAILED_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.UCARD_WALLET_WITHDRAWAL_FAILED_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 钱包提现申请给财务发邮件
     */
    @Override
    public void sendWalletWithdrawalReviewEmail(UcardWalletWithdrawTable table) throws Exception {
        CardMerchantUserTable userTable = merchantUserRepository.getByUniqueId(table.getUniqueId());
        SysUcardConfigTable sysUcardConfigTable = sysUcardConfigService.getByCode(ConfigConstants.WALLET_WITHDRAWAL_REVIEW_EMAIL);
        if (sysUcardConfigTable == null) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        //构建map 用于后面邮件模版替换
        Map<String, String> map = new HashMap<>(10);
        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.C_ID, String.valueOf(userTable.getCId()));
        map.put(MapConstants.V_FIRST_NAME_V, userTable.getFirstName());
        map.put(MapConstants.V_LAST_NAME_V, userTable.getLastName());
        map.put(MapConstants.EMAIL, userTable.getEmail());
        map.put(MapConstants.PHONE, userTable.getMobile());
        map.put(MapConstants.WALLET_ADDRESS, table.getAddress());
        map.put(MapConstants.AMOUNT, String.valueOf(table.getAmount()));
        map.put(MapConstants.APPLY_TIME, DateUtil.formatTime(table.getAddTime()));

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG WALLET WITHDRAWAL REVIEW");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.WALLET_WITHDRAWAL_REVIEW);
        sysEmailSendEntity.setMap(map);
        sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.WALLET_WITHDRAWAL_REVIEW);
        sysEmailSendEntity.setUsers(sysUcardConfigTable.getValue());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 审核失败发送邮件
     */
    @Override
    public void sendWalletWithdrawalReviewFailEmail(UcardWalletWithdrawTable table, Long reason) throws ServiceException {
        CardMerchantUserTable userTable = merchantUserRepository.getByUniqueId(table.getUniqueId());

        String reasonsRefusal = "";
        if (reason != null) {
            SysReasonsRefusalTable rejectTable = sysReasonsRefusalRepository.getFirstById(reason);
            if (rejectTable != null) {
                if (CountryUtil.isCN(userTable.getCountry())) {
                    reasonsRefusal = rejectTable.getContent();
                } else {
                    reasonsRefusal = rejectTable.getEnContent();
                }
            }
        }
        String address = MaskUtil.mask(table.getAddress(), 6, 4);

        //构建map 用于后面邮件模版替换
        String name = CustomInfoUtil.getName(userTable.getFirstName(), null, userTable.getLastName());
        Map<String, String> map = new HashMap<>(8);
        map.put(MapConstants.DATE_TIME, DateUtil.formatTime(new Date()));
        map.put(MapConstants.NAME, name);
        map.put(MapConstants.WALLET_ADDRESS, address);
        map.put(MapConstants.AMOUNT, String.valueOf(table.getAmount()));
        map.put(MapConstants.FEE, String.valueOf(BigDecimal.ZERO));
        map.put(MapConstants.DEPOSIT_TRANSFORM_CURRENCY, table.getReceivedCurrency());
        map.put(MapConstants.TRANSFORM_AMOUNT, String.valueOf(table.getReceivedAmount()));
        map.put(MapConstants.APPLY_TIME, DateUtil.formatTime(table.getAddTime()));
        map.put(MapConstants.REASONS_REFUSAL, StringUtils.isEmpty(reasonsRefusal) ? "" : reasonsRefusal);

        SysEmailSendEntity sysEmailSendEntity = new SysEmailSendEntity();
        sysEmailSendEntity.setSubject("CWG WALLET WITHDRAWAL REVIEW FAIL");
        sysEmailSendEntity.setEmailSendEnum(EmailSendEnum.WALLET_WITHDRAWAL_REVIEW_FAIL);
        sysEmailSendEntity.setMap(map);
        if (CountryUtil.isCN(userTable.getCountry())) {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.WALLET_WITHDRAWAL_FAIL_REVIEW_CN);
        } else {
            sysEmailSendEntity.setTemplateName(SystemEmailCodeConstants.WALLET_WITHDRAWAL_FAIL_REVIEW_EN);
        }
        sysEmailSendEntity.setUsers(userTable.getEmail());
        try {
            emailService.sendEmail(sysEmailSendEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
