ShipmentAPIService.java 17.5 KB
package com.huaheng.api.general.service;
import com.google.common.collect.Lists;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;


import com.alibaba.fastjson.JSON;
import com.huaheng.api.general.domain.ShipmentItems;
import com.huaheng.api.general.domain.ShipmentObject;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.IAddressService;
import com.huaheng.pc.general.company.domain.Company;
import com.huaheng.pc.general.company.service.ICompanyService;
import com.huaheng.pc.general.material.domain.Material;
import com.huaheng.pc.general.material.service.IMaterialService;
import com.huaheng.pc.general.warehouse.domain.Warehouse;
import com.huaheng.pc.general.warehouse.service.IWarehouseService;
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 com.huaheng.pc.system.dict.service.IDictDataService;
import com.huaheng.pc.task.task.domain.Task;
import com.huaheng.pc.task.task.service.ITaskService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.ITaskDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
@Service
public class ShipmentAPIService {

    @Autowired
    private IDictDataService dictDataService;
    @Autowired
    IShipmentDetailService shipmentDetailService;
    @Autowired
    ICompanyService companyService;
    @Autowired
    IShipmentHeaderService shipmentHeaderService;
    @Autowired
    private IMaterialService materialService;
    @Autowired
    private IWarehouseService warehouseService;
    @Autowired
    private ITaskService taskService;
    @Autowired
    private ITaskDetailService taskDetailService;
    @Autowired
    private IAddressService addressService;



