package com.srs.flowable.listener;

import com.srs.common.doman.vo.TeacherVo;
import com.srs.common.utils.DateUtils;
import com.srs.common.utils.SecurityUtils;
import com.srs.common.utils.spring.SpringUtils;
import com.srs.flowable.domain.StuLeaveApplication;
import com.srs.flowable.mapper.LeaveMapper;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

/**
 * 查询该生的辅导员
 */
@Slf4j
@Component
public class CounselorListener implements ExecutionListener {

    @Override
    public void notify(DelegateExecution delegateExecution) {
        LeaveMapper leaveMapper = (LeaveMapper) SpringUtils.getBean("leaveMapper");


        Long stuId = (Long) delegateExecution.getVariable("stuId");

        TeacherVo teacherInfo = leaveMapper.getCounselorInfo(stuId);

        // 把辅导员的userId放到approval中
        delegateExecution.setVariable("approval", teacherInfo.getUserId());

        // 修改销假状态
        String leaveApplicationId = delegateExecution.getVariable("leaveApplicationId").toString();

        StuLeaveApplication stuLeaveApplication = leaveMapper.selectRtStuLeaveApplicationByLeaveApplicationId(leaveApplicationId);

        if (stuLeaveApplication != null) {
            stuLeaveApplication.setCancellation(1);
        }

        stuLeaveApplication.setUpdateBy(SecurityUtils.getUsername());

        stuLeaveApplication.setUpdateTime(DateUtils.getNowDate());

        leaveMapper.updateRtStuLeaveApplication(stuLeaveApplication);

        // todo 企业微信推送消息


        log.info("查询辅导员信息：{}", teacherInfo);

    }
}
