package com.huatek.busi.controller;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

import com.huatek.busi.entity.TransformerLoadRate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.entity.GisAssessmentResult;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.service.gis.GisAssessService;
import com.huatek.busi.service.gis.GisAssessmentResultService;
import com.huatek.busi.vo.BusiTreeNodeVo;
import com.huatek.busi.vo.GisAssessmentResultVo;
import com.huatek.busi.vo.GisSf6ThresholdVo;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = {"gis评估"})
@RestController
@RequestMapping("/api/GisAssess")
public class GisAssessController {

    @Reference(check = false)
    private GisAssessService gisAssessService;

    @Reference(check = false)
    private BusiSiteTreeService busiSiteTreeService;

    @Reference(check = false)
    private GisAssessmentResultService gisAssessmentResultService;

//    @PostMapping(value = "/getGisAssessmentResult")
//    public Map<String, Object>  getGisAssessmentResult( @RequestParam ("siteId") String siteId,  @RequestParam ("mainTreeId") String mainTreeId) {
//        GisAssessmentResult gisAssessmentResult =  gisAssessService.assessmentForGis(siteId, mainTreeId);
//        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", gisAssessmentResult);
//    }

    /**
     * 查询GIS评估
     * @param
     * @return
     */
   @ApiOperation(value ="查询GIS评估", response = GisSf6ThresholdVo.class)
   @GetMapping(value="/queryGisAssessmentResultVo", produces = {"application/json;charset=utf-8"})
   public Map<String, Object> queryGisAssessmentResultVo(@RequestParam ("siteId") String siteId,
                                                         @RequestParam ("mainTreeId") String mainTreeId,
                                                         @RequestParam(value = "page", required = false) Integer page,
                                                         @RequestParam(value = "limit", required = false) Integer limit,
                                                         @RequestParam(value = "startTime", required = false) String startTime,
                                                         @RequestParam(value = "endTime", required = false) String endTime,
                                                         @RequestParam(value = "type", required = false) String type) {
       DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
       LocalDate start = null;
       LocalDate end = null;


       if (StringUtils.isNotBlank(startTime)) {
           start = LocalDate.parse(startTime, df);
       }

       if (StringUtils.isNotBlank(endTime)) {
           end = LocalDate.parse(endTime, df).plusDays(1);
       }
	   PageInfo<GisAssessmentResultVo> gisAssessmentResultVo = gisAssessmentResultService.findGisAssessmentByMainTreeId(siteId, mainTreeId, page, limit, start, end, type);
       return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, gisAssessmentResultVo.getTotal(), "", gisAssessmentResultVo.getList());
   }

    /**
     * 所有GIS最近15天负荷率查询
     *
     */
    @ApiOperation(value = "所有GIS最近15天负荷率查询")
    @GetMapping(value="/findGisLoadRate")
    public Map<String, Object> findGisLoadRate () {
        List<TransformerLoadRate> gisLoadRate = gisAssessmentResultService.findGisLoadRate();
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", gisLoadRate);
    }


    /**
     * 结果追溯
     * @param
     * @return
     */
    @ApiOperation(value ="结果追溯", response = GisSf6ThresholdVo.class)
    @GetMapping(value="/findCalculateStep", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> findCalculateStep(@RequestParam ("pid") String pid, @RequestParam (value = "type",required = false) String type){
        return gisAssessmentResultService.findCalculateStep(pid,type);
    }

    /**
     * 评估参数同步
     * @param
     * @return
     */
    @ApiOperation(value ="评估参数同步", response = GisSf6ThresholdVo.class)
    @GetMapping(value="/syncParams", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> syncParams(String token,
                                          @RequestParam ("siteId") String siteId,
                                          @RequestParam ("mainTreeId") String mainTreeId,
                                          @RequestParam ("number") String number){
        return gisAssessmentResultService.syncParams(token,siteId,mainTreeId,number);
    }

//   @PostMapping(value = "/getGisAssessmentTask")
//   public Map<String, Object>  getGisAssessmentTask() {
//       String[] mainEquipments = {SiteConstants.SiteTreeEnum.GIS_HL_EQM.getValue()};
//       List<BusiTreeNodeVo> list =  busiSiteTreeService.findMainEquipmentList(mainEquipments);
//       for (int i = 0; i < list.size(); i++) {
//           try {
//               gisAssessService.assessmentForGis(list.get(i).getSiteId(), list.get(i).getId());
//           } catch (Exception e) {
//               System.err.println("GIS评测出错【站点："+list.get(i).getSiteId()+";设备Code："+list.get(i).getId()+"】,错误信息如下");
//               e.printStackTrace();
//           }
//       }
//       return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", list);
//   }
}
