package com.xydl.cac.task;

import com.xydl.cac.entity.ModevType;
import com.xydl.cac.entity.NSensor;
import com.xydl.cac.entity.ThermalConfig;
import com.xydl.cac.repository.NSensorRepository;
import com.xydl.cac.service.ModevTypeService;
import com.xydl.cac.service.ThermalConfigService;
import com.xydl.cac.thermal.ThermalCollectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class ClientThermalTask {

    @Resource
    ThermalConfigService configService;
    @Resource
    ModevTypeService modevTypeService;
    @Resource
    NSensorRepository sensorRepository;
    @Resource
    ThermalCollectService collectService;

    int shutdown = 0;
    boolean inDoing = false;

    @PreDestroy
    private void preDestroy() {
        shutdown = 1;
    }

    @Scheduled(cron = "0 * * * * ?")
    public void collectAll() {
        ThermalConfig config = configService.getConfig();
        if (config == null) {
            return;
        }
        if (inDoing) {
            return;
        }
        inDoing = true;
        try {
            Calendar cal = Calendar.getInstance();
            int h = cal.get(Calendar.HOUR_OF_DAY);
            int m = cal.get(Calendar.MINUTE);
            m = h * 60 + m;

            ModevType modevType = modevTypeService.detail(config.getTypeId());
            int intervals = 30;
            if (modevType.getIntervals() != null) {
                intervals = modevType.getIntervals();
            }
            int x = m % intervals;
            if (x == 0) {
                this.collectOneConfig(config, modevType);
            }
        } catch (Exception e) {
            log.error("ClientThermalTask.collectAll error.", e);
        } finally {
            inDoing = false;
        }
    }

    private void collectOneConfig(ThermalConfig config, ModevType modevType) {
        if (shutdown == 1) {
            return;
        }
        List<NSensor> list = sensorRepository.findByTypeId(modevType.getId());
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        HashMap<String, List<NSensor>> map = new HashMap<>();

        for (NSensor sensor : list) {
            sensor.setTableName(modevType.getTablename());
            if (sensor.canTempMeasure()) {
                String key = sensor.getIp() + sensor.getPort() + sensor.getUsername();
                List<NSensor> sub = map.get(key);
                if (sub == null) {
                    sub = new ArrayList<>();
                    map.put(key, sub);
                }
                sub.add(sensor);
            }
        }
        Iterator<String> it = map.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            List<NSensor> sub = map.get(key);
            collectService.collectAndSave(sub, config);
        }
    }
}
