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

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.module.activiti.dao.oa.INodeFieldDao;
import ink.wgink.module.activiti.pojo.dtos.oa.NodeFieldDTO;
import ink.wgink.module.activiti.pojo.vos.oa.nodemanage.config.NodeFormFieldVO;
import ink.wgink.module.activiti.service.oa.INodeFieldService;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.map.HashMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description: 表单字段与流程节点
 * @Author: WenG
 * @Date: 2022/3/17 20:09
 * @Version: 1.0
 **/
@Service
public class NodeFieldServiceImpl extends DefaultBaseService implements INodeFieldService {

    @Autowired
    private INodeFieldDao nodeFieldDao;

    @Override
    public void save(String modelId, Integer modelVersion, String nodeId, Integer nodeIndex, String formId, List<NodeFormFieldVO> formFields) {
        LOG.debug("新增字段配置");
        formFields.forEach(nodeFormFieldVO -> {
            Map<String, Object> params = HashMapUtil.beanToMap(nodeFormFieldVO);
            params.put("nodeFieldId", UUIDUtil.getUUID());
            params.put("modelId", modelId);
            params.put("modelVersion", modelVersion);
            params.put("nodeId", nodeId);
            params.put("nodeIndex", nodeIndex);
            params.put("formId", formId);
            params.put("isVisible", nodeFormFieldVO.getVisible() ? 1 : 0);
            params.put("isEditable", nodeFormFieldVO.getEditable() ? 1 : 0);
            nodeFieldDao.save(params);
        });
    }

    @Override
    public void update(String modelId, Integer modelVersion, String nodeId, int nodeIndex, String formId, List<NodeFormFieldVO> formFields) {
        delete(modelId, modelVersion, nodeId, formId);
        save(modelId, modelVersion, nodeId, nodeIndex, formId, formFields);
    }

    @Override
    public void updateDeploymentId(String modelId, Integer modelVersion, String deploymentId) {
        Map<String, Object> params = getHashMap(6);
        params.put("modelId", modelId);
        params.put("modelVersion", modelVersion);
        params.put("deploymentId", deploymentId);
        nodeFieldDao.updateDeploymentId(params);
    }

    @Override
    public void updateVisible(String nodeFieldId, Integer isVisible) {
        Map<String, Object> params = getHashMap(4);
        params.put("nodeFieldId", nodeFieldId);
        params.put("isVisible", isVisible);
        nodeFieldDao.update(params);
    }

    @Override
    public void updateEditable(String nodeFieldId, Integer isEditable) {
        Map<String, Object> params = getHashMap(4);
        params.put("nodeFieldId", nodeFieldId);
        params.put("isEditable", isEditable);
        nodeFieldDao.update(params);
    }

    @Override
    public void deleteDeploymentId(String deploymentId) {
        nodeFieldDao.deleteDeploymentId(deploymentId);
    }

    @Override
    public void deleteByModelIdAndModelVersion(String modelId, Integer modelVersion) {
        Map<String, Object> params = getHashMap(4);
        params.put("modelId", modelId);
        params.put("modelVersion", modelVersion);
        nodeFieldDao.delete(params);
    }

    @Override
    public List<NodeFieldDTO> listNodeByDeploymentId(String deploymentId) {
        Map<String, Object> params = getHashMap(2);
        params.put("deploymentId", deploymentId);
        return nodeFieldDao.listNode(params);
    }

    @Override
    public List<NodeFieldDTO> listByDeploymentIdAndNodeId(String deploymentId, String nodeId) {
        Map<String, Object> params = getHashMap(2);
        params.put("deploymentId", deploymentId);
        params.put("nodeId", nodeId);
        return nodeFieldDao.list(params);
    }

    /**
     * 删除
     *
     * @param modelId      模型ID
     * @param modelVersion 模型版本
     * @param nodeId       节点ID
     * @param formId       表单ID
     */
    private void delete(String modelId, Integer modelVersion, String nodeId, String formId) {
        LOG.debug("删除原有字段配置");
        Map<String, Object> params = getHashMap(6);
        params.put("modelId", modelId);
        params.put("modelVersion", modelVersion);
        params.put("nodeId", nodeId);
        params.put("formId", formId);
        nodeFieldDao.delete(params);
    }

}
