package ink.wgink.module.activiti.service.activiti.impl;

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.module.activiti.pojo.dtos.ActivitiProcdefDTO;
import ink.wgink.module.activiti.service.activiti.IActivitiProcdefService;
import ink.wgink.module.activiti.service.oa.INodeAssigneeService;
import ink.wgink.module.activiti.service.oa.INodeFieldService;
import ink.wgink.pojo.result.SuccessResultList;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: ActivitiProcdefServiceImpl
 * @Description: 流程定义
 * @Author: wanggeng
 * @Date: 2021/12/7 10:11 PM
 * @Version: 1.0
 */
@Service
public class ActivitiProcdefServiceImpl extends DefaultBaseService implements IActivitiProcdefService {

    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private INodeAssigneeService nodeAssigneeService;
    @Autowired
    private INodeFieldService nodeFieldService;

    @Override
    public void delete(String deploymentId) {
        repositoryService.deleteDeployment(deploymentId, true);
        LOG.debug("删除代理人配置部署ID");
        nodeAssigneeService.deleteDeploymentId(deploymentId);
        LOG.debug("删除表单配置部署ID");
        nodeFieldService.deleteDeploymentId(deploymentId);
    }

    @Override
    public SuccessResultList<List<ActivitiProcdefDTO>> listPage(int page, int rows) {
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        List<ProcessDefinition> processDefinitions = processDefinitionQuery.orderByDeploymentId().asc().listPage(page - 1, rows);

        List<ActivitiProcdefDTO> activitiProcdefDTOs = new ArrayList<>();
        processDefinitions.forEach(processDefinition -> {
            ActivitiProcdefDTO activitiProcdefDTO = new ActivitiProcdefDTO();
            BeanUtils.copyProperties(processDefinition, activitiProcdefDTO);
            activitiProcdefDTOs.add(activitiProcdefDTO);
        });

        return new SuccessResultList<>(activitiProcdefDTOs, page, processDefinitionQuery.count());
    }

}
