package com.persagy.adm.server.custom.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.adm.server.custom.dao.*;
import com.persagy.adm.server.custom.entity.db.*;
import com.persagy.adm.server.custom.service.IConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ConfigServiceImpl implements IConfigService {

	@Autowired
	private AdmM2dEquipMapper m2dEquipMapper;

	@Autowired
	private AdmInfosConfigMapper infosConfigMapper;

	@Autowired
	private AdmRelsConfigMapper relsConfigMapper;

	@Autowired
	private AdmPipeConfigMapper pipeConfigMapper;

	@Autowired
	private AdmConfigMapper commonConfigMapper;

	@Autowired
	private AdmComponentMapper componentMapper;

	@Autowired
	private AdmContainerConfigMapper containerConfigMapper;

	@Autowired
	AdmDefineProblemTypeMapper admDefineProblemTypeMapper;

	@Override
	public List<AdmM2dEquip> queryM2dEquip(String projectId) {
		return m2dEquipMapper.selectList(null);
	}

	@Override
	public List<AdmInfosConfig> queryInfosConfig(String projectId) {
		return infosConfigMapper.selectList(null);
	}

//	private void addInfoCfg(String code, String label, String group, String inputType, List<Object> cfgInfos){
//		HashMap<String, Object> item = new HashMap<>();
//		item.put("code", code);
//		item.put("label", label);
//		if(StrUtil.isNotBlank(group))
//			item.put("group", group);
//		if(StrUtil.isNotBlank(inputType))
//			item.put("inputType", inputType);
//		cfgInfos.add(item);
//	}

	@Override
	public List<AdmRelsConfig> queryRelsConfig(String projectId) {
		return relsConfigMapper.selectList(new QueryWrapper<AdmRelsConfig>().orderByAsc("graph_code", "rel_code"));
	}

	@Override
	public List<AdmPipeConfig> queryPipeConfig(String projectId) {
		return pipeConfigMapper.selectList(new QueryWrapper<AdmPipeConfig>().orderByAsc("pipe_type", "pipe_sub_type"));
	}

	@Override
	public List<AdmConfig> queryCommonConfig(String projectId) {
		return commonConfigMapper.selectList(null);
	}

	@Override
	public List<AdmComponent> queryComponent(String projectId) {
		return componentMapper.selectList(null);
	}

	@Override
	public List<AdmContainerConfig> queryContainerConfig(String projectId) {
		return containerConfigMapper.selectList(null);
	}

	@Override
	public ArrayNode queryNewEquipTypes(String projectId) {
		List<AdmConfig> list = commonConfigMapper.selectList(new QueryWrapper<AdmConfig>().eq("code", "new_equip_types"));
		if(CollUtil.isNotEmpty(list)){
			AdmConfig cfg = list.get(0);
			return (ArrayNode) cfg.getData();
		}
		return null;
	}

	@Override
	public ObjectNode queryRenameEquipTypes(String projectId) {
		List<AdmConfig> list = commonConfigMapper.selectList(new QueryWrapper<AdmConfig>().eq("code", "rename_equip_types"));
		if(CollUtil.isNotEmpty(list)){
			AdmConfig cfg = list.get(0);
			return (ObjectNode) cfg.getData();
		}
		return null;
	}

	@Override
	public List<AdmDefineProblemType> queryDefineProblemTypes() {
		return admDefineProblemTypeMapper.selectList(null);
	}
}
