package com.huatek.busi.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.huatek.busi.dto.CwzzThresholdDto;
import com.huatek.busi.service.threshold.CwzzThresholdService;
import com.huatek.busi.vo.CwzzThresholdVo;
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.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/CwzzThreshold")
public class CwzzThresholdController {
    protected final Logger logger = LoggerFactory.getLogger(CwzzThresholdController.class);

    @Reference(check = false)
    private CwzzThresholdService cwzzThresholdService;

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

    /**
     * 新增修改测温阈值
     * @param token
     * @param cwzzThresholdDto
     * @return
     */
   @ApiOperation(value = "新增修改阈值")
   @PostMapping(value="/insertOrUpdate", produces = {"application/json;charset=utf-8"})
   public Map<String, Object> insertOrUpdate(@RequestHeader("token")String token,@RequestBody CwzzThresholdDto[] cwzzThresholdDto) {
       int count = cwzzThresholdService.insertOrUpdate(token,cwzzThresholdDto);
       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="/addSensorInsertCwzz", produces = {"application/json;charset=utf-8"})
  public Map<String, Object> addSensorInsertCwzz(@RequestHeader("token")String token,@RequestParam(value="sensorCode")String sensorCode,@RequestParam(value="type")String type) {
      int count = cwzzThresholdService.addSensorInsertCwzz(token,sensorCode,type);
      logger.debug("insertOrUpdate: {}", count);
      return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "测温阈值保存成功", "");
  }
}
