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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.distributed.print.business.entity.ExamTask;
import com.qmth.distributed.print.business.service.ExamTaskService;
import com.qmth.teachcloud.common.bean.params.ApproveUserResult;
import com.qmth.teachcloud.common.contant.SpringContextHolder;
import com.qmth.teachcloud.common.service.SysUserService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * @Description: 教研室主任审批
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2021/7/29
 */
public class DirectorApproveListener implements TaskListener {
    private final static Logger log = LoggerFactory.getLogger(DirectorApproveListener.class);

    /**
     * 查找同级和以下人员
     *
     * @param delegateTask
     */
    @Override
    public void notify(DelegateTask delegateTask) {
        ExamTaskService examTaskService = SpringContextHolder.getBean(ExamTaskService.class);
        SysUserService sysUserService = SpringContextHolder.getBean(SysUserService.class);

        String flowId = delegateTask.getProcessInstanceId();
        QueryWrapper<ExamTask> examTaskQueryWrapper = new QueryWrapper<>();
        examTaskQueryWrapper.lambda().eq(ExamTask::getFlowId, Long.parseLong(flowId));
        ExamTask examTask = examTaskService.getOne(examTaskQueryWrapper);

        List<ApproveUserResult> sysUserList = sysUserService.findByDirectorApprove(examTask.getCourseCode(), null);
        List<String> ids = sysUserList.stream().map(s -> String.valueOf(s.getId())).collect(Collectors.toList());
        //添加或签的人员，一人通过即可进入下一环节
        delegateTask.addCandidateUsers(ids);
    }
}
