package com.njcn.bpm.service.task.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.bpm.enums.BpmResponseEnum;
import com.njcn.bpm.mapper.task.ProcessInstanceCopyMapper;
import com.njcn.bpm.pojo.po.task.ProcessInstanceCopy;
import com.njcn.bpm.service.IBpmProcessDefinitionService;
import com.njcn.bpm.service.task.IBpmProcessInstanceService;
import com.njcn.bpm.service.task.IBpmTaskService;
import com.njcn.bpm.service.task.IProcessInstanceCopyService;
import com.njcn.common.pojo.exception.BusinessException;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * BPM 流程实例抄送表 服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2024-05-10
 */
@Service
public class ProcessInstanceCopyServiceImpl extends ServiceImpl<ProcessInstanceCopyMapper, ProcessInstanceCopy> implements IProcessInstanceCopyService {


    @Resource
    @Lazy
    private IBpmTaskService taskService;

    @Resource
    @Lazy
    private IBpmProcessInstanceService processInstanceService;
    @Resource
    @Lazy
    private IBpmProcessDefinitionService processDefinitionService;

    @Override
    public void createProcessInstanceCopy(Collection<String> userIds, String taskId) {
        // 1.1 校验任务存在
        Task task = taskService.getTask(taskId);
        if (ObjectUtil.isNull(task)) {
            throw new BusinessException(BpmResponseEnum.TASK_NOT_EXISTS);
        }
        // 1.2 校验流程实例存在
        String processInstanceId = task.getProcessInstanceId();
        ProcessInstance processInstance = processInstanceService.getProcessInstance(processInstanceId);
        if (processInstance == null) {
            throw new BusinessException(BpmResponseEnum.PROCESS_INSTANCE_NOT_EXISTS);
        }
        // 1.3 校验流程定义存在
        ProcessDefinition processDefinition = processDefinitionService.getProcessDefinition(
                processInstance.getProcessDefinitionId());
        if (processDefinition == null) {
            throw new BusinessException(BpmResponseEnum.PROCESS_DEFINITION_NOT_EXISTS);
        }

        // 2. 创建抄送流程
        List<ProcessInstanceCopy> copyList = userIds.stream().map(userId -> {
            ProcessInstanceCopy processInstanceCopy = new ProcessInstanceCopy();
            processInstanceCopy.setUserId(userId);
            processInstanceCopy.setStartUserId(processInstance.getStartUserId());
            processInstanceCopy.setProcessInstanceId(processInstanceId);
            processInstanceCopy.setProcessInstanceName(processInstance.getName());
            processInstanceCopy.setCategory(processDefinition.getCategory());
            processInstanceCopy.setTaskId(taskId);
            processInstanceCopy.setTaskName(task.getName());
            return processInstanceCopy;
        }).collect(Collectors.toList());

        this.saveBatch(copyList);
    }
}
