package com.qmth.distributed.print.business.activiti;

import com.qmth.teachcloud.common.bean.params.ApproveUserResult;
import com.qmth.teachcloud.common.contant.SpringContextHolder;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.service.SysUserService;
import com.qmth.teachcloud.common.util.ServletUtil;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description: 教学院长审批
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2021/7/29
 */
public class PresidentApproveListener implements TaskListener {
    private final static Logger log = LoggerFactory.getLogger(PresidentApproveListener.class);

    /**
     * 查找同级和以上人员
     *
     * @param delegateTask
     */
    @Override
    public void notify(DelegateTask delegateTask) {
        SysUserService sysUserService = SpringContextHolder.getBean(SysUserService.class);
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        if (Objects.isNull(sysUser.getOrgId())) {
            throw ExceptionResultEnum.ERROR.exception("审批人机构为空");
        }
        List<ApproveUserResult> sysUserList = sysUserService.findByPresidentApprove(Arrays.asList(sysUser.getOrgId()), null);
        List<String> ids = sysUserList.stream().map(s -> String.valueOf(s.getId())).collect(Collectors.toList());
        //添加或签的人员，一人通过即可进入下一环节
        delegateTask.addCandidateUsers(ids);
    }
}