package com.tofly.analyse.controller;

import cn.hutool.core.collection.CollUtil;
import com.tofly.analyse.entity.FlowDirAnalysis;
import com.tofly.analyse.entity.FlowDirAnalysisDto;
import com.tofly.analyse.service.PipelineService;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/pipeline")
@Api(tags = "管线维护信息")
public class PipelineController {

   @Autowired	
   private PipelineService pipelineService;
      
   @ToFlyAppLog(title = "追踪分析")
   @ApiOperation(value = "追踪分析 参数（pipeLength：管线长度，tableName：数据集表名称，pipePointNo：管线唯一标识,type(0:上游 1:下游)）")
   @PostMapping("/getPipelineConnect")
   public ResultRespone getPipelineConnect(@RequestBody HashMap<String,String> map) throws Exception {
       return ResultRespone.success(pipelineService.getPipelineConnect(map));
   }

   @ToFlyAppLog(title = "流向分析")
   @ApiOperation(value = "流向分析")
   @PostMapping("/getFlowDirConnect")
   public ResultRespone getFlowDirConnect(@RequestBody FlowDirAnalysisDto flowDirAnalysisDto) {
      Map<String,Object> map=new HashMap<>();
      List<FlowDirAnalysis> flowDirAnalyses = flowDirAnalysisDto.getFlowDirAnalyses();
      if(CollUtil.isNotEmpty(flowDirAnalyses)){
         flowDirAnalyses.forEach(d->{
            Map<String,String> param=new HashMap<>();
            param.put("tableName",d.getDatasetName());
            param.put("pipePointNo",d.getSid());
            param.put("type",d.getType());
            Map<String, Object> pipelineConnect = null;
            try {
               pipelineConnect = pipelineService.getPipelineConnect(param);
            } catch (Exception e) {
               throw new RuntimeException(e);
            }
            map.put(d.getSid(),pipelineConnect);
         });
      }
      return ResultRespone.success(map);
   }
   
   @ApiOperation(value = "获取管点具体信息")
   @GetMapping("/point/{expNo}")
   public ResultRespone getPointByExpNo(@PathVariable("expNo") String expNo) {	   
	   Map<String,Object> res = pipelineService.getPointByExpNo(expNo);
       return ResultRespone.success(res);
   }

   @ApiOperation(value = "根据编码、地址获取图层数据")
   @GetMapping("/getDataBykeyValue/{keyValue}")
   public ResultRespone getDataBykeyValue(@RequestParam(value = "layerName") String layerName, @PathVariable("keyValue") String keyValue) {
      return pipelineService.getDataBykeyValue(layerName,keyValue);
   }
   
     
}