package com.persagy.adm.diagram.controller;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.adm.diagram.core.DataStrategy;
import com.persagy.adm.diagram.entity.DiagramType;
import com.persagy.adm.diagram.frame.BdtpRequest;
import com.persagy.adm.diagram.service.BdtpDataService;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.common.utils.ResultHelper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 公共接口
 * @author zhaoyk
 * @date 2021-12-27
 */
@RestController
@CrossOrigin
public class CommonController {

	@Autowired
	private BdtpDataService bdtpDataService;

	@Autowired
	@Qualifier(DataStrategy.IMPL_QUALIFIER)
	private DataStrategy dataStrategy;

	@GetMapping("equipTypes")
	public CommonResult<List<ObjectNode>> equipTypes(){
		return ResultHelper.single(bdtpDataService.getEquipTypeList(BdtpRequest.getCurrent(), null));
	}

	@GetMapping("relTypes")
	public CommonResult<List<Map<String, Object>>> relTypes(){
		return ResultHelper.single(bdtpDataService.getRelTypeTree(BdtpRequest.getCurrent()));
	}

	@GetMapping("majSys")
	public CommonResult<List<ObjectNode>> majSys(){
		BdtpRequest req = BdtpRequest.getCurrent();
		List<ObjectNode> majList = bdtpDataService.getMajorList(req);
		List<ObjectNode> sysList = bdtpDataService.getSystemList(req);

		for(ObjectNode maj : majList){
			ArrayNode childrenSys = maj.putArray("children");
			String majorCode = maj.get("code").asText();
			maj.put("name", maj.get("label").asText());
			for(Iterator<ObjectNode> sysIter = sysList.iterator(); sysIter.hasNext();){
				ObjectNode sys = sysIter.next();
				if(sys.get("majorCode") != null && sys.get("majorCode").asText().equals(majorCode)){
					childrenSys.add(sys);
					sysIter.remove();
				}
			}
		}
		return ResultHelper.single(majList);
	}

	@ApiOperation("查询系统图类型列表")
	@GetMapping("diagramTypes")
	public CommonResult<List<DiagramType>> diagramTypes(){
		return ResultHelper.single(dataStrategy.getDiagramTypes());
	}

}
