package com.srs.flowable.listener.reentrySchool;

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

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

        StuReentrySchool reentryId = stuReentrySchoolMapper.selectRtStuReentrySchoolByReentryId(Long.parseLong(delegateTask.getVariable("reentryId").toString()));

        if (reentryId != null) {
            reentryId.setApplyStatus(1L);
            stuReentrySchoolMapper.updateRtStuReentrySchool(reentryId);

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