package com.sckw.slope.detection.controller;

import com.alibaba.fastjson2.JSONObject;
import com.sckw.core.annotation.Log;
import com.sckw.core.web.response.HttpResult;
import com.sckw.log.TraceLog.TraceLog;
import com.sckw.slope.detection.model.dto.LocationDTO;
import com.sckw.slope.detection.model.param.DeviceAdd;
import com.sckw.slope.detection.model.param.DeviceQuery;
import com.sckw.slope.detection.model.param.DeviceReferenceAdd;
import com.sckw.slope.detection.service.DeviceService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lfdc
 * @description 设备controller
 * @date 2023-10-30 17:10:24
 */
@Slf4j
@RestController
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    DeviceService deviceService;

    @Log(description = "设备删除")
    //@RepeatSubmit(interval = 3000, message = "两次请求间隔未超过3秒")
    @RequestMapping(name = "设备删除", value = "/dels", method = RequestMethod.GET)
    public HttpResult dels(@RequestParam("ids") @NotBlank(message = "设备id不能为空") String ids, HttpServletRequest request) {
        log.info("设备删除 delete param:{}", ids);
        return deviceService.dels(ids, request);
    }

    @Log(description = "所属设备查询")
    //@RepeatSubmit(interval = 3000, message = "两次请求间隔未超过3秒")
    @RequestMapping(name = "所属设备查询", value = "/deviceAll", method = RequestMethod.GET)
    public HttpResult deviceAll(@RequestParam("projectId") String projectId, HttpServletRequest request) {
        log.info("所属设备查询 deviceAll param :{}", projectId);
        return deviceService.deviceAll(projectId,request);
    }

    @Log(description = "设备查询-分页")
    //@RepeatSubmit(interval = 3000, message = "两次请求间隔未超过3秒")
    @RequestMapping(name = "设备查询-分页", value = "/select", method = RequestMethod.POST)
    public HttpResult select(@Valid @RequestBody DeviceQuery deviceQuery, HttpServletRequest request) {
        log.info("设备查询 select param:{}", JSONObject.toJSONString(deviceQuery));
        return HttpResult.ok(deviceService.select(deviceQuery, request));
    }

    @Log(description = "设备新增")
    //@RepeatSubmit(interval = 3000, message = "两次请求间隔未超过3秒")
    @RequestMapping(name = "设备新增", value = "/add", method = RequestMethod.POST)
    public HttpResult add(@Valid @RequestBody DeviceAdd deviceAdd, HttpServletRequest request) {
        log.info("设备新增 add param:{}", JSONObject.toJSONString(deviceAdd));
        return deviceService.add(deviceAdd, request);
    }

    @Log(description = "设备详情")
    //@RepeatSubmit(interval = 3000, message = "两次请求间隔未超过3秒")
    @RequestMapping(name = "设备详情", value = "/detail", method = RequestMethod.GET)
    public HttpResult detail(@RequestParam("id") @NotNull(message = "数据id不能为空") long id, HttpServletRequest request) {
        log.info("设备 detail param:{}", id);
        return deviceService.detail(id, request);
    }

    @Log(description = "设备位置配置")
    //@RepeatSubmit(interval = 3000, message = "两次请求间隔未超过3秒")
    @RequestMapping(name = "设备位置配置", value = "/locationSet", method = RequestMethod.POST)
    public HttpResult locationSet(@RequestBody @Valid LocationDTO list, HttpServletRequest request) {
        try {
            log.info("设备位置配置 locationSet param :{}", JSONObject.toJSONString(list));
            return deviceService.locationSet(list, request);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Log(description = "设备监测值-原始/当前/测量")
    //@RepeatSubmit(interval = 3000, message = "两次请求间隔未超过3秒")
    @RequestMapping(name = "设备监测值-原始/当前/测量", value = "/outputValue", method = RequestMethod.GET)
    public HttpResult outputValue(@RequestParam("projectId") @NotBlank(message = "项目id不能为空") String projectId,
                                  @RequestParam("deviceId") @NotBlank(message = "设备id不能为空") String deviceId,
                                  HttpServletRequest request) {
        log.info("设备监测值-原始/当前/测量 outputValue param1 :{},param2:{}", projectId, deviceId);
        return deviceService.outputValue(projectId, deviceId, request);
    }

    @Log(description = "设备编辑")
    //@RepeatSubmit(interval = 3000, message = "两次请求间隔未超过3秒")
    @RequestMapping(name = "设备编辑", value = "/update", method = RequestMethod.POST)
    public HttpResult update(@Valid @RequestBody DeviceAdd deviceAdd, HttpServletRequest request) {
        log.info("设备编辑 update param:{}", JSONObject.toJSONString(deviceAdd));
        return deviceService.update(deviceAdd, request);
    }

    @Log(description = "设置基准值")
    //@RepeatSubmit(interval = 3000, message = "两次请求间隔未超过3秒")
    @RequestMapping(name = "设置基准值", value = "/referenceEdit", method = RequestMethod.POST)
    public HttpResult referenceEdit(@Valid @RequestBody DeviceReferenceAdd deviceReference, HttpServletRequest request) {
        log.info("设置基准值 referenceEdit param:{}", JSONObject.toJSONString(deviceReference));
        return deviceService.referenceEdit(deviceReference, request);
    }

    @Log(description = "设备监测要素项")
    //@RepeatSubmit(interval = 3000, message = "两次请求间隔未超过3秒")
    @RequestMapping(name = "设备监测要素项", value = "/monitorItem", method = RequestMethod.GET)
    public HttpResult monitorItem(@RequestParam("id") @NotBlank(message = "数据id不能为空") String id, HttpServletRequest request) {
        log.info("设备监测要素项 select param:{}", id);
        return deviceService.monitorItem(id, request);
    }


    //    @Log(description = "查询型号下的设备")
    @TraceLog(description = "查询产品类型下的设备")
    @RequestMapping(name = "查询产品类型下的设备", value = "/findDeviceListByModelId", method = RequestMethod.GET)
    public HttpResult findDeviceListByModelId(@RequestParam("modelType") @NotBlank(message = "产品类型不能为空") String modelType, HttpServletRequest request) {
        log.info("查询产品类型下的设备 findDeviceListByModelId query :{}",modelType);
        return deviceService.findDeviceListByModelType(modelType, request);
    }

}
