package com.srs.flowable.listener.relieve;

import com.srs.common.utils.spring.SpringUtils;
import com.srs.flowable.domain.StuDisciplinaryApplication;
import com.srs.flowable.domain.StuDisciplinaryRelieve;
import com.srs.flowable.mapper.DisciplinaryMapper;
import com.srs.flowable.mapper.RelieveMapper;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * 修改处分和解除处分的状态
 */
@Component
@Slf4j
public class RelieveStatusListener implements TaskListener {

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

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


        StuDisciplinaryRelieve relieveId = relieveMapper.selectRtStuDisciplinaryRelieveByRelieveId(Long.parseLong(delegateTask.getVariable("relieveId").toString()));

        if (relieveId!=null){
            relieveId.setPenaltyStatus(1L);
            relieveMapper.updateRtStuDisciplinaryRelieve(relieveId);


            StuDisciplinaryApplication stuDisciplinaryApplication = disciplinaryMapper.selectRtStuDisciplinaryApplicationByApplicationId(Long.parseLong(relieveId.getPenaltyNumber()));
            if (stuDisciplinaryApplication!=null){
                stuDisciplinaryApplication.setPenaltyStatus(1L);
                disciplinaryMapper.updateRtStuDisciplinaryApplication(stuDisciplinaryApplication);
            }else {
                throw new RuntimeException("未找到处分申请相关信息");
            }
        }else {
            throw new RuntimeException("未找到解除处分申请相关信息");
        }

    }
}
