package com.huimv.env.admin.timer;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.env.admin.entity.BasePigpen;
import com.huimv.env.admin.entity.Gateway;
import com.huimv.env.admin.entity.Terminal;
import com.huimv.env.admin.entity.TerminalPort;
import com.huimv.env.admin.entity.dto.HeartDto;
import com.huimv.env.admin.mapper.GatewayMapper;
import com.huimv.env.admin.mapper.HeartInfoMapper;
import com.huimv.env.admin.mapper.TerminalMapper;
import com.huimv.env.admin.mapper.TerminalPortMapper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

@Configuration
@EnableScheduling
public class DeviceTimer {

    @Autowired
    private HeartInfoMapper heartInfoMapper;
    @Autowired
    private TerminalMapper terminalMapper;
    @Autowired
    private GatewayMapper gatewayMapper;
    @Autowired
    private TerminalPortMapper portMapper;

    @Scheduled(cron = "0 0 0/1 * * ? ")
    private void action() {
        Date date = new Date(System.currentTimeMillis() - 1000 * 60 * 60); // 1小时前

        List<TerminalPort> terminalPortss = portMapper.selectList(null);
        for (TerminalPort terminal : terminalPortss) {
            if (null == terminal.getUpdateTime() || date.after(terminal.getUpdateTime())) {
                terminal.setStatus(0);
            } else {
                terminal.setStatus(1);
            }
            portMapper.updateById(terminal);
        }
        List<Terminal> terminals = terminalMapper.selectList(null);
        for (Terminal terminal : terminals) {
            terminal.setStatus(0);
            terminalMapper.updateById(terminal);
        }
        List<Gateway> geteways = gatewayMapper.selectList(null);
        for (Gateway gateway : geteways) {
            gateway.setStatus(0);
            gatewayMapper.updateById(gateway);
        }

        List<HeartDto> heartDtos = heartInfoMapper.listHeart();
        for (HeartDto heartDto : heartDtos) {
            if (date.after(heartDto.getCreateTime())) {
                //说明一个小时内都没有心跳
                QueryWrapper<Terminal> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("device_code", heartDto.getTerminalCode());
                Terminal terminal = terminalMapper.selectOne(queryWrapper);
                if (ObjectUtil.isNotEmpty(terminal)) {
                    terminal.setStatus(0);
                    terminalMapper.updateById(terminal);
                    QueryWrapper<TerminalPort> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("terminal_id", terminal.getId());
                    List<TerminalPort> terminalPorts = portMapper.selectList(queryWrapper1);
                    for (TerminalPort terminalPort : terminalPorts) {
                        terminalPort.setStatus(0);
                        portMapper.updateById(terminalPort);
                    }
                }
            } else {
                QueryWrapper<Terminal> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("device_code", heartDto.getTerminalCode());
                Terminal terminal = terminalMapper.selectOne(queryWrapper);
                if (ObjectUtil.isNotEmpty(terminal)) {
                    terminal.setStatus(1);
                    terminalMapper.updateById(terminal);
//                    QueryWrapper<TerminalPort> queryWrapper1 = new QueryWrapper<>();
//                    queryWrapper1.eq("terminal_id", terminal.getId());
//                    List<TerminalPort> terminalPorts = portMapper.selectList(queryWrapper1);
//                    for (TerminalPort terminalPort : terminalPorts) {
//                        terminalPort.setStatus(1);
//                        portMapper.updateById(terminalPort);
//                    }
                }
            }
        }
        List<HeartDto> heartDtos1 = heartInfoMapper.listHeartGateway();//网关
        for (HeartDto heartDto : heartDtos1) {
            if (date.after(heartDto.getCreateTime())) {
                //说明一个小时内都没有心跳
                QueryWrapper<Gateway> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("device_code", heartDto.getTerminalCode());
                Gateway terminal = gatewayMapper.selectOne(queryWrapper);
                if (ObjectUtil.isNotEmpty(terminal)) {
                    terminal.setStatus(0);
                    gatewayMapper.updateById(terminal);
                }
            } else {
                QueryWrapper<Gateway> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("device_code", heartDto.getTerminalCode());
                Gateway terminal = gatewayMapper.selectOne(queryWrapper);
                if (ObjectUtil.isNotEmpty(terminal)) {
                    terminal.setStatus(1);
                    gatewayMapper.updateById(terminal);
                }
            }
        }
    }

}
