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

import lombok.extern.slf4j.Slf4j;
import net.vtstar.user.mybatis.provider.OrderBy;
import net.vtstar.user.mybatis.provider.WhereFilter;
import net.vtstar.user.search.service.SearchService;
import net.vtstar.zhongtong.avi.equipment.domain.PaintshopArea;
import net.vtstar.zhongtong.avi.equipment.domain.PaintshopLane;
import net.vtstar.zhongtong.avi.equipment.domain.PaintshopStation;
import net.vtstar.zhongtong.avi.equipment.mapper.PaintshopAreaMapper;
import net.vtstar.zhongtong.avi.equipment.mapper.PaintshopStationMapper;
import net.vtstar.zhongtong.avi.global.constant.Constant;
import net.vtstar.zhongtong.avi.monitoring.domain.Skid;
import net.vtstar.zhongtong.avi.monitoring.domain.Vehicle;
import net.vtstar.zhongtong.avi.monitoring.domain.Warning;
import net.vtstar.zhongtong.avi.monitoring.domain.WorkRoom;
import net.vtstar.zhongtong.avi.monitoring.domain.enums.ProductionType;
import net.vtstar.zhongtong.avi.monitoring.domain.enums.StationState;
import net.vtstar.zhongtong.avi.monitoring.mapper.SkidMapper;
import net.vtstar.zhongtong.avi.monitoring.mapper.VehicleMapper;
import net.vtstar.zhongtong.avi.monitoring.mapper.WarningMapper;
import net.vtstar.zhongtong.avi.monitoring.mapper.WorkRoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.cache.Cache;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Order(value = 1)
@Component
public class ReadDataToCacheRunner implements CommandLineRunner {

    @Resource(name = "stationCache")
    private Cache stationCache;
    @Resource(name = "vehicleCache")
    private Cache vehicleCache;
    @Resource(name = "laneStationCache")
    private Cache laneStationCache;
    @Resource(name = "webStationCache")
    private Cache webStationCache;
    @Resource(name = "warningCache")
    private Cache warningCache;
    @Resource(name = "roomCache")
    private Cache roomCache;

    @Resource(name = "redisTemplate")
    protected HashOperations hashOperations;
    @Resource(name = "redisTemplate")
    protected ValueOperations valueOperations;
    @Autowired
    private SearchService searchService;
    @Autowired
    private PaintshopStationMapper stationMapper;
    @Autowired
    private VehicleMapper vehicleMapper;
    @Autowired
    private SkidMapper skidMapper;
    @Autowired
    private PaintshopAreaMapper areaMapper;
    @Autowired
    private WarningMapper warningMapper;
    @Autowired
    private WorkRoomMapper workRoomMapper;

    @Override
    public void run(String... args) throws Exception {

        List<Warning> warnings = warningMapper.findList(new WhereFilter() {
            {
                addOrderBy("warning", "id", OrderBy.ASC);
            }
        }, Warning.class);
        for (Warning warning : warnings) {
            warningCache.put(warning.getCode(), warning);
        }

//        List<WorkRoom> rooms = workRoomMapper.findList(new WhereFilter(), WorkRoom.class);
//
//
//        Map<String, List<WorkRoom>> listMap = rooms.stream().collect(Collectors.groupingBy(WorkRoom::getLedNo));
//        for (Map.Entry<String, List<WorkRoom>> entry : listMap.entrySet()) {
//            roomCache.put(Constant.LED_PREFIX + entry.getKey(), entry.getValue());
//        }

        List<PaintshopStation> stations = stationMapper.findList(new WhereFilter() {{
            addJoin(PaintshopStation.class, "area_id", PaintshopArea.class, "id");
            addJoin(PaintshopStation.class, "lane_id", PaintshopLane.class, "id");
            addOrderBy(PaintshopStation.class, "id", OrderBy.ASC);
        }}, PaintshopStation.class);

//todo: 初始化工位信息
        for (int i = 0; i < stations.size(); i++) {
            PaintshopStation station = stations.get(i);
            if (i % 5 == 0) {
                station.setBusType(ProductionType.OWN);
                station.setBusNo("VIN" + i);
                station.setWorkOrderNo("1");
                station.setState(StationState.USE);
            } else {
                station.setState(StationState.FREE);
            }
            station.setState(StationState.FREE);
            valueOperations.set(Constant.STATION_PREFIX + station.getCode(),station);
        }




        for (PaintshopStation station : stations) {
            stationCache.put(station.getCode(), station);
            PaintshopStation sta = (PaintshopStation) valueOperations.get(Constant.STATION_PREFIX + station.getCode());
            if (null == sta) {
                valueOperations.set(Constant.STATION_PREFIX + station.getCode(), station);
            }
//            station.setTurnF(false);
//            station.setTurnR(false);
//            station.setArrive(false);
//            station.setBusType(ProductionType.OWN);
//            station.setBusNo("eee");
//            station.setState(StationState.USE);
//            valueOperations.set(Constant.STATION_PREFIX + station.getCode() ,station);
        }

        Map<Long, List<PaintshopStation>> stationMap = stations.stream().filter(sta -> sta.getLaneId() != null).collect(Collectors.groupingBy(PaintshopStation::getLaneId));
        for (Map.Entry<Long, List<PaintshopStation>> entry : stationMap.entrySet()) {
            laneStationCache.put(entry.getKey(), entry.getValue());
        }

        List<Vehicle> vehicles = vehicleMapper.findList(new WhereFilter(), Vehicle.class);
        for (
                Vehicle vehicle : vehicles)

        {
            vehicleCache.put(vehicle.getMqttPrefix(), vehicle);
        }

        //
//        Map<String, Skid> skidMap = hashOperations.entries("skid");
//        if (null == skidMap) {
//            List<Skid> skids = skidMapper.findList(null, Skid.class);
//            Map<String, Skid> skid = skids.stream().collect(Collectors.toMap(Skid::getRfid, s -> s, (k1, k2) -> k1));
//            hashOperations.putAll("skid", skid);
//        }
//        List<Skid> skids = skidMapper.findList(new WhereFilter(), Skid.class);
//        for (PaintshopStation station : stations) {
//            stationCache.put(station.getCode(), station);
//        }
        List<PaintshopArea> list = areaMapper.findList(new WhereFilter(), PaintshopArea.class);
        webStationCache.put("stationList", list);

        for (
                PaintshopArea area : list)

        {
            stationCache.put(Constant.AREA_PREFIX + area.getCode(), area);
        }
    }
}
