package net.vtstar.zhongtong.avi.ledlamp.service;

import net.vtstar.scada.base.equipmgt.service.EquipDataService;
import net.vtstar.scada.base.gather.domain.EquipmentData;
import net.vtstar.utils.DateUtils;
import net.vtstar.zhongtong.avi.global.constant.Constant;
import net.vtstar.zhongtong.avi.ledlamp.domain.vo.LedStationVO;
import net.vtstar.zhongtong.avi.monitoring.domain.Warning;
import net.vtstar.zhongtong.avi.monitoring.domain.WorkRoom;
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.enums.StationState;
import net.vtstar.zhongtong.avi.monitoring.domain.vo.CarStatisticsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class LedService {

    @Resource(name = "warningCache")
    private Cache warningCache;
    @Resource(name = "webStationCache")
    private Cache webStationCache;
    @Resource(name = "stationCache")
    private Cache stationCache;
    @Resource(name = "roomCache")
    private Cache roomCache;
    @Autowired
    private EquipDataService equipDataService;


    public LedStationVO getInfoByNo(String ledNo) {

        LedStationVO vo = new LedStationVO();
        if (ledNo.equals("1")) {
            vo.setName("电泳");
        } else if (ledNo.equals("2")) {
            vo.setName("腻子");
        } else if (ledNo.equals("3")) {
            vo.setName("中涂");
        } else if (ledNo.equals("4")) {
            vo.setName("面/清漆");
        } else if (ledNo.equals("5") || ledNo.equals("6") || ledNo.equals("7")) {
            vo.setName("喷漆室/烘干室");
        }

        //获得车辆区域进出车统计
        List<CarStatisticsVO> sta = webStationCache.get("busSta", ArrayList.class);
        if (!CollectionUtils.isEmpty(sta)) {
            for (CarStatisticsVO carStatisticsVO : sta) {
                if (!carStatisticsVO.getAreaNo().equals(ledNo)) {
                    continue;
                }
                if (!carStatisticsVO.getDateType().equals(DateType.TODAY)) {
                    continue;
                }
                if (carStatisticsVO.getBusType().equals(BusType.IN)) {
                    vo.setBusIn(carStatisticsVO.getSize());
                }
                if (carStatisticsVO.getBusType().equals(BusType.OUT)) {
                    vo.setBusOut(carStatisticsVO.getSize());
                }
            }
        }
        //TODO:临时的假数据, 进车数 = 当前小时 * 5;
        int hours = new Date().getHours();
        int minutes = new Date().getMinutes();
        if (hours > 6){
            vo.setBusIn(new BigDecimal((hours - 6) * 4 + minutes / 15 + Integer.valueOf(ledNo)));
            if (vo.getBusIn().intValue() > 12){
                vo.setBusOut(new BigDecimal((hours - 6) * 4 + minutes / 15 - Integer.valueOf(ledNo) - 7));
            }
        }

        if (ledNo.equals("1") || ledNo.equals("2") || ledNo.equals("3") || ledNo.equals("4")) {
            vo.setRoomInfoList(new ArrayList<>());
            List warningList = warningCache.get(Constant.LED_PREFIX + ledNo, ArrayList.class);
            vo.setWarningList(warningList);
        } else {
            List<WorkRoom> room = new ArrayList<>();
            List<String> eqpCodeList = new ArrayList<>();
            if (ledNo.equals("5")) {
                eqpCodeList = Arrays.asList("中涂喷漆室349", "中涂喷漆室350");
            }
            if (ledNo.equals("6")) {
                eqpCodeList = Arrays.asList("彩条喷漆室365", "彩条喷漆室367", "彩条喷漆室369", "彩条喷漆室371");
            }
            if (ledNo.equals("7")) {
                eqpCodeList = Arrays.asList("罩光喷漆室374", "面漆喷漆室359", "面漆喷漆室361", "面漆喷漆室363");
            }
            String startOfDay = DateUtils.parseDateToString(DateUtils.getStartOfDay(0), "yyyy-MM-dd HH:mm:ss");
            String endOfDay = DateUtils.parseDateToString(DateUtils.getEndOfDay(0), "yyyy-MM-dd HH:mm:ss");
            for (String eqpCode : eqpCodeList) {
                List<EquipmentData> runData = equipDataService.findRunByCode(eqpCode, startOfDay, endOfDay);
                for (EquipmentData data : runData) {
                    if (data.getBeginTime() == null || data.getEndTime() == null) {
                        continue;
                    }
                    WorkRoom item = new WorkRoom();
                    item.setName(data.getEquipCode());
                    if ("RUN".equals(data.getStatus())) {
                        item.setStationState(StationState.USE);
                    } else {
                        item.setStationState(StationState.FREE);
                    }
                    long workTime = data.getEndTime().getTime() - data.getBeginTime().getTime();
                    item.setWorkingHours(new BigDecimal(workTime / 1000 / 60f).setScale(2, BigDecimal.ROUND_HALF_UP));
                    room.add(item);
                }
            }
            vo.setRoomInfoList(room);
        }
        return vo;
    }
}
