ShipmentApiService.java 22.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
package com.huaheng.api.general.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.api.general.domain.ShipmentDomain;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.common.utils.spring.SpringUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.customer.domain.Customer;
import com.huaheng.pc.config.customer.service.CustomerServiceImpl;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.shipmentType.domain.ShipmentType;
import com.huaheng.pc.config.shipmentType.service.ShipmentTypeService;
import com.huaheng.pc.config.supplier.domain.Supplier;
import com.huaheng.pc.config.warehouse.domain.Warehouse;
import com.huaheng.pc.config.warehouse.service.WarehouseService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentDetailHistory.domain.ShipmentDetailHistory;
import com.huaheng.pc.shipment.shipmentDetailHistory.service.ShipmentDetailHistoryService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.shipment.shipmentHeaderHistory.domain.ShipmentHeaderHistory;
import com.huaheng.pc.shipment.shipmentHeaderHistory.service.ShipmentHeaderHistoryService;
import org.krysalis.barcode4j.output.bitmap.BitmapEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ricard
 * @time 19/11/11
 */


@Component
@Transactional(rollbackFor = Exception.class)
public class ShipmentApiService {

    @Autowired
    private ShipmentHeaderService shipmentHeaderService;

    @Autowired
    private ShipmentDetailService shipmentDetailService;

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private CustomerServiceImpl customerService;

    @Autowired
    private ShipmentTypeService shipmentTypeService;

    @Autowired
    private MaterialService materialService;

    @Resource
    private ShipmentHeaderHistoryService shipmentHeaderHistoryService;

    @Resource
    private ShipmentDetailHistoryService shipmentDetailHistoryService;

    @Resource
    private InventoryDetailService inventoryDetailService;

