OrderAssignApiService.java 14.5 KB
package com.huaheng.api.SSP.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huaheng.api.SSP.domain.ConfirmHeaderModel;
import com.huaheng.api.SSP.domain.ConfirmModel;
import com.huaheng.api.SSP.domain.ConfirmTypeModel;
import com.huaheng.api.U8.domain.ICSReceiptHeader;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.general.material.domain.Material;
import com.huaheng.pc.general.material.service.IMaterialService;
import com.huaheng.pc.inventory.inventory.domain.Inventory;
import com.huaheng.pc.inventory.inventory.service.IInventoryService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.IReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.IReceiptHeaderService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.IShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.IShipmentHeaderService;
import net.bytebuddy.implementation.bytecode.Throw;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**   出入库单下发Service
 *
 *  1. 此Service的作用是将系统中未被钱柜扫描过的出入库单据发送给钱柜,目前考虑到系统资源,一次只能查询或发送一条单据,发送规则是根据子单的库区是否为钱柜
 *  进行发送,发送成功后会将子单的状态改为100,若主单内的所有子单都不属于钱柜库区,则将不进行发送,但是无论发送与否,都会对主单的扫描标识进行修改,目前钱柜
 * 的标识是1,若主单据的扫描标识的第一位为1则代表此单据被钱柜扫描过。1`
 *
 * 2. 目前此Service出入库单的下发逻辑是先扫描入库单据,若入库单据已全部下发成功,将自动扫描出库单据。
 *
 * 3. 目前钱柜编码写死为QG,默认为库区编码为QG的物料为钱柜的物料。
 */
@Transactional
@Service
public class OrderAssignApiService {

    @Autowired
    IReceiptHeaderService iReceiptHeaderService;

    @Autowired
    IReceiptDetailService iReceiptDetailService;

    @Autowired
    IShipmentHeaderService iShipmentHeaderService;

    @Autowired
    IShipmentDetailService iShipmentDetailService;

    @Autowired
    IMaterialService iMaterialService;
    @Autowired
    ItemDownloadApiService itemDownloadApiService;
    @Autowired
    IInventoryService inventoryService;

    //private  static String address="http://172.16.30.238:4322/api/SSP/";

    @Transactional
    public AjaxResult receiptAssign(String code){
        //查询出一条未被钱柜扫描过的入库单
        AjaxResult ajaxResult=new AjaxResult();
        ReceiptHeader item =  iReceiptHeaderService.selectNosScanSSP(code);
        ConfirmHeaderModel cfh = new ConfirmHeaderModel();
        cfh.setOrderHeaderId(item.getId());
        cfh.setOrderCode(item.getCode());
        cfh.setOrderType("receipt");
        cfh.setWarehouseCode(item.getWarehouseCode());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cfh.setcDatetime(df.format(new Date()));
        ConfirmModel confirmModel = new ConfirmModel();
//        confirmModel.setConfirmHeaderModel(cfh);


        ReceiptDetail receiptDetail = new ReceiptDetail();
        receiptDetail.setReceiptId(item.getId());
        receiptDetail.setZoneCode("QG");
        List<ReceiptDetail> details = iReceiptDetailService.selectListEntityByEqual(receiptDetail);
        List<ConfirmTypeModel> typeModels = new ArrayList<>();
        //遍历入库子单
        int i = 0;
        List<Integer> list = new ArrayList();
        for (ReceiptDetail ds:details){
            Material material = new Material();
            material.setCode(ds.getMaterialCode());
            material=iMaterialService.selectFirstEntity(material);
            if (material != null ) {
                if(StringUtils.isEmpty(material.getZoneCode())){
                    material.setZoneCode("QG");
                    if(!itemDownloadApiService.sendMaterial(material).hasErr()){
//                        iMaterialService.updateByModel(material);
                    }else {
                        throw new ServiceException(material.getCode()+"物料同步失败");
                    }
                }
                if(StringUtils.isNotEmpty(material.getZoneCode()) && !material.getZoneCode().equals("QG") ){
                    if(itemDownloadApiService.sendMaterial(material).hasErr()){
                        throw new ServiceException(material.getCode()+"物料同步失败");
                    }
                }
                ConfirmTypeModel cft = new ConfirmTypeModel();
                cft.setOrderDetailId(ds.getId());
                cft.setCompanyCode(ds.getCompanyCode());
                Inventory inventory=new Inventory();
                inventory.setZoneCode("QG");
                inventory.setWarehouseCode(ShiroUtils.getWarehouseCode());
                inventory.setMaterialCode(material.getCode());
                inventory=inventoryService.selectFirstEntity(inventory);
                if(inventory!=null){
                    cft.setContainerCode(inventory.getContainerCode());
                }else {
                    cft.setContainerCode("0");
                }
                cft.setItemCode(ds.getMaterialCode());
                cft.setItemName(material.getName());
                cft.setItemBarcode(material.getCode());
                cft.setItemState("良品");
                float a = Convert.toFloat(ds.getQty());
                cft.setQty(a);
                cft.setItemUnit(material.getMasterUnit());
                typeModels.add(cft);
                list.add(ds.getId());
                i++;
            }
        }
        //若此单据无钱柜物料 则修改状态
        ReceiptHeader receiptHeaderOver = new ReceiptHeader();
        receiptHeaderOver.setId(item.getId());
        if (i < 1){
            StringBuffer str = new StringBuffer();
            str.append(item.getScanid());
            str.replace(0, 1, "1");
            receiptHeaderOver.setScanid(String.valueOf(str));
            iReceiptHeaderService.updateByModel(receiptHeaderOver);
            return AjaxResult.success("已扫描一条入单据,没有发现钱柜物料");
        }else {
            //此处将入库主单,入库子单整合成发送给钱柜的的实体
            cfh.setCubeCode("QG");
            confirmModel.setConfirmHeaderModel(cfh);
            confirmModel.setOrderItems(typeModels);
            try {
                String url=itemDownloadApiService.getQGUrl()+"OrderAssign";
                String JsonParam = JSON.toJSONString(confirmModel);
                String result = HttpUtils.bodypost(url, JsonParam);
                if(StringUtils.isEmpty(result)){
                    throw new ServiceException("接口地址错误");
                }
                ajaxResult = JSON.parseObject(result, AjaxResult.class);
                if(ajaxResult.code==200) {
                    //修改已经下发的子单状态
                    short statusover = 100;
                    ReceiptDetail detail = new ReceiptDetail();
                    for (int j = 0; j < list.size(); j++) {
                        detail.setId(list.get(j));
                        detail.setStatus(statusover);
                        iReceiptDetailService.updateByModel(detail);
                    }
                    StringBuffer str = new StringBuffer();
                    str.append(item.getScanid());
                    str.replace(0, 1, "1");
                    receiptHeaderOver.setScanid(String.valueOf(str));
                    if (iReceiptHeaderService.updateByModel(receiptHeaderOver) < 1) {
                        throw new ServiceException("入库订单状态更新失败!");
                    }
                }
            } catch (Exception e) {
                throw new ServiceException("入库订单下发发送失败!");
            }
            return ajaxResult;
        }
    }


    @Transactional
    public AjaxResult shipmentAssign(ShipmentHeader item,List<ShipmentDetail> shipmentDetails) {
        //查询出一条未被钱柜扫描过的出库单
        AjaxResult ajaxResult=new AjaxResult();
        ConfirmHeaderModel cfh = new ConfirmHeaderModel();
        cfh.setOrderHeaderId(item.getId());
        cfh.setOrderCode(item.getCode());
        cfh.setOrderType("shipment");
        cfh.setWarehouseCode(item.getWarehouseCode());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cfh.setcDatetime(df.format(new Date()));
        ConfirmModel confirmModel = new ConfirmModel();
//        confirmModel.setConfirmHeaderModel(cfh);

        List<ConfirmTypeModel> typeModels = new ArrayList<>();
        //遍历出库子单
        int i = 0;
        List<Integer> list = new ArrayList();
        for (ShipmentDetail ds : shipmentDetails) {
            if(ds.getStatus()==0) {
                Material material = new Material();
                material.setCode(ds.getMaterialCode());
                material = iMaterialService.selectFirstEntity(material);
                if (material != null) {
                    if (StringUtils.isEmpty(material.getZoneCode())) {
                        material.setZoneCode("QG");
                        if (!itemDownloadApiService.sendMaterial(material).hasErr()) {
//                        iMaterialService.updateByModel(material);
                        } else {
                            throw new ServiceException(material.getCode() + "物料同步失败");
                        }
                    }
                    if (StringUtils.isNotEmpty(material.getZoneCode()) && !material.getZoneCode().equals("QG")) {
                        if (itemDownloadApiService.sendMaterial(material).hasErr()) {
                            throw new ServiceException(material.getCode() + "物料同步失败");
                        }
                    }
                    ConfirmTypeModel cft = new ConfirmTypeModel();
                    Inventory inventory = new Inventory();
                    inventory.setZoneCode("QG");
                    inventory.setWarehouseCode(ShiroUtils.getWarehouseCode());
                    inventory.setMaterialCode(material.getCode());
                    inventory = inventoryService.selectFirstEntity(inventory);
                    if (inventory == null) {
                        throw new ServiceException("该物料没有库存");
                    } else {
                        cft.setContainerCode(inventory.getContainerCode());
                    }
                    cft.setOrderDetailId(ds.getId());
                    cft.setCompanyCode(ds.getCompanyCode());
                    cft.setItemCode(ds.getMaterialCode());
                    cft.setItemName(material.getName());
                    cft.setItemBarcode(material.getCode());
                    cft.setItemState("良品");
                    float a = Convert.toFloat(ds.getQty());
                    cft.setQty(a);
                    cft.setItemUnit(material.getMasterUnit());
                    typeModels.add(cft);
                    list.add(ds.getId());
                    i++;
                }
            }
        }
        //若此单据无钱柜物料 则修改状态
//        ShipmentHeader shipmentHeaderOver = new ShipmentHeader();
//        shipmentHeaderOver.setId(item.getId());
        if (i < 1) {
//            StringBuffer str = new StringBuffer();
//            str.append(item.getScanid());
//            str.replace(0, 1, "1");
//            shipmentHeaderOver.setScanid(String.valueOf(str));
//            iShipmentHeaderService.updateByModel(shipmentHeaderOver);
            return AjaxResult.success("选中的子单都已下发过");
        } else {
            //此处将出库主单,出库子单整合成发送给钱柜的的实体
            cfh.setCubeCode("QG");
            confirmModel.setConfirmHeaderModel(cfh);
            confirmModel.setOrderItems(typeModels);
            try {
                String url = itemDownloadApiService.getQGUrl()+"OrderAssign";
                String JsonParam = JSON.toJSONString(confirmModel);
                String result = HttpUtils.bodypost(url, JsonParam);
                if(StringUtils.isEmpty(result)){
                    throw new ServiceException("接口地址错误");
                }
                ajaxResult = JSON.parseObject(result, AjaxResult.class);

                if (ajaxResult.code == 200) {
                    //修改已经下发的子单状态
                    short statusover = 100;
                    ShipmentDetail detail = new ShipmentDetail();
                    for (int j = 0; j < list.size(); j++) {
                        detail.setId(list.get(j));
                        detail.setStatus(statusover);
                        iShipmentDetailService.updateByModel(detail);
                    }
                    //如果该主单据的所有钱柜子单都已下发,则改变该主单的状态
                    ShipmentDetail dt=new ShipmentDetail();
                    dt.setShipmentCode(item.getCode());
                    dt.setZoneCode("QG");
                    dt.setStatus((short)0);
                    List<ShipmentDetail> details=iShipmentDetailService.selectListEntityByEqual(dt);
                    if(details==null) {
                        StringBuffer str = new StringBuffer();
                        str.append(item.getScanid());
                        str.replace(0, 1, "1");
                        item.setScanid(String.valueOf(str));
                        if (iShipmentHeaderService.updateByModel(item) < 1) {
                            throw new ServiceException("出库单据状态更新失败");
                        }
                    }
                }
            } catch (Exception e) {
                throw new ServiceException("出库订单发送失败!");
            }
        }
        return ajaxResult;
    }
}