package cn.pluss.platform.job;

import cn.pluss.platform.entity.UserTask;
import cn.pluss.platform.notice.NoticeService;
import cn.pluss.platform.schedule.UserTaskService;
import cn.pluss.platform.wx.impl.WxTalkServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

/**
 * @author DJH
 */
@Slf4j
@Configuration
@ConditionalOnProperty(prefix = "scheduling", name = "enabled", havingValue = "true")
public class TaskScheduleJob {

    @Autowired
    private UserTaskService userTaskService;

    @Autowired
    private NoticeService noticeService;

    /**
     * 前一天的数据播报
     */
    @Scheduled(cron = "0 1 10 * * ? ")
    public void userTask() {
        LambdaQueryWrapper<UserTask> qWrapper = Wrappers.lambdaQuery();
        qWrapper.eq(UserTask::getType, UserTask.STATUS_ENABLED);

        final List<UserTask> userTaskList = userTaskService.list(qWrapper);

        for (UserTask userTask : userTaskList) {
            try {
                if ("SCHE_NOTICE".equals(userTask.getTaskCode())) {
                    sendNotice(userTask);
                }
            } catch (Exception e) {
                log.debug("====定时通知执行异常");
                e.printStackTrace();
            }

        }
    }

    private void sendNotice(UserTask userTask) {

        if (UserTask.TYPE_ONCE.equals(userTask.getType())) {
            // 发送一次后即标记为过期
            noticeService.sendTempNotice(userTask.getArg0(), userTask.getArg1(), userTask.getArg3(), userTask.getArg2(), userTask.getUserId() + "", new HashMap<>());
            userTask.setStatus(UserTask.STATUS_EXPIRED);
            userTaskService.updateById(userTask);
            return;
        }

        if (UserTask.TYPE_DAY.equals(userTask.getType())) {
            noticeService.sendTempNotice(userTask.getArg0(), userTask.getArg1(), userTask.getArg3(), userTask.getArg2(), userTask.getUserId() + "", new HashMap<>());
            return;
        }

        Calendar calendar = Calendar.getInstance();

        if (UserTask.TYPE_WEEK.equals(userTask.getType())) {
            final int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            if (dayOfWeek == Calendar.SUNDAY) {
                noticeService.sendTempNotice(userTask.getArg0(), userTask.getArg1(), userTask.getArg3(), userTask.getArg2(), userTask.getUserId() + "", new HashMap<>());
            } else {
                return;
            }
        }

        if (UserTask.TYPE_MONTH.equals(userTask.getType())) {
            final int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            if (dayOfMonth == 1) {
                noticeService.sendTempNotice(userTask.getArg0(), userTask.getArg1(), userTask.getArg3(), userTask.getArg2(), userTask.getUserId() + "", new HashMap<>());
            }
        }
    }
}
