package org.jeecg.modules.largeScreen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.itdmDataHistory.entity.ItdmWenxiang;
import org.jeecg.modules.itdmDataHistory.mapper.ItdmWenxiangMapper;
import org.jeecg.modules.itdmDevice.entity.ItdmDevice;
import org.jeecg.modules.itdmDevice.service.IItdmDeviceService;
import org.jeecg.modules.largeScreen.convert.ItdmDeviceConvert;
import org.jeecg.modules.largeScreen.vo.ItdmDeviceRepairAlarmListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


@Api(tags="测试设备维护预警列表")
@RestController
@RequestMapping("/ceshi/weihu")
@Slf4j
public class CeshiController {

    @Autowired
    @SuppressWarnings("all")
    private IItdmDeviceService deviceService;
    @Autowired
    @SuppressWarnings("all")
    private ItdmWenxiangMapper wenxiangMapper;

    @ApiOperation(value="测试设备维护预警列表")
    @GetMapping("/list")
    public List<ItdmDeviceRepairAlarmListVO> ceshi(){

        /**设备维护预警列表*/
        ItdmDevice device = new ItdmDevice();
        device.setIsRepair(0);//0未维护

        QueryWrapper<ItdmDevice> wrapper  = new QueryWrapper<>(device);
        wrapper.isNotNull("repair_time");
        //维护时间小于等于当前时间+15天
        Date afterNow15 = new Date(new Date().getTime() + 15*1000*3600*24);
        wrapper.le("repair_time",afterNow15);

        wrapper.orderByAsc("repair_time");
        List<ItdmDevice> deviceList1 = deviceService.list(wrapper);

        List<ItdmDeviceRepairAlarmListVO>  repairAlarmListVOList = deviceList1.stream().map(i->{
            Date star = new Date();
            Long starTime=star.getTime();
            Long endTime=i.getRepairTime().getTime();
            Long num=endTime-starTime;//时间戳相差的毫秒数
            Integer day = Math.toIntExact(num / (24 * 60 * 60 * 1000));
            return ItdmDeviceConvert.INSTANCE.toList(i,day);
        }).collect(Collectors.toList());

        return repairAlarmListVOList;
    }


    @ApiOperation(value="温箱折线图")
    @GetMapping("/zxlist")
    public List<ItdmWenxiang> getWXList(@RequestParam(value = "deviceId") String deviceId){
        return wenxiangMapper.selectWXGraphList(deviceId);
    }

}
