package com.srs.flowable.listener.disciplinary;


import com.srs.common.enums.PenaltyStatus;
import com.srs.common.utils.DateUtils;
import com.srs.common.utils.SecurityUtils;
import com.srs.common.utils.spring.SpringUtils;
import com.srs.flowable.domain.StuDisciplinaryApplication;
import com.srs.flowable.mapper.DisciplinaryMapper;
import liquibase.pro.packaged.S;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.stereotype.Component;

/**
 * 修改处分状态：已处分
 */
@Component
@Slf4j
public class DisciplinaryConfirmListener implements TaskListener {

    @Override
    public void notify(DelegateTask delegateTask) {
        DisciplinaryMapper disciplinaryMapper = (DisciplinaryMapper)SpringUtils.getBean("disciplinaryMapper");

        if (delegateTask.getEventName().equals(TaskListener.EVENTNAME_COMPLETE)) {
            String leaveApplicationId = delegateTask.getVariable("applicationId").toString();
            if (leaveApplicationId != null) {
                StuDisciplinaryApplication stuDisciplinaryApplication = disciplinaryMapper.selectRtStuDisciplinaryApplicationByApplicationId(Long.valueOf(leaveApplicationId));
                if (stuDisciplinaryApplication != null) {
                    stuDisciplinaryApplication.setPenaltyStatus(0L);
                    stuDisciplinaryApplication.setUpdateBy(SecurityUtils.getUsername());
                    stuDisciplinaryApplication.setUpdateTime(DateUtils.getNowDate());
                    disciplinaryMapper.updateRtStuDisciplinaryApplication(stuDisciplinaryApplication);
                }
            }else{
                throw new RuntimeException("未找到相关处分申请记录");
            }
        }
    }
}
