package com.huatek.busi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.huatek.busi.common.BusiConstant;
import com.huatek.busi.constant.ApiConstant;
import com.huatek.busi.dto.BusiSemiAutomaticEvaluateDataDto;
import com.huatek.busi.service.BusiSemiAutomaticEvaluateDataService;
import com.huatek.busi.service.ITransformerEvaluationService;
import com.huatek.busi.service.gis.GisAssessService;
import com.huatek.busi.vo.BusiParamTransformerVo;
import com.huatek.busi.vo.BusiSemiAutomaticEvaluateDataVo;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import com.huatek.torch.frame.validate.KerValid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags = {ApiConstant.Tags.BUSI_SEMI_AUTOMATIC_EVALUATE_DATA})
@Slf4j
@RestController
@RequestMapping("/api/BusiSemiAutomaticEvaluateData")
public class BusiSemiAutomaticEvaluateDataController {

    @Reference(check = false)
    private BusiSemiAutomaticEvaluateDataService busiSemiAutomaticEvaluateDataService;

    @Reference(check = false)
    ITransformerEvaluationService transformerEvaluationService;

    @Reference(check = false)
    GisAssessService gisAssessService;

    /**
     * 保存半自动评估数据并计算
     *
     * @return
     */
    @ApiOperation(value = "保存半自动评估数据并计算",httpMethod="POST", notes= "保存半自动评估数据并计算", produces = "application/json;charset=utf-8",response= Map.class)
    @PostMapping(value = "/saveAndCalculate", produces = { "application/json;charset=utf-8"})
    public  @KerValid Map<String, Object> saveAndCalculate(@RequestHeader("token") String token, @RequestBody BusiSemiAutomaticEvaluateDataDto dto) {
        boolean result = busiSemiAutomaticEvaluateDataService.add(token,dto);
        if (result) {
            if(BusiConstant.EqType.EQ_TYPE1.equals(dto.getEquipmentType())){
                transformerEvaluationService.transformerSemiAutomaticEvaluation(dto.getSiteId(), dto.getMainEquipmentId());
            }else if(BusiConstant.EqType.EQ_TYPE2.equals(dto.getEquipmentType())){
                gisAssessService.semiAutoAssessmentForGis(dto.getSiteId(), dto.getMainEquipmentId());
            }
        }
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "",result);
    }

    @ApiOperation(value = "查询最新的一条数据", response = BusiParamTransformerVo.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "siteId", value = "站点ID"),
            @ApiImplicitParam(name = "mainEquipmentId", value = "主设备ID"),
            @ApiImplicitParam(name = "equipmentType", value = "主设备类型")
    })
    @GetMapping(value = "/getLatestData", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> getLatestData(@RequestParam("siteId") String siteId,
                                            @RequestParam("mainEquipmentId") String mainEquipmentId,
                                            @RequestParam("equipmentType") String equipmentType) {
        BusiSemiAutomaticEvaluateDataVo vo = busiSemiAutomaticEvaluateDataService.getLatest(equipmentType, mainEquipmentId,siteId);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", vo);
    }
}
