package com.zd.laboratory.utils;

import com.zd.common.core.domain.R;
import com.zd.common.core.utils.StringUtils;
import com.zd.laboratory.domain.LabMessageDynamic;
import com.zd.laboratory.domain.LabNegativelistHistory;
import com.zd.laboratory.domain.vo.LabBlacklistVO;
import com.zd.laboratory.domain.vo.LabSecurityApplyVO;
import com.zd.laboratory.mapper.LabMessageContentMapper;
import com.zd.laboratory.service.ILabBlacklistService;
import com.zd.laboratory.service.ILabMessageDynamicService;
import com.zd.laboratory.service.ILabMessageUserService;
import com.zd.laboratory.service.ILabNegativeListRecordsService;
import com.zd.system.api.exam.RemoteExamService;
import com.zd.system.api.laboratory.domain.LabMessageContent;
import com.zd.system.api.laboratory.domain.LabMessageUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.config.Task;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @Author: liujh
 * @Date: 2021/12/21/19:44
 * @Description:
 */
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class InblackListScheduleTask {

    @Autowired
    private ILabNegativeListRecordsService iLabNegativeListRecordsService;
    @Autowired
    private RemoteExamService remoteExamService;

    @Autowired
    private ILabBlacklistService iLabBlacklistService;

    @Autowired
    private ILabMessageDynamicService labMessageDynamicService;

    @Autowired
    private ILabMessageUserService labMessageUserService;

    @Autowired
    private LabMessageContentMapper labMessageContentMapper;

    /**
     * 每10分钟执行一次    //    @Scheduled(cron = "0 47 12 ? * *")
     */
    @Scheduled(cron = "0 */10 * * * ?")
    public void configureTasks() {
        System.err.println("执行定时任务时间: " + LocalDateTime.now());
        Map <String,Object> markconfigMap = new HashMap <>();
        R<Object> data = remoteExamService.getPointsConfig(markconfigMap);
        if (data!=null) {
            String code = data.getCode() + "";
            if (code.equals("200")) {
                Map <String, Object> pointsConfig = (Map <String, Object>) data.getData();
                R list = iLabNegativeListRecordsService.getExpireTimeData();
                List <LabNegativelistHistory> labNegativelistHistoryList = (List <LabNegativelistHistory>) list.getData();
                for (LabNegativelistHistory history : labNegativelistHistoryList) {

                    Long deducPoints = history.getCreditScore() - Long.parseLong(pointsConfig.get("blackScore")+"");
                    if(deducPoints>0){
                        //这里扣减信用分
                        if(deducPointsFun(history.getUserId(),deducPoints)){
                            //这里移入黑名单
                            LabBlacklistVO labBlacklistVO = new LabBlacklistVO();
                            labBlacklistVO.setJoinUserId(history.getUserId());
                            labBlacklistVO.setCreditScore(deducPoints+"");
                            labBlacklistVO.setSurplusCreditScore((history.getCreditScore()-deducPoints)+"");
                            labBlacklistVO.setReason("从负面清单移入到黑名单");
                            labBlacklistVO.setFlagAction(true);
                            iLabBlacklistService.addBlacklist(labBlacklistVO);
                            //最后移除负面清单
                            iLabNegativeListRecordsService.updateByUserId(history.getUserId(),history.getCreditScore()-deducPoints);

//                            //逾期消息通知
//                            LabMessageDynamic labMessageDynamic = new LabMessageDynamic();
//                            labMessageDynamic.setSendRange(3);
//                            labMessageDynamic.setMessClass(3);
//                            labMessageDynamic.setContent("您有违规已经逾期，请及时处理，详细请登录系统查看具体处理方式方法。");
//                            labMessageDynamic.setDynamicId(labBlacklistVO.getId());
//                            labMessageDynamic.setRemark("违规逾期通知");
//                            labMessageDynamic.setCreateBy("系统用户");
//                            labMessageDynamic.setCreateTime(new Date());
//                            int dynamicId = labMessageDynamicService.insertLabMessageDynamicByTask(labMessageDynamic);
//                            if(dynamicId>0){
//                                //给逾期未处理的人发送
//                                LabMessageUser labMessageUser = new LabMessageUser();
//                                labMessageUser.setJoinUserId(history.getUserId());
//                                labMessageUser.setIsRead(false);
//                                labMessageUser.setMessageId(labMessageDynamic.getId());
//                                labMessageUserService.insertLabMessageUser(labMessageUser);
//                            }

                            //逾期消息通知
                            LabMessageContent labMessageContent = new LabMessageContent();
                            labMessageContent.setSendRange(3);
                            labMessageContent.setMessClass(1);
                            labMessageContent.setContent("您有违规已经逾期，请及时处理，详细请登录系统查看具体处理方式方法。");
                            labMessageContent.setUserIds(history.getUserId()+"");
                            labMessageContent.setUserId(-2L);
                            labMessageContent.setCreateTime(new Date());
                            labMessageContent.setCreateBy("系统");
                            labMessageContentMapper.insertLabMessageContent(labMessageContent);

                        }
                    }
                }
            }
        }
    }


    private boolean deducPointsFun(Long joinUserId,Long deductPoints){
        Map <String,Object> deducPointsMap = new HashMap <>();
        deducPointsMap.put("joinUserId",joinUserId);
        deducPointsMap.put("deductPoints",deductPoints);
        deducPointsMap.put("pointsType",1);
        deducPointsMap.put("reason","没有在指定时间完成负面清单学习，扣减信用分移入黑名单！！");
        R<Object> data = remoteExamService.deductionPoints(deducPointsMap);
        if (data!=null) {
            String code = data.getCode() + "";
            if (code.equals("200")) {
                return true;
            }
        }
        return false;
    }
}
