/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.workcalendar.service;

import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.modules.workcalendar.dao.WorkCalendarDao;
import com.jeeplus.modules.workcalendar.entity.WorkCalendar;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 工作日历Service
 * @author ssrh
 * @version 2018-07-17
 */
@Service
@Lazy(false)
@Transactional(readOnly = true)
public class WorkCalendarTaskService  {

    private Logger logger = LoggerFactory.getLogger(WorkCalendarTaskService.class);
    private static final ExecutorService threadPool = Executors.newFixedThreadPool(10);

    @Autowired
    private WorkCalendarService workCalendarService;

    @Scheduled(cron= "0 0/1 * * * ?")
    public void notifyTask() {
        logger.info("-----------定时任务开始------------------");
        //查询待发送提醒的日历
        WorkCalendar select = new WorkCalendar();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND,0);
        calendar.set(Calendar.SECOND,0);
        select.setRemindTime(calendar.getTime());
        calendar.add(Calendar.MINUTE,1);
        select.setNextRemindTime(calendar.getTime());
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        select.setStartDate(calendar.getTime());
        select.setRemind("1");
        select.setWeekDay(DateUtils.getWeek());
        select.setMonthDay(DateUtils.getDay());
        List<WorkCalendar> totalList = workCalendarService.findTaskList(select);
        if(totalList!=null&&totalList.size()>0) {
            for (final WorkCalendar workCalendar : totalList) {
                workCalendarService.queryDetails(workCalendar);
                threadPool.execute(new Runnable() {
                    @Override
                    public void run() {
                        if("1".equals(workCalendar.getSendDaily())) {
                            workCalendarService.createDailyRoutine(workCalendar);
                        }
                        workCalendarService.notifyUsers(workCalendar);
                    }
                });
            }
        }
        logger.info("------------定时任务结束------------------");
    }
    @Scheduled(cron= "0 0 0 * * ?")
    public void notifyTask2() {
        logger.info("-----------凌晨发送日常事务任务开始------------------");
        //查询不提醒的日历
        WorkCalendar select = new WorkCalendar();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        select.setStartDate(calendar.getTime());
        select.setRemind("0");
        select.setRepeatType("1");
        select.setWeekDay(DateUtils.getWeek());
        select.setMonthDay(DateUtils.getDay());
        List<WorkCalendar> totalList = workCalendarService.findTaskList(select);
        if(totalList!=null&&totalList.size()>0) {
            for (final WorkCalendar workCalendar : totalList) {
                workCalendarService.queryDetails(workCalendar);
                threadPool.execute(new Runnable() {
                    @Override
                    public void run() {
                        if("1".equals(workCalendar.getSendDaily())) {
                            workCalendarService.createDailyRoutine(workCalendar);
                        }
                    }
                });
            }
        }
        logger.info("-----------凌晨发送日常事务任务开始------------------");
    }
}