    /**
     * 出库单下发
     *
     * @param shipmentDomain
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult shipment(ShipmentDomain shipmentDomain) {

        //1、判断出库主单和子单列是否为空
        ShipmentHeader shipmentHeader = shipmentDomain.getShipmentHeader();
        List<ShipmentDetail> shipmentDetails = shipmentDomain.getShipmentDetails();
        if (shipmentHeader == null) {
            return AjaxResult.error("出库主单为空");
        }
        if (shipmentDetails.isEmpty()) {
            return AjaxResult.error("出库子单为空");
        }
        String storageType = "";
        String saleArea = "";
        Customer customer = null;

        List<ShipmentHeader> shipmentHeaders = shipmentHeaderService.list(new LambdaQueryWrapper<ShipmentHeader>().eq(ShipmentHeader::getReferCode, shipmentHeader.getReferCode()));
        if (!shipmentHeaders.isEmpty()) {
            try {
                for (ShipmentHeader shipment : shipmentHeaders) {
                    BigDecimal totalQty = new BigDecimal(0);
                    for (ShipmentDetail item : shipmentDetails) {
                        totalQty = totalQty.add(item.getUnitId1Qty());
                    }
                    if (shipment.getLastStatus() <= QuantityConstant.SHIPMENT_HEADER_BUILD) {
                        shipment.setTotalLines(shipmentDetails.size());

                        shipment.setTotalQty(totalQty);
                        shipment.setReferCode(shipmentHeader.getReferCode());
                        shipment.setReferId(shipmentHeader.getReferId());
                        shipment.setShipmentType(shipmentHeader.getShipmentType());
                        shipment.setCustomerCode(shipmentHeader.getCustomerCode());
                        shipmentHeaderService.updateById(shipment);
                        LambdaQueryWrapper<ShipmentDetail> shipmentHeaderlLQM = new LambdaQueryWrapper<ShipmentDetail>().eq(ShipmentDetail::getShipmentId, shipment.getId());
                        shipmentDetailService.remove(shipmentHeaderlLQM);
                        shipmentHeader = shipmentHeaderService.getById(shipment);
                        AjaxResult<List<ShipmentDetail>> listAjaxResult = this.checkShipmentDetail(shipmentDetails, shipmentHeader);
                        if (listAjaxResult.getCode() == 400) {
                            throw new ServiceException(listAjaxResult.getMsg());
                        }
                        shipmentDetails = listAjaxResult.getData();
                    } else {
                        throw new ServiceException("出库单已被使用,不能覆盖了");
                    }
                }
            } catch (Exception e) {
                throw new ServiceException(e.getMessage());
            }
        } else {

            //2、检查出库主单的合法性
            this.checkShipmentHeader(shipmentHeader);

            //3、检查出库子单的合法性
//            shipmentDetails =this.checkShipmentDetail(shipmentDetails,shipmentHeader);
            AjaxResult<List<ShipmentDetail>> listAjaxResult = this.checkShipmentDetail(shipmentDetails, shipmentHeader);
            if (listAjaxResult.getCode() == 400) {
                throw new ServiceException(listAjaxResult.getMsg());
            }
            shipmentDetails = listAjaxResult.getData();

            //4、保存出库主表
            BigDecimal totalQty = new BigDecimal(0);
            for (ShipmentDetail item : shipmentDetails) {
                totalQty = totalQty.add(item.getUnitId1Qty());
            }
            shipmentHeader.setCreatedBy(QuantityConstant.PLATFORM_ERP);
            shipmentHeader.setTotalLines(shipmentDetails.size());
            shipmentHeader.setTotalQty(totalQty);
            String shipmentCode = shipmentHeaderService.createCode(shipmentHeader.getShipmentType());
            shipmentHeader.setCode(shipmentCode);
            shipmentHeader.setFirstStatus(QuantityConstant.SHIPMENT_HEADER_BUILD);
            shipmentHeader.setLastStatus(QuantityConstant.SHIPMENT_HEADER_BUILD);
            shipmentHeader.setCompanyCode("BHF");

            if ("XSCKD".equals(shipmentHeader.getShipmentType()) || "FBSTCK".equals(shipmentHeader.getShipmentType())) {
                if (StringUtils.isNotEmpty(shipmentHeader.getShipmentType())) {
                    customer = customerService.getOne(new LambdaQueryWrapper<Customer>().eq(Customer::getCode, shipmentHeader.getCustomerCode()));
                    if (StringUtils.isNull(customer)) {
                        throw new ServiceException("wms此客户不存在" + shipmentHeader.getCustomerCode()+",请先在ERP同步客户!");
                    }
                    saleArea = customer.getProvince() + "-" + customer.getCity() + "-" + customer.getAddress1() + "-" + customer.getAddress2();
                }
            }

            if (!shipmentHeaderService.save(shipmentHeader)) {
                throw new ServiceException("保存出库主表失败");
            }

            if (shipmentHeader.getAssistant() != null) {
                //销售出库单(报废)
                if ("006".equals(shipmentHeader.getAssistant())) {
                    storageType = "1";
                } else {
                    //销售出库单(标准)
                    storageType = "0";
                }
            }
        }


        //5、保存出库子表
        for (ShipmentDetail shipmentDetail : shipmentDetails) {
//                shipmentDetail.setCompanyCode(customer.getAllCreditCode());
//                shipmentDetail.setCompanyName(customer.getName());
            if (customer != null) {
                shipmentDetail.setAllCreditCode(customer.getAllCreditCode());
                shipmentDetail.setEchelonName(customer.getName());
//                    shipmentDetail.setCustomerName(customer.getName());
                shipmentDetail.setEchelonCode(customer.getCode());
            }
            shipmentDetail.setSaleArea(saleArea);
            shipmentDetail.setFentity(shipmentDetail.getReferId() + "");//体制码
            shipmentDetail.setInventorySts(QuantityConstant.GOOD);
            shipmentDetail.setShipmentId(shipmentHeader.getId());
            shipmentDetail.setShipQty(shipmentDetail.getUnitId1Qty());
            shipmentDetail.setShipmentCode(shipmentHeader.getCode());
            shipmentDetail.setUnitId1Qty(shipmentDetail.getUnitId1Qty());
            shipmentDetail.setUnitId2Qty(shipmentDetail.getUnitId2Qty());
            shipmentDetail.setUnitId1(shipmentDetail.getUnitId1());
            shipmentDetail.setUnitId2(shipmentDetail.getUnitId2());
            shipmentDetail.setCompanyCode("BHF");
            shipmentDetail.setShipmentType(shipmentHeader.getShipmentType());
            shipmentDetail.setWarehouseCode("CS0001");
            shipmentDetail.setAttribute1(shipmentHeader.getAssistant());
            shipmentDetail.setStorageType(storageType);
//            shipmentDetail.setBatteryPackTwoCode(shipmentDetail.getBatteryPackTwoCode());
//                shipmentDetail.setUserDef3("0");
        }
        int num = 0;
        List<ShipmentDetail> shipmentDetailList = new ArrayList<>();
        if (shipmentDetails.size() > 500) {
            for (ShipmentDetail item : shipmentDetails) {
                num++;
                shipmentDetailList.add(item);
                if (num % 500 == 0 || num == shipmentDetails.size()) {
                    if (!shipmentDetailService.saveBatch(shipmentDetailList)) {
                        throw new ServiceException("保存出库子表失败");
                    }
                    shipmentDetailList = new ArrayList<>();
                }
            }
        } else {
            for (ShipmentDetail item : shipmentDetails) {
                shipmentDetailList.add(item);
                if (!shipmentDetailService.saveBatch(shipmentDetailList)) {
                    throw new ServiceException("保存出库子表失败");
                }
                shipmentDetailList = new ArrayList<>();
            }
//                if(!shipmentDetailService.insertDetails(shipmentDetails)){
        }

        return AjaxResult.success("出库单下发成功");
    }


    /**
     * 检查出库主单的合法性
     *
     * @param shipmentHeader
     * @return
     */
    public AjaxResult checkShipmentHeader(ShipmentHeader shipmentHeader) {
        //1、判断仓库和货主
        if (StringUtils.isEmpty(shipmentHeader.getWarehouseCode())) {
            return AjaxResult.error("仓库为空");
        }
        if (StringUtils.isEmpty(shipmentHeader.getCompanyCode())) {
            return AjaxResult.error("货主为空");
        }


        LambdaQueryWrapper<Warehouse> warehouseLamb = Wrappers.lambdaQuery();
        warehouseLamb.eq(Warehouse::getCode, shipmentHeader.getWarehouseCode());
        Warehouse warehouse = warehouseService.getOne(warehouseLamb);
        if (warehouse == null) {
            return AjaxResult.error("wms没有此仓库");
        }

        LambdaQueryWrapper<Company> companyLamb = Wrappers.lambdaQuery();
        companyLamb.eq(Company::getCode, shipmentHeader.getCompanyCode());
        Company company = companyService.getOne(companyLamb);
        if (company == null) {
            return AjaxResult.error("wms没有此货主");
        }

        //2、判断出库类型
        LambdaQueryWrapper<ShipmentType> shipmentTypeLamb = Wrappers.lambdaQuery();
        shipmentTypeLamb.eq(ShipmentType::getCode, shipmentHeader.getShipmentType())
                .eq(ShipmentType::getWarehouseCode, shipmentHeader.getWarehouseCode());
        ShipmentType shipmentType = shipmentTypeService.getOne(shipmentTypeLamb);
        if (shipmentType == null) {
            return AjaxResult.error("wms没有此出库类型");
        }

        //3、检查上游单号是否存在
        LambdaQueryWrapper<ShipmentHeader> shipmentHeaderLamb = Wrappers.lambdaQuery();
        shipmentHeaderLamb.eq(ShipmentHeader::getWarehouseCode, shipmentHeader.getWarehouseCode())
                .eq(ShipmentHeader::getReferCode, shipmentHeader.getReferCode());
        List<ShipmentHeader> shipmentHeaders = shipmentHeaderService.list(shipmentHeaderLamb);
        if (shipmentHeaders.size() > 0 || shipmentHeaders != null) {
            return AjaxResult.success("出库单已经下发");
        }

        //4、检查客户
        if (StringUtils.isNotEmpty(shipmentHeader.getCustomerCode())) {
            LambdaQueryWrapper<Customer> customerLamb = Wrappers.lambdaQuery();
            customerLamb.eq(Customer::getWarehouseCode, shipmentHeader.getWarehouseCode())
                    .eq(Customer::getCode, shipmentHeader.getCustomerCode());
            Customer customer = customerService.getOne(customerLamb);
            if (customer == null) {
                return AjaxResult.error("wms没有此客户");
            }
        }
        return AjaxResult.success(shipmentHeader);
    }


