Storage.java 8.2 KB
package com.myproject.bean.update;

import com.myproject.bean.BaseMongoBean;
import com.myproject.bean.json.PlateSizeBean;
import com.myproject.bean.json.UsageItem;
import com.myproject.util.PLATE_SIZE;
import com.myproject.util.StorageConstants;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.data.annotation.Transient;

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

/**
 * Created by kangmor on 2015/12/3.
 */
public class Storage extends BaseMongoBean {
    @NotEmpty(message = "{storage.name.empty}")
    private String name;
    /**
     * 程序路径
     */
    private String sourcePath;
    private String cid;
    private int totalSlots;
    private int emptySlots;

    /**
     * 兼容类型:完全匹配,完全兼容,同尺寸兼容
     */
    private StorageConstants.COMPATIBLE_TYPE compatibleType = StorageConstants.COMPATIBLE_TYPE.EXACT_MATCH;

    /**
     * 料仓类型:单台自动料仓,手动料仓流水线料仓
     */
    private String type = StorageConstants.TYPE.AUTO.name();

    //包含料仓 Box数量
    private Integer boxCount=1;
    public Integer getBoxCount() {
        if (boxCount == null){
            boxCount = 1;
        }
        return boxCount;
    }

    /**
     * 使用情况
     */
    private Map<String, UsageItem> usageMap = new ConcurrentHashMap<>();

    /**
     * 禁止出入库的 box
     */
    private List<Integer> disableBoxes;

    public List<Integer> getDisableBoxes() {
        return disableBoxes;
    }

    /**
     * 判断 box 是否可用
     */
    public boolean isBoxEnable(Integer boxId){
        if(disableBoxes != null){
            return disableBoxes.contains(boxId);
        }
        return true;
    }

    public void setDisableBoxes(List<Integer> disableBoxes) {
        this.disableBoxes = disableBoxes;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setBoxCount(Integer boxCount) {
        this.boxCount = boxCount;
    }

    private boolean available = true;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }

    public int getTotalSlots() {
        return totalSlots;
    }

    public void setTotalSlots(int totalSlots) {
        this.totalSlots = totalSlots;
    }

    public int getEmptySlots() {
        return emptySlots;
    }

    public void setEmptySlots(int emptySlots) {
        this.emptySlots = emptySlots;
    }

    public boolean isAvailable() {
        return available;
    }

    public void setAvailable(boolean available) {
        this.available = available;
    }

    /**
     * 是否是上下层的在线料仓
     * @return
     */
    public boolean isOnlineStorage(){
        return StorageConstants.TYPE.ONLINE.name().equals(type);
    }

    /**
     * 是否是指上下料的料仓
     * @return
     */
    public boolean isBatchStorage(){
        return StorageConstants.TYPE.BATCH.name().equals(type);
    }

    /**
     * 是否是单台自动仓
     */
    public boolean isAuto(){
        return StorageConstants.TYPE.AUTO.name().equals(type);
    }


    /**
     * 是否是虚拟仓
     */
    public boolean isVirtual(){
        return StorageConstants.TYPE.VIRTUAL.name().equals(type);
    }


    /**
     * 是否是流水线料仓
     */
    public boolean isLine() {
        return StorageConstants.TYPE.LINE.name().equals(type);
    }

    /**
     * 是否是智能料架
     */
    public boolean isShelf() {
        return StorageConstants.TYPE.SHELF.name().equals(type);
    }
    /**
     * 是否是ACC智能料架
     */
    public boolean isAccShelf() {
        return StorageConstants.TYPE.ACCSHELF.name().equals(type);
    }

    /**
     * 是否是扫码料架
     */
    public boolean isCodeShelf() {
        return StorageConstants.TYPE.CODESHELF.name().equals(type);
    }

    /**
     * 是否是锡膏料仓
     */
    public boolean isSolderPaste(){
        return StorageConstants.TYPE.SOLDERPASTE.name().equals(type);
    }


    /**
     * 是否是料柜
     */
    public boolean isCabinet() {
        return StorageConstants.TYPE.CABINET.name().equals(type);
    }


