package com.srs.flowable.listener;

import com.srs.common.enums.ApplicationStatus;
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;

import java.math.BigDecimal;

/**
 * 领导审核
 * 当请假天数大于等于4天以上，由最后一个节点的领导审核通过后，请假单才能流转状态
 */
@Component
@Slf4j
public class LeadAuditListener implements ExecutionListener {

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

        log.info("任务流相关数据", delegateExecution);

        // 辅导员审批结果
        String approval = delegateExecution.getVariable("approval").toString();

        // 拿到流程所有参数
        String approved = delegateExecution.getVariable("approved").toString();

        // 获取请假天数
        BigDecimal leaveDays = (BigDecimal) delegateExecution.getVariable("leaveDays");


        // 请假单id
        // 报错信息
        String leaveApplicationId = delegateExecution.getVariable("leaveApplicationId").toString();

        log.info("审批结果为：", approval);

        StuLeaveApplication stuLeaveApplication = leaveMapper.selectRtStuLeaveApplicationByLeaveApplicationId(leaveApplicationId);

        if (stuLeaveApplication != null) {
            // 同意
            if ("true".equals(approved)) {
                stuLeaveApplication.setStatus(ApplicationStatus.APPROVED.getCode());
            }
        }

        stuLeaveApplication.setUpdateBy(SecurityUtils.getUsername());

        stuLeaveApplication.setUpdateTime(DateUtils.getNowDate());

        leaveMapper.updateRtStuLeaveApplication(stuLeaveApplication);

        log.info("审核成功");
    }
}
