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.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 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 ReadStationToCacheRunner 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 = "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;

    @Override
    public void run(String... args) throws Exception {
//        PaintshopStation paintshopStation = (PaintshopStation) valueOperations.get(Constant.STATION_PREFIX + "HDG1-13");
//        Skid skid = new Skid();
//        skid.setBusNo("eeee");
//        skid.setBusType(ProductionType.OWN);
//        paintshopStation.setState(StationState.USE);
//        paintshopStation.setSkid(skid);
//        valueOperations.set("station:HDG1-13",paintshopStation);

        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);

        for (PaintshopStation station : stations) {
            stationCache.put(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<String, List<PaintshopStation>> stationMap = stations.stream().filter(sta -> sta.getLaneCode() != null).collect(Collectors.groupingBy(PaintshopStation::getLaneCode));
        for (Map.Entry<String, 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);
    }
}
