# ErpServiceImpl.java

    package com.mycim.server.erp.service;

    import com.alipay.sofa.runtime.api.annotation.SofaService;
    import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
    import com.fa.sesa.exception.Assert;
    import com.fa.sesa.exception.Errors;
    import com.mycim.framework.utils.exception.ExceptionUtils;
    import com.fa.sesa.exception.SystemIllegalException;
    import com.mycim.framework.jdbc.Page;
    import com.mycim.framework.logging.Logger;
    import com.mycim.framework.logging.LoggerFactory;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.framework.utils.lang.collections.MapUtils;
    import com.mycim.framework.utils.lang.time.DateUtils;
    import com.mycim.framework.utils.msg.JsonUtils;
    import com.mycim.server.erp.dto.*;
    import com.mycim.server.erp.manager.ErpManager;
    import com.mycim.valueobject.erp.ErpMesProductBindBean;
    import com.mycim.valueobject.prp.Operation;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;

    /**
     * @author Luopeng.Wang
     * @version 6.0.0
     * @date 2021/5/24
     **/
    @Service
    @SofaService(interfaceType = ErpService.class, bindings = {@SofaServiceBinding(bindingType = "bolt")})
    public class ErpServiceImpl implements ErpService {
        private final static Logger logger = LoggerFactory.getLogger(ErpConstants.ERP_LOGGER_NAME);

        @Autowired
        private ErpManager erpManager;

        /**
         * 接收物料主数据的接口
         */
        @Override
        public void receiveErpMaterial() {
            Date startDate = new Date();

            List<ErpMaterialDto> erpMaterialDtos = erpManager.getAvaliableErpMaterial();

            logger.info("ReceiveMaterial --> Data size : " + erpMaterialDtos.size() + " , Receive time : " +
                                DateUtils.formatDate(startDate, DateUtils.DATE_FORMAT4DATE));

            for (ErpMaterialDto erpMaterialDto : erpMaterialDtos) {
                try {
                    erpManager.receiveErpMaterial(erpMaterialDto);

                    logger.info("ReceiveMaterial -->  Excute success , Data: " + JsonUtils.toString(erpMaterialDto));

                } catch (Exception e) {
                    erpManager.changeErpMaterialStatus(erpMaterialDto, ErpConstants.STATUS_FAIL);

                    SystemIllegalException systemIllegalException = ExceptionUtils.findSystemException(e);
                    logger.error("ReceiveMaterial -->  Excute failed , Data: " + JsonUtils.toString(erpMaterialDto) +
                                         " , Error msg:" + systemIllegalException.getMessage());
                }

            }

        }

        @Override
        public List<Map<String, String>> getCanReceiveMaterialList() {
            return erpManager.getCanReceiveMaterialList();
        }

        /**
         * 接收工单以及工单明细的接口
         */
        @Override
        public void receiveErpWorkOrder() {
            Date startDate = new Date();

            List<ErpWorkOrderDto> erpWorkOrderDtos = erpManager.getAvaliableErpWorkOrder();

            logger.info("ReceiveWorkOrder -->Head Data size : " + erpWorkOrderDtos.size() + " , Receive time : " +
                                DateUtils.formatDate(startDate, DateUtils.DATE_FORMAT4DATE));

            List<ErpWorkOrderLineDto> erpWorkOrderLineDtos = erpManager.getAvaliableErpWorkOrderLine();

            logger.info("ReceiveWorkOrder -->Line Data size : " + erpWorkOrderLineDtos.size() + " , Receive time : " +
                                DateUtils.formatDate(startDate, DateUtils.DATE_FORMAT4DATE));

            Map<String, List<ErpWorkOrderLineDto>> lineMap = erpWorkOrderLineDtos.stream().collect(
                    Collectors.groupingBy(ErpWorkOrderLineDto::getAufnr));

            for (ErpWorkOrderDto workOrderDto : erpWorkOrderDtos) {

                List<ErpWorkOrderLineDto> workOrderLineDtos = new ArrayList<>();
                try {
                    Object object = MapUtils.getObject(lineMap, workOrderDto.getAufnr());

                    Assert.isFalse(object == null,
                                   Errors.create().content("ZMES_POREL_H WorkOrder {} not match ZMES_POREL_I Data")
                                         .args(workOrderDto.getAufnr()).build());

                    workOrderLineDtos = (List<ErpWorkOrderLineDto>) object;

                    workOrderDto.setLines(workOrderLineDtos);

                    erpManager.receiveErpWorkOrder(workOrderDto);
                    logger.info("ReceiveWorkOrder -->  Excute success , Data: " + JsonUtils.toString(workOrderDto));
                } catch (Exception e) {
                    erpManager.changeErpWorkOrderStatus(workOrderDto, ErpConstants.STATUS_FAIL);
                    erpManager.changeErpWorkOrderLineStatus(workOrderLineDtos, ErpConstants.STATUS_FAIL);

                    SystemIllegalException systemIllegalException = ExceptionUtils.findSystemException(e);
                    logger.error("ReceiveMaterial -->  Excute failed , Data: " + JsonUtils.toString(workOrderDto) +
                                         " , Error msg:" + systemIllegalException.getMessage());
                }

            }
        }

        /**
         * Wafer Start 接口
         * @param transInfo
         * @return
         */
        @Override
        public Map createLot4Erp(Map transInfo) {
            return erpManager.createLot(transInfo);
        }

        /**
         * MES界面手动接收 物料批
         * @param receiveList
         * @param warehouse
         */
        @Override
        public void batchReceiveMaterial(List<String> receiveList, Operation warehouse) {
            erpManager.batchReceiveMaterial(receiveList, warehouse);
        }

        @Override
        public void recordMaterialReturnLog(String warehouseId, String materialId, String sourceType,
                                            List<Map<String, Object>> conversionList) {
            erpManager.recordMaterialReturnLog(warehouseId, materialId, sourceType, conversionList);
        }

        @Override
        public void returnErpMaterial() {
            List<Map<String,String>> confirmedReturnRequest = erpManager.getConfirmedReturnRequest();
            if (CollectionUtils.isNotEmpty(confirmedReturnRequest)){
                erpManager.actuallyReturnMaterial("WAFER_BANK", confirmedReturnRequest);
            }

        }

        @Override
        public List<Map<String, String>> getReceiveOrReturnBomList(String moveType) {
            return erpManager.getReceiveOrReturnBomList(moveType);
        }

        @Override
        public void batchReturnBom(List<String> returnList, Operation warehouse, String conversionType) {
            erpManager.batchReturnMaterialBom(returnList, warehouse, conversionType);
        }

        @Override
        public List<String> queryMaterialIdByWorkOrderId(String workOrderId) {
            return erpManager.queryMaterialIdByWorkOrderId(workOrderId);
        }

        @Override
        public void shipLot4Erp() {
            Date startDate = new Date();

            List<ErpShipLotDto> erpShipLotDtos = erpManager.getAvaliableErpShipLot();

            logger.info("ShipLot4Erp --> Data size : " + erpShipLotDtos.size() + " , Receive time : " +
                                DateUtils.formatDate(startDate, DateUtils.DATE_FORMAT4DATE));

            for (ErpShipLotDto erpShipLotDto : erpShipLotDtos) {
                try {
                    erpManager.shipLot4Erp(erpShipLotDto);

                    logger.info("ShipLot4Erp -->  Excute success , Data: " + JsonUtils.toString(erpShipLotDto));

                } catch (Exception e) {
                    erpManager.changeErpShipLotStatus(erpShipLotDto, ErpConstants.STATUS_FAIL);

                    SystemIllegalException systemIllegalException = ExceptionUtils.findSystemException(e);
                    logger.error("ShipLot4Erp -->  Excute failed , Data: " + JsonUtils.toString(erpShipLotDto) +
                                         " , Error msg:" + systemIllegalException.getMessage());
                }

            }
        }

        @Override
        public void shipLotRequest(List<Map<String, String>> paramMap) {
            erpManager.shipLotRequest(paramMap);
        }

        @Override
        public void insertBindMesProduct(ErpMesProductBindBean erpMesProductBindBean) {
            erpManager.insertBindMesProduct(erpMesProductBindBean);
        }

        @Override
        public void updateBindMesProduct(ErpMesProductBindBean erpMesProductBindBean) {
            erpManager.updateBindMesProduct(erpMesProductBindBean);
        }

        @Override
        public void deleteBindMesProduct(ErpMesProductBindBean erpMesProductBindBean) {
            erpManager.deleteBindMesProduct(erpMesProductBindBean);
        }

        @Override
        public Page queryBindMesProduct(Page page, ErpMesProductBindBean erpMesProductBindBean) {
            return erpManager.queryBindMesProduct(page, erpMesProductBindBean);
        }

        @Override
        public List<ErpMesProductBindBean> queryNotBindMesProduct() {
            return erpManager.queryNotBindMesProduct();
        }

        @Override
        public void batchReceiveBom(List<String> receiveList, Operation warehouse) {
            erpManager.batchReceiveMaterialBom(receiveList, warehouse);
        }

    }