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

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.module.activiti.pojo.vos.ActivitiModelVO;
import ink.wgink.module.activiti.pojo.vos.oa.nodemanage.OaNodeManageVO;
import ink.wgink.module.activiti.pojo.vos.oa.nodemanage.config.AssigneeVO;
import ink.wgink.module.activiti.pojo.vos.oa.nodemanage.config.NodeFormFieldVO;
import ink.wgink.module.activiti.service.oa.INodeAssigneeService;
import ink.wgink.module.activiti.service.oa.INodeButtonService;
import ink.wgink.module.activiti.service.oa.INodeFieldService;
import ink.wgink.module.activiti.service.oa.INodeManageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: NodeConfigServiceImpl
 * @Description: 节点配置
 * @Author: wanggeng
 * @Date: 2022/4/5 20:28
 * @Version: 1.0
 */
@Service
public class NodeManageServiceImpl extends DefaultBaseService implements INodeManageService {

    @Autowired
    private INodeAssigneeService nodeAssigneeService;
    @Autowired
    private INodeFieldService nodeFieldService;
    @Autowired
    private INodeButtonService nodeButtonService;

    @Override
    public void save(ActivitiModelVO activitiModelVO, String modelId, Integer modelVersion, String deploymentId) {
        if (!activitiModelVO.getOa()) {
            return;
        }
        LOG.debug("保存节点管理");
        List<OaNodeManageVO> oaNodeManages = activitiModelVO.getOaNodeManages();
        if (!StringUtils.isBlank(deploymentId)) {
            for (int i = 0; i < oaNodeManages.size(); i++) {
                OaNodeManageVO oaNodeManageVO = oaNodeManages.get(i);
                String nodeId = oaNodeManageVO.getNodeId();
                AssigneeVO assignee = oaNodeManageVO.getConfig().getAssignee();
                String formId = oaNodeManageVO.getConfig().getFormId();
                List<NodeFormFieldVO> formFields = oaNodeManageVO.getConfig().getFormFields();
                // 保存节点与代理人配置
                nodeAssigneeService.save(modelId, modelVersion, nodeId, i, assignee);
                // 保存节点与表单字段配置
                nodeFieldService.save(modelId, modelVersion, nodeId, i, formId, formFields);
                // 保存节点按钮
                nodeButtonService.save(modelId, modelVersion, nodeId, i, formId, oaNodeManageVO.getConfig().getFormButton());
            }
        } else {
            for (int i = 0; i < oaNodeManages.size(); i++) {
                OaNodeManageVO oaNodeManageVO = oaNodeManages.get(i);
                String nodeId = oaNodeManageVO.getNodeId();
                AssigneeVO assignee = oaNodeManageVO.getConfig().getAssignee();
                String formId = oaNodeManageVO.getConfig().getFormId();
                List<NodeFormFieldVO> formFields = oaNodeManageVO.getConfig().getFormFields();
                // 保存节点与代理人配置
                nodeAssigneeService.update(modelId, modelVersion, nodeId, i, assignee);
                // 保存节点与表单字段配置
                nodeFieldService.update(modelId, modelVersion, nodeId, i, formId, formFields);
                // 保存节点按钮
                nodeButtonService.update(modelId, modelVersion, nodeId, i, formId, oaNodeManageVO.getConfig().getFormButton());
            }
        }
    }

    @Override
    public void delete(String modelId, Integer modelVersion) {
        LOG.debug("删除代理人配置");
        nodeAssigneeService.deleteByModelIdAndModelVersion(modelId, modelVersion);
        LOG.debug("删除表单字段配置");
        nodeFieldService.deleteByModelIdAndModelVersion(modelId, modelVersion);
    }

}
