FutureHoldContextValueManagerImpl.java

package com.mycim.server.ctx.exec.manager.impl;

import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.jdbc.Page;
import com.mycim.framework.oid.IDGenerators;
import com.mycim.framework.oid.type.IDNames;
import com.mycim.framework.oid.type.SequenceNames;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.server.base.manager.NamedObjectManager;
import com.mycim.server.base.manager.TransactionLogManager;
import com.mycim.server.ctx.exec.dao.FutureHoldContextValueDAO;
import com.mycim.server.ctx.exec.manager.FutureHoldContextValueManager;
import com.mycim.server.ctx.exec.manager.RecipeContextManager;
import com.mycim.server.ctx.manager.ContextValueManager;
import com.mycim.server.wip.dao.LotQueryDAO;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.TransactionLog;
import com.mycim.valueobject.consts.ContextNames;
import com.mycim.valueobject.consts.EenActionType;
import com.mycim.valueobject.consts.TransactionNames;
import com.mycim.valueobject.prp.ContextValue;
import com.mycim.valueobject.prp.FutureHold;
import com.mycim.valueobject.wip.Lot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;

/**
 * @author shijie.deng
 * @version 6.0.0
 * @date 2019/9/21
 **/
@Service
@Transactional
public class FutureHoldContextValueManagerImpl implements FutureHoldContextValueManager {

    @Autowired
    private FutureHoldContextValueDAO futureHoldContextValueDAO;

    @Autowired
    private RecipeContextManager recipeContextManager;

    @Autowired
    private LotQueryDAO lotQueryDAO;

    @Autowired
    private ContextValueManager contextValueManager;

    @Autowired
    private NamedObjectManager namedObjectManager;

    @Autowired
    private TransactionLogManager transactionLogManager;

    @Override
    public List<Map> getFutureHoldListByLot(long refRrn, long lotRrn) {
        List<Map> futureHoldList = futureHoldContextValueDAO.getFutureHoldListByLot(refRrn, lotRrn);
        for (Map map : futureHoldList) {
            paddingData(map);
            String recipeId = recipeContextManager.getProcessRecipeId(MapUtils.getLongValue(map, "productRrn"),
                                                                      MapUtils.getInteger(map, "productVersion"),
                                                                      MapUtils.getLongValue(map, "processRrn"),
                                                                      MapUtils.getIntValue(map, "processVersion"),
                                                                      MapUtils.getLongValue(map, "routeRrn"),
                                                                      MapUtils.getLongValue(map, "operationRrn"));
            map.put("recipeId", recipeId);
        }
        return futureHoldList;
    }

    @Override
    public List<Map> getFutureHoldListByProduct(long refRrn, long productRrn, long processRrn, long lotRrn) {
        List<Map> futureHoldList = futureHoldContextValueDAO
                .getFutureHoldListByProduct(refRrn, productRrn, processRrn, lotRrn);
        Integer processVersion = null;
        if (lotRrn > 0) {
            Lot lot = lotQueryDAO.getLot(lotRrn);
            processVersion = lot.getProcessVersion();

        }
        for (Map map : futureHoldList) {
            map = paddingData(map);
            String recipeId = recipeContextManager.getProcessRecipeId(MapUtils.getLongValue(map, "productRrn"),
                                                                      MapUtils.getInteger(map, "productVersion"),
                                                                      MapUtils.getLongValue(map, "processRrn"),
                                                                      processVersion,
                                                                      MapUtils.getLongValue(map, "routeRrn"),
                                                                      MapUtils.getLongValue(map, "operationRrn"));
            map.put("recipeId", recipeId);
        }
        return futureHoldList;
    }

