package com.huatek.busi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.huatek.busi.constant.ApiConstant;
import com.huatek.busi.dto.CwzzThresholdDto;
import com.huatek.busi.dto.GeneratorAmplThresholdDto;
import com.huatek.busi.service.threshold.CwzzThresholdService;
import com.huatek.busi.service.threshold.GeneratorAmplThresholdService;
import com.huatek.busi.vo.CwzzThresholdVo;
import com.huatek.busi.vo.GeneratorAmplThresholdVo;
import com.huatek.torch.frame.log.KerLog;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 测温数据
 * @author buck_guo
 * @date 2019/08/08
 */
@Api(tags = {"振动/压力装置告警及阈值详情"})
@RestController
@RequestMapping("/api/GeneratorAmplThreshold")
public class GeneratorAmplThresholdController {
    protected final Logger logger = LoggerFactory.getLogger(GeneratorAmplThresholdController.class);

    @Reference(check = false)
    private CwzzThresholdService cwzzThresholdService;

    @Reference(check = false)
    private GeneratorAmplThresholdService generatorAmplThresholdService;

     /**
      * √
      * 查询测温阈值查询
      * @param sensorCode
      * @return
      */
    @ApiOperation(value = "查询阈值")
    @PostMapping(value = "/queryGeneratorAmplThreshold", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> queryGeneratorAmplThreshold(@RequestParam(value = "sensorCode") String sensorCode,
        @RequestParam(value = "workStatus") String workStatus,
        @RequestParam(value = "type") String type) {
        GeneratorAmplThresholdVo generatorAmplThresholdVo =
                generatorAmplThresholdService.queryGeneratorAmplThreshold(sensorCode, workStatus);
        if (generatorAmplThresholdVo == null) {
            generatorAmplThresholdVo = new GeneratorAmplThresholdVo();
            generatorAmplThresholdVo.setWorkStatus(workStatus);
            generatorAmplThresholdVo.setSensorCode(sensorCode);
            generatorAmplThresholdVo.setType(type);
        }
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", Arrays.asList(generatorAmplThresholdVo));
    }

    /**
     * 新增修改阈值
     * @param token
     * @param generatorAmplThresholdDto
     * @return
     */
   @ApiOperation(value = "新增修改阈值")
   @PostMapping(value="/insertOrUpdate", produces = {"application/json;charset=utf-8"})
   public Map<String, Object> insertOrUpdate(@RequestHeader("token")String token,@RequestBody GeneratorAmplThresholdDto[] generatorAmplThresholdDto) {
       int count = generatorAmplThresholdService.insertOrUpdate(token,generatorAmplThresholdDto);
       logger.debug("insertOrUpdate: {}", count);
       return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "阈值保存成功", "");
   }


    /**
     * 新增sensor时增加16个绕组测温阈值
     * @param token
     * @param sensorCode
     * @param type
     * @return
     */
  @ApiOperation(value = "新增sensor时增加3个测温阈值")
  @KerLog(projectName = "PROJECTNAME", modelName = "IECBASECONF.MODELNAME", methodName = "IECBASECONF.METHODNAME.ADDIECBASECONF")
  @PostMapping(value="/addSensorInsertGeneratorAmpl", produces = {"application/json;charset=utf-8"})
  public Map<String, Object> addSensorInsertGeneratorAmpl(@RequestHeader("token")String token,@RequestParam(value="sensorCode")String sensorCode,@RequestParam(value="type")String type) {
      int count = generatorAmplThresholdService.addSensorInsertGeneratorAmpl(token,sensorCode,type);
      logger.debug("insertOrUpdate: {}", count);
      return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "阈值保存成功", "");
  }

    @ApiOperation(value = ApiConstant.Tags.BUSI_YSP_TYPE)
    @ApiImplicitParam(name = "findType", value = "振动装置雷类型查询")
    @GetMapping(value = "/findType", produces = {"application/json;charset=utf-8"})
    public Map<String, Object> findType( @RequestParam("sensorCode") String sensorCode) {
        Map<String, Object> typeMap = generatorAmplThresholdService.findType(sensorCode);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", typeMap);
    }
}
