package com.wms.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wms.entity.table.Config;
import com.wms.entity.table.Location;
import com.wms.entity.table.WorkDate;
import com.wms.service.ConfigService;
import com.wms.service.LocationService;
import com.wms.service.WorkDateService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Order(1)
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class InitLocalConfig implements ApplicationRunner {
    /**
     * 配置类
     */
    private final ConfigService configService;
    /**
     * 库位服务
     */
    private final LocationService locationService;
    /**
     * 工作日历服务
     */
    private final WorkDateService workDateService;

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    public static Map<String, String> configMap = new HashMap<>();
    public static Map<String, Location> instantLocationMap = new HashMap<>();
    public static List<LocalDate> localWorkDateList = new ArrayList<>();
    @Override
    public void run(ApplicationArguments args) {
        List<Config> configs = configService.selectConfigs("");
        if (configs.size() > 0) {
            for (Config config : configs) {
                configMap.put(config.getConfigKey(), config.getConfigValue());
                logger.info("导入系统配置成功---{}：{}", config.getConfigName(), config.getConfigValue());
            }
        }
        // 生成库位Map
        instantLocationMap = locationService.list().stream().collect(Collectors.toMap(Location::getLocationId, location -> location));
        logger.info("生成库位Map成功。");
        // 生成工作日历
        localWorkDateList = workDateService.list(new LambdaQueryWrapper<WorkDate>().orderByAsc(WorkDate::getWorkDate)).stream().map(WorkDate::getWorkDate).distinct().toList();
        logger.info("生成工作日历Map成功。");
    }
}