    @Override
    public void removeFutureHoldContextValue(long refRrn, int seqNum, long lotRrn, long productRrn, long operationRrn,
                                             String type) {
        if (refRrn > 0 && seqNum > 0) {
            ContextValue contextValue = new ContextValue();
            contextValue.setContextRrn(refRrn);
            contextValue.setSequenceNumber(seqNum);
            if (StringUtils.equals(type, "lot")) {
                contextValue.setContextKey2(StringUtils.toString(operationRrn));
                contextValue.setContextKey3(StringUtils.toString(lotRrn));
            } else if (StringUtils.equals(type, "product")) {
                contextValue.setContextKey1(StringUtils.toString(productRrn));
            }
            contextValueManager
                    .removeContextValue(contextValue, LocalContext.getFacilityRrn(), LocalContext.getUserRrn());
        }
    }

    @Override
    public void deleteFutureHoldByLotRrn(Long facilityRrn, Long userRrn, Long lotRrn) {
        ContextValue contextValue = buildEENContextHoldLotContextValue(facilityRrn);

        contextValue.setContextKey3(StringUtils.toString(lotRrn));

        contextValueManager.deleteContextValueByContextKey("Lot Reassign", contextValue);
    }

    @Override
    public List<Map> getFutureHoldListbyOperation(long refRrn, long operationRrn) {
        List<Map> futureHoldList = futureHoldContextValueDAO.getFutureHoldListbyOperation(refRrn, operationRrn);
        for (Map map : futureHoldList) {
            String recipeId = recipeContextManager.getProcessRecipeId(MapUtils.getLongValue(map, "productRrn"),
                                                                      MapUtils.getInteger(map, "productVersion"),
                                                                      MapUtils.getLongValue(map, "processRrn"),
                                                                      MapUtils.getIntValue(map, "processVersion"),
                                                                      MapUtils.getLongValue(map, "routeRrn"),
                                                                      MapUtils.getLongValue(map, "operationRrn"));
            map.put("recipeId", recipeId);
        }

        return futureHoldList;
    }

    @Override
    public List getFutureHoldLotList(Long refRrn, Long routeRrn, Long operationRrn, Long lotRrn, Long routeSeq,
                                     Long operationSeq) {
        List<Map> temp = futureHoldContextValueDAO
                .getFutureHoldLotList(refRrn, routeRrn, operationRrn, lotRrn, routeSeq, operationSeq);
        for (Map map : temp) {
            paddingData(map);
        }
        return temp;
    }

    @Override
    public List getFutureHoldProductList(Long refRrn, Long routeRrn, Long operationRrn, Long lotRrn, Long productRrn,
                                         Long processRrn) {
        List futureHoldProductList = futureHoldContextValueDAO
                .getFutureHoldProductList(refRrn, routeRrn, operationRrn, lotRrn, productRrn, processRrn);
        List resultList = new ArrayList();
        for (Object obj : futureHoldProductList) {
            Map map = (Map) obj;
            paddingData(map);
            Lot lot = (Lot) map.get("recipeId");
            String recipeString = recipeContextManager.getRecipeString(lot);
            String recipeId = recipeContextManager.parseRecipeId(recipeString);
            map.put("recipeId", recipeId);
            resultList.add(map);
        }
        return resultList;
    }

    @Override
    public List<ContextValue> getFutureHoldList(Map<String, String> contextKey) {

        List<ContextValue> result = new ArrayList<>();

        result.addAll(getFutureHoldByContextKey(LocalContext.getFacilityRrn(), contextKey));
        result.addAll(getFutureHoldByProduct(contextKey));
        result.addAll(getFutureHoldByOperation(contextKey));

        return result;
    }

    @Override
    public List<ContextValue> getFutureHoldByContextKey(Long facilityRrn, Map<String, String> contextKey) {
        ContextValue contextValue = buildEENContextHoldLotContextValue(facilityRrn);

        contextValue.setContextKey1(MapUtils.getString(contextKey, "routeRrn"));
        contextValue.setContextKey2(MapUtils.getString(contextKey, "operationRrn"));
        contextValue.setContextKey3(MapUtils.getString(contextKey, "lotRrn"));

        return contextValueManager.getContextValuesByContextKey(contextValue);
    }

