package com.bonus.sgzb.material.controller;

import com.bonus.sgzb.common.core.web.controller.BaseController;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.core.web.page.TableDataInfo;
import com.bonus.sgzb.material.domain.*;
import com.bonus.sgzb.material.service.IotMachineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author ma_sh
 * @create 2024/7/4 16:23
 * iot设备管理控制层
 */
@Api(tags = " iot设备管理控制层")
@RestController
@RequestMapping("/iotMachine")
@Slf4j
public class IotMachineController extends BaseController {


    @Resource
    private IotMachineService iotMachineService;

    /**
     * 获取设备管理列表
     */
    @ApiOperation(value = "获取设备管理列表")
    @GetMapping("/getIotList")
    public TableDataInfo getIotList(IotDto iotDto) {
        log.info("获取设备管理列表传参：{}", iotDto);
        startPage();
        List<IotVo> list = iotMachineService.getIotList(iotDto);
        return getDataTable(list);
    }

    /**
     * 添加或者修改设备
     * @param iotDto
     * @return
     */
    @ApiOperation("添加或者修改设备")
    @PostMapping("/addOrUpdate")
    public AjaxResult addOrUpdate(@Validated @RequestBody IotDto iotDto) {
        log.info("添加或者修改设备传参：{}", iotDto);
        if (iotDto.getIotId() != null) {
            return iotMachineService.update(iotDto);
        }
        return iotMachineService.add(iotDto);
    }

    /**
     * 删除设备
     * @param iotId
     * @return
     */
    @ApiOperation(value = "删除设备")
    @DeleteMapping("/deleteById/{iotId}")
    public AjaxResult deleteByIds(@PathVariable("iotId") Long iotId) {
        log.info("删除设备传参：{}", iotId);
        return iotMachineService.deleteById(iotId);
    }

    /**
     * 绑定设备
     * @param iotDto
     * @return
     */
    @ApiOperation("绑定设备")
    @PostMapping("/bind")
    public AjaxResult bind(@Validated @RequestBody IotDto iotDto) {
        log.info("绑定设备传参：{}", iotDto);
        return iotMachineService.bind(iotDto);
    }

    /**
     * 绑定设备APP
     * @param iotDto
     * @return
     */
    @ApiOperation("App绑定设备")
    @PostMapping("/bindApp")
    public AjaxResult bindApp(@Validated @RequestBody IotDto iotDto) {
        log.info("绑定设备传参：{}", iotDto);
        return iotMachineService.bindApp(iotDto);
    }

    /**
     * 解绑设备
     * @param iotDto
     * @return
     */
    @ApiOperation("解绑设备")
    @PostMapping("/unbind")
    public AjaxResult unbind(@Validated @RequestBody IotDto iotDto) {
        log.info("解绑设备传参：{}", iotDto);
        return iotMachineService.unbind(iotDto);
    }

    /**
     * 根据类型id查询设备绑定列表
     * @param iotDto
     * @return
     */
    @ApiOperation("根据类型id查询设备绑定列表")
    @GetMapping("/getTypeList")
    public TableDataInfo getTypeList(IotDto iotDto) {
        startPage();
        log.info("根据类型id查询设备绑定列表传参：{}", iotDto);
        List<IotVo> typeList = iotMachineService.getTypeList(iotDto);
        return getDataTable(typeList);
    }

    /**
     * 根据iot设备类型查询设备编码
     * @param iotDto
     * @return
     */
    @ApiOperation("根据iot设备类型查询设备编码")
    @GetMapping("/selectList")
    public AjaxResult selectList(IotDto iotDto) {
        log.info("根据iot设备类型查询设备编码：{}", iotDto);
        return iotMachineService.selectList(iotDto);
    }

    /**
     * 根据iot设备获取绑定记录（分页）
     * @param iotDto
     * @return
     */
    @ApiOperation("根据iot设备获取绑定记录（分页）")
    @GetMapping("/getRecordList")
    public TableDataInfo getRecordList(IotDto iotDto) {
        startPage();
        log.info("根据iot设备获取绑定记录（分页）：{}", iotDto);
        List<IotRecordVo> list = iotMachineService.getRecordList(iotDto);
        return getDataTable(list);
    }

    /**
     * 根据iot设备获取绑定记录（全量不分页，供前端数据使用）
     * @param iotDto
     * @return
     */
    @ApiOperation("根据iot设备获取绑定记录（全量不分页，供前端数据使用）")
    @GetMapping("/getRecordListAll")
    public AjaxResult getRecordListAll(IotDto iotDto) {
        List<IotRecordVo> list = iotMachineService.getRecordList(iotDto);
        return AjaxResult.success(list);
    }


    @ApiOperation("获取定位")
    @PostMapping("/getLocation")
    public AjaxResult getLocation(@Validated @RequestBody IotLocationVo iotLocationVo) {
        log.info("获取定位接口：{}", iotLocationVo);
        return AjaxResult.success(iotMachineService.getLocation(iotLocationVo));
    }

    @ApiOperation("查询行程")
    @PostMapping("/searchItinerary")
    public AjaxResult searchItinerary(@Validated @RequestBody IotLocationVo iotLocationVo) {
        log.info("查询行程接口：{}", iotLocationVo);
        return AjaxResult.success(iotMachineService.searchItinerary(iotLocationVo));
    }

    @ApiOperation("查询停留点")
    @PostMapping("/reportParkDetailByTime")
    public AjaxResult reportParkDetailByTime(@Validated @RequestBody IotLocationVo iotLocationVo) {
        log.info("查询停留点接口：{}", iotLocationVo);
        return AjaxResult.success(iotMachineService.reportParkDetailByTime(iotLocationVo));
    }

    @ApiOperation("报警记录")
    @PostMapping("/reportAlarm")
    public AjaxResult reportAlarm(@Validated @RequestBody IotLocationVo iotLocationVo) {
        log.info("报警记录接口：{}", iotLocationVo);
        return AjaxResult.success(iotMachineService.reportAlarm(iotLocationVo));
    }

    @ApiOperation("报警推送")
    @GetMapping("/alarmPush")
    public AjaxResult alarmPush() {
        log.info("报警推送：=======");
        return AjaxResult.success(iotMachineService.alarmPush());
    }

    @ApiOperation("报警列表")
    @PostMapping("/alarmList")
    public AjaxResult alarmList(@Validated @RequestBody IotLocationVo iotLocationVo) {
        log.info("报警列表：=======");
        return AjaxResult.success(iotMachineService.alarmList(iotLocationVo));
    }

    @ApiOperation("报警处置")
    @PostMapping("/alarmManagement")
    public AjaxResult alarmManagement(@Validated @RequestBody ReportAlarm reportAlarm) {
        log.info("报警处置：=======");
        return AjaxResult.success(iotMachineService.alarmManagement(reportAlarm));
    }
}
