package com.xhpc.activity.service.impl;

import com.xhpc.activity.domain.XhpcClearingBankDomain;
import com.xhpc.activity.domain.XhpcOperatorDomain;
import com.xhpc.activity.mapper.XhpcClearingBankMapper;
import com.xhpc.activity.mapper.XhpcOperatorMapper;
import com.xhpc.activity.service.XhpcClearingBankService;
import com.xhpc.common.api.SmsService;
import com.xhpc.common.core.exception.CustomException;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.redis.service.RedisService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;


@Service
public class XhpcClearingBankServiceImpl implements XhpcClearingBankService {


    @Resource
    XhpcClearingBankMapper bankMapper;

    @Resource
    XhpcOperatorMapper operatorMapper;

    @Resource
    SmsService smsService;

    @Resource
    RedisService redisService;


    @Override
    public List<XhpcClearingBankDomain> getPage(Map<String, Object> params){

        return bankMapper.selectByParams(params);
    }


    @Override
    public XhpcClearingBankDomain getDomainByPk(Long bankId){
        return bankMapper.selectByPrimaryKey(bankId);

    }


    @Override
    public boolean insertDomain(XhpcClearingBankDomain domain){

        if(StringUtils.isEmpty(domain.getContactPhone()) || StringUtils.isEmpty(domain.getContactName())
                || StringUtils.isEmpty(domain.getBankNo()) || StringUtils.isEmpty(domain.getBankName())
                || StringUtils.isEmpty(domain.getCode())){
            throw new CustomException("必填项不能为空");
        }

        String key = "opbind:" + domain.getContactPhone();
        String redisCode = redisService.getCacheObject(key);
        if(StringUtils.isEmpty(redisCode)){
            throw new CustomException("短信验证码已失效，请重新发送");
        }

        if(!redisCode.equals(domain.getCode())){
            throw new CustomException("短信验证码错误，请重试");
        }

        XhpcOperatorDomain operatorDomain = operatorMapper.selectByPrimaryKey(domain.getOperatorId());
        if(operatorDomain == null){
            throw new CustomException("运营商ID是：" + domain.getOperatorId() + " 的运营商不存在");
        }

        if(StringUtils.isEmpty(domain.getOperatorName())){
            domain.setOperatorName(operatorDomain.getName());
            domain.setOperatorAccount(operatorDomain.getName());
        }

        bankMapper.deleteLogicByOperatorId(domain.getOperatorId());
        return bankMapper.insert(domain) > 0;
    }


    @Override
    public boolean updateDomain(XhpcClearingBankDomain domain){
        String key = "opbind:" + domain.getContactPhone();
        String redisCode = redisService.getCacheObject(key);
        if(StringUtils.isEmpty(redisCode)){
            throw new CustomException("短信验证码已失效，请重新发送");
        }

        if(!redisCode.equals(domain.getCode())){
            throw new CustomException("短信验证码错误，请重试");
        }

        return bankMapper.updateByPrimaryKey(domain) > 0;
    }


    @Override
    public boolean deleteByPk(Long bankId){

        return bankMapper.deleteLogicByPrimaryKey(bankId) > 0;
    }


    @Override
    public boolean sendBindBankSms(String phone){
        String random = getRandom();

        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("phone", phone);
        paramMap.put("code", random);
        paramMap.put("content", "您的动态码为：" + random + "，您正在进行绑定对公银行账户操作，如非本人操作，请忽略本短信！");
        smsService.sendNotice(paramMap);

        String key = "opbind:" + phone;
        redisService.setCacheObject(key, random, 300L, TimeUnit.SECONDS);

        return true;
    }

    @Override
    public boolean sendCashSms(String phone){
        String random = getRandom();
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("phone", phone);
        paramMap.put("code", random);
        paramMap.put("content", "【翔桦充电】您的动态码为：" + random + "，您正在进行提现到银行账户操作，如非本人操作，请忽略本短信！");
        smsService.sendNotice(paramMap);

        String key = "opcash:" + phone;
        redisService.setCacheObject(key, random, 300L, TimeUnit.SECONDS);
        return true;
    }


    private String getRandom(){
        Random rnd = new Random();
        int i = rnd.nextInt(999999);
        if(i<100000){
            i=i+100000;
        }
        return i+"";
    }

}
