tasly.greathealth.erp.order.services.impl.DefaultOrderUpdateService.java Source code

Java tutorial

Introduction

Here is the source code for tasly.greathealth.erp.order.services.impl.DefaultOrderUpdateService.java

Source

/*
 * [y] hybris Platform
 *
 * Copyright (c) 2000-2014 hybris AG
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of hybris
 * ("Confidential Information"). You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the
 * license agreement you entered into with hybris.
 */
package tasly.greathealth.erp.order.services.impl;

import com.hybris.oms.service.service.AbstractHybrisService;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.transaction.annotation.Transactional;

import tasly.greathealth.erp.api.order.updateorder.dto.EccBillto;
import tasly.greathealth.erp.api.order.updateorder.dto.EccDelivery;
import tasly.greathealth.erp.api.order.updateorder.dto.Item;
import tasly.greathealth.erp.api.order.updateorder.dto.Message;
import tasly.greathealth.erp.api.order.updateorder.dto.OmsOrder;
import tasly.greathealth.erp.api.order.updateorder.dto.OmsOrders;
import tasly.greathealth.erp.api.order.updateorder.dto.Refund;
import tasly.greathealth.erp.api.order.updateorder.dto.Shipto;
import tasly.greathealth.erp.order.services.OrderUpdateService;
import tasly.greathealth.erp.util.TaslyERPConstants;
import tasly.greathealth.erp.util.TaslyUtils;
import tasly.greathealth.oms.domain.order.TaslyOrderData;
import tasly.greathealth.oms.log.OmsLoggerFactory;
import tasly.greathealth.oms.soapclient.updateorder.ZSTRSDOMSBILLTO;
import tasly.greathealth.oms.soapclient.updateorder.ZSTRSDOMSDELIVERY;
import tasly.greathealth.oms.soapclient.updateorder.ZSTRSDOMSORDERITEMREFUND;
import tasly.greathealth.oms.soapclient.updateorder.ZSTRSDOMSSALEORDERCOMMIN;
import tasly.greathealth.oms.soapclient.updateorder.ZSTRSDOMSSALEORDERCOMMTAB;
import tasly.greathealth.oms.soapclient.updateorder.ZSTRSDOMSSALEORDERSCOMMIN;
import tasly.greathealth.oms.soapclient.updateorder.ZSTRSDOMSSHIPTO;
import tasly.greathealth.oms.soapclient.updateorder.ZSTRSDOMSSOITEMREFUND;
import tasly.greathealth.oms.soapclient.updateorder.ZSTRUPIBASEINFO2;

/**
 * created by libin539 for TS-689
 * OMS???SAP ERP
 */
