package net.vtstar.zhongtong.avi.monitoring.job;

import lombok.extern.slf4j.Slf4j;
import net.vtstar.scada.base.gather.job.GatherJob;
import net.vtstar.zhongtong.avi.equipment.domain.PaintshopArea;
import net.vtstar.zhongtong.avi.equipment.domain.PaintshopStation;
import net.vtstar.zhongtong.avi.global.constant.Constant;
import net.vtstar.zhongtong.avi.monitoring.domain.enums.BusType;
import net.vtstar.zhongtong.avi.monitoring.domain.enums.DateType;
import net.vtstar.zhongtong.avi.monitoring.domain.vo.CarStatisticsVO;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.cache.Cache;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Component
public class RedisToCacheJob extends GatherJob {

    public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd");
    @Resource(name = "redisTemplate")
    private ValueOperations valueOperations;
    @Resource(name = "redisTemplate")
    private RedisTemplate redisTemplate;
    @Resource(name = "webStationCache")
    private Cache webStationCache;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        log.info("______________");
        log.info("RedisToCacheJob start");

        Set<String> keys = redisTemplate.keys(Constant.STATION_PREFIX + "*");
        List<PaintshopStation> stations = new ArrayList<>();

        for (String key : keys) {
            PaintshopStation station = (PaintshopStation) valueOperations.get(key);
            stations.add(station);
        }
        Map<Long, List<PaintshopStation>> listMap = stations.stream().collect(Collectors.groupingBy(PaintshopStation::getAreaId));

        Calendar instance = Calendar.getInstance();
        String today = dateFormat.format(instance.getTime());
        instance.add(Calendar.DATE, -1);
        String previous = dateFormat.format(instance.getTime());

        List<CarStatisticsVO> vos = new ArrayList<>();
        addStatistics(today, vos, "0", DateType.TODAY);
        addStatistics(previous, vos, "0", DateType.YESTERDAY);

        List<PaintshopArea> areas = webStationCache.get("stationList", ArrayList.class);
        for (PaintshopArea area : areas) {
            Long id = area.getId();
            List<PaintshopStation> stationList = listMap.get(id);
            Map<String, PaintshopStation> collect = stationList.stream().collect(Collectors.toMap(PaintshopStation::getCode, sta -> sta, (key1, key2) -> key1));
            area.setStationMap(collect);
            addStatistics(today, vos, area.getCode(), DateType.TODAY);
        }
        webStationCache.put("busSta", vos);
        webStationCache.put("stationList", areas);
    }

    private void addStatistics(String today, List<CarStatisticsVO> vos, String code, DateType dateType) {
        CarStatisticsVO vo = new CarStatisticsVO();
        vo.setAreaNo(code);
        Integer todaySize = (Integer) valueOperations.get(Constant.BUS_SIZE_IN + code + ":" + today);
        vo.setDateType(dateType);
        if (null == todaySize) {
            vo.setSize(BigDecimal.ZERO);
        } else {
            vo.setSize(BigDecimal.valueOf(todaySize));
        }
        vo.setBusType(BusType.IN);

        CarStatisticsVO out = new CarStatisticsVO();
        out.setAreaNo(code);
        Integer todayOut = (Integer) valueOperations.get(Constant.BUS_SIZE_OUT + code + ":" + today);
        out.setDateType(dateType);
        if (null == todayOut) {
            out.setSize(BigDecimal.ZERO);
        } else {
            out.setSize(BigDecimal.valueOf(todayOut));
        }
        out.setBusType(BusType.OUT);

        vos.add(vo);
        vos.add(out);
    }


}
