package com.qmth.distributed.print.business.service.impl;

import com.qmth.distributed.print.business.entity.ExamTask;
import com.qmth.distributed.print.business.enums.ExamStatusEnum;
import com.qmth.distributed.print.business.mapper.ExamTaskMapper;
import com.qmth.distributed.print.business.service.BasicMessageService;
import com.qmth.distributed.print.business.service.SmsSendService;
import com.qmth.teachcloud.common.entity.SysConfig;
import com.qmth.teachcloud.common.enums.MessageEnum;
import com.qmth.teachcloud.common.service.SysConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SmsSendServiceImpl implements SmsSendService {

    @Resource
    SysConfigService sysConfigService;

    @Resource
    BasicMessageService basicMessageService;

    @Resource
    ExamTaskMapper examTaskMapper;

    /**
     * 统计截止到当天为止的即将逾期的审核待办任务总数
     */
    @Override
    public void sendSmsExpireTask() {
        SysConfig sysConfig = sysConfigService.getByKey("sys.warning.days");
        if (sysConfig != null && StringUtils.isNotBlank(sysConfig.getConfigValue())) {
            Integer configValue = Integer.valueOf(sysConfig.getConfigValue());
            // 当天15点
            Calendar c1 = Calendar.getInstance();
            c1.set(Calendar.HOUR_OF_DAY, 15);
            c1.set(Calendar.MINUTE, 0);
            c1.set(Calendar.SECOND, 0);
            long startTime = c1.getTimeInMillis();
            // 3天后
            Calendar c2 = Calendar.getInstance();
            c2.setTime(c1.getTime());
            c2.add(Calendar.DATE, configValue);
            long endTime = c2.getTimeInMillis();
            // 命题任务到期预警
            String[] examTaskStatus1 = {ExamStatusEnum.SUBMIT.name()};
            List<ExamTask> examTasks1 = examTaskMapper.listExamTaskExpire(startTime, endTime, examTaskStatus1);
            if (examTasks1 != null && examTasks1.size() > 0) {
                Map<String, List<String>> listMap = examTasks1.stream().collect(Collectors.groupingBy(m -> m.getSchoolId() + "#" + m.getUserId(), Collectors.mapping(m -> m.getId().toString(), Collectors.toList())));
                for (Map.Entry<String, List<String>> entry : listMap.entrySet()) {
                    String[] keys = entry.getKey().split("#");
                    Long schoolId = Long.valueOf(keys[0]);
                    Long userId = Long.valueOf(keys[1]);
                    basicMessageService.sendNoticeExpireOrOverdue(schoolId, MessageEnum.NOTICE_OF_EXAM_TASK_WILL_EXPIRE, userId, entry.getValue());
                }
            }
            // 命题分配到期预警
            String[] examTaskStatus2 = {ExamStatusEnum.SUBMIT.name()};
            List<ExamTask> examTasks2 = examTaskMapper.listExamTaskExpire(startTime, endTime, examTaskStatus2);
            if (examTasks2 != null && examTasks2.size() > 0) {
                Map<String, List<String>> listMap = examTasks2.stream().collect(Collectors.groupingBy(m -> m.getSchoolId() + "#" + m.getCreateId(), Collectors.mapping(m -> m.getId().toString(), Collectors.toList())));
                for (Map.Entry<String, List<String>> entry : listMap.entrySet()) {
                    String[] keys = entry.getKey().split("#");
                    Long schoolId = Long.valueOf(keys[0]);
                    Long userId = Long.valueOf(keys[1]);
                    basicMessageService.sendNoticeExpireOrOverdue(schoolId, MessageEnum.NOTICE_OF_ALLOCATION_WILL_EXPIRE, userId, entry.getValue());
                }
            }

            // 审核待办到期预警
            List<ExamTask> examTasks3 = examTaskMapper.listExamTaskAuditExpire(startTime, endTime);
            if (examTasks3 != null && examTasks3.size() > 0) {
                Map<String, List<String>> listMap = examTasks3.stream().collect(Collectors.groupingBy(m -> m.getSchoolId() + "#" + m.getCreateId(), Collectors.mapping(m -> m.getId().toString(), Collectors.toList())));
                for (Map.Entry<String, List<String>> entry : listMap.entrySet()) {
                    String[] keys = entry.getKey().split("#");
                    Long schoolId = Long.valueOf(keys[0]);
                    Long userId = Long.valueOf(keys[1]);
                    basicMessageService.sendNoticeExpireOrOverdue(schoolId, MessageEnum.NOTICE_OF_AUDIT_WILL_EXPIRE, userId, entry.getValue());
                }
            }
        }
    }

    /**
     * 统计当天逾期的审核任务增量
     */
    @Override
    public void sendSmsOverdueTask() {
        // 当天9点
        Calendar c1 = Calendar.getInstance();
        c1.set(Calendar.HOUR_OF_DAY, 9);
        c1.set(Calendar.MINUTE, 0);
        c1.set(Calendar.SECOND, 0);
        long endTime = c1.getTimeInMillis();

        // 1天前
        Calendar cal = Calendar.getInstance();
        cal.setTime(c1.getTime());
        cal.add(Calendar.DATE, -1);
        long startTime = cal.getTimeInMillis();
        // 命题任务已逾期
        String[] examTaskStatus1 = {ExamStatusEnum.SUBMIT.name()};
        List<ExamTask> examTasks1 = examTaskMapper.listExamTaskExpire(startTime, endTime, examTaskStatus1);
        if (examTasks1 != null && examTasks1.size() > 0) {
            Map<String, List<String>> listMap = examTasks1.stream().collect(Collectors.groupingBy(m -> m.getSchoolId() + "#" + m.getUserId(), Collectors.mapping(m -> m.getId().toString(), Collectors.toList())));
            for (Map.Entry<String, List<String>> entry : listMap.entrySet()) {
                String[] keys = entry.getKey().split("#");
                Long schoolId = Long.valueOf(keys[0]);
                Long userId = Long.valueOf(keys[1]);
                basicMessageService.sendNoticeExpireOrOverdue(schoolId, MessageEnum.NOTICE_OF_EXAM_TASK_OVERDUE, userId, entry.getValue());
            }
        }
        // 命题分配已逾期
        String[] examTaskStatus2 = {ExamStatusEnum.SUBMIT.name()};
        List<ExamTask> examTasks2 = examTaskMapper.listExamTaskExpire(startTime, endTime, examTaskStatus2);
        if (examTasks2 != null && examTasks2.size() > 0) {
            Map<String, List<String>> listMap = examTasks2.stream().collect(Collectors.groupingBy(m -> m.getSchoolId() + "#" + m.getCreateId(), Collectors.mapping(m -> m.getId().toString(), Collectors.toList())));
            for (Map.Entry<String, List<String>> entry : listMap.entrySet()) {
                String[] keys = entry.getKey().split("#");
                Long schoolId = Long.valueOf(keys[0]);
                Long userId = Long.valueOf(keys[1]);
                basicMessageService.sendNoticeExpireOrOverdue(schoolId, MessageEnum.NOTICE_OF_ALLOCATION_OVERDUE, userId, entry.getValue());
            }
        }

        // 审核待办已逾期
        List<ExamTask> examTasks3 = examTaskMapper.listExamTaskAuditExpire(startTime, endTime);
        if (examTasks3 != null && examTasks3.size() > 0) {
            Map<String, List<String>> listMap = examTasks3.stream().collect(Collectors.groupingBy(m -> m.getSchoolId() + "#" + m.getCreateId(), Collectors.mapping(m -> m.getId().toString(), Collectors.toList())));
            for (Map.Entry<String, List<String>> entry : listMap.entrySet()) {
                String[] keys = entry.getKey().split("#");
                Long schoolId = Long.valueOf(keys[0]);
                Long userId = Long.valueOf(keys[1]);
                basicMessageService.sendNoticeExpireOrOverdue(schoolId, MessageEnum.NOTICE_OF_AUDIT_OVERDUE, userId, entry.getValue());
            }
        }
    }
}
