# MaterialManagerImpl.java

    package com.mycim.server.asm.manager.impl;

    import com.fa.sesa.threadlocal.LocalContext;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.MapUtils;
    import com.mycim.framework.utils.lang.math.NumberUtils;
    import com.mycim.server.asm.dao.MaterialDAO;
    import com.mycim.server.asm.manager.MaterialManager;
    import com.mycim.server.base.manager.NamedObjectExtManager;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.base.manager.TransactionLogManager;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.bas.NamedObject;
    import com.mycim.valueobject.bas.TransactionLog;
    import com.mycim.valueobject.consts.TransactionNames;
    import com.mycim.valueobject.inv.InventoryTransHistoryDO;
    import com.mycim.valueobject.inv.LotStepBomHistoryDO;
    import com.mycim.valueobject.inv.MaterialDO;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.HashMap;
    import java.util.List;

    @Service
    @Transactional
    public class MaterialManagerImpl implements MaterialManager {

        @Autowired
        MaterialDAO materialDAO;

        @Autowired
        TransactionLogManager transactionLogManager;

        @Autowired
        NamedObjectManager namedObjectManager;

        @Autowired
        NamedObjectExtManager namedObjectExtManager;

        @Override
        public long insertMaterial(MaterialDO material) {
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(material.getTransPerformedby(), TransactionNames.CREATE_KEY);
            transactionLog.setTransSequence(1L);
            material.setInstanceRrn(namedObjectManager.insertNamedObject(material, transactionLog.getTransRrn()));
            if (ObjectList.BOM_KEY.equals(material.getObjectType()) ) {
                HashMap<String, Object> map = MapUtils.newHashMap();
                if (StringUtils.isNotEmptyTrim(material.getAttribute3())) {
                    map.put("ATTRIBUTE_DATA3", material.getAttribute3());
                }
                namedObjectExtManager
                        .insertNamedObjectExt(material.getInstanceRrn(), StringUtils.toString(NumberUtils.INTEGER_ZERO), map);
            }
            materialDAO.insertMaterial(material);
            materialDAO.insertMaterialHistory(material.getInstanceRrn(), transactionLog.getTransRrn(), 1L, 0D);
            //数据同步至新表
            materialDAO.insertMaterial(material, LocalContext.getUserRrn());
            materialDAO.insertMaterialHistory(material, transactionLog);

            transactionLogManager.markTransactionLog(transactionLog);

            return material.getInstanceRrn();
        }

        @Override
        public void deleteMaterial(MaterialDO material) {
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(material.getTransPerformedby(), TransactionNames.DELETE_KEY);
            transactionLog.setTransSequence(1L);
            long transRrn = transactionLog.getTransRrn();
            namedObjectManager.deleteNamedObject(material, transRrn);
            materialDAO.insertMaterialHistory(material.getInstanceRrn(), transRrn, 1L, 0D);
            materialDAO.deleteMaterial(material.getInstanceRrn());
            //数据同步至新表
            materialDAO.insertMaterialHistory(material, transactionLog);
            materialDAO.deleteMaterial(material);

            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public void updateMaterial(MaterialDO material) {
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(material.getTransPerformedby(), TransactionNames.MODIFY_KEY);
            transactionLog.setTransSequence(1L);
            long transRrn = transactionLog.getTransRrn();

            namedObjectManager.updateNamedObject(material, transRrn);
            if (ObjectList.BOM_KEY.equals(material.getObjectType()) ) {
                namedObjectExtManager.updateNamedObjectExt(material);
            }
            materialDAO.updateMaterial(material);
            materialDAO.insertMaterialHistory(material.getInstanceRrn(), transRrn, 1L, 0D);

            //数据同步至新表
            materialDAO.updateMaterial(material, LocalContext.getUserRrn());
            materialDAO.insertMaterialHistory(material, transactionLog);

            transactionLogManager.markTransactionLog(transactionLog);

        }

        @Override
        public MaterialDO getMaterial(MaterialDO material) {
            NamedObject namedObject = namedObjectManager.getNamedObject(material);

            if (namedObject == null) {
                return null;
            }

            MaterialDO materialDO = materialDAO.getMaterial(namedObject.getInstanceRrn());

            materialDO.copyNamedObject(namedObject);

            return materialDO;
        }

        @Override
        public boolean isMaterialInUsed(Long materialRrn) {
            List<Long> materialInUsed = materialDAO.getMaterialInWarehouse(materialRrn);
            if (materialInUsed == null || materialInUsed.isEmpty()) {
                materialInUsed = materialDAO.getMaterialInBor(materialRrn);
            }

            return materialInUsed == null || materialInUsed.isEmpty() ? false : true;
        }

        @Override
        public boolean hasMaterialExtNumber(Long materialRrn, String itemExtendNumber) {
            return materialDAO.hasMaterialExtNumber(materialRrn, itemExtendNumber);
        }

        @Override
        public void saveMaterialForReceived(InventoryTransHistoryDO ithd) {

            materialDAO.updateMaterialForReceiptQty(ithd.getItemRrn(), ithd.getTransQty());
            materialDAO.insertMaterialHistory(ithd.getItemRrn(), ithd.getTransRrn(), ithd.getTransSequence(),
                                              ithd.getTransQty());
            //同步数据至新历史表
            MaterialDO md = new MaterialDO();
            md.setInstanceRrn(ithd.getItemRrn());
            materialDAO.insertMaterialHistory(md, new TransactionLog(ithd.getTransRrn(), ithd.getTransId(),
                                                                     ithd.getTransSequence(), ithd.getTransDate()));
        }

        @Override
        public void saveMaterialForConsumed(InventoryTransHistoryDO ithd) {
            materialDAO.updateMaterialForIssueQty(ithd.getItemRrn(), ithd.getTransQty());
            materialDAO.insertMaterialHistory(ithd.getItemRrn(), ithd.getTransRrn(), ithd.getTransSequence(),
                                              ithd.getTransQty());
            //同步数据至新历史表
            MaterialDO md = new MaterialDO();
            md.setInstanceRrn(ithd.getItemRrn());
            materialDAO.insertMaterialHistory(md, new TransactionLog(ithd.getTransRrn(), ithd.getTransId(),
                                                                     ithd.getTransSequence(), ithd.getTransDate()));
        }

        @Override
        public void saveMaterialForStocktake(InventoryTransHistoryDO ithd) {
            materialDAO.updateMaterialForAdjustQty(ithd.getItemRrn(), ithd.getTransQty());
            materialDAO.insertMaterialHistory(ithd.getItemRrn(), ithd.getTransRrn(), ithd.getTransSequence(),
                                              ithd.getTransQty());
            //同步数据至新历史表
            MaterialDO md = new MaterialDO();
            md.setInstanceRrn(ithd.getItemRrn());
            materialDAO.insertMaterialHistory(md, new TransactionLog(ithd.getTransRrn(), ithd.getTransId(),
                                                                     ithd.getTransSequence(), ithd.getTransDate()));
        }

        @Override
        public String getMaterials4lot(Long warehouseRrn, Long productRrn) {
            return materialDAO.getMaterials4lot(warehouseRrn, productRrn);
        }

        @Override
        public int getMaterialTotalQuantity(long materialRrn) {
            return materialRrn > 0 ? materialDAO.getMaterialTotalQuantity(materialRrn) : 0;
        }

        @Override
        public void insertLotStepBomDao(LotStepBomHistoryDO lotStepBomHistoryDO) {
            materialDAO.insertLotStepBomDao(lotStepBomHistoryDO);
        }

    }