    public boolean canPutInPos(int w, int h, int PosW, int posH){

        if(compatibleType == StorageConstants.COMPATIBLE_TYPE.EXACT_MATCH){//完全匹配
            if(w == PosW && h == posH){
                return true;
            }
        }else if(compatibleType == StorageConstants.COMPATIBLE_TYPE.FULLY_COMPATIBLE){//完全兼容
            if(w <= PosW && h <= posH){
                return true;
            }
        }else if(compatibleType == StorageConstants.COMPATIBLE_TYPE.SIZE_COMPATIBLE){//同尺寸兼容
            if(w == PosW && h <= posH){
                return true;
            }
        }
        return false;
    }

    /**
     * 判断料盘是否能够放入该仓库: 0=完全匹配,1=完全兼容,2=同尺寸兼容
     */
    public boolean canPutIn(int w, int h){
        if(usageMap != null){
            for (UsageItem usageItem : usageMap.values()) {
                if(canPutInPos(w,h, usageItem.getW(), usageItem.getH())){
                    return true;
                }
            }
        }
        return false;
    }

    public String getSourcePath() {
        return sourcePath;
    }

    public void setSourcePath(String sourcePath) {
        this.sourcePath = sourcePath;
    }

    public StorageConstants.COMPATIBLE_TYPE getCompatibleType() {
        return compatibleType;
    }

    public void setCompatibleType(StorageConstants.COMPATIBLE_TYPE compatibleType) {
        this.compatibleType = compatibleType;
    }
    public Map<String, UsageItem> getUsageMap() {
        return usageMap;
    }

    public void setUsageMap(Map<String, UsageItem> usageMap) {
        this.usageMap = usageMap;
    }

    /**
     * 使用一个仓位,更新使用情况
     */
    public void useOnePos(StoragePos pos){
        if(usageMap != null){
            String sizeStr = pos.getSizeStr();
            UsageItem usageItem = usageMap.get(sizeStr);
            if(usageItem != null){
                this.emptySlots = this.emptySlots - 1;
                int usedCount = usageItem.getUsedCount();
                usageItem.setUsedCount(usedCount + 1);
                usageMap.put(sizeStr, usageItem);
            }
        }
    }

    public void emptyOnePos(StoragePos pos){
        if(usageMap != null){
            String sizeStr = pos.getSizeStr();
            UsageItem usageItem = usageMap.get(sizeStr);
            if(usageItem != null){
                this.emptySlots = this.emptySlots + 1;
                int usedCount = usageItem.getUsedCount();
                usageItem.setUsedCount(usedCount - 1);
                usageMap.put(sizeStr, usageItem);
            }
        }
    }

    public void initUsage(List<PlateSizeBean> plateSizeBeanList){
        usageMap = new ConcurrentHashMap<>();
        int totalPosCount = 0;
        int emptyPosCount = 0;
        for (PlateSizeBean plateSizeBean : plateSizeBeanList) {
            String sizeStr = plateSizeBean.getSizeStr();
            UsageItem usageItem = usageMap.get(sizeStr);
            if(usageItem == null){
                usageItem = new UsageItem();
                usageItem.setW(plateSizeBean.getPlateSize().getW());
                usageItem.setH(plateSizeBean.getPlateSize().getH());
            }
            if(plateSizeBean.getPlateSize().isUsed()){
                int usedCount = plateSizeBean.getCount();
                usageItem.setUsedCount(usedCount);
                usageItem.setTotalCount(usedCount + usageItem.getTotalCount());
            }else{
                //未使用的数量
                int idleCount = plateSizeBean.getCount();
                usageItem.setTotalCount(idleCount + usageItem.getTotalCount());
                emptyPosCount = emptyPosCount + idleCount;
            }
            totalPosCount = totalPosCount + plateSizeBean.getCount();
            usageMap.put(sizeStr, usageItem);
        }
        this.setEmptySlots(emptyPosCount);
        this.setTotalSlots(totalPosCount);
    }
}