package com.bonus.material.approval.service.impl;

import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.approval.domain.ApprovalNode;
import com.bonus.material.approval.domain.ApprovalProcess;
import com.bonus.material.approval.mapper.ApprovalNodeMapper;
import com.bonus.material.approval.mapper.ApprovalProcessMapper;
import com.bonus.material.approval.service.IApprovalProcessService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 审批流程配置服务实现
 * 
 * @author syruan
 */
@Service
public class ApprovalProcessServiceImpl implements IApprovalProcessService {

    @Resource
    private ApprovalProcessMapper processMapper;

    @Resource
    private ApprovalNodeMapper nodeMapper;

    @Override
    public List<ApprovalProcess> selectProcessList(ApprovalProcess process) {
        Long deptId = SecurityUtils.getLoginUser().getSysUser().getDeptId();
        process.setDeptId(deptId);
        return processMapper.selectProcessList(process);
    }

    @Override
    public ApprovalProcess selectProcessById(Long id) {
        ApprovalProcess process = processMapper.selectProcessById(id);
        if (process != null) {
            // 查询节点列表
            List<ApprovalNode> nodeList = nodeMapper.selectNodeListByProcessId(id);
            process.setNodeList(nodeList);
        }
        return process;
    }

    @Override
    public ApprovalProcess selectProcessByBusinessType(String businessType) {
        ApprovalProcess process = processMapper.selectProcessByBusinessType(businessType);
        if (process != null) {
            // 查询节点列表
            List<ApprovalNode> nodeList = nodeMapper.selectNodeListByProcessId(process.getId());
            process.setNodeList(nodeList);
        }
        return process;
    }

    @Override
    public ApprovalProcess selectProcessByBusinessTypeAndOrgId(String businessType, Long orgId) {
        if (orgId == null) {
            orgId = SecurityUtils.getLoginUser().getSysUser().getDeptId();
        }
        ApprovalProcess process = processMapper.selectProcessByBusinessTypeAndOrgId(businessType, orgId);
        if (process != null) {
            // 查询节点列表
            List<ApprovalNode> nodeList = nodeMapper.selectNodeListByProcessId(process.getId());
            process.setNodeList(nodeList);
        }
        return process;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertProcess(ApprovalProcess process) {
        if (process == null) {
            throw new RuntimeException("审批流程信息不能为空");
        }
        if (process.getOrgId() == null) {
            process.setOrgId(SecurityUtils.getLoginUser().getSysUser().getDeptId());
            process.setOrgName(SecurityUtils.getLoginUser().getSysUser().getDept().getDeptName());
        }
        // 检查是否已存在相同业务类型和组织机构的流程
        ApprovalProcess existingProcess = processMapper.selectProcessByBusinessTypeAndOrgId(process.getBusinessType(), process.getOrgId());
        if (existingProcess != null) {
            throw new RuntimeException("业务类型 [" + process.getBusinessType() + "] 在组织机构 ["
                + process.getOrgName() + "] 的审批流程已存在，流程名称：" + existingProcess.getProcessName());
        }

        // 插入流程
        int rows = processMapper.insertProcess(process);

        if (rows > 0) {
            // 插入节点
            if (process.getNodeList() != null && !process.getNodeList().isEmpty()) {
                for (ApprovalNode node : process.getNodeList()) {
                    node.setProcessId(process.getId());
                }
                nodeMapper.batchInsertNode(process.getNodeList());
            }
        }

        return rows;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateProcess(ApprovalProcess process) {
        // 更新流程
        int rows = processMapper.updateProcess(process);

        // 删除旧节点
        nodeMapper.deleteNodeByProcessId(process.getId());

        // 插入新节点
        if (process.getNodeList() != null && !process.getNodeList().isEmpty()) {
            for (ApprovalNode node : process.getNodeList()) {
                node.setProcessId(process.getId());
            }
            nodeMapper.batchInsertNode(process.getNodeList());
        }
        return rows;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteProcessById(Long id) {
        // 删除节点
        nodeMapper.deleteNodeByProcessId(id);
        // 删除流程
        return processMapper.deleteProcessById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteProcessByIds(Long[] ids) {
        for (Long id : ids) {
            deleteProcessById(id);
        }
        return ids.length;
    }

    @Override
    public int enableProcess(Long id) {
        return processMapper.updateProcessStatus(id, "1");
    }

    @Override
    public int disableProcess(Long id) {
        return processMapper.updateProcessStatus(id, "0");
    }
}

