# PackageManagerImpl.java

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

    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.time.DateUtils;
    import com.mycim.server.workorder.dao.PackageDao;
    import com.mycim.server.workorder.manager.PackageManager;
    import com.mycim.server.workorder.manager.WorkOrderManager;
    import com.mycim.valueobject.erp.lotScrapShip.ErpPackage;
    import com.mycim.valueobject.erp.lotScrapShip.ErpPackage.UnitInfo;
    import com.mycim.valueobject.erp.lotScrapShip.ErpPackageStatus;
    import com.mycim.valueobject.prp.WorkOrder;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.Date;
    import java.util.List;

    @Service
    @Transactional
    public class PackageManagerImpl implements PackageManager {
        @Autowired
        private PackageDao packageDao;

        @Autowired
        private WorkOrderManager workOrderManager;

        @Override
        public String savePackageData(ErpPackage packageData) {

            Long transRrn = Long
                    .valueOf(IDGenerators.get(IDNames.SEQUENCE).generateId(SequenceNames.MODULE_SEQ_TRANSACTION_RRN));
            packageData.setTransRrn(transRrn);

            String lpnPrex = DateUtils.formatDate(new Date(), "yyyyMMdd");
            String maxLpn = packageDao.getMaxLpnNum(lpnPrex + "%");
            if (maxLpn != null) {
                String lpnPost = String.format("%03d", Integer.valueOf(maxLpn.substring(lpnPrex.length())) + 1);
                packageData.setLpn(lpnPrex + lpnPost);
            } else {
                packageData.setLpn(lpnPrex + String.format("%03d", 1));
            }

            packageData.setPrintCount(1);
            packageData.setSendFlag("N");
            packageData.setQuantity(packageData.getUnitInfo().stream().mapToInt(UnitInfo::getPackageQuantity).sum());
            packageDao.insertPackage(packageData);
            packageDao.insertPackageUnitList(packageData.getUnitInfo(), packageData.getTransRrn());

            //完成后关闭那个workorder
            WorkOrder workOrderById = workOrderManager.getWorkOrderById(packageData.getWorkOrderId());
            int compleWf = workOrderManager.getCompleteDieQtyByWorkOrderRrn(workOrderById.getWorkorderRrn());
            if (compleWf >= workOrderById.getOrderDieQty()) {
                workOrderManager.completeWorkOrder(workOrderById.getWorkorderRrn());
            }

            return packageData.getLpn();

        }

        @Override
        public Page getPackage(Page page, String packageType, String lotId, String productId) {
            return packageDao.getPackage(page, packageType, lotId, productId);
        }

        @Override
        public List<ErpPackage.UnitInfo> getPackageDetails(Long transRrn) {
            return packageDao.getPackageDetails(transRrn);
        }

        @Override
        public int getPackageDieCount(long workorderRrn, String packageType) {
            return packageDao.getPackageDieCount(workorderRrn, packageType);
        }

        @Override
        public int getErpPackageCount(long lotRrn) {
            return packageDao.getErpPackageCount(lotRrn);
        }

        @Override
        public List<ErpPackage> getPackage(long transRrn, String type) {
            return packageDao.getPackage(transRrn, type);
        }

        @Override
        public void setPackageSendFlagY(List<ErpPackage> list) {
            packageDao.setPackageSendFlagY(list);
        }

        @Override
        public void updatePackageFlag(List<ErpPackageStatus> list) {
            packageDao.updatePackageFlag(list);
        }

    }