# RecipeServiceImpl.java

    package com.mycim.server.rcp.service;

    import com.alipay.sofa.runtime.api.annotation.SofaService;
    import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
    import com.fa.sesa.threadlocal.LocalContext;
    import com.mycim.framework.jdbc.Page;
    import com.mycim.framework.utils.beans.BeanUtils;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.framework.utils.lang.collections.MapUtils;
    import com.mycim.server.base.manager.RelationManager;
    import com.mycim.server.ctx.exec.manager.RecipeContextManager;
    import com.mycim.server.rcp.manager.RecipeManager;
    import com.mycim.server.rcp.manager.RecipeVersionManager;
    import com.mycim.server.wip.manager.JobManager;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.automonitor.entity.LotAutoMonitorInfo;
    import com.mycim.valueobject.automonitor.util.AutoMonitorUtils;
    import com.mycim.valueobject.bas.NamedObject;
    import com.mycim.valueobject.bas.Relation;
    import com.mycim.valueobject.consts.EventName;
    import com.mycim.valueobject.consts.VersionStatus;
    import com.mycim.valueobject.ems.Entity;
    import com.mycim.valueobject.prp.*;
    import com.mycim.valueobject.wip.Lot;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import java.lang.Override;
    import java.util.*;

    /**
     * recipe Service impl
     *
     * @author pinyan song
     * @version 6.0.0
     * @date 2019-8-30
     **/
    @Service
    @SofaService(interfaceType = RecipeService.class, bindings = {@SofaServiceBinding(bindingType = "bolt")})
    public class RecipeServiceImpl implements RecipeService {

        @Autowired
        private RecipeManager recipeManager;

        @Autowired
        private RecipeVersionManager recipeVersionManager;

        @Autowired
        private RelationManager relationManager;

        @Autowired
        private RecipeContextManager recipeContextManager;

        @Autowired
        private JobManager jobManager;

        @Override
        public Recipe getRecipe(long recipeRrn) {
            return recipeManager.getRecipe(recipeRrn);
        }

        @Override
        public Recipe getRecipe(String recipeId, long facilityRrn) {
            return recipeManager.getRecipe(recipeId, facilityRrn);
        }

        @Override
        public Recipe getRecipe(String recipeId, String namedSpace) {
            return recipeManager.getRecipe(recipeId, namedSpace);
        }

        @Override
        public List<RecipeVersion> getRecipeVersions(Long recipeRrn) {
            return recipeVersionManager.getRecipeVersions(recipeRrn);
        }

        @Override
        public List<Map> getChamberTypes(List<String> chamberTypeKeys) {
            return recipeManager.getChamberTypes(chamberTypeKeys);
        }

        @Override
        public String getRecipeTime(long instanceRrn) {
            return recipeManager.getRecipeTime(instanceRrn);
        }

        @Override
        public List<Relation> getEqptByRecipe(long instanceRrn) {
            return recipeManager.getEqptByRecipe(instanceRrn);
        }

        @Override
        public boolean isSetForTemp(long instanceRrn) {
            return recipeManager.isSetForTemp(instanceRrn);
        }

        @Override
        public long insertRecipe(Recipe recipe) {
            return recipeManager.insertRecipe(recipe);
        }

        @Override
        public void updateRecipesStatus(String nowStatus, String targetStatus, String recipeRrns, Relation relation) {
            recipeManager.updateRecipesStatus(nowStatus, targetStatus, recipeRrns, relation);
        }

        @Override
        public List<Map> validateRecipeExipreTime(List<Map> list) {
            return recipeManager.validateRecipeExipreTime(list);
        }

        @Override
        public void saveRecipeTime(long recipeRrn, String recipeTime, String idleTime) {
            recipeManager.saveRecipeTime(recipeRrn, recipeTime, idleTime);
        }

        @Override
        public boolean isHasRunningLotForRecipe(long facilityRrn, String recipeId) {
            return recipeManager.isHasRunningLotForRecipe(facilityRrn, recipeId);
        }

        @Override
        public boolean isHasEqptForRecipe(long recipeRrn) {
            return recipeManager.isHasEqptForRecipe(recipeRrn);
        }

        @Override
        public boolean isHasRunEqptForRecipe(long recipeRrn) {
            return recipeManager.isHasRunEqptForRecipe(recipeRrn);
        }

        @Override
        public void updateRecipe(Recipe recipe) {
            recipeManager.updateRecipe(recipe);
        }

        @Override
        public void deleteRecipe(Recipe recipe) {
            recipeManager.deleteRecipe(recipe);
        }

        @Override
        public String queryRecipeVersionExt(long recipeRrn) {
            return recipeVersionManager.queryRecipeVersionExt(recipeRrn);
        }

        @Override
        public boolean isSubRecipeLegal(Relation relation) {
            return recipeManager.isSubRecipeLegal(relation);
        }

        @Override
        public List<Map> getParaValLimits(long recipeRrn, String orderByFlag) {
            return recipeManager.getParaValLimits(recipeRrn, orderByFlag);
        }

        @Override
        public void saveRecipeCondition(RecipeCondition recipeCondition) {
            recipeManager.saveRecipeCondition(recipeCondition);
        }

        @Override
        public List<RecipeCondition> getRecipeCondition(long recipeRrn) {
            return recipeManager.getRecipeCondition(recipeRrn);
        }

        @Override
        public List<Map<String, Object>> getEquipmentUsesRecipeList(long recipeRrn, Map<String, Object> conditions) {
            return recipeManager.getEquipmentUsesRecipeList(recipeRrn, conditions);
        }

        @Override
        public boolean isHasProdProcContextSetup(long facilityRrn, long recipeRrn) {
            return recipeManager.isHasProdProcContextSetup(facilityRrn, recipeRrn);
        }

        @Override
        public void createRecipeVersion(RecipeVersion recipeVersion) {
            recipeVersionManager.createRecipeVersion(recipeVersion);
        }

        @Override
        public RecipeVersion getRecipeVersion(RecipeVersion recipeVersion) {
            return recipeVersionManager
                    .getRecipeVersion(recipeVersion.getInstanceRrn(), recipeVersion.getInstanceVersion());
        }

        @Override
        public List<ContextValue> getContextValues(String recipeId) {
            return recipeContextManager.getContextValues(recipeId);
        }

        @Override
        public List<RecipeVersion> getRecipeVersionListWithPpid(String ppid, String namedSpace) {
            return recipeVersionManager.getRecipeVersionListWithPpid(ppid, namedSpace);
        }

        @Override
        public void updateRecipeVersion(RecipeVersion instance, String eqptCapability) {
            recipeVersionManager.updateRecipeVersion(instance, eqptCapability);
        }

        @Override
        public void deleteRecipeVersion(RecipeVersion recipeVersion) {
            recipeVersionManager.deleteRecipeVersion(recipeVersion);
        }

        @Override
        public RecipeVersion getNowActiveRecipeVersion(long subRecipeRrn) {
            return recipeVersionManager.getNowActiveRecipeVersion(subRecipeRrn);
        }

        @Override
        public List<Map<String, Object>> getRecipesByEquipmentForParallelMode(Long equipmentRrn, String recipeId) {
            return recipeManager.getRecipesByEquipmentForParallelMode(equipmentRrn, recipeId);
        }

        @Override
        public List<Map<String, Object>> getRecipesByEquipmentForShow(Long equipmentRrn, String recipeId,
                                                                      String chamberMode) {
            return recipeManager.getRecipesByEquipmentForShow(equipmentRrn, recipeId, chamberMode);
        }

        @Override
        public List<Map<String, Object>> getDeleteRelationHistoryByEntityForRecipe(Long entityRrn) {
            return recipeManager.getDeleteRelationHistoryByEntityForRecipe(entityRrn);
        }

        @Override
        public List<Map<String, Object>> getOpenRecipesByEquipmentForSerialMode(long equipmentRrn, Long parentRecipeRrn) {
            return recipeManager.getOpenRecipesByEquipmentForSerialMode(equipmentRrn, parentRecipeRrn);
        }

        @Override
        public void updateRecipesStatus(String userId, String nowStatus, String targetStatus, Relation relation,
                                        List<Long> toRrns, String comments) {
            recipeManager.updateRecipesStatus(userId, nowStatus, targetStatus, relation, toRrns, comments);
        }

        @Override
        public List<Map> getFullRelationForRecipe(long instanceRrn, String entityToRecipe, String searchId) {
            return recipeManager.getFullRelationForRecipe(instanceRrn, entityToRecipe, searchId);
        }

        @Override
        public List<Map> getFullRelationForRecipe(long instanceRrn, String entityToRecipe) {
            return recipeManager.getFullRelationForRecipe(instanceRrn, entityToRecipe, null);
        }

        @Override
        public RecipeVersion getLotRecipe(Map<String, Object> lotInfo) {
            return recipeManager.getLotRecipe(lotInfo);
        }

        @Override
        public RecipeVersion getLotRecipe(Lot lot) {
            return recipeManager.getLotRecipe(lot);
        }

        @Override
        public RecipeVersion getLotRecipe(Lot lot, Long eqptRrn) {
            RecipeVersion recipeVersion = recipeManager.getLotRecipe(lot);
            if (recipeVersion != null) {
                return getAvailableRecipeVersion(lot, eqptRrn, recipeVersion.getInstanceRrn());
            }
            return null;
        }

        @Override
        public RecipeVersion getAvailableRecipeVersion(Lot lot, Long eqptRrn, Long logicRecipeRrn) {
            Map<String, Object> map = jobManager.getAvailableSubRecipes(lot, eqptRrn, EventName.IDLE_TO_RUN, logicRecipeRrn,
                                                                        lot.getInt_qty1().longValue());

            List<Map<String, Object>> childRecipes = (List<Map<String, Object>>) MapUtils
                    .getObject(map, "childRecipes");
            if (CollectionUtils.isNotEmpty(childRecipes)) {
                Map<String, Object> subRecipeMap = childRecipes.get(0);
                String subRecipeId = MapUtils.getString(subRecipeMap, "recipeId");
                Long subRecipeRrn = MapUtils.getLong(subRecipeMap, "recipeRrn");
                if (StringUtils.isNotBlank(subRecipeId)) {
                    RecipeVersion subRecipeVersion = recipeVersionManager.getNowActiveRecipeVersion(subRecipeRrn);
                    Recipe subRecipe = recipeManager.getRecipe(subRecipeId, LocalContext.getFacilityRrn());
                    if (subRecipeVersion == null) {
                        subRecipeVersion = new RecipeVersion();
                    }
                    if (subRecipe != null && StringUtils.isNotBlank(subRecipe.getInstanceId())) {
                        subRecipeVersion.copyNamedObject(subRecipe);
                    }

                    if (StringUtils.isBlank(subRecipeVersion.getPpid())) {
                        subRecipeVersion.setPpid(recipeManager.buildRecipePhysicalId(subRecipeId, BeanUtils.copyBeanToMap(lot)));
                    }
                    return subRecipeVersion;
                }
            }
            return null;
        }

        @Override
        public List<Map<String, Object>> getRecipeList4Equipment(long equipmentRrn) {
            return recipeManager.getRecipeList4Equipment(equipmentRrn);
        }

        @Override
        public String buildRecipePhysicalId(String logicalId, Map replaceInfo) {
            return recipeManager.buildRecipePhysicalId(logicalId, replaceInfo);
        }

        @Override
        public List<Map> getRelation4Recipe(long fromRrn, String linkType) {
            return recipeManager.getRelation4Recipe(fromRrn, linkType);
        }

        @Override
        public void updateRecipeVersionExt(Long recipeRrn, Long recipeVersionValue, String equipmentCapability) {
            recipeVersionManager.updateRecipeVersionExt(recipeRrn, recipeVersionValue, equipmentCapability);
        }

        @Override
        public RecipeVersion createRecipeVersionWithEcn(RecipeVersion recipeVersion,Ecn ecn) {
            return recipeVersionManager.createRecipeVersionWithEcn(recipeVersion,ecn);
        }

        @Override
        public List<Map<String, Object>> getRelation4RecipeByWaferQty(long fromRrn, String linkType, Long totalWaferQty) {
            return recipeManager.getRelation4RecipeByWaferQty(fromRrn, linkType, totalWaferQty);
        }

        @Override
        public List<Map> getRelation4AllExperimentRecipe() {
            return recipeManager.getRelation4AllExperimentRecipe();

        }

        @Override
        public void updateStatus4AllExperimentRecipe(List<Map> recipeList) {
            recipeManager.updateStatus4AllExperimentRecipe(recipeList);
        }

        @Override
        public void updateExpireTime4ExperimentRecipe(Map recipe) {
            recipeManager.updateExpireTime4ExperimentRecipe(recipe);
        }

        @Override
        public RecipeVersion getActiveRecipeVersion(Recipe recipe) {
            return recipeVersionManager.getActiveRecipeVersion(recipe);
        }

        @Override
        public long isMainRecipe(Long recipeRrn) {
            return recipeManager.isMainRecipe(recipeRrn);
        }

        @Override
        public long insertRecipeGroup(RecipeGroup recipeGroup) {
            return recipeManager.insertRecipeGroup(recipeGroup);
        }

        @Override
        public void deleteRecipeGroup(RecipeGroup recipeGroup) {
            recipeManager.deleteRecipeGroup(recipeGroup);
        }

        @Override
        public List<RecipeGroup> getRecipeGroupList(RecipeGroup condition) {
            return recipeManager.getRecipeGroupList(condition);
        }

        @Override
        public List<NamedObject> getRecipeFamilyByGroup(long recipeGroupRrn) {
            return recipeManager.getRecipeFamilyByGroup(recipeGroupRrn);
        }

        @Override
        public Page getRecipeFamilyByGroupByPage(Page page, long recipeGroupRrn) {
            return recipeManager.getRecipeFamilyByGroupByPage(page, recipeGroupRrn);
        }

        @Override
        public Page getAllRecipeFamily(Page page) {
            return recipeManager.getAllRecipeFamily(page);
        }

        @Override
        public List<NamedObject> getrecipeAreadyInGroup(long recipeGroupRrn, List<Long> collect) {
            return recipeManager.getrecipeAreadyInGroup(recipeGroupRrn, collect);
        }

        @Override
        public Page getAllRecipeFamilyNotInGroup(Page page, long recipeGroupRrn, String recipeIdLike) {
            return recipeManager.getAllRecipeFamilyNotInGroup(page, recipeGroupRrn, recipeIdLike);
        }

        @Override
        public Page getParalleProtallList(Page page, Map<String, Object> reqMap) {
            return recipeManager.getParalleProtallList(page, reqMap);
        }

        @Override
        public Page getUnParalleProtallList(Page page, Map<String, Object> reqMap) {
            return recipeManager.getUnParalleProtallList(page, reqMap);
        }

        @Override
        public void insertRelation4RecipeFamilyToRecipe(Relation relation) {
            recipeManager.insertRelation4RecipeFamilyToRecipe(relation);
        }

        @Override
        public void deleteRelation4RecipeFamilyToRecipe(Relation relation) {
            recipeManager.deleteRelation4RecipeFamilyToRecipe(relation);
        }

        /**
         * 通过主recipe和chamberType获取子recipe
         *
         * @param mainRecipeRrn 主recipeRrn
         * @param chamberType
         */
        @Override
        public Long getChildRecipeByChamberType(Long mainRecipeRrn, Long totalWaferQty, String chamberType) {
            List<Map<String, Object>> childRecipes = recipeManager.getChildRecipesByParent(mainRecipeRrn, totalWaferQty);
            for (Map<String, Object> childRecipeMap : childRecipes) {
                Long chamberRecipeRrn = MapUtils.getLong(childRecipeMap, "recipeRrn");
                RecipeVersion chamberRecipeVersion = recipeVersionManager.getNowActiveRecipeVersion(chamberRecipeRrn);

                if (StringUtils.equals(chamberRecipeVersion.getChamberTypes(), chamberType)) {
                    return chamberRecipeRrn;
                }
            }

            return null;
        }

        @Override
        public void updateRecipeVersionStatus(RecipeVersion recipeVersion) {
            if (StringUtils.equals(VersionStatus.ACTIVE_KEY, recipeVersion.getVersionStatus())) {
                recipeVersionManager.activeRecipeVersion(recipeVersion);
            }
        }

        @Override
        public String getMainRecipeId(String recipeId) {
            return recipeManager.getMainRecipeId(recipeId);
        }

    }