LocationStatisticsServiceImpl.java 4.76 KB
package com.huaheng.api.digitalTwins.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.digitalTwins.domain.CskInfoOverviewDto;
import com.huaheng.api.digitalTwins.domain.LkInfoOverviewDto;
import com.huaheng.api.digitalTwins.domain.LocationInfoOverviewDto;
import com.huaheng.api.digitalTwins.domain.WarehouseInfoOverviewDto;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 库位统计相关业务接口实现类
 */
@Service
@RequiredArgsConstructor
public class LocationStatisticsServiceImpl implements LocationStatisticsService {

    private final LocationService locationService;

    @Override
    public void getLocationInfoOverview(WarehouseInfoOverviewDto warehouseInfoOverviewDto) {
        // 获取所有库位信息
        LambdaQueryWrapper<Location> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(Location::getCode, Location::getContainerCode, Location::getZoneCode);
        List<Location> locationList = locationService.list(queryWrapper);
        // TODO: L、C魔法值
        // 过滤出立库库位
        List<Location> lkLocationList = locationList.stream()
                .filter(x -> "L".equals(x.getZoneCode()))
                .collect(Collectors.toList());
        // 封装立库信息
        LkInfoOverviewDto lkInfoOverviewDto = new LkInfoOverviewDto();
        lkInfoOverviewDto.setLength(BigDecimal.ONE);
        lkInfoOverviewDto.setWidth(BigDecimal.ONE);
        lkInfoOverviewDto.setHeight(BigDecimal.ONE);
        lkInfoOverviewDto.setVolume(BigDecimal.ONE);
        if (StringUtils.isNotEmpty(lkLocationList)) {
            lkInfoOverviewDto.setLocationCount(lkLocationList.size());
            // 集合类型转换 实体->Dto
            List<LocationInfoOverviewDto> locationInfoOverviewDtoList = lkLocationList.stream()
                    .map(location -> {
                        LocationInfoOverviewDto locationInfoOverviewDto = new LocationInfoOverviewDto();
                        BeanUtils.copyProperties(location, locationInfoOverviewDto);
                        return locationInfoOverviewDto;
                    })
                    .collect(Collectors.toList());
            lkInfoOverviewDto.setLocations(locationInfoOverviewDtoList);
        }
        // 过滤出穿梭库库位
        List<Location> cskLocationList = locationList.stream()
                .filter(x -> "C".equals(x.getZoneCode()))
                .collect(Collectors.toList());
        // 封装穿梭库信息
        CskInfoOverviewDto cskInfoOverviewDto = new CskInfoOverviewDto();
        cskInfoOverviewDto.setLength(BigDecimal.valueOf(2));
        cskInfoOverviewDto.setWidth(BigDecimal.valueOf(2));
        cskInfoOverviewDto.setHeight(BigDecimal.valueOf(2));
        cskInfoOverviewDto.setVolume(BigDecimal.valueOf(8));
        if (StringUtils.isNotEmpty(cskLocationList)){
            cskInfoOverviewDto.setLocationCount(cskLocationList.size());
            // 集合类型转换 实体->Dto
            List<LocationInfoOverviewDto> locationInfoOverviewDtoList = cskLocationList.stream()
                    .map(location -> {
                        LocationInfoOverviewDto locationInfoOverviewDto = new LocationInfoOverviewDto();
                        BeanUtils.copyProperties(location, locationInfoOverviewDto);
                        return locationInfoOverviewDto;
                    })
                    .collect(Collectors.toList());
            cskInfoOverviewDto.setLocations(locationInfoOverviewDtoList);
        }

        // 封装仓库概况信息
        warehouseInfoOverviewDto.setLk(lkInfoOverviewDto);
        warehouseInfoOverviewDto.setCsk(cskInfoOverviewDto);
    }

    @Override
    public BigDecimal getLocationUtilizationRate() {
        // 获取所有库位
        List<Location> allLocationList = locationService.list();
        // 过滤出已使用(与托盘有关联)的库位
        List<Location> usedLocationList = allLocationList.stream()
                .filter(x -> StringUtils.isNotEmpty(x.getContainerCode()))
                .collect(Collectors.toList());
        // 库容使用率=仓库使用/仓库容积
        if (StringUtils.isEmpty(usedLocationList)) {
            return BigDecimal.valueOf(1);
        } else {
            return BigDecimal.valueOf((double) usedLocationList.size() / (double) allLocationList.size()).setScale(3, RoundingMode.UP);
        }
    }


}