    //出库单下发
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertShipment(ShipmentObject shipmentObject){
        //主单校验
        if(StringUtils.isEmpty(shipmentObject.getShipmentCode()) ||
                StringUtils.isEmpty(shipmentObject.getWarehouseCode()) ||
                StringUtils.isEmpty(shipmentObject.getCompanyCode()) ||
                StringUtils.isEmpty(shipmentObject.getShipmentType())
        ){
            return AjaxResult.error("入库信息主单数据必填项有误");
        }
        //明细校验
        if(shipmentObject.getShipmentItems() == null || shipmentObject.getShipmentItems().size() < 1){
            return AjaxResult.error("明细子单没有数据!");
        }

        if(shipmentObject.getShipmentItems().size() > 200){
            return AjaxResult.error("明细单不能超过200行");
        }
        List<ShipmentItems> shipmentItemsList = shipmentObject.getShipmentItems();
        /*if(dictDataService.checkConfig("shipmentType", shipmentApiHeader.getShipmentType()) == false)
        {
            throw new ServiceException("没有对应的出库单类型");
        }*/
        //插入出库主单数据
        ShipmentHeader shipmentHeader = new ShipmentHeader();
        shipmentHeader.setCode(shipmentHeaderService.createCode(shipmentObject.getShipmentType()));
        Company company=new Company();
        Warehouse warehouse= new Warehouse();
        company.setCode(shipmentObject.getCompanyCode());
        company = companyService.selectFirstEntity(company);
        if (company == null) {
            return AjaxResult.error("系统没有该货主");
        } else {
            shipmentHeader.setCompanyId(company.getId());
        }
        warehouse.setCode(shipmentObject.getWarehouseCode());
        warehouse = warehouseService.selectFirstEntity(warehouse);
        if (warehouse == null) {
            return AjaxResult.error("系统没有该仓库");
        } else {
            shipmentHeader.setWarehouseId(warehouse.getId());
        }
        Date date = null;
        if(StringUtils.isNotEmpty(shipmentObject.getcDatetime())){
            try{
                DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                date = fmt.parse(shipmentObject.getcDatetime());
            }catch (Exception e){
                throw new ServiceException(e.toString());
            }
            shipmentHeader.setCreated(date);
        }else {
            shipmentHeader.setCreated(new Date());
        }
        shipmentHeader.setWarehouseCode(warehouse.getCode());
        shipmentHeader.setCompanyCode(company.getCode());
        shipmentHeader.setSourceCode(shipmentObject.getShipmentCode()); //上游系统编码
        shipmentHeader.setSourcePlatform("ERP");
        shipmentHeader.setType(shipmentObject.getShipmentType());
        shipmentHeader.setPriority(10);
        shipmentHeader.setStation(("1"));
        shipmentHeader.setTotalQty(new BigDecimal("0"));
        shipmentHeader.setTotalLines(0);
        shipmentHeader.setFirstStatus(0);
        shipmentHeader.setLastStatus(0);
        shipmentHeader.setUploadStatus(0);
        //shipmentHeader.setCreated(new Date());
        shipmentHeader.setCreatedBy("ERP");
        shipmentHeader.setLastUpdated(new Date());
        shipmentHeader.setLastUpdatedBy("ERP");
        shipmentHeader.setUserDef1(shipmentObject.getShipmentId().toString());//上游系统单号
        shipmentHeader.setUserDef2("");
        shipmentHeader.setUserDef3("");
        int j = shipmentHeaderService.insert(shipmentHeader);
        if(j < 1){
            throw new ServiceException("插入出库主单失败");
        }
        //添加细表到shipmentDetail
        List<ShipmentDetail> shipmentDetailList = new ArrayList<>();
        BigDecimal totalQty = BigDecimal.ZERO;
        for(ShipmentItems item : shipmentItemsList) {
            //先校验
            if(StringUtils.isEmpty(item.getItemCode()) ||
                    item.getQty() == null ||
                    item.getQty() ==0 ){
                throw new ServiceException("入库明细单必填项数据错误!");
            }

            Material materialTmp = new Material();
            materialTmp.setCode(item.getItemCode());
            materialTmp.setSpecification(item.getItemSpecification());
            materialTmp.setName(item.getItemName());
            materialTmp.setWarehouseCode(shipmentHeader.getWarehouseCode());
            Material material = materialService.selectFirstEntity(materialTmp);
            if (StringUtils.isNull(material)) {
                throw new ServiceException("物料未找到");
            }
            ShipmentDetail shipmentDetail = new ShipmentDetail();
            shipmentDetail.setWarehouseId(shipmentHeader.getWarehouseId());
            shipmentDetail.setWarehouseCode(shipmentHeader.getWarehouseCode());
            shipmentDetail.setCompanyId(shipmentHeader.getCompanyId());
            shipmentDetail.setCompanyCode(shipmentHeader.getCompanyCode());
            shipmentDetail.setSourceCode(shipmentObject.getShipmentCode());
            shipmentDetail.setSourceLine(shipmentObject.getShipmentId().toString());
            shipmentDetail.setShipmentId(shipmentHeader.getId());
            shipmentDetail.setShipmentCode(shipmentHeader.getCode());
            shipmentDetail.setMaterialId(material.getId());
            shipmentDetail.setMaterialCode(material.getCode());
            //shipmentDetail.setMaterialName(material.getName());
            //shipmentDetail.setSpecification(material.getSpecification());
            shipmentDetail.setZoneCode("LK");
            shipmentDetail.setUnit("PCS");
            shipmentDetail.setBatch(item.getItemBatch());
            shipmentDetail.setLot("");
            shipmentDetail.setProject(item.getItemProject());
            shipmentDetail.setManufactureDate(null);
            shipmentDetail.setExpirationDate(null);
            shipmentDetail.setAgingDate(null);
            if(StringUtils.isNotEmpty(item.getItemState())){
                shipmentDetail.setInventoryStatus(item.getItemState());
            }else{
                shipmentDetail.setInventoryStatus("good");
            }
            shipmentDetail.setQtyCompleted(new BigDecimal("0"));
            try {
                shipmentDetail.setQty(BigDecimal.valueOf(item.getQty()));
            }catch (Exception e){
                throw new ServiceException(e.toString());
            }
            shipmentDetail.setPrice(new BigDecimal("0"));
            shipmentDetail.setStatus((short)0);
            shipmentDetail.setCreated(date);
            shipmentDetail.setCreatedBy("ERP");
            shipmentDetail.setLastUpdated(new Date());
            shipmentDetail.setLastUpdatedBy("ERP");
            shipmentDetail.setUserDef1("");
            shipmentDetail.setUserDef2("");
            shipmentDetail.setUserDef3("");

            //shipmentDetailService.insert(shipmentDetail);
            shipmentDetailList.add(shipmentDetail);
            //更新表头的总行数和总数量统计
            totalQty = totalQty.add(shipmentDetail.getQty());

        }
        //批量插入出库单
        int k = shipmentDetailService.insertBatch(shipmentDetailList);
        if(k != shipmentDetailList.size()){
            throw new ServiceException("明细插入失败!");
        }
        //更新单据总行数与总数量
        shipmentHeader.setTotalLines(shipmentDetailList.size());
        shipmentHeader.setTotalQty(totalQty);
        shipmentHeaderService.updateByModel(shipmentHeader );
        return AjaxResult.success("出库单下发成功");
    }


