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.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;
    @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", "中途烘干室351", "中途烘干室352", "腻子烘干室336", "腻子烘干室335", "腻子烘干室377");
            }
            if (ledNo.equals("6")) {
                eqpCodeList = Arrays.asList("彩条喷漆室365", "彩条喷漆室367", "彩条喷漆室369", "彩条喷漆室371", "彩条烘干室366", "彩条烘干室368", "彩条烘干室370", "彩条烘干室372");
            }
            if (ledNo.equals("7")) {
                eqpCodeList = Arrays.asList("罩光喷漆室374", "面漆喷漆室359", "面漆喷漆室361", "面漆喷漆室363", "面漆烘干室360", "面漆烘干室362", "面漆烘干室364", "罩光漆烘干室");
            }
            long startOfDay = DateUtils.getStartOfDay(0).getTime();
            for (String eqpCode : eqpCodeList) {
                EquipmentData lastEquipData = equipDataService.getLastEquipData(eqpCode);
                WorkRoom item = new WorkRoom();
                item.setName(eqpCode);
                if (null == lastEquipData || null == lastEquipData.getId() || lastEquipData.getBeginTime().getTime() < startOfDay){
                    item.setStationState(StationState.FREE);
                    item.setWorkingHours(new BigDecimal(0));
                    room.add(item);
                    continue;
                }
                long workTime = System.currentTimeMillis() - lastEquipData.getBeginTime().getTime();
                if ("RUN".equals(lastEquipData.getEquipStatus().name())) {
                    item.setStationState(StationState.USE);
                    item.setWorkingHours(new BigDecimal(workTime / 1000 / 60f).setScale(2, BigDecimal.ROUND_HALF_UP));
                } else {
                    item.setStationState(StationState.FREE);
                    item.setWorkingHours(new BigDecimal(0));
                }

                room.add(item);
            }
            vo.setRoomInfoList(room);
        }
        return vo;
    }
}
