package com.cn.fdapfe.biz.service.impl;

import java.util.List;
import com.cn.fdapfe.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.cn.fdapfe.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.cn.fdapfe.biz.domain.WorkflowNode;
import com.cn.fdapfe.biz.mapper.WorkflowMapper;
import com.cn.fdapfe.biz.domain.Workflow;
import com.cn.fdapfe.biz.service.IWorkflowService;

/**
 * 流程任务Service业务层处理
 * 
 * @author Allen
 * @date 2025-03-07
 */
@Service
public class WorkflowServiceImpl implements IWorkflowService 
{
    @Autowired
    private WorkflowMapper workflowMapper;

    /**
     * 查询流程任务
     * 
     * @param id 流程任务主键
     * @return 流程任务
     */
    @Override
    public Workflow selectWorkflowById(Long id)
    {
        return workflowMapper.selectWorkflowById(id);
    }

    /**
     * 查询流程任务列表
     * 
     * @param workflow 流程任务
     * @return 流程任务
     */
    @Override
    public List<Workflow> selectWorkflowList(Workflow workflow)
    {
        return workflowMapper.selectWorkflowList(workflow);
    }

    /**
     * 新增流程任务
     * 
     * @param workflow 流程任务
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWorkflow(Workflow workflow)
    {
        workflow.setCreateTime(DateUtils.getNowDate());
        int rows = workflowMapper.insertWorkflow(workflow);
        insertWorkflowNode(workflow);
        return rows;
    }

    /**
     * 修改流程任务
     * 
     * @param workflow 流程任务
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWorkflow(Workflow workflow)
    {
        workflow.setUpdateTime(DateUtils.getNowDate());
        workflowMapper.deleteWorkflowNodeByTaskId(workflow.getId());
        insertWorkflowNode(workflow);
        return workflowMapper.updateWorkflow(workflow);
    }

    /**
     * 批量删除流程任务
     * 
     * @param ids 需要删除的流程任务主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkflowByIds(Long[] ids)
    {
        workflowMapper.deleteWorkflowNodeByTaskIds(ids);
        return workflowMapper.deleteWorkflowByIds(ids);
    }

    /**
     * 删除流程任务信息
     * 
     * @param id 流程任务主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkflowById(Long id)
    {
        workflowMapper.deleteWorkflowNodeByTaskId(id);
        return workflowMapper.deleteWorkflowById(id);
    }

    /**
     * 新增流程节点信息
     * 
     * @param workflow 流程任务对象
     */
    public void insertWorkflowNode(Workflow workflow)
    {
        List<WorkflowNode> workflowNodeList = workflow.getWorkflowNodeList();
        Long id = workflow.getId();
        if (StringUtils.isNotNull(workflowNodeList))
        {
            List<WorkflowNode> list = new ArrayList<WorkflowNode>();
            for (WorkflowNode workflowNode : workflowNodeList)
            {
                workflowNode.setTaskId(id);
                list.add(workflowNode);
            }
            if (list.size() > 0)
            {
                workflowMapper.batchWorkflowNode(list);
            }
        }
    }
}