    /**
     * 检查出库子单的合法性
     *
     * @param shipmentDetails
     * @return
     */
    public AjaxResult<List<ShipmentDetail>> checkShipmentDetail(List<ShipmentDetail> shipmentDetails, ShipmentHeader shipmentHeader) {

        for (ShipmentDetail shipmentDetail : shipmentDetails) {
            //1、检查物料
            if (StringUtils.isEmpty(shipmentDetail.getMaterialCode())) {
                throw new ServiceException("物料编码为空");
            }
            List<ShipmentDetail> a = null;
            LambdaQueryWrapper<Material> materialLamb = Wrappers.lambdaQuery();
            materialLamb.eq(Material::getCode, shipmentDetail.getMaterialCode()).eq(Material::getWarehouseCode, shipmentHeader.getWarehouseCode());
            Material material = materialService.getOne(materialLamb);
            if (material == null) {
                throw new ServiceException("wms没有此物料," + shipmentDetail.getMaterialCode());
            }
            if(StringUtils.isNotEmpty(shipmentDetail.getBatteryPackTwoCode())){
                LambdaQueryWrapper<InventoryDetail> inventoryLamb = Wrappers.lambdaQuery();
                List<String> batteryPackTwolist= Arrays.asList(Convert.toStrArray(shipmentDetail.getBatteryPackTwoCode()));
                int  count=batteryPackTwolist.size();
                inventoryLamb.in(InventoryDetail::getBatteryPackTwoCode, batteryPackTwolist);
                inventoryLamb.eq(InventoryDetail::getMaterialCode,shipmentDetail.getMaterialCode());
                List<InventoryDetail> inventorylist=inventoryDetailService.list(inventoryLamb);
                if(inventorylist==null||inventorylist.size()==0){
                    throw new ServiceException("wms没有此库存,电池包号" + shipmentDetail.getBatteryPackTwoCode()+",物料编号"+shipmentDetail.getMaterialCode());
                }
                if(count!=inventorylist.size()){
                    //筛选出没有库存的电池包
                    List<String> nolist=batteryPackTwolist.stream().filter(t->{for(InventoryDetail inventoryDetail:inventorylist){
                        if(inventoryDetail.getBatteryPackTwoCode().equals(t)){
                            return false;
                        }
                    }
                        return true;
                    }).collect(Collectors.toList());
                    throw new ServiceException("wms没有此库存,电池包号" + nolist.toString()+",物料编号"+shipmentDetail.getMaterialCode());
                }

//                InventoryDetail inventoryDetail=inventorylist.get(0);
//                if(inventoryDetail.getQty().compareTo(shipmentDetail.getShipQty()) < 0){
//                    return AjaxResult.error("wms库存不够,电池包号" + shipmentDetail.getBatteryPackTwoCode());
//                }
            }
//            if (new BigDecimal(0).compareTo(shipmentDetail.getUnitId2Qty() != null ?
//                    shipmentDetail.getUnitId2Qty() : new BigDecimal(0)) == 0) {
//                return AjaxResult.error("数量为空");
//            }

            //2、赋值物料属性
            shipmentDetail.setMaterialName(material.getName());
            shipmentDetail.setMaterialSpec(material.getSpec());
            shipmentDetail.setMaterialUnit(material.getUnit());
            shipmentDetail.setMaterialIsBattery(material.getIsBattery());
        }
        return AjaxResult.success(shipmentDetails);
    }

    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(List<String> shipmentCodeList) {
        LambdaQueryWrapper<ShipmentHeader> headerQueryWrapper;
        for (String shipmentCode : shipmentCodeList) {
            headerQueryWrapper = Wrappers.lambdaQuery();
            headerQueryWrapper.eq(ShipmentHeader::getCode, shipmentCode);
            ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(headerQueryWrapper);
            if (shipmentHeader == null) {
                return AjaxResult.success("");
            }
            if ((shipmentHeader.getFirstStatus() >= QuantityConstant.SHIPMENT_HEADER_RETURN && shipmentHeader.getLastStatus() >= QuantityConstant.SHIPMENT_HEADER_RETURN) ||
                    (shipmentHeader.getFirstStatus() < QuantityConstant.SHIPMENT_HEADER_POOL & shipmentHeader.getLastStatus() < QuantityConstant.SHIPMENT_HEADER_POOL)) {
                ShipmentHeaderHistory shipmentHeaderHistory = new ShipmentHeaderHistory();
                List<ShipmentDetailHistory> shipmentDetailHistoryList = new ArrayList<>();
                //查询入库单明细
                LambdaQueryWrapper<ShipmentDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
                lambdaQueryWrapper.eq(ShipmentDetail::getShipmentCode, shipmentCode);
                List<ShipmentDetail> list = shipmentDetailService.list(lambdaQueryWrapper);

                //复制到入库历史实体
                com.huaheng.common.utils.bean.BeanUtils.copyBeanProp(shipmentHeaderHistory, shipmentHeader);
                for (ShipmentDetail shipmentDetail : list) {
                    ShipmentDetailHistory shipmentDetailHistory = new ShipmentDetailHistory();
                    com.huaheng.common.utils.bean.BeanUtils.copyBeanProp(shipmentDetailHistory, shipmentDetail);
                    shipmentDetailHistoryList.add(shipmentDetailHistory);
                }


                shipmentHeaderHistory.setLastUpdatedBy(ShiroUtils.getLoginName());
                if (!shipmentHeaderService.removeById(shipmentHeader.getId())) {
                    return AjaxResult.error("删除头表失败");
                }
                if (!shipmentHeaderHistoryService.save(shipmentHeaderHistory)) {
                    return AjaxResult.error("新增历史出库单失败");
                }
                // 当存在明细时删除
                if (list.size() != 0) {
                    //删除入库明细
                    List<Integer> shipmentDetailIds = new ArrayList<>();
                    for (int i = 0; i < shipmentDetailHistoryList.size(); i++) {
                        shipmentDetailHistoryList.get(i).setLastUpdatedBy(ShiroUtils.getLoginName());
                        shipmentDetailHistoryList.get(i).setShipmentId(shipmentHeaderHistory.getId());
                        shipmentDetailIds.add(shipmentDetailHistoryList.get(i).getId());
                    }
                    if (!shipmentDetailService.removeByIds(shipmentDetailIds)) {
                        throw new ServiceException("删除明细表失败");
                    }
                    if (!shipmentDetailHistoryService.saveBatch(shipmentDetailHistoryList)) {
                        throw new ServiceException("新增明细失败");
                    }
                }
            } else {
                return AjaxResult.success("出库单没有完成,无法删除");
            }
        }
        return AjaxResult.success("删除成功");
    }

