package com.huatek.busi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.common.SiteConstants;
import com.huatek.busi.dto.ArresterCalDto;
import com.huatek.busi.entity.ArresterAssessmentResult;
import com.huatek.busi.entity.TransformerLoadRate;
import com.huatek.busi.service.ArresterAssessmentResultService;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.service.ArresterAssessService;
import com.huatek.busi.vo.ArresterAssessmentResultVo;
import com.huatek.busi.vo.BusiTreeNodeVo;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

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

@Api(tags = {"避雷器评估"})
@RestController
@RequestMapping("/api/ArresterAssess")
public class ArresterAssessController {

    @Reference(check = false)
    private ArresterAssessService iArresterEvaluationService;

    @Reference(check = false)
    private BusiSiteTreeService busiSiteTreeService;

    @Reference(check = false)
    private ArresterAssessmentResultService arresterAssessmentResultService;

    @PostMapping(value = "/getArresterAssessmentResult")
    public Map<String, Object>  getArresterAssessmentResult(@RequestBody ArresterCalDto arresterCalDto) {
        ArresterAssessmentResult arresterAssessmentResult =  iArresterEvaluationService.assessmentForArrester(arresterCalDto.getSiteId(), arresterCalDto.getMainTreeId(),"1",
                arresterCalDto.getX1(),arresterCalDto.getX2(),arresterCalDto.getX3(),arresterCalDto.getX4(),arresterCalDto.getX5(),arresterCalDto.getX6(),arresterCalDto.getΩ(),arresterCalDto.getΒ());
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", arresterAssessmentResult);
    }

    /**
     * 查询GIS评估
     * @param
     * @return
     */
   @ApiOperation(value ="查询避雷器评估", response = ArresterAssessmentResultVo.class)
   @GetMapping(value="/queryArresterAssessmentResultVo", produces = {"application/json;charset=utf-8"})
   public Map<String, Object> queryArresterAssessmentResultVo(@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<ArresterAssessmentResultVo> arresterAssessmentResultVo = arresterAssessmentResultService.findArresterAssessmentByMainTreeId(siteId, mainTreeId, page, limit, start, end, type);
       return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, arresterAssessmentResultVo.getTotal(), "", arresterAssessmentResultVo.getList());
   }

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


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

    /**
     * 评估参数同步
     * @param
     * @return
     */
    @ApiOperation(value ="评估参数同步")
    @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 arresterAssessmentResultService.syncParams(token,siteId,mainTreeId,number);
    }

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