package com.iformall.service.impl;

import com.iformall.common.IdWorker;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.domain.po.WxMsgLimit;
import com.iformall.enums.EnumMsgLimitType;
import com.iformall.mapper.WxMsgLimitMapper;
import com.iformall.service.WxMsgLimitService;
import com.iformall.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author luozukai
 * 消息发送限制检查
 */
@Service
public class WxMsgLimitServiceImpl implements WxMsgLimitService {
    @Autowired
    private WxMsgLimitMapper wxMsgLimitMapper;

    private List<WxMsgLimit> findList(WxMsgLimit record) {
        return wxMsgLimitMapper.findList(record);
    }

    private void add(WxMsgLimit record){
        final IdWorker idWorker = IdWorker.get();
        // 删除老的, 手机, openId会出现旧数据，删掉
        if(record.getType().equals(EnumMsgLimitType.SMS_VALID.getCode()) ||
                record.getType().equals(EnumMsgLimitType.SMS_SALE.getCode()) ||
                record.getType().equals(EnumMsgLimitType.WEAPP_OPENID_SEND.getCode()) ||
                record.getType().equals(EnumMsgLimitType.WXCHAT_OPENID_SEND.getCode())
            ){
            wxMsgLimitMapper.delLimitNum(record);
        }
        // 新增，根据类型获取限制条数
        setLimitNum(record);
        // 保存
        record.setId(idWorker.nextId());
        wxMsgLimitMapper.insert(record);
    }

    private WxMsgLimit setLimitNum(WxMsgLimit record) {
        Date curDate = new Date();
        if(record.getType().equals(EnumMsgLimitType.SMS_VALID.getCode())) {
            // 短信 - 验证码， 24小时内10条
            record.setLimitNum(9);
            Date limitDate = DateUtils.getTimeAfterDays(1, curDate);
            record.setLimitDate(limitDate);
        } else if(record.getType().equals(EnumMsgLimitType.SMS_SALE.getCode())) {
            // 短信 - 营销， 24小时内3条
            record.setLimitNum(2);
            Date limitDate = DateUtils.getTimeAfterDays(1, curDate);
            record.setLimitDate(limitDate);
        } else if(record.getType().equals(EnumMsgLimitType.WEAPP_PREPAYID.getCode())) {
            // 小程序prepayId， 7天内3条
            record.setLimitNum(2);
            Date limitDate = DateUtils.getTimeAfterDays(7, curDate);
            record.setLimitDate(limitDate);
        } else if(record.getType().equals(EnumMsgLimitType.WEAPP_FORMID.getCode())) {
            // 小程序formId， 7天内1条
            record.setLimitNum(0);
            Date limitDate = DateUtils.getTimeAfterDays(7, curDate);
            record.setLimitDate(limitDate);
        } else if(record.getType().equals(EnumMsgLimitType.WEAPP_OPENID_SEND.getCode())) {
            // 小程序openId， 1天内1条
            record.setLimitNum(0);
            Date limitDate = DateUtils.getTimeAfterDays(1, curDate);
            record.setLimitDate(limitDate);
        } else if(record.getType().equals(EnumMsgLimitType.WXCHAT_OPENID_SEND.getCode())) {
            // 公众号openId， 1天内1条
            record.setLimitNum(0);
            Date limitDate = DateUtils.getTimeAfterDays(1, curDate);
            record.setLimitDate(limitDate);
        } else {
            // 未知的，暂定1天内1条
            record.setLimitNum(0);
            Date limitDate = DateUtils.getTimeAfterDays(1, curDate);
            record.setLimitDate(limitDate);
        }
        return record;
    }

    private int updateLimit(WxMsgLimit record) {
        int ret = wxMsgLimitMapper.updateLimitNum(record);
        return ret;
    }

    @Override
    public boolean checkIsLimit(WxMsgLimit record) {
        if(record.getExpireDate() == null) {
            record.setExpireDate(new Date());
        }
        record.setSortColumns(BaseEntity.SortField.LimitDate_DESC, BaseEntity.SortField.Id_DESC);
        List<WxMsgLimit> list = wxMsgLimitMapper.findList(record);
        if(list.size() > 0) {
            // 已有限制条件
            WxMsgLimit msgLimit = list.get(0);
            if(msgLimit.getLimitNum() > 0) {
                int ret = updateLimit(msgLimit);
                if(ret > 0) {
                    // 更新失败，未达到限制条件
                    return false;
                } else {
                    // 更新失败，已达到限制条件
                    return true;
                }
            } else {
                // 已达到限制条件，不发
                return true;
            }
        } else {
            // 没有限制条件
            add(record);
            return false;
        }

    }
}