    public AjaxResult search(String shipmentCode, String companyCode, String warehouseCode) {
        if (companyCode == null) {
            return AjaxResult.error("货主编码不能为空");
        }
        if (warehouseCode == null) {
            return AjaxResult.error("仓库编码不能为空");
        }
        ShipmentDomain shipmentDomain = new ShipmentDomain();
        LambdaQueryWrapper<ShipmentHeader> headerLambdaQuery = Wrappers.lambdaQuery();
        LambdaQueryWrapper<ShipmentDetail> detailLambdaQuery = Wrappers.lambdaQuery();
        headerLambdaQuery.eq(ShipmentHeader::getWarehouseCode, warehouseCode)
                .eq(ShipmentHeader::getCompanyCode, companyCode)
                .eq(ShipmentHeader::getCode, shipmentCode);
        detailLambdaQuery.eq(ShipmentDetail::getWarehouseCode, warehouseCode)
                .eq(ShipmentDetail::getCompanyCode, companyCode)
                .eq(ShipmentDetail::getShipmentCode, shipmentCode);
        ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(headerLambdaQuery);
        List<ShipmentDetail> shipmentDetailList = shipmentDetailService.list(detailLambdaQuery);
        shipmentDomain.setShipmentHeader(shipmentHeader);
        shipmentDomain.setShipmentDetails(shipmentDetailList);
        return AjaxResult.success("查询成功", shipmentDomain);
    }
}