XnTransServices.java 8.46 KB
package com.huaheng.mobile.receipt;

import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryTransaction.domain.InventoryTransaction;
import com.huaheng.pc.inventory.inventoryTransfer.service.InventoryTransferService;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerHeader;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerView;
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.ReceiptTaskService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class XnTransServices {
    public static final String RECEIPT_TYPE = "XNT";

    @Autowired
    private ReceiptHeaderService receiptHeaderService;

    @Autowired
    private ReceiptDetailService receiptDetailService;

    @Autowired
    private InventoryTransferService inventoryTransferService;

    @Autowired
    private InventoryDetailService inventoryDetailService;

    @Autowired
    TaskHeaderService taskHeaderService;
    @Autowired
    ReceiptTaskService receiptTaskService;

    @Autowired
    private ReceiptContainerHeaderService receiptContainerHeaderService;

    @Transactional(rollbackFor = Exception.class)
    public AjaxResult trans(XnTransBean bean) {
        check(bean);
        ReceiptHeader receiptHeader = createTransReceipt(bean);
        List<ReceiptDetail> receiptDetailList = createTransReceiptDetail(receiptHeader, bean);
        TaskHeader task = combine(bean, receiptHeader, receiptDetailList);
        transOut(bean);
        finishTask(task);
        return AjaxResult.success(task.getId());
    }

    private void finishTask(TaskHeader task){
        try {
            if(task.getZoneCode().equalsIgnoreCase("PK") || task.getZoneCode().equalsIgnoreCase("AGV")){
                String ids = task.getId().toString();
                Integer[] idArray = Convert.toIntArray(ids);
                taskHeaderService.completeTaskByWMS(idArray,null);
                return;
            }
            if (task.getStatus() == 1) {
                taskHeaderService.sendTaskToWcs(Convert.toIntArray(task.getId().toString()));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private boolean check(XnTransBean bean) {
        if (StringUtils.isEmpty(bean.getContainerCode())) {
            throw new RuntimeException("容器号不能为空");
        }

        if (bean.getInventoryList() == null || bean.getInventoryList().size() == 0) {
            throw new RuntimeException("明细不能为空");
        }
        return true;
    }

    /**
     * 创建转移入库单
     **/
    private ReceiptHeader createTransReceipt(XnTransBean bean) {
        ReceiptHeader receiptHeader = new ReceiptHeader();
        receiptHeader.setReceiptType(RECEIPT_TYPE);
        receiptHeader.setCompanyCode(bean.getCompanyCode());
//        receiptHeader.set(true);

        AjaxResult result = receiptHeaderService.saveReceiptHeader(receiptHeader);
        if (result.hasErr()) {
            throw new ServiceException("创建转移单头出错:" + result.getMsg());
        }
        return receiptHeader;
    }

    private void transOut(XnTransBean bean){
        for (InventoryItem item: bean.getInventoryList()) {
            InventoryDetail inventory = inventoryDetailService.getById(item.getId());
            //库存出交易
            String types = "transferShipment";
            InventoryTransaction transaction = inventoryTransferService.transferInventoryTransaction(inventory, types, item.getQty());

            //虚拟库存的修改或删除
            if (inventory.getQty().compareTo(item.getQty()) == 0) {
                if (!inventoryDetailService.removeById(inventory.getId())) {
                    throw new ServiceException("删除虚拟库存失败");
                }
            }
            if (inventory.getQty().compareTo(item.getQty()) == 1) {
                inventory.setQty(inventory.getQty().subtract(item.getQty()));
                if (!inventoryDetailService.updateById(inventory)) {
                    throw new ServiceException("修改虚拟库存失败");
                }
            }
        }
    }
    /**
     * 创建转移单明细
     **/
    private List<ReceiptDetail> createTransReceiptDetail(ReceiptHeader header, XnTransBean bean) {
        List<ReceiptDetail> receiptDetailList = new ArrayList<>();
        for (InventoryItem item : bean.getInventoryList()) {
            ReceiptDetail detail = new ReceiptDetail();
            detail.setReceiptId(header.getId());
            detail.setReceiptCode(header.getCode());
            detail.setCompanyCode(header.getCompanyCode());

            InventoryDetail inventory = inventoryDetailService.getById(item.getId());
            detail.setQty(item.getQty());
            detail.setMoCode(inventory.getMoCode());
            detail.setBatch(inventory.getBatch());
            detail.setMaterialSpec(inventory.getMaterialSpec());
            detail.setLot(inventory.getLot());
            detail.setUWarehouseCode(inventory.getUWarehouseCode());
            detail.setInventorySts("good");
            detail.setMaterialCode(inventory.getMaterialCode());
            AjaxResult result = receiptDetailService.saveReceiptDetaial(detail);
            if (result.hasErr()) {
                throw new ServiceException("创建转移明细单出错:" + result.getMsg());
            }
            receiptDetailList.add(detail);
        }
        return receiptDetailList;
    }

    private ReceiptContainerHeader getUnCompleteCombine(String containerCode) {
        ReceiptContainerHeader query = new ReceiptContainerHeader();
        query.setStatus( 0);
        query.setContainerCode(containerCode);
        ReceiptContainerHeader ret = receiptContainerHeaderService.selectFirstEntity(query);
        if (ret == null) {
            query.setStatus( 10);
            ret = receiptContainerHeaderService.selectFirstEntity(query);
        }
        return ret;
    }

    private TaskHeader combine(XnTransBean bean, ReceiptHeader header, List<ReceiptDetail> detailList) throws RuntimeException {

        ReceiptContainerHeader receiptContainerHeader = null;

        for (int i = 0; i < detailList.size(); i++) {
            ReceiptDetail detail = detailList.get(i);
            ReceiptContainerView record = new ReceiptContainerView();
            record.setReceiptCode(header.getCode());
            record.setQty(detail.getQty());
            record.setReceiptDetailId(detail.getId());
            record.setContainerCode(bean.getContainerCode());
            receiptContainerHeaderService.saveContainer(record);
            receiptContainerHeader = getUnCompleteCombine(bean.getContainerCode());
        }

        ReceiptContainerHeader query = new ReceiptContainerHeader();
        query.setStatus( 0);
        List<ReceiptContainerHeader> receiptContainerHeaders = receiptContainerHeaderService.selectFirstEntityList(query);
        for (ReceiptContainerHeader item : receiptContainerHeaders) {
            String ids = item.getId().toString();
            Integer[] idArray = Convert.toIntArray(ids);
            try {
                receiptTaskService.createReceiptTask(Arrays.asList(idArray.clone()));
//                taskHeaderService.createReceiptTask(idArray);
            } catch (Exception e) {
                throw new RuntimeException("组盘生成任务时出错:" + e.getMessage());
            }
        }

        TaskHeader task = new TaskHeader();
        task.setAllocationHeadId(receiptContainerHeader.getId());
        task = taskHeaderService.selectFirstEntity(task);
        return task;
    }
}