UpgradeClassJob.java 4.19 KB
package com.meishu.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.meishu.mapper.ClassesDictHistoryMapper;
import com.meishu.mapper.ClassesDictMapper;
import com.meishu.model.ClassesDictDO;
import com.meishu.model.ClassesDictHistoryDO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 定时任务 每天凌晨查看班级里是否有需要晋升的
 */

@Component
public class UpgradeClassJob {

    @Autowired
    private ClassesDictMapper classesDictMapper;

    @Autowired
    private ClassesDictHistoryMapper classesDictHistoryMapper;

    @Scheduled(cron = "0 10 0 * * ?")
    public void updateGrade(){

        List<ClassesDictDO> classesDictDOS = classesDictMapper.selectList(new QueryWrapper<ClassesDictDO>()
        .lambda()
        .eq(ClassesDictDO::getStatus,"在读"));

        for (ClassesDictDO classesDictDO : classesDictDOS){

            //获取班级的晋升时间
            Date date = classesDictDO.getUpgradeDate();
            Date current = new Date();

            if (null!=date && date.getYear() == current.getYear() && date.getMonth() == current.getMonth() && date.getDate() == current.getDate()){

                //将history表里该班级的记录置为归档
                ClassesDictHistoryDO classesDictHistoryDO = classesDictHistoryMapper.selectOne(new QueryWrapper<ClassesDictHistoryDO>()
                .lambda()
                .eq(ClassesDictHistoryDO::getStatus,"在读")
                .eq(ClassesDictHistoryDO::getClassDictId,classesDictDO.getId()));

                if (null != classesDictHistoryDO){
                    classesDictHistoryDO.setStatus("归档");
                    classesDictHistoryMapper.updateById(classesDictHistoryDO);
                }

                //将classdict表里的grade修改为新的年级
                if ("高三".equals(classesDictDO.getGrade())){
                    classesDictDO.setStatus("归档");
                    classesDictMapper.updateById(classesDictDO);
                }else {
                    Calendar cal = Calendar.getInstance();
                    int year = cal.get(Calendar.YEAR);
                    int month = cal.get(Calendar.MONTH)+1;//获取月份

                    if (month>=7){
                        date.setYear(year-1899);
                        date.setMonth(7);
                        date.setDate(1);
                        date.setHours(0);
                        date.setMinutes(0);
                        date.setSeconds(0);
                    }else {
                        date.setYear(year-1900);
                        date.setMonth(7);
                        date.setDate(1);
                        date.setHours(0);
                        date.setMinutes(0);
                        date.setSeconds(0);
                    }
                    classesDictDO.setUpgradeDate(date);
                    classesDictDO.setGrade(changeGrade(classesDictDO.getGrade()));
                    classesDictDO.setClassName(changeClassName(classesDictDO.getClassName()));
                    classesDictMapper.updateById(classesDictDO);

                    //在history中插入新的记录
                    ClassesDictHistoryDO classesDictHistoryDO1 = new ClassesDictHistoryDO();
                    BeanUtils.copyProperties(classesDictDO,classesDictHistoryDO1);
                    classesDictHistoryDO1.setClassDictId(classesDictDO.getId());
                    classesDictHistoryMapper.insert(classesDictHistoryDO1);
                }
            }
        }
    }


    public String changeGrade(String grade){

        if("高一".equals(grade)){
            return "高二";
        }else {
            return "高三";
        }
    }

    public String changeClassName(String className){

        String newName = "";
        if (className.contains("高一")){
            newName = className.replace("高一","高二");
        }else {
            newName = className.replace("高二","高三");
        }
        return newName;
    }


}