public class DefaultOrderUpdateService extends AbstractHybrisService
        implements OrderUpdateService, TaslyERPConstants {
    private static final Logger erpOrderLog = OmsLoggerFactory.getErporderlog();
    private TaslyOrderQueryFactory orderQueries;

    @Required
    public void setOrderQueries(final TaslyOrderQueryFactory orderQueries) {
        this.orderQueries = orderQueries;
    }

    /**
     * TS-689:Hybris OMS???SAP ERP
     */
    @Transactional
    @Override
    public TaslyOrderData getTaslyOrderDataByOrderID(final String orderId) throws Exception {
        TaslyOrderData taslyOrderData = null;
        taslyOrderData = (TaslyOrderData) this.findOneSingle(this.orderQueries.getOrderByID(orderId));
        return taslyOrderData;
    }

    @Override
    public boolean checkOrderStatus(final String orderId) throws Exception {

        String orderStatus = REPLICATIONSTATUS_N;
        boolean flag = false;
        if (orderId != null && orderId.length() > 0) {
            final TaslyOrderData taslyOrderData = getTaslyOrderDataByOrderID(orderId);
            if (taslyOrderData != null) {
                orderStatus = taslyOrderData.getReplication_status();
            }
            if (orderStatus.equalsIgnoreCase(REPLICATIONSTATUS_S)
                    || orderStatus.equalsIgnoreCase(REPLICATIONSTATUS_W)) {
                flag = true;
            }
        } else {
            erpOrderLog.error(UO_LogHead + "OMS?ID?");
        }
        return flag;
    }

    @Override
    public ZSTRUPIBASEINFO2 createEccBaseInfo() throws Exception {
        final ZSTRUPIBASEINFO2 baseInfo = new ZSTRUPIBASEINFO2();
        final String sendTime = TaslyUtils.convertTimeToString();
        // generated ramdom code
        String msgID = "OMS_VENDOR_";
        msgID = msgID.concat(sendTime).concat("_").concat(TaslyUtils.generateRandomCode());
        baseInfo.setMSGID(msgID);
        // baseInfo.setPMSGID(value);
        baseInfo.setSENDTIME(sendTime);
        baseInfo.setSSYSTEM(UO_SSYSTEM);
        baseInfo.setSERVICENAME(UO_SERVICENAME);
        baseInfo.setTSYSTEM(UO_TSYSTEM);
        baseInfo.setRETRY(UO_RETRY);
        return baseInfo;
    }

    @Override
    public ZSTRSDOMSSALEORDERSCOMMIN omsMessage2Ecc(final Message ordersMessage) throws Exception {
        // ??
        if (ordersMessage == null) {
            // erpOrderLog.warn("UpdateOrder?:Message is null,please check!");
            return null;
        }
        /**
         * omsorders:?
         * omsorderList:?list
         */
        OmsOrders omsorders = null;
        List<OmsOrder> omsorderList = null;
        /**
         * eccOrderId:Ecc?ID
         * operation:02  ?|03   (???PI)|04  ?|05  |06  
         * omsOrderId:OMS ?ID
         * userNotes:
         * csNotes:?
         */
        String eccOrderId = null;
        String operation = null;
        String omsOrderId = null;
        String userNotes = null;
        String csNotes = null;
        /**
         * 
         *
         * ECC9
         * ??
         * ?
         * 
         * 
         * ?
         * 
         * ??
         * ???
         */
        Shipto shipto = null;
        String eccShiptoId = null;
        String eccShiptoName = null;
        String eccShiptoRegion = null;
        String eccShiptoCity = null;
        String eccShiptoDistrict = null;
        String eccShiptoAddress = null;
        String eccShiptoZipcode = null;
        String eccShiptoMob = null;
        String eccShiptoTel = null;
        /**
         * eccBillto:EccBillto
         *
         * eccInvoiceType:?
         * eccInvoiceTitle:?
         * eccInvoiceContent:?
         * eccTaxpayerNumber:?
         * eccBankName:??
         * eccBankNumber:?
         * eccCustomerAddress:?
         * eccCustomerPhone:?
         */
        EccBillto eccBillto = null;
        String eccInvoiceType = null;
        String eccInvoiceTitle = null;
        String eccInvoiceContent = null;
        String eccTaxpayerNumber = null;
        String eccBankName = null;
        String eccBankNumber = null;
        String eccCustomerAddress = null;
        String eccCustomerPhone = null;
        /**
         * eccDelivery:ECC_DELIVERY
         * eccExpressId:?code
         * eccExpressName:???
         */
        EccDelivery eccDelivery = null;
        String eccExpressId;
        String eccExpressName;
        /**
         * refund:
         * itemList:list
         * refundOmsItemId:OMSID
         * refundType:
         * refundQuantity:?
         * refundAmount:?
         */
        Refund refund = null;
        List<Item> itemList = null;
        String refundOmsItemId = null;
        String refundType = null;
        String refundQuantity = null;
        double refundAmount;

        /**
         * WSDL BEAN
         *
         * zstrsdomssaleorderscommin:ECC,name = "ORDERS"
         * saleordercommtab/zssaleordercommin:OMS?ORDER
         * zstrsdomsshipto:SHIPTO
         */
        ZSTRSDOMSSALEORDERSCOMMIN zstrsdomssaleorderscommin = null;
        ZSTRSDOMSSALEORDERCOMMTAB saleordercommtab = null;
        ZSTRSDOMSSALEORDERCOMMIN zssaleordercommin = null;
        ZSTRSDOMSSHIPTO zstrsdomsshipto = null;

        // ??
        omsorders = ordersMessage.getOmsOrders();
        omsorderList = omsorders.getOmsOrder();
        for (final OmsOrder omsorder : omsorderList) {
            operation = omsorder.getOperation();
            eccOrderId = omsorder.getEccOrderId();
            omsOrderId = omsorder.getOmsOrderId();
            erpOrderLog.info(UO_LogHead + "Messageoperation,eccOrderId,omsOrderId"
                    + operation + "," + eccOrderId + "," + omsOrderId + ".");
            // ?02?03?04
            if (operation.equals(UO_LOCKSTATUS) || operation.equals(UO_ALTERTATUS)
                    || operation.equals(UO_DEBLOCKSTATUS)) {
                if (checkOrderId(eccOrderId, 35, CHECKLENGTH) && checkOrderId(omsOrderId, 64, CHECKLENGTH)) {
                    zstrsdomssaleorderscommin = new ZSTRSDOMSSALEORDERSCOMMIN();
                    saleordercommtab = new ZSTRSDOMSSALEORDERCOMMTAB();
                    zssaleordercommin = new ZSTRSDOMSSALEORDERCOMMIN();
                    // ??
                    userNotes = omsorder.getUserNotes();
                    csNotes = omsorder.getCsNotes();
                    // ???
                    if (CHECKLENGTH) {
                        userNotes = controlFileLength("userNotes", userNotes, 255);
                        csNotes = controlFileLength("csNotes", csNotes, 255);
                    }
                    shipto = omsorder.getShipto();
                    eccBillto = omsorder.getEccBillto();
                    eccDelivery = omsorder.getEccDelivery();

                    if (shipto != null) {
                        // ?Shipto
                        eccShiptoId = shipto.getEccShiptoId();
                        eccShiptoName = shipto.getEccShiptoName();
                        eccShiptoRegion = shipto.getEccShiptoRegion();
                        eccShiptoCity = shipto.getEccShiptoCity();
                        eccShiptoDistrict = shipto.getEccShiptoDistrict();
                        eccShiptoAddress = shipto.getEccShiptoAddress();
                        eccShiptoZipcode = shipto.getEccShiptoZipcode();
                        eccShiptoMob = shipto.getEccShiptoMob();
                        eccShiptoTel = shipto.getEccShiptoTel();
                        if (CHECKLENGTH) {
                            eccShiptoId = controlFileLength("eccShiptoId", eccShiptoId, 10);
                            eccShiptoName = controlFileLength("eccShiptoName", eccShiptoName, 64);
                            eccShiptoRegion = controlFileLength("eccShiptoRegion", eccShiptoRegion, 64);
                            eccShiptoCity = controlFileLength("eccShiptoCity", eccShiptoCity, 64);
                            eccShiptoDistrict = controlFileLength("eccShiptoDistrict", eccShiptoDistrict, 64);
                            eccShiptoAddress = controlFileLength("eccShiptoAddress", eccShiptoAddress, 255);
                            eccShiptoZipcode = controlFileLength("eccShiptoZipcode", eccShiptoZipcode, 12);
                            eccShiptoMob = controlFileLength("eccShiptoMob", eccShiptoMob, 64);
                            eccShiptoTel = controlFileLength("eccShiptoTel", eccShiptoTel, 64);
                        }
                        zstrsdomsshipto = new ZSTRSDOMSSHIPTO();
                        zstrsdomsshipto.setECCSHIPTOID(eccShiptoId);
                        zstrsdomsshipto.setECCSHIPTONAME(eccShiptoName);
                        zstrsdomsshipto.setECCSHIPTOREGION(eccShiptoRegion);
                        zstrsdomsshipto.setECCSHIPTOCITY(eccShiptoCity);
                        zstrsdomsshipto.setECCSHIPTODISTRICT(eccShiptoDistrict);
                        zstrsdomsshipto.setECCSHIPTOADDRESS(eccShiptoAddress);
                        zstrsdomsshipto.setECCSHIPTOZIPCODE(eccShiptoZipcode);
                        zstrsdomsshipto.setECCSHIPTOMOB(eccShiptoMob);
                        zstrsdomsshipto.setECCSHIPTOTEL(eccShiptoTel);
                    } else {
                        erpOrderLog.warn(UO_LogHead + "MessageShipto.");
                    }
                    // converter
                    ZSTRSDOMSBILLTO zstrsdomsbillto = null;
                    if (eccBillto != null) {
                        // ?BILLTO
                        eccInvoiceType = eccBillto.getEccInvoiceType();
                        eccInvoiceTitle = eccBillto.getEccInvoiceTitle();
                        eccInvoiceContent = eccBillto.getEccInvoiceContent();
                        eccTaxpayerNumber = eccBillto.getEccTaxpayerNumber();
                        eccBankName = eccBillto.getEccBankName();
                        eccBankNumber = eccBillto.getEccBankNumber();
                        eccCustomerAddress = eccBillto.getEccCustomerAddress();
                        eccCustomerPhone = eccBillto.getEccCustomerPhone();
                        if (CHECKLENGTH) {
                            eccInvoiceType = controlFileLength("eccInvoiceType", eccInvoiceType, 8);
                            eccInvoiceTitle = controlFileLength("eccInvoiceTitle", eccInvoiceTitle, 80);
                            eccInvoiceContent = controlFileLength("eccInvoiceContent", eccInvoiceContent, 128);
                            eccTaxpayerNumber = controlFileLength("eccTaxpayerNumber", eccTaxpayerNumber, 30);
                            eccBankName = controlFileLength("eccBankName", eccBankName, 80);
                            eccBankNumber = controlFileLength("eccBankNumber", eccBankNumber, 30);
                            eccCustomerAddress = controlFileLength("eccCustomerAddress", eccCustomerAddress, 80);
                            eccCustomerPhone = controlFileLength("eccCustomerPhone", eccCustomerPhone, 18);
                        }
                        zstrsdomsbillto = new ZSTRSDOMSBILLTO();
                        zstrsdomsbillto.setECCINVOICETYPE(eccInvoiceType);
                        zstrsdomsbillto.setECCINVOICETITLE(eccInvoiceTitle);
                        zstrsdomsbillto.setECCINVOICECONTENT(eccInvoiceContent);
                        zstrsdomsbillto.setECCTAXPAYERNUMBER(eccTaxpayerNumber);
                        zstrsdomsbillto.setECCBANKNAME(eccBankName);
                        zstrsdomsbillto.setECCBANKNUMBER(eccBankNumber);
                        zstrsdomsbillto.setECCCUSTOMERADDRESS(eccCustomerAddress);
                        zstrsdomsbillto.setECCCUSTOMERPHONE(eccCustomerPhone);
                    } else {
                        erpOrderLog.warn(UO_LogHead + "MessageEccBillto.");

                    }
                    ZSTRSDOMSDELIVERY zstrsdomsdelivery = null;
                    if (eccDelivery != null) {
                        // ?EccDelivery
                        eccExpressId = eccDelivery.getEccExpressId();
                        eccExpressName = eccDelivery.getEccExpressName();
                        if (CHECKLENGTH) {
                            eccExpressId = controlFileLength("eccExpressId", eccExpressId, 10);
                            eccExpressName = controlFileLength("eccExpressName", eccExpressName, 64);
                        }

                        zstrsdomsdelivery = new ZSTRSDOMSDELIVERY();
                        zstrsdomsdelivery.setECCEXPRESSID(eccExpressId);
                        zstrsdomsdelivery.setECCEXPRESSNAME(eccExpressName);
                    } else {
                        erpOrderLog.warn(UO_LogHead + "MessageEccDelivery.");
                    }

                    // TS-168 added by Liang Weng
                    // eccItemNode:item
                    // eccRefundNode:refund

                    final ZSTRSDOMSSOITEMREFUND eccRefundNode = new ZSTRSDOMSSOITEMREFUND();
                    final List<ZSTRSDOMSORDERITEMREFUND> eccItemList = eccRefundNode.getItem();
                    refund = omsorder.getRefund();
                    if (null != refund) {
                        itemList = refund.getItem();
                    } else {
                        erpOrderLog.warn(UO_LogHead + "MessageRefund.");
                    }
                    BigDecimal refundAmountBD = null;
                    // ??
                    if (operation.equals(UO_DEBLOCKSTATUS)) {
                        if (null != itemList && !itemList.isEmpty()) {
                            final Iterator<Item> itemIter = itemList.iterator();
                            while (itemIter.hasNext()) {
                                final ZSTRSDOMSORDERITEMREFUND eccItemNode = new ZSTRSDOMSORDERITEMREFUND();
                                final Item item = itemIter.next();
                                refundOmsItemId = item.getRefundOmsItemId();
                                refundType = item.getRefundType();
                                refundQuantity = item.getRefundQuantity();
                                refundAmount = item.getRefundAmount();
                                if (CHECKLENGTH) {
                                    refundOmsItemId = controlFileLength("refundOmsItemId", refundOmsItemId, 35);
                                    refundType = controlFileLength("refundType", refundType, 1);
                                    refundQuantity = controlFileLength("refundQuantity", refundQuantity, 8);
                                    refundAmountBD = double2BigDecimal(refundAmount);

                                }
                                eccItemNode.setREFUNDOMSITEMID(refundOmsItemId);
                                eccItemNode.setREFUNDTYPE(refundType);
                                eccItemNode.setREFUNDQUANTITY(refundQuantity);
                                eccItemNode.setREFUNDAMOUNT(refundAmountBD);
                                eccItemList.add(eccItemNode);
                            }
                        } else {
                            erpOrderLog.warn(UO_LogHead + "RefundItem.");
                        }
                    }

                    // SET
                    zssaleordercommin.setOPERATION(operation);
                    zssaleordercommin.setECCORDERID(eccOrderId);
                    zssaleordercommin.setOMSORDERID(omsOrderId);
                    zssaleordercommin.setCSNOTES(csNotes);
                    zssaleordercommin.setUSERNOTES(userNotes);
                    zssaleordercommin.setSHIPTO(zstrsdomsshipto);
                    zssaleordercommin.setECCBILLTO(zstrsdomsbillto);
                    zssaleordercommin.setECCDELIVERY(zstrsdomsdelivery);
                    zssaleordercommin.setREFUND(eccRefundNode);

                    saleordercommtab.getItem().add(zssaleordercommin);
                    zstrsdomssaleorderscommin.setORDERS(saleordercommtab);
                } else {
                    erpOrderLog.error(UO_LogHead
                            + "MessageeccOrderIdmsOrderId,??.");
                }
            } else {
                erpOrderLog.error(
                        UO_LogHead + "Messageoperation??,??.");
            }
        }
        return zstrsdomssaleorderscommin;
    }

    // ?NULL??0?
    private boolean checkOrderId(final String parameter, final int length, final boolean checkLength) {
        boolean falg = false;
        if (parameter != null && checkLength) {
            if (parameter.length() > 0 && parameter.length() <= length) {
                falg = true;
            }
        } else {
            if (parameter != null && parameter.length() > 0) {
                falg = true;
            }
        }
        return falg;
    }

    // 
    private String controlFileLength(final String fileName, final String fileValue, final int length) {
        String result = null;
        if (fileValue != null && fileValue.length() > length) {
            result = fileValue.substring(0, length);
            erpOrderLog.warn(UO_LogHead + "Message:" + fileName + ":" + fileValue
                    + "," + fileValue.length() + ",?" + length + ".");
            erpOrderLog.warn(UO_LogHead + "Message:" + fileName
                    + ",?????ECC.");
        } else {
            result = fileValue;
        }
        return result;
    }

    // ?(15,3)
    private BigDecimal double2BigDecimal(final double refundAmount) {
        final BigDecimal decimal = new BigDecimal(Double.toString(refundAmount));
        decimal.setScale(3, BigDecimal.ROUND_HALF_UP);
        if (decimal.precision() > 18) {
            erpOrderLog.warn(UO_LogHead + "Message: " + "refundAmount" + ": " + refundAmount
                    + "," + decimal.precision() + ",?" + "(15,3)" + ".");
            erpOrderLog.warn(UO_LogHead + "Message:" + "refundAmount"
                    + ",?????ECC.");
        }
        return decimal;
    }
}