package com.usky.iot.controller.web;

import com.usky.backend.domain.*;
import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.core.utils.poi.ExcelUtil;
import com.usky.demo.domain.LastInnerQueryVO;
import com.usky.iot.domain.DmpDeviceInfo;
import com.usky.iot.service.DmpDeviceInfoService;
import com.usky.iot.service.vo.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 设备信息表
 *
 * @author ya
 * @since 2022-10-08
 */
@RestController
@RequestMapping("/dmpDeviceInfo")
public class DmpDeviceInfoController {

    @Autowired
    private DmpDeviceInfoService dmpDeviceInfoService;

    /**
     * 新增
     * @param dmpDeviceInfo
     * @return
     */
    @PostMapping
    public ApiResult<Void> add(@RequestBody DmpDeviceInfo dmpDeviceInfo) {
        dmpDeviceInfoService.add(dmpDeviceInfo);
        return ApiResult.success();
    }

    /**
     * 修改
     * @param dmpDeviceInfo
     * @return
     */
    @PutMapping
    public ApiResult<Void> edit(@RequestBody DmpDeviceInfo dmpDeviceInfo) {
        dmpDeviceInfoService.update(dmpDeviceInfo);
        return ApiResult.success();
    }

    /**
     * 更新设备列表状态
     * @param queryVO
     * @return
     */
    @PostMapping("updateDeviceStatus")
    public ApiResult<Void> updateDeviceStatus(@RequestBody LastInnerQueryVO queryVO){
        dmpDeviceInfoService.updateDeviceStatus(queryVO);
        return ApiResult.success();
    }

    /**
     * 分页
     * @param dmpDeviceInfoRequest
     * @return
     */
    @PostMapping("/page")
    public ApiResult<CommonPage<DmpDeviceInfo>> page(@RequestBody DmpDeviceInfoRequest dmpDeviceInfoRequest) {
        return ApiResult.success(dmpDeviceInfoService.page(dmpDeviceInfoRequest));
    }


    /**
     * 分页-白名单
     * @param dmpDeviceInfoRequest
     * @return
     */
    @PostMapping("/pageWhite")
    public ApiResult<CommonPage<DmpDeviceInfo>> pageWhite(@RequestBody DmpDeviceInfoRequest dmpDeviceInfoRequest) {
        return ApiResult.success(dmpDeviceInfoService.pageWhite(dmpDeviceInfoRequest));
    }

    /**
     * 网关设备信息下拉接口
     * @return
     */
    @GetMapping("gatewayDeviceList")
    public ApiResult<List<DmpDeviceInfo>> gatewayDeviceList() {
        return ApiResult.success(dmpDeviceInfoService.gatewayDeviceList());
    }

    /**
     * 删除记录
     */
    @DeleteMapping("/{id}")
    public ApiResult<Void> remove(@PathVariable("id") Integer id) {
        dmpDeviceInfoService.remove(id);
        return ApiResult.success();
    }

    /**
     * 批量导入
     * @param multipartFiles
     * @return
     */
    @PostMapping("/addByFile")
    public ApiResult<Void> addByFile(@RequestParam(value = "file") MultipartFile multipartFiles) {
        dmpDeviceInfoService.addByFile(multipartFiles);
        return ApiResult.success();
    }

    /**
     * 批量导入-对接客户
     *
     * @param list
     * @return
     */
    @PostMapping("/importDeviceInfo")
    public ApiResult<Void> importDeviceInfo(@RequestBody List<DmpDeviceInfo> list) {
        dmpDeviceInfoService.importDeviceInfo(list);
        return ApiResult.success();
    }

    /**
     * 导出
     * @param dmpDeviceInfoRequest
     * @param response
     * @return
     */
    @PostMapping("/export")
    public void export(@RequestBody DmpDeviceInfoRequest dmpDeviceInfoRequest, HttpServletResponse response) {
        dmpDeviceInfoService.export(dmpDeviceInfoRequest, response);
    }

