package com.ruoyi.scheduledTask;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.cs.domain.CsExpansionRecord;
import com.ruoyi.cs.domain.CsMessageInfo;
import com.ruoyi.cs.domain.CsOpenRecord;
import com.ruoyi.cs.domain.CsRenewalRecord;
import com.ruoyi.cs.service.ICsExpansionRecordService;
import com.ruoyi.cs.service.ICsOpenRecordService;
import com.ruoyi.cs.service.ICsRenewalRecordService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *  订单定时任务
 */
@Component
public class ScheduledTaskUtils {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    //账户到期提醒
    private static final String ACCOUNT_EXPIRATION_LOCK = "ACCOUNT_EXPIRATION_LOCK";

    private static final String TEST_LOCK = "TEST_LOCK";


    @Resource
    private ScheduledTaskService scheduledTaskService;

    @Resource
    private RedisCache redisCache;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private ICsExpansionRecordService iCsExpansionRecordService;

    @Resource
    private ICsRenewalRecordService iCsRenewalRecordService;

    @Resource
    private ICsOpenRecordService iCsOpenRecordService;

    /**
     * 测试
     */
    //@Scheduled(cron = "*/5 * * * * ?")
    public void testTask(){
        Date nowDate = DateUtils.getNowDate();
        if(!redisCache.hasKey(TEST_LOCK)){
            // 执行任务
            logger.info("定时任务: 测试 任务开始执行 "+nowDate);
            scheduledTaskService.testTask(TEST_LOCK);
        }else{
            logger.info("定时任务: 测试 退回{"+TEST_LOCK+":"+ nowDate+"}");
        }

    }


    /**
     * 账户到期提醒
     * 每天上午0点
     * 0 0 0 * * ?
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void accountExpiration(){
        Date nowDate = DateUtils.getNowDate();
        if(!redisCache.hasKey(ACCOUNT_EXPIRATION_LOCK)){
            // 执行任务
            logger.info("定时任务: 账户到期提醒 任务开始执行 "+nowDate);
            scheduledTaskService.accountExpirationTask(ACCOUNT_EXPIRATION_LOCK);
        }else{
            logger.info("定时任务: 账户到期提醒 退回{"+ACCOUNT_EXPIRATION_LOCK+":"+ nowDate+"}");
        }

    }

    /**
     * 备忘录提醒任务，每分钟执行一次
     */
    @Scheduled(cron = "0 */1 * * * ?")
    public void warnTask(){
        List<CsExpansionRecord> expansionMemberUserIdList = iCsExpansionRecordService.searchExpansionMemberUserIdList();// 查询到期的扩容记录列表
        if(CollectionUtils.isNotEmpty(expansionMemberUserIdList)){
            CsMessageInfo csMessageInfo = null;
            for(int i = 0;i < expansionMemberUserIdList.size();i++){
                if(iCsExpansionRecordService.searchMessageCountByBusinessId(expansionMemberUserIdList.get(i).getExpansionRecordId()) == 0){// 如果扩容记录备忘录提醒消息未创建，则创建
                    csMessageInfo = new CsMessageInfo();
                    csMessageInfo.setId(UUID.randomUUID().toString().replace("-",""));// 主键ID默认UUID
                    csMessageInfo.setTitle("扩容变更后到期");
                    csMessageInfo.setContent("扩容变更后到期");
                    csMessageInfo.setUserId(expansionMemberUserIdList.get(i).getMemberUserId());
                    csMessageInfo.setIsRead(0);// 默认未读
                    csMessageInfo.setIsDelete(0);// 默认未删除
                    csMessageInfo.setCreateTime(new Date());// 创建时间默认当前时间
                    csMessageInfo.setBusinessId(expansionMemberUserIdList.get(i).getExpansionRecordId());
                    iCsExpansionRecordService.saveMessageInfo(csMessageInfo);
                }
            }
        }
        List<CsRenewalRecord> csRenewalRecordList = iCsRenewalRecordService.searchRenewalRecordList();// 查询到期的续期记录列表
        if(CollectionUtils.isNotEmpty(csRenewalRecordList)){
            CsMessageInfo csMessageInfo = null;
            for(int i = 0;i < csRenewalRecordList.size();i++){
                if(iCsExpansionRecordService.searchMessageCountByBusinessId(csRenewalRecordList.get(i).getRenewalRecordId()) == 0){// 如果续期记录备忘录提醒消息未创建，则创建
                    csMessageInfo = new CsMessageInfo();
                    csMessageInfo.setId(UUID.randomUUID().toString().replace("-",""));// 主键ID默认UUID
                    csMessageInfo.setTitle("续期变更后到期");
                    csMessageInfo.setContent("续期变更后到期");
                    csMessageInfo.setUserId(csRenewalRecordList.get(i).getMemberUserId());
                    csMessageInfo.setIsRead(0);// 默认未读
                    csMessageInfo.setIsDelete(0);// 默认未删除
                    csMessageInfo.setCreateTime(new Date());// 创建时间默认当前时间
                    csMessageInfo.setBusinessId(csRenewalRecordList.get(i).getRenewalRecordId());
                    iCsExpansionRecordService.saveMessageInfo(csMessageInfo);
                }
            }
        }
        List<CsOpenRecord> csOpenRecordList = iCsOpenRecordService.searchOpenRecordList();// 查询到期的开户记录列表
        if(CollectionUtils.isNotEmpty(csOpenRecordList)){
            CsMessageInfo csMessageInfo = null;
            for(int i = 0;i < csOpenRecordList.size();i++){
                if(iCsExpansionRecordService.searchMessageCountByBusinessId(csOpenRecordList.get(i).getOpenRecordId()) == 0){// 如果开户记录备忘录提醒消息未创建，则创建
                    csMessageInfo = new CsMessageInfo();
                    csMessageInfo.setId(UUID.randomUUID().toString().replace("-",""));// 主键ID默认UUID
                    csMessageInfo.setTitle("开户账号到期");
                    csMessageInfo.setContent("开户账号到期");
                    csMessageInfo.setUserId(csOpenRecordList.get(i).getMemberUserId());
                    csMessageInfo.setIsRead(0);// 默认未读
                    csMessageInfo.setIsDelete(0);// 默认未删除
                    csMessageInfo.setCreateTime(new Date());// 创建时间默认当前时间
                    csMessageInfo.setBusinessId(csOpenRecordList.get(i).getOpenRecordId());
                    iCsExpansionRecordService.saveMessageInfo(csMessageInfo);
                }
            }
        }
    }

}