    /**
     * 出库单回传
     * 任务回传
     * */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult confirmShipment() {
        /*出库任务完成时回传*/
        //查询已完成的出库任务
        Task task = taskService.selectShipmentConfirm();
        if(StringUtils.isNull(task)){
            return AjaxResult.success("没有需要回传的出库任务");
        }
        //任务明细
        TaskDetail taskDetailt = new TaskDetail();
        taskDetailt.setWarehouseCode(task.getWarehouseCode());
        taskDetailt.setTaskId(task.getId());
        List<TaskDetail> taskDetails = taskDetailService.selectListEntityByEqual(taskDetailt);
        if(taskDetails == null || taskDetails.size() < 1){
            return AjaxResult.success(task.getId() + "任务没有明细");
        }
        //set回传实体
        String timeStr = null; //时间
        try{
            timeStr= LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }catch (Exception e) {
            throw new ServiceException("获取时间错误!");
        }
        ShipmentObject shipmentObject = new ShipmentObject();
        shipmentObject.setShipmentId(task.getId());
        shipmentObject.setWarehouseCode(task.getWarehouseCode());
        shipmentObject.setCompanyCode(task.getCompanyCode());
        shipmentObject.setcDatetime(timeStr);
        shipmentObject.setContainerCode(task.getContainerCode());
        shipmentObject.setLocationCode(task.getSourceLocation());
        //set明细
        List<ShipmentItems> shipmentItemsList = new ArrayList<>();
        for(TaskDetail detail:taskDetails){
            ShipmentItems shipmentItems = new ShipmentItems();
            //物料
            Material material = materialService.getMaterial(detail.getMaterialCode());
            if(StringUtils.isNull(material)){
                return AjaxResult.success("物料错误");
            }
            //出库明细
            ShipmentDetail shipmentDetail = shipmentDetailService.selectEntityById(detail.getBillDetailId());
            shipmentItems.setItemProject(detail.getProject());
            shipmentItems.setShipmentCode(detail.getBillCode());
            shipmentItems.setItemId(detail.getId());
            shipmentItems.setItemCode(detail.getMaterialCode());
            shipmentItems.setItemName(detail.getMaterialName());
            shipmentItems.setItemSpecification(material.getSpecification());
            shipmentItems.setItemBatch(detail.getBatch());
            shipmentItems.setItemState(shipmentDetail.getInventoryStatus());
            try{
                shipmentItems.setQty(detail.getQty().floatValue());
            }catch (Exception e){
                throw new ServiceException(e.toString());
            }
            shipmentItems.setItemUnit("PCS");
            shipmentItemsList.add(shipmentItems);
        }
        shipmentObject.setShipmentItems(shipmentItemsList);

        //json
        String jsonParam = JSON.toJSONString(shipmentObject);
        //url
        Address address = new Address();
        address.setWarehouseCode("TD0001");
        address.setParam("u8ConfirmShipment");
        address = addressService.selectEntity(address);
        if(address == null || StringUtils.isEmpty(address.getUrl())){
            return AjaxResult.error("回传URL为空!");
        }
        String url = address.getUrl();
        String result = HttpUtils.bodypost(url, jsonParam);
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        if(ajaxResult.getCode() != 200){
            throw new ServiceException("入库单回传ERP回复异常,回传失败!");
        }
        //修改任务的回传状态
        task.setFirstStatus((short) 120);
        task.setLastStatus((short) 120);
        int j = taskService.updateByModel(task);
        if(j < 1){
            throw new ServiceException(task.getId() + "任务回传状态修改失败");
        }
        return AjaxResult.success("出库任务" + task.getId() + "回传ERP成功");
    }





