MatrixUtils.java 6.07 KB
package com.huaheng.api.wcs.domain;

import com.huaheng.common.constant.GenConstants;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;

import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.function.Function;

/***
 * @author tongzonghao
 *
 */
public class MatrixUtils {

    /**
     * 添加矩阵数据
     *
     * @param matr
     * @param inventoryDetailList
     */
    public static void add(@NotNull int[][] matr, List<InventoryDetail> inventoryDetailList) {
        Map<Integer, InventoryDetail> map = new HashMap<>();
        for (InventoryDetail inventoryDetail : inventoryDetailList) {
            map.put(inventoryDetail.getContainerDetailNumber(), inventoryDetail);
        }

        int num = 0;
        for (int i = 0; i < matr.length; i++) {
            for (int j = 0; j < matr[0].length; j++) {
                num++;
                if (map.containsKey(num)) {
                    matr[i][j] = num;
                } else {
                    matr[i][j] = 0;
                }
            }
        }
    }

    public static void addPoint(@NotNull int[][] matrix,List<Integer> positions){
        addPoint(matrix,positions,false);
    }

    /**
     * 填充二维数组
     * @param matrix 空二维数组
     * @param positions 添加点位
     * @param mirrorFill 是否镜像填充 默认false true反转填充
     */
    public static void addPoint(@NotNull int[][] matrix,List<Integer> positions,boolean mirrorFill){
        Map<Integer, Integer> map = new HashMap<>();
        for (Integer integer : positions) {
            map.put(integer, integer);
        }
        int num = 0;
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                num++;
                boolean containsKey = map.containsKey(num);
                boolean isMirrorFill = mirrorFill ? !containsKey : containsKey;

                if (isMirrorFill) {
                    matrix[i][j] = num;
                } else {
                    matrix[i][j] = 0;
                }
            }
        }
    }
    /**
     * 通过二维数组获取目标 坐标位置
     * @param matrix 二维数组
     * @param target 目标值
     * @return 返回 数组 [0]X坐标 [1]Y坐标
     */
    public static int[] getPosition(int[][] matrix,int target){
        Map<Integer, List<int[]>> valueToCoords = new HashMap<>();

        // 遍历二维数组,记录每个值在数组中的位置
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                int value = matrix[i][j];
                if (valueToCoords.containsKey(value)) {
                    valueToCoords.get(value).add(new int[]{i, j});
                } else {
                    List<int[]> list = new ArrayList<>();
                    list.add(new int[]{i, j});
                    valueToCoords.put(value, list);
                }
            }
        }
        if (valueToCoords.containsKey(target)) {
            List<int[]> coords = valueToCoords.get(target);
            if(coords.size()>1){
                throw new SecurityException("检索指定值【"+target+"】的位置数量大于1");
            }
            return coords.get(0);
        }else{
            throw new SecurityException("未检索指定值【"+target+"】的位置数量");
        }
    }

    public static int getMaxValue(int[][] data){
        int maxValue = data[0][0];
        for (int j = 0; j < data.length; j++) {
            for (int i = 0; i < data[j].length; i++) {
                if (data[j][i] > maxValue) {
                    maxValue = data[j][i];
                }
            }
        }
        return maxValue;
    }

    public static int offsetPosition(int[][] data,int maxValue,int offset){
        int[] position = MatrixUtils.getPosition(data, maxValue);
        if(data.length * data[0].length <= offset){
            throw new ServiceException("偏移量offset过多超出索引");
        }

        int maxRow = position[0];
        int maxColumn = position[1];
        maxRow += (maxColumn + offset) / data[0].length;
        int newColumn =  (maxColumn + offset) % data[0].length;

        if((maxColumn + offset) > data[0].length - 1){
            if(maxRow > data[0].length){
                throw new ServiceException("偏移量过多超出索引");
            }
            if( maxRow > data.length -1){
                throw new ServiceException("数据异常点位已满");
            }
        }
        if((maxColumn + offset) < data[0].length - 1){
            if(maxRow > data[0].length){
                throw new ServiceException("偏移量过多超出索引");
            }
            if( maxRow > data.length -1){
                throw new ServiceException("数据异常点位已满");
            }
        }
        setForEach(data,x -> x);
        return data[maxRow][newColumn];
    }

    public static void setForEach(int[][] data,Function<Integer, Integer> funtion) {
        int dataLength = data[0].length;
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[0].length; j++) {
                data[i][j] = funtion.apply( (i * dataLength) + j + 1);
            }
        }
    }

    /**
     * 垛型
     * @param type 垛型:由箱子类型 和 箱子数量组合而成 boxType * 100 + totalQty
     * @return
     */
    public static int[][] getMatrix(int type){
        int[][] matrix = null;
        type = type / 100;

        switch (type){
            case 2:
            case 3:
                matrix = new int[3][3];
                break;
            case 1:
                matrix = new int[3][4];
                break;
            default:
        }
        return matrix;
    }

    public static int rowValueCount(int[][] data,int row){
        int maxValeXCount = 0;
        for (int i = 0; i < data[0].length; i++) {
            maxValeXCount += data[row][i];
        }
        return maxValeXCount;
    }

    public static int columnValueCount(int[][] data,int column){
        return 0;
    }
}