package org.wcs.helper;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.wcs.mapper.intf.AppConveyLocationService;
import org.wcs.mapper.intf.AppStackerLocationService;
import org.wcs.mapper.intf.AppStackerStandService;
import org.wcs.mapper.intf.AppTrayConveyLocationService;
import org.wcs.model.po.app.AppConveyLocation;
import org.wcs.model.po.app.AppStackerLocation;
import org.wcs.model.po.app.AppStackerStand;
import org.wcs.model.po.app.AppTrayConveyLocation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 点位帮助类
 */
@Component
@RequiredArgsConstructor
public class LocationHelper {

    private final AppStackerLocationService stackerLocationService;
    private final AppStackerStandService stackerStandService;
    private final AppTrayConveyLocationService trayConveyLocationService;
    private final AppConveyLocationService conveyLocationService;

    /**
     * 点位映射 < 业务点位，系统点位 >
     */
    Map<String, String> btLocationMap;


    /**
     * 点位映射 < 系统点位，业务点位 >
     */
    Map<String, String> tbLocationMap;

    /**
     * 初始化数据
     */
    public synchronized void init() {
        List<AppStackerLocation> stackerLocations = stackerLocationService.queryStackerLocationAll();
        if(stackerLocations == null) {
            return;
        }
        List<AppStackerStand> stackerStands = stackerStandService.queryAll();
        if(stackerStands == null) {
            return;
        }
        List<AppTrayConveyLocation> trayConveyLocations = trayConveyLocationService.queryAll();
        if(trayConveyLocations == null) {
            return;
        }
        List<AppConveyLocation> conveyLocations = conveyLocationService.queryAll();
        if(conveyLocations == null) {
            return;
        }
        btLocationMap = new ConcurrentHashMap<>();
        tbLocationMap = new ConcurrentHashMap<>();
        stackerLocations.forEach(stackerLocation -> {
            btLocationMap.put(stackerLocation.getBusinessLocation(), stackerLocation.getLocationId());
            tbLocationMap.put(stackerLocation.getLocationId(), stackerLocation.getBusinessLocation());
        });
        stackerStands.forEach(stackerStand -> {
            btLocationMap.put(stackerStand.getStandId(), stackerStand.getStandId());
            tbLocationMap.put(stackerStand.getStandId(), stackerStand.getStandId());
        });
        trayConveyLocations.forEach(trayConveyLocation -> {
            btLocationMap.put(trayConveyLocation.getBusinessLocationId(), trayConveyLocation.getLocationId());
            tbLocationMap.put(trayConveyLocation.getLocationId(), trayConveyLocation.getBusinessLocationId());
        });
        conveyLocations.forEach(conveyLocation -> {
            btLocationMap.put(conveyLocation.getBusinessLocationId(), conveyLocation.getLocationId());
            tbLocationMap.put(conveyLocation.getLocationId(), conveyLocation.getBusinessLocationId());
        });
    }

    /**
     * 获取业务点位
     * @param appLocationId 系统点位
     * @return 业务点位
     */
    public String getBusinessLocationId(String appLocationId) {
        if(tbLocationMap == null) {
            init();
        }
        if(tbLocationMap == null) {
            return null;
        }
        return tbLocationMap.get(appLocationId);
    }

    /**
     * 获取系统点位
     * @param businessLocationId 业务点位
     * @return 系统点位
     */
    public String getAppLocationId(String businessLocationId) {
        if(btLocationMap == null) {
            init();
        }
        if(btLocationMap == null) {
            return null;
        }
        return btLocationMap.get(businessLocationId);
    }





}
