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

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.SaveException;
import ink.wgink.module.activiti.dao.oa.INodeButtonDao;
import ink.wgink.module.activiti.pojo.dtos.oa.NodeButtonDTO;
import ink.wgink.module.activiti.pojo.vos.oa.nodemanage.config.NodeFormButtonVO;
import ink.wgink.module.activiti.service.oa.INodeButtonService;
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.Map;

/**
 * @ClassName: NodeButtonServiceImpl
 * @Description: 节点按钮
 * @Author: wanggeng
 * @Date: 2022/4/27 11:39
 * @Version: 1.0
 */
@Service
public class NodeButtonServiceImpl extends DefaultBaseService implements INodeButtonService {

    @Autowired
    private INodeButtonDao nodeButtonDao;

    @Override
    public void save(String modelId, Integer modelVersion, String nodeId, int nodeIndex, String formId, NodeFormButtonVO nodeButton) {
        if(nodeButton == null) {
            throw new SaveException("节点按钮不能为空");
        }
        Map<String, Object> params = HashMapUtil.beanToMap(nodeButton);
        params.put("nodeButtonId", UUIDUtil.getUUID());
        params.put("modelId", modelId);
        params.put("modelVersion", modelVersion);
        params.put("nodeId", nodeId);
        params.put("nodeIndex", nodeIndex);
        params.put("formId", formId);
        nodeButtonDao.save(params);
    }

    @Override
    public void update(String modelId, Integer modelVersion, String nodeId, int nodeIndex, String formId, NodeFormButtonVO nodeButton) {
        delete(modelId, modelVersion, nodeId, formId);
        save(modelId, modelVersion, nodeId, nodeIndex, formId, nodeButton);
    }

    @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);
        nodeButtonDao.updateDeploymentId(params);
    }

    @Override
    public NodeButtonDTO getByDeploymentIdAndNodeId(String deploymentId, String nodeId) {
        Map<String, Object> params = getHashMap(4);
        params.put("deploymentId", deploymentId);
        params.put("nodeId", nodeId);
        return nodeButtonDao.get(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);
        nodeButtonDao.delete(params);
    }
}
