package com.njcn.device.device.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.dto.SimpleDTO;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.device.common.service.GeneralDeviceService;
import com.njcn.device.device.service.IDeviceService;
import com.njcn.device.pq.pojo.dto.DevComFlagDTO;
import com.njcn.device.pq.pojo.dto.DeviceDTO;
import com.njcn.device.pq.pojo.dto.GeneralDeviceDTO;
import com.njcn.device.pq.pojo.param.ConstantValueParam;
import com.njcn.device.pq.pojo.param.DeviceInfoParam;
import com.njcn.device.pq.pojo.po.Device;
import com.njcn.device.pq.pojo.vo.DevStatusNumVO;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * pqs
 * 设备相关
 * @author cdf
 * @date 2023/4/4
 */
@Slf4j
@Api(tags = "台账-设备相关")
@RestController
@RequestMapping("/dev")
@RequiredArgsConstructor
public class DeviceController extends BaseController {

    private final IDeviceService iDeviceService;

    private final GeneralDeviceService generalDeviceService;

//    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
//    @PostMapping("/socketLine")
//    @ApiOperation("获取监测点定值信息")
//    public HttpResult<String> socketLine(@RequestBody @Validated ConstantValueParam.Constant param) {
//        String methodDescribe = getMethodDescribe("socketLine");
//        if(StrUtil.isBlank(param.getIp())){
//            param.setIp(RequestUtil.getRealIp());
//        }
//        String s = iDeviceService.sentLine(param);
//        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, s, methodDescribe);
//    }
//
//    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
//    @PostMapping("/updateSocketLine")
//    @ApiOperation("修改监测点定值信息")
//    public HttpResult<String> updateSocketLine(@RequestBody @Validated ConstantValueParam.ValueData param) {
//        String methodDescribe = getMethodDescribe("updateSocketLine");
//        if(StrUtil.isBlank(param.getIp())){
//            param.setIp(RequestUtil.getRealIp());
//        }
//        String s = iDeviceService.sentLineData(param);
//        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, s, methodDescribe);
//    }
//
//    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
//    @PostMapping("/socketDev")
//    @ApiOperation("获取终端定值信息")
//    public HttpResult<String> socketDev(@RequestBody @Validated ConstantValueParam.Constant param) {
//        String methodDescribe = getMethodDescribe("socketDev");
//        if(StrUtil.isBlank(param.getIp())){
//            param.setIp(RequestUtil.getRealIp());
//        }
//        String s = iDeviceService.sentDev(param);
//        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, s, methodDescribe);
//    }
//    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
//    @PostMapping("/updateSocketDev")
//    @ApiOperation("修改终端定值信息")
//    public HttpResult<String> updateSocketDev(@RequestBody @Validated ConstantValueParam.ValueData param) {
//        String methodDescribe = getMethodDescribe("updateSocketDev");
//        if(StrUtil.isBlank(param.getIp())){
//            param.setIp(RequestUtil.getRealIp());
//        }
//        String s = iDeviceService.sentDevData(param);
//        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, s, methodDescribe);
//    }
//
//    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
//    @PostMapping("/socketDevProperty")
//    @ApiOperation("获取终端性能信息")
//    public HttpResult<String> socketDevProperty(String devID) {
//        String methodDescribe = getMethodDescribe("socketDevProperty");
//        String s = iDeviceService.socketDevProperty(devID);
//        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, s, methodDescribe);
//    }
//
//    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
//    @PostMapping("/socketDevPropertyClose")
//    @ApiOperation("终端性能关闭")
//    public HttpResult<String> socketDevPropertyClose(String devID) {
//        String methodDescribe = getMethodDescribe("socketDevPropertyClose");
//        String s = iDeviceService.socketDevPropertyClose(devID);
//        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, s, methodDescribe);
//    }
//
//    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
//    @PostMapping("/realTimeData")
//    @ApiOperation("监测点实时数据查看")
//    public HttpResult<String> realTimeData(String lineID) {
//        String methodDescribe = getMethodDescribe("realTimeData");
//        String s = iDeviceService.realTimeData(lineID);
//        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, s, methodDescribe);
//    }
//
//    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
//    @PostMapping("/heartRealTimeData")
//    @ApiOperation("监测实施数据心跳")
//    public HttpResult<String> heartRealTimeData(String lineID) {
//        String methodDescribe = getMethodDescribe("heartRealTimeData");
//        String s = iDeviceService.heartRealTimeData(lineID);
//        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, s, methodDescribe);
//    }
//
//    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
//    @PostMapping("/closeRealTimeData")
//    @ApiOperation("监测点实施数据关闭")
//    public HttpResult<String> closeRealTimeData(String lineID) {
//        String methodDescribe = getMethodDescribe("closeRealTimeData");
//        String s = iDeviceService.closeRealTimeData(lineID);
//        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, s, methodDescribe);
//    }
//
//    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
//    @PostMapping("/getDevUpgrades")
//    @ApiOperation("终端版本升级")
//    public HttpResult<String> getDevUpgrades(@RequestBody @Validated ConstantValueParam.Upgrades param) {
//        String methodDescribe = getMethodDescribe("getDevUpgrades");
//        String s = iDeviceService.getDevUpgrades(param.getList(),param.getEdIndex());
//        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, s, methodDescribe);
//    }
//
//    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
//    @PostMapping("/closeUpgrades")
//    @ApiOperation("终端升级取消")
//    public HttpResult<String> closeUpgrades(@RequestBody List<String> devList) {
//        String methodDescribe = getMethodDescribe("closeUpgrades");
//        String s = iDeviceService.closeUpgrades(devList);
//        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, s, methodDescribe);
//    }
//
//    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
//    @PostMapping("/restartDev")
//    @ApiOperation("重启装置命令")
//    public HttpResult<String> restartDev(@RequestBody List<String> devList) {
//        String methodDescribe = getMethodDescribe("restartDev");
//        String s = iDeviceService.restartDev(devList);
//        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, s, methodDescribe);
//    }
//    @MqttSubscribe(value = "/zl/devData/{devID}",qos = 1)
//    public void responseRtData(String topic, @NamedValue("devID") String pageId, MqttMessage message, @Payload String payload) {
//        Console.log("receive from    : {}", topic);
//        Console.log("receive from    : {}", pageId);
//        Console.log("message     : {}", message.getPayload());
//        Console.log("message payload : {}", new String(message.getPayload(), StandardCharsets.UTF_8));
//        Console.log("string payload  : {}", payload);
//    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/updateDevCheckTime")
    @ApiOperation("技术监督修改装置定检时间")
    public HttpResult<String> updateDevCheckTime(String devId, String thisTimeCheck, String nextTimeCheck) {
        String methodDescribe = getMethodDescribe("updateDevCheckTime");
        iDeviceService.updateDevCheckTime(devId,thisTimeCheck,nextTimeCheck);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, "success", methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/updateDevComFlag")
    @ApiOperation("修改装置通讯状态及时间")
    public HttpResult<Boolean> updateDevComFlag(@RequestBody DevComFlagDTO devComFlagDTO) {
        String methodDescribe = getMethodDescribe("updateDevComFlag");
        boolean update = iDeviceService.lambdaUpdate().set(Objects.nonNull(devComFlagDTO.getStatus()),Device::getComFlag,devComFlagDTO.getStatus() ).set(Objects.nonNull(devComFlagDTO.getDate()),Device::getUpdateTime, devComFlagDTO.getDate()).eq(Device::getId, devComFlagDTO.getId()).update();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, update, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/getDeviceInfo")
    @ApiOperation("查询设备详情")
    public HttpResult<DeviceDTO> getDeviceInfo(@RequestParam("devId") String devId) {
        String methodDescribe = getMethodDescribe("getDeviceInfo");
        Device one = iDeviceService.lambdaQuery().eq(Device::getId, devId).one();
        DeviceDTO dto = new DeviceDTO();
        BeanUtils.copyProperties(one,dto);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, dto, methodDescribe);
    }

    @ApiOperation("台账状态统计")
    @PostMapping(value = "statisticDevNum")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    public HttpResult<DevStatusNumVO> statisticDevNum(@RequestBody DeviceInfoParam deviceInfoParam) {
        DevStatusNumVO devStatusNumVO = new DevStatusNumVO();
        String methodDescribe = getMethodDescribe("statisticDevNum");
        deviceInfoParam.setStatisticalType(new SimpleDTO());
        List<GeneralDeviceDTO> generalDeviceDTOList = generalDeviceService.getDeviceInfo(deviceInfoParam,new ArrayList<>(), Stream.of(1).collect(Collectors.toList()));
        List<String> devIds = generalDeviceDTOList.stream().flatMap(list->list.getDeviceIndexes().stream()).collect(Collectors.toList());
        if(CollectionUtil.isEmpty(devIds)){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, devStatusNumVO, methodDescribe);
        }
        devStatusNumVO = iDeviceService.statisticDevNum(devIds);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, devStatusNumVO, methodDescribe);
    }

}
