package com.srs.flowable.listener.dropOutSchoo;

import com.srs.common.utils.DateUtils;
import com.srs.common.utils.SecurityUtils;
import com.srs.common.utils.spring.SpringUtils;
import com.srs.dormitory.domain.SrsDormitoryStudent;
import com.srs.flowable.domain.StuDropOutSchool;
import com.srs.flowable.domain.StuQuitSchool;
import com.srs.flowable.domain.Student;
import com.srs.flowable.mapper.DisciplinaryMapper;
import com.srs.flowable.mapper.StuDropOutSchoolMapper;
import com.srs.flowable.mapper.StuQuitSchoolMapper;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.stereotype.Component;

/**
 * 修改退学状态
 */
@Component
@Slf4j
public class DropOutSchoolStatusListener implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        StuDropOutSchoolMapper stuDropOutSchoolMapper =  SpringUtils.getBean("stuDropOutSchoolMapper");

        DisciplinaryMapper disciplinaryMapper =  SpringUtils.getBean("disciplinaryMapper");

        StuDropOutSchool stuDropOutSchool = stuDropOutSchoolMapper.selectRtStuDropOutSchoolByDropOutSchoolId(Long.parseLong(delegateTask.getVariable("dropOutSchoolId").toString()));

        if (stuDropOutSchool!=null){

            stuDropOutSchool.setApplyStatus(1L);
            stuDropOutSchoolMapper.updateRtStuDropOutSchool(stuDropOutSchool);

            // 修改学生学籍状态
            Student student=new Student();
            // 状态（01：在校  02：变更离校   03：毕业
            student.setStatus("02");
            student.setStuNo(stuDropOutSchool.getStuNo());
            student.setUpdateBy(SecurityUtils.getUsername());
            student.setUpdateTime(DateUtils.getNowDate());
            disciplinaryMapper.updateStudentStatus(student);

            // todo 学生自动退学：
            SrsDormitoryStudent dormitoryStudent=new SrsDormitoryStudent();
            dormitoryStudent.setStuNo(stuDropOutSchool.getStuNo());
            disciplinaryMapper.deleteDormitoryStudent(dormitoryStudent);
        }else{
            throw new RuntimeException("该退学申请不存在");
        }
    }
}
