package com.xy.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xy.annotate.RestMappingController;
import com.xy.annotation.LogOperate;
import com.xy.collections.list.JArrayList;
import com.xy.collections.map.JMap;
import com.xy.device.EnumDeviceActiveStatus;
import com.xy.device.EnumDeviceFreezeStatus;
import com.xy.device.EnumDeviceOnlineStatus;
import com.xy.dto.*;
import com.xy.dto.be.MercDto;
import com.xy.dto.common.MercPlaceDto;
import com.xy.entity.*;
import com.xy.enums.LogEnum;
import com.xy.enums.MercStatus;
import com.xy.service.*;
import com.xy.service.be.MercFeignService;
import com.xy.service.be.MercService;
import com.xy.service.common.MercLineService;
import com.xy.service.common.MercPlaceService;
import com.xy.service.factory.device.DeviceFactory;
import com.xy.utils.*;
import com.xy.utils.Enum.AlgorithmTypeEnum;
import com.xy.utils.enums.*;
import com.xy.vo.DeviceDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.xy.utils.Beans.copy;

/**
 * 商管PC-设备
 */
@RestMappingController("merc-pc/device")
@AllArgsConstructor
@Api(tags = "商管PC-设备")
public class MercPcDeviceController {

    private final DeviceInfoServiceImpl deviceService;
    private final MercService mercService;
    private final DeviceStatusServiceImpl deviceStatusService;
    private final DeviceEventMsgService deviceEventMsgService;
    private final DeviceNetRecordServiceImpl deviceNetRecordService;

    @ApiOperation("设备分页-商户")
    @PostMapping("page")
    public R<PageBean<DeviceInfoDto.Vo2>> page(@RequestBody DeviceInfoDto.Page page) {
        page.setMercId(page.getMercId() == null ? MercAuthUtils.getMercId() : page.getMercId());
        //非商户管理员，按设备权限过滤
        if (!MercAuthUtils.isMercAdmin()) {
            List<Long> mercDeviceIds = getMercDeviceIds(true);
            if (CollUtil.isEmpty(mercDeviceIds)) {
                return R.ok(new PageBean<>());
            }
            page.setDeviceIdList(mercDeviceIds);

        }
        PageBean<DeviceInfoDto.Vo2> vo2PageBean = deviceService.queryPage(page);
        return R.ok(vo2PageBean);
    }

    @ApiOperation("设备分页-统计")
    @PostMapping("pageCount")
    public R<DeviceInfoDto.PageCount> pageCount(@RequestBody DeviceInfoDto.Page page) {
        page.setMercId(page.getMercId() == null ? MercAuthUtils.getMercId() : page.getMercId());
        //非商户管理员，按设备权限过滤

        List<Long> mercDeviceIds = getMercDeviceIds(true);
        if (CollUtil.isEmpty(mercDeviceIds)) {
            return R.ok(new DeviceInfoDto.PageCount ());
        }
        page.setMyDeviceIds(mercDeviceIds);

        return R.ok(deviceService.pageCountForPc(page));
    }


    /**
     * 获取商户设备列表
     *
     * @return {@link List}<{@link Long}>
     */
    public List<Long> getMercDeviceIds(Boolean showNoActive) {
        MercDto.MercDevicesDTO mercDevicesDTO = new MercDto.MercDevicesDTO();
        mercDevicesDTO.setMercAdmin(MercAuthUtils.isMercAdmin());
        mercDevicesDTO.setMercId(MercAuthUtils.getMercId());
        Long userId = MercAuthUtils.getMercAuth().getUserInfo().getUserId();
        mercDevicesDTO.setUserId(userId);
        mercDevicesDTO.setShowNoActive(showNoActive);
        List<Long> deviceIds = R.feignCheckData(mercService.getMercDevices(mercDevicesDTO));
        return deviceIds;
    }


    @ApiOperation("设备温度异常事件测试")
    @PostMapping("testDeviceTempAlarm")
    public R<?> testDeviceTempAlarm(@RequestBody DeviceInfoDto.Vo vo) {
        //添加事件
        String msg = "温度异常-温度阈值%d - %d,当前温度%d";
        DeviceEventMsgDto.Save deviceEventMsg = new DeviceEventMsgDto.Save()
                .setDeviceId(vo.getDeviceId());
        deviceEventMsg.setCode(DeviceErrorRecordTypesEnum.T.getCode());
        deviceEventMsg.setMsg(String.format(msg, -30, 30, 40));
        deviceEventMsgService.save(deviceEventMsg);
        return R.ok();
    }
    @ApiOperation("测试设备离线告警")
    @PostMapping("testDeviceOfflineAlarm")
    public R<?> testDeviceOfflineAlarm(@RequestBody DeviceInfoDto.Vo vo) {
        deviceNetRecordService.deviceOfflineTimeOutPushMsg(vo.getDeviceId());
        return R.ok();
    }

}