    @Override
    public ContextValue buildEENContextHoldLotContextValue(Long facilityRrn) {
        return buildBaseContextValue(facilityRrn, ContextNames.EEN_CONTEXT_HOLDLOT);
    }

    @Override
    public Page getFutureHoldPage(Page page, Map<String, Object> condition) {
        return futureHoldContextValueDAO.getFutureHoldPage(page, condition);
    }

    public ContextValue buildEENContextHoldProductContextValue(Long facilityRrn) {
        return buildBaseContextValue(facilityRrn, ContextNames.EEN_CONTEXT_HOLDPRODUCT);
    }

    public ContextValue buildEENContextHoldProductContextValue() {
        return buildEENContextHoldProductContextValue(LocalContext.getFacilityRrn());
    }

    public ContextValue buildBaseContextValue(Long facilityRrn, String contextId) {
        ContextValue contextValue = new ContextValue();
        contextValue.setContextId(contextId);
        contextValue.setContextRrn(
                namedObjectManager.getNamedObjectRrn(contextValue.getContextId(), facilityRrn, ObjectList.CONTEXT_KEY));
        return contextValue;
    }

    public ContextValue buildEENContextHoldLotContextValue() {
        return buildEENContextHoldLotContextValue(LocalContext.getFacilityRrn());
    }

    private Map paddingData(Map map) {
        // 补充数据
        long processRrn = MapUtils.getLongValue(map, "processRrn");
        if (processRrn != 0) {
            map.put("processId", namedObjectManager.getInstanceId(processRrn));
        }

        long routeRrn = MapUtils.getLongValue(map, "routeRrn");
        if (routeRrn != 0) {
            map.put("routeId", namedObjectManager.getInstanceId(routeRrn));
        }

        long operationRrn = MapUtils.getLongValue(map, "operationRrn");
        if (operationRrn != 0) {
            map.put("operationId", namedObjectManager.getInstanceId(operationRrn));
            map.put("operationDesc", namedObjectManager.getNamedObjectDesc(operationRrn));
        }
        return map;
    }

    private List<ContextValue> getFutureHoldByOperation(Map<String, String> contextKey) {
        ContextValue contextValue = buildEENContextHoldOperationContextValue();
        contextValue.setContextKey1(MapUtils.getString(contextKey, "routeRrn"));
        contextValue.setContextKey2(MapUtils.getString(contextKey, "operationRrn"));
        return contextValueManager.getContextValuesByContextKey(contextValue);
    }

    private ContextValue buildEENContextHoldOperationContextValue() {
        return buildEENContextHoldOperationContextValue(LocalContext.getFacilityRrn());
    }

    private ContextValue buildEENContextHoldOperationContextValue(long facilityRrn) {
        return buildBaseContextValue(facilityRrn, ContextNames.EEN_CONTEXT_HOLDOPERATION);
    }

    private List<ContextValue> getFutureHoldByProduct(Map<String, String> contextKey) {
        ContextValue contextValue = buildEENContextHoldProductContextValue();

        contextValue.setContextKey1(MapUtils.getString(contextKey, "productRrn"));
        contextValue.setContextKey2(MapUtils.getString(contextKey, "processRrn"));
        contextValue.setContextKey3(MapUtils.getString(contextKey, "routeRrn"));
        contextValue.setContextKey4(MapUtils.getString(contextKey, "operationRrn"));
        return contextValueManager.getContextValuesByContextKey(contextValue);
    }

    @Override
    public List<FutureHold> getAllFutureHoldListByLot(long contextRrn, long lotRrn) {
        return futureHoldContextValueDAO.getAllFutureHoldListByLot(contextRrn,lotRrn);
    }

    @Override
    public List<FutureHold> getAllFutureHoldListByProduct(long contextRrn, Long productRrn, Long processRrn) {
        return futureHoldContextValueDAO.getAllFutureHoldListByProduct(contextRrn,productRrn,processRrn);
    }
}