    /**
     * 网关配置查询
     * @param gatewayDeviceInfoRequestVO
     * @return
     */
    @PostMapping("/gatewayExport")
    public ApiResult<Map<String, Map<String, GatewayDeviceExportItemVO>>> gatewayExport(@RequestBody GatewayDeviceInfoRequestVO gatewayDeviceInfoRequestVO) {
        return ApiResult.success(dmpDeviceInfoService.gatewayExport(gatewayDeviceInfoRequestVO));
    }

    /**
     * 数据中心-设备实时心跳数据接口
     *
     * @param deviceName 设备名称
     * @param installAddress 设备安装位置
     * @param productCode   产品编码
     * @param pageNum  当前页
     * @param pageSize  每页条数
     * @return
     */
    @GetMapping("deviceCurrentDataList")
    public ApiResult<CommonPage<Object>> deviceCurrentDataList(@RequestParam(value = "deviceName", required = false) String deviceName,
                                                               @RequestParam(value = "installAddress", required = false) String installAddress,
                                                               @RequestParam(value = "productCode", required = false) String productCode,
                                                               @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                               @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return ApiResult.success(dmpDeviceInfoService.deviceCurrentDataList(deviceName, installAddress, productCode, pageNum, pageSize));
    }

    /**
     * 监控电视墙实时数据
     *
     * @param requestVO
     * @return
     */
    @PostMapping("dataTVList")
    public ApiResult<CommonPage<DataTVResponseVO>> dataTVList(@RequestBody DataTVRequestVO requestVO) {
        return ApiResult.success(dmpDeviceInfoService.dataTVList(requestVO));
    }

    /**
     * 监控电视墙设备（告警、离线）统计
     *
     * @param requestVO
     * @return
     */
    @PostMapping("dataTVStatistic")
    public ApiResult<List<DataTVStatisticResponseVO>> dataTVStatistic(@RequestBody DataTVRequestVO requestVO) {
        return ApiResult.success(dmpDeviceInfoService.dataTVStatistic(requestVO));
    }

    /**
     * 获取设备某个时间端内的汇总值
     * @param requestVO
     * @return
     */
    @PostMapping("deviceDataTotal")
    @ApiOperation("获取设备某个时间端内的汇总值")
    public ApiResult<List<Map<String, Object>>> deviceDataTotal(@RequestBody DataTotalRequestVO requestVO) {
        return ApiResult.success(dmpDeviceInfoService.deviceDataTotal(requestVO));
    }

    /**
     * 设备日平均值、月平均值、年平均值
     * @param requestVO
     * @return
     */
    @PostMapping("deviceDataAverage")
    @ApiOperation("获取设备日平均、月平均、年平均值")
    public ApiResult<List<DataAverageResponseVO>> deviceDataAverage(@RequestBody DataAverageRequestVO requestVO) {
        return ApiResult.success(dmpDeviceInfoService.deviceDataAverage(requestVO));
    }

    /**
     * 设备日平均值、月平均值、年平均值导出
     * @param requestVO
     * @param response
     * @throws IOException
     */
    @PostMapping("deviceDataAverageExport")
    public void export(@RequestBody DataAverageRequestVO requestVO, HttpServletResponse response) throws IOException {
        List<DataAverageExportVO> list = dmpDeviceInfoService.DataAverageExport(requestVO);
        ExcelUtil<DataAverageExportVO> util = new ExcelUtil<DataAverageExportVO>(DataAverageExportVO.class);
        util.exportExcel(response, list, requestVO.getSheetName(), requestVO.getTitleName());
    }

    /**
     * 获取设备topic列表
     * @param requestVO
     * @return
     */
    @PostMapping("topic")
    public ApiResult<CommonPage<DmpTopicResponseVO>> topic(@RequestBody DmpTopicRequestVO requestVO) {
        return ApiResult.success(dmpDeviceInfoService.topic(requestVO));
    }

    /**
     * 获取设备告警请求信息
     * @param deviceId 设备id
     * @param productCode 产品编码
     * @return
     */
    @GetMapping("alarmResponse")
    public ApiResult<DmpHttpAlarmResponseVO> alarmResponse(@RequestParam(value = "deviceId") String deviceId,
                                                           @RequestParam(value = "productCode") String productCode){
        return ApiResult.success(dmpDeviceInfoService.alarmResponse(deviceId, productCode));
    }
}

