package com.jeequan.jeepay.mch.mq;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.bizcommons.manage.email.EmailManager;
import com.jeequan.jeepay.bizcommons.manage.sms.SmsManager;
import com.jeequan.jeepay.components.mq.model.AccountBalanceMQ;
import com.jeequan.jeepay.components.mq.vender.IMQSender;
import com.jeequan.jeepay.core.cache.RedisUtil;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.AccountRuleExt;
import com.jeequan.jeepay.core.model.smsconfig.SmsBizDiyContentModel;
import com.jeequan.jeepay.db.entity.AccountInfo;
import com.jeequan.jeepay.service.impl.AccountInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * TODO
 * 接收MQ消息
 * @author crystal
 * @date 2023/12/19 15:41
 */
@Slf4j
@Component
public class AccountBalanceMQReceiver implements AccountBalanceMQ.IMQReceiver {

    @Autowired private AccountInfoService accountInfoService;
    @Autowired private SmsManager smsManager;
    @Autowired private EmailManager emailManager;
    @Autowired private IMQSender imqSender;

    @Override
    public void receive(AccountBalanceMQ.MsgPayload payload) {
        log.info("进入账户余额mq消息");
        String accountNo = payload.getAccountNo();
        AccountInfo info = accountInfoService.getById(accountNo);
        if(info == null){
            return ;
        }
        if(AccountInfo.REMIND_OPNE.equals(info.getRemindState())){
            Long balacne = info.getBalacne();
            AccountRuleExt rule = info.getRemindRuleExt().toJavaObject(AccountRuleExt.class);
            BigDecimal calcAmount = new BigDecimal(balacne).divide(new BigDecimal(100));
            //触发通知
            if(rule.getAmount().compareTo(calcAmount) >= 0){
                List<String> typeList = rule.getTypeList();
                List<JSONObject> receiveList = rule.getReceiveList();
                for (JSONObject item:receiveList) {
                    if(typeList.contains(AccountRuleExt.EMAIL)){
                        String[] toArr = {item.getString(AccountRuleExt.EMAIL)};
                        String body = "【收银呗】尊敬的用户，截至"+ DateUtil.format(new DateTime(), DatePattern.NORM_DATETIME_PATTERN) +"，您的退款账户剩余可用额度为"+calcAmount.toPlainString()+"元，为了避免欠费导致产品停止服务，请您尽快前往后台充值，祝您生活愉快";
                        emailManager.sendEmail(toArr,null,"账户余额不足提醒",body,null,null);
                    }
                    if(typeList.contains(AccountRuleExt.PHONE)){
                        JSONObject extra = new JSONObject();
                        extra.put("name",AccountInfo.Type.getVal(info.getType()).getDesc());
                        extra.put("amount",new BigDecimal(balacne).divide(new BigDecimal(100)).toPlainString());
                        SmsBizDiyContentModel model = SmsBizDiyContentModel.genModel(item.getString(AccountRuleExt.PHONE), CS.SMS_TYPE_API_ENUM.TYPE_ACCOUNT_BALANCE, extra);
                        smsManager.sendDiyContentSms(model);
                    }
                }
            }
            imqSender.send(AccountBalanceMQ.build(info.getAccountNo()),rule.getInterval() * 60 * 60);
        }else{
            RedisUtil.set(info.getAccountNo(),false);
        }
    }
}
