package com.srs.flowable.listener.quitSchool;

import com.srs.common.utils.DateUtils;
import com.srs.common.utils.SecurityUtils;
import com.srs.common.utils.spring.SpringUtils;
import com.srs.flowable.domain.StuQuitSchool;
import com.srs.flowable.domain.Student;
import com.srs.flowable.mapper.DisciplinaryMapper;
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 QuitSchoolStatusListener implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        StuQuitSchoolMapper stuQuitSchoolMapper = (StuQuitSchoolMapper) SpringUtils.getBean("stuQuitSchoolMapper");

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


        StuQuitSchool quitSchoolId = stuQuitSchoolMapper.selectRtStuQuitSchoolByQuitSchoolId(Long.parseLong(delegateTask.getVariable("quitSchoolId").toString()));

        if (quitSchoolId != null) {
            quitSchoolId.setApplyStatus(1L);
            stuQuitSchoolMapper.updateRtStuQuitSchool(quitSchoolId);

            // 修改学生学籍状态
            Student student = new Student();
            // 状态（01：在校  02：变更离校   03：毕业
            student.setStatus("02");
            student.setStuNo(quitSchoolId.getStuNo());
            student.setUpdateBy(SecurityUtils.getUsername());
            student.setUpdateTime(DateUtils.getNowDate());
            disciplinaryMapper.updateStudentStatus(student);
        } else {
            throw new RuntimeException("该休学申请不存在");
        }
    }
}
