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

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