    /**
     * 出库单回传
     * 根据单据回传
     * */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult confirmShipmentA(String url)
    {
        //查询符合回传条件的主单,再根据主单回传子单
        ShipmentHeader header = new ShipmentHeader();
        header.setLastStatus(300);
        header.setWarehouseCode("TD0001");
        ShipmentHeader shipmentHeader = shipmentHeaderService.selectFirstEntity(header);
        if (StringUtils.isNull(shipmentHeader)) {
            return AjaxResult.success("没有主单需要回传");
        }
        ShipmentDetail shipmentDetail = new ShipmentDetail();
        shipmentDetail.setWarehouseCode(shipmentHeader.getWarehouseCode());
        shipmentDetail.setShipmentCode(shipmentHeader.getCode());
        List<ShipmentDetail> shipmentDetails = shipmentDetailService.selectListEntityByEqual(shipmentDetail);
        if (shipmentDetails == null || shipmentDetails.size() < 1) {
            return AjaxResult.error("出库回传子单不存在");
        }
        //
        ShipmentObject shipmentObject = new ShipmentObject();

        String dateTime = null;
        try{
            dateTime = new java.sql.Timestamp(System.currentTimeMillis()).toString();
        }catch (Exception e){
            throw new ServiceException(e.toString());
        }
        //主
        shipmentObject.setcDatetime(dateTime);
        shipmentObject.setCompanyCode(shipmentHeader.getCompanyCode());
        shipmentObject.setShipmentCode(shipmentHeader.getCode());
        shipmentObject.setShipmentId(shipmentHeader.getId());
        shipmentObject.setShipmentType(shipmentHeader.getType());
        shipmentObject.setWarehouseCode(shipmentHeader.getWarehouseCode());
        //明细
        List<ShipmentItems> shipmentItemsList = new ArrayList<>();
         for(ShipmentDetail item:shipmentDetails){
            ShipmentItems shipmentItems = new ShipmentItems();
            Material materialTmp = new Material();
            materialTmp.setCode(item.getMaterialCode());
            materialTmp.setWarehouseCode(item.getWarehouseCode());
            Material material = materialService.selectFirstEntity(materialTmp);
            if (StringUtils.isNull(material) ) {
                return AjaxResult.error("没有该物料");
            }
            shipmentItems.setItemBatch(item.getProject()); //project
            shipmentItems.setItemCode(material.getCode());
            shipmentItems.setItemId(item.getId());
            shipmentItems.setItemName(material.getName());
            shipmentItems.setItemSpecification(material.getSpecification());
            shipmentItems.setItemState(item.getInventoryStatus());
            shipmentItems.setItemUnit("PCS");
            try{
                shipmentItems.setQty(item.getQty().floatValue());
            }catch (Exception e){
                throw new ServiceException(e.toString());
            }
            shipmentItemsList.add(shipmentItems);
        }
        //set
        shipmentObject.setShipmentItems(shipmentItemsList);
        String JsonParam = JSON.toJSONString(shipmentObject);
        String result = HttpUtils.bodypost(url, JsonParam);
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        if(ajaxResult.getCode() != 200){
            throw new ServiceException("出库单回传ERP回复异常,回传失败!");
        }
        shipmentHeader.setFirstStatus(900);
        shipmentHeader.setLastStatus(900);
        shipmentHeader.setUploadTime(new Date());
        shipmentHeaderService.updateByModel(shipmentHeader);
        return ajaxResult;
    }
}