MesServiceImpl.java 21.3 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
package com.huaheng.api.mes.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.mes.domain.*;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.http.OkHttpUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.materialWarnning.domain.MaterialWarning;
import com.huaheng.pc.config.materialWarnning.service.IMaterialWarningService;
import com.huaheng.pc.config.warehouse.domain.Warehouse;
import com.huaheng.pc.config.warehouse.service.WarehouseService;
import com.huaheng.pc.config.zone.domain.Zone;
import com.huaheng.pc.config.zone.service.ZoneService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
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.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class MesServiceImpl implements MesService {

    @Resource
    private InventoryHeaderService inventoryHeaderService;
    @Resource
    private MaterialService materialService;
    @Resource
    private ZoneService zoneService;
    @Resource
    private LocationService locationService;
    @Resource
    private InventoryDetailService inventoryDetailService;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private ContainerService containerService;
    @Resource
    private IMaterialWarningService materialWarningService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ShipmentDetailService shipmentDetailService;
    @Resource
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private AddressService addressService;

    @Override
    public AjaxResult searchInventory(String materialCode, String area) {
        String zoneCode = null;
        BigDecimal totalQty = BigDecimal.ZERO;// 总数量
        BigDecimal goodQty = BigDecimal.ZERO;// 可用数量
        BigDecimal shipmentQty = BigDecimal.ZERO;// 出库单据数量
        BigDecimal receiptQty = BigDecimal.ZERO;// 入库单据数量
        if (!StringUtils.isEmpty(area)) {
            Zone zoneByArea = zoneService.getZoneByArea(area);
            zoneCode = zoneByArea.getCode();
        }
        String warehouseCode = QuantityConstant.DEFAULT_WAREHOUSE;
        List<InventoryDetail> inventoryDetails = inventoryDetailService.selectInventoryDetailByWarehouse(warehouseCode, materialCode, zoneCode);
        if (inventoryDetails == null) {
            inventoryDetails = Collections.emptyList();
        }
        List<ReceiptDetail> list =
            receiptDetailService.list(new LambdaQueryWrapper<ReceiptDetail>().lt(ReceiptDetail::getProcessStamp, QuantityConstant.RECEIPT_HEADER_POSTING));

        List<ShipmentDetail> shipmentDetails =
            shipmentDetailService.list(new LambdaQueryWrapper<ShipmentDetail>().le(ShipmentDetail::getStatus, QuantityConstant.SHIPMENT_HEADER_GROUPDISK));

        List<BigDecimal> collect = shipmentDetails.stream().map(item -> item.getQty().subtract(item.getTaskQty())).collect(Collectors.toList());

        List<BigDecimal> collect1 = list.stream().map(item -> item.getQty().subtract(item.getTaskQty())).collect(Collectors.toList());

        for (BigDecimal bigDecimal : collect1) {
            receiptQty = receiptQty.add(bigDecimal);
        }
        for (BigDecimal bigDecimal : collect) {
            shipmentQty = shipmentQty.add(bigDecimal);
        }
        List<BigDecimal> qtys = inventoryDetails.stream().map(InventoryDetail::getQty).collect(Collectors.toList());

        for (BigDecimal qty : qtys) {
            totalQty = totalQty.add(qty);
        }

        if (totalQty.compareTo(shipmentQty) > 0) {
            goodQty = totalQty.subtract(shipmentQty);
        }
        InventoryDomain domain = new InventoryDomain();
        domain.setInventoryDetails(inventoryDetails);
        domain.setTotalQty(totalQty);
        domain.setShipmentQty(shipmentQty);
        domain.setReceiptQty(receiptQty);
        domain.setGoodQty(goodQty);
        return AjaxResult.success(domain);
    }

    @Override
    public AjaxResult searchLocations(String area, String locationCode) {
        String warehouseCode = QuantityConstant.DEFAULT_WAREHOUSE;
        String type = null;
        if (!StringUtils.isEmpty(area)) {
            LambdaQueryWrapper<Zone> zoneLambdaQueryWrapper = Wrappers.lambdaQuery();
            zoneLambdaQueryWrapper.in(Zone::getArea, area);
            Zone zone = zoneService.getOne(zoneLambdaQueryWrapper);
            type = zone.getCode();
        }

        /* 查询库位信息 */
        AjaxResult ajaxResult = locationService.selectLocationByLocationTypeAndWarehouseCode(type, warehouseCode);
        if (ajaxResult.hasErr()) {
            return ajaxResult;
        }
        List<Location> locations = (List<Location>)ajaxResult.getData();
        List<Location> locationList = new ArrayList<>();
        /* 查询库存明细 */
        List<InventoryDetail> inventoryDetailList = inventoryDetailService.selectInventoryDetailByWarehouse(warehouseCode, null, "A");

        for (Location location1 : locations) {
            List<InventoryDetail> inventoryDetails =
                inventoryDetailList.stream().filter(inventoryDetail1 -> inventoryDetail1.getLocationCode().equals(location1.getCode())).collect(Collectors.toList());

            List<MaterialInfo> materialInfos = getMaterialInfo(inventoryDetails);
            int materialInfoSize = materialInfos.size();
            String containerCode = location1.getContainerCode();
            String status = location1.getStatus();

            if (QuantityConstant.STATUS_LOCATION_EMPTY.equals(status)) {
                if (StringUtils.isNotEmpty(containerCode)) {
                    if (materialInfoSize > 0) {
                        location1.setContainerStatus(QuantityConstant.STATUS_CONTAINER_SOME);
                    } else if (materialInfoSize == 0) {
                        location1.setContainerStatus(QuantityConstant.STATUS_CONTAINER_EMPTY);
                    }
                }
            }
            if (QuantityConstant.STATUS_LOCATION_LOCK.equals(status)) {
                if (StringUtils.isNotEmpty(containerCode)) {
                    location1.setContainerStatus(QuantityConstant.STATUS_CONTAINER_LOCK);
                }
            }
            location1.setMaterialInfos(materialInfos);
            locationList.add(location1);
        }

        return AjaxResult.success(locationList);
    }

    @Override
    public AjaxResult searchWarehouse() {
        Warehouse one = warehouseService.getOne(new LambdaQueryWrapper<Warehouse>().eq(Warehouse::getCode, QuantityConstant.DEFAULT_WAREHOUSE));
        return AjaxResult.success(one);
    }

    @Override
    public AjaxResult searchContainer() {
        List<Container> list = containerService.list();
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult searchMaterialWarning() {
        List<MaterialWarning> list = materialWarningService.list();
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult searchZone() {
        List<Zone> list = zoneService.list();
        return AjaxResult.success(list);
    }

    @Override
    @Transactional
    public AjaxResult back(Integer id) {
        if (StringUtils.isEmpty(id + "")) {
            return AjaxResult.error("任务id不存在");
        }
        TaskHeader taskHeader = taskHeaderService.getById(id);
        Integer taskType = taskHeader.getInternalTaskType();
        if (taskHeader == null) {
            return AjaxResult.error("id为:【" + id + "】任务不存在");
        }
        List<TaskDetail> taskDetails = taskDetailService.findByTaskId(id);
        if (taskHeader.getStatus() != QuantityConstant.TASK_STATUS_COMPLETED) {
            return AjaxResult.error("id为:【" + id + "】任务没有完成,不可回传。");
        }
        AjaxResult ajaxResult = null;
        switch (taskType) {
            case 100:
                ajaxResult = receiptBack(taskHeader, taskDetails);
                break;
            case 200:
                ajaxResult = shipmentBack(taskHeader, taskDetails);
                break;
            default:
                return AjaxResult.success();
        }

        return ajaxResult;
    }

    @Override
    public AjaxResult returnQuery(String referCode, String referType) {
        if (Integer.valueOf(referType)==QuantityConstant.TASK_TYPE_WHOLERECEIPT) {
            LambdaQueryWrapper<ReceiptHeader> receiptHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            receiptHeaderLambdaQueryWrapper.eq(ReceiptHeader::getReferCode, referCode);
            ReceiptHeader receiptHeader = receiptHeaderService.getOne(receiptHeaderLambdaQueryWrapper);
            if (receiptHeader == null) {
                return AjaxResult.error("入库单不存在");
            }

            LambdaQueryWrapper<ReceiptDetail> receiptDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
            receiptDetailLambdaQueryWrapper.eq(ReceiptDetail::getReceiptId, receiptHeader.getId());
            List<ReceiptDetail> receiptDetails = receiptDetailService.list(receiptDetailLambdaQueryWrapper);
            if (receiptDetails.size() < 1) {
                return AjaxResult.error("入库单明细不存在");
            }

            ReturnDome returnDome = new ReturnDome();
            returnDome.setFBillNo(receiptHeader.getReferCode());
            returnDome.setFDocumentStatus(receiptHeader.getLastStatus());
            List<Material> materials = new ArrayList<>();
            for (ReceiptDetail receiptDetail : receiptDetails) {
                Material material = new Material();
                material.setMaterialId(receiptDetail.getMaterialCode());
                material.setFMaterialName(receiptDetail.getMaterialName());
                if (StringUtils.isNotEmpty(receiptDetail.getMaterialSpec()))
                {
                material.setFUOM(receiptDetail.getMaterialSpec());
                }
                material.setFMaterialType(receiptDetail.getMaterialUnit());
                material.setFUnitID(receiptDetail.getMaterialUnit());
                material.setFMustQty(receiptDetail.getQty());
                material.setFRealQty(receiptDetail.getTaskQty());
                material.setFLot(receiptDetail.getBatch());
                material.setFStockId(receiptDetail.getWarehouseCode());
                material.setFStockLocId("暂时这里没这个值,已经出完的要到历史库存里面,入库了到库存查,非必要先不传影响速率");
                material.setFStockStatusId(receiptDetail.getInventorySts());
                materials.add(material);
            }
            returnDome.setListMaterial(materials);
            String jsonParam = JSON.toJSONString(returnDome);
            return AjaxResult.success(jsonParam);
        } else if (Integer.valueOf(referType)==QuantityConstant.TASK_TYPE_WHOLESHIPMENT)
        {
            LambdaQueryWrapper<ShipmentHeader> shipmentHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            shipmentHeaderLambdaQueryWrapper.eq(ShipmentHeader::getReferCode, referCode);
            ShipmentHeader shipmentHeader = shipmentHeaderService.getOne(shipmentHeaderLambdaQueryWrapper);
            if (shipmentHeader == null) {
                return AjaxResult.error("出库单不存在");
            }

            LambdaQueryWrapper<ShipmentDetail> shipmentDetailLambdaQueryWrapper = Wrappers.lambdaQuery();
            shipmentDetailLambdaQueryWrapper.eq(ShipmentDetail::getShipmentId, shipmentHeader.getId());
            List<ShipmentDetail> shipmentDetails = shipmentDetailService.list(shipmentDetailLambdaQueryWrapper);
            if (shipmentDetails.size() < 1) {
                return AjaxResult.error("出库单明细不存在");
            }

            ReturnDome returnDome = new ReturnDome();
            returnDome.setFBillNo(shipmentHeader.getReferCode());
            returnDome.setFDocumentStatus(shipmentHeader.getLastStatus());
            List<Material> materials = new ArrayList<>();
            for (ShipmentDetail shipmentDetail : shipmentDetails) {
                Material material = new Material();
                material.setMaterialId(shipmentDetail.getMaterialCode());
                material.setFMaterialName(shipmentDetail.getMaterialName());
                if (StringUtils.isNotEmpty(shipmentDetail.getMaterialSpec())) {
                    material.setFUOM(shipmentDetail.getMaterialSpec());
                }
                material.setFMaterialType(shipmentDetail.getMaterialUnit());
                material.setFUnitID(shipmentDetail.getMaterialUnit());
                material.setFMustQty(shipmentDetail.getQty());
                material.setFRealQty(shipmentDetail.getTaskQty());
                material.setFLot(shipmentDetail.getBatch());
                material.setFStockId(shipmentDetail.getWarehouseCode());
                material.setFStockLocId("暂时这里没这个值,已经出完的要到历史库存里面,入库了到库存查,非必要先不传影响速率");
                material.setFStockStatusId(shipmentDetail.getInventorySts());
                materials.add(material);
            }
            returnDome.setListMaterial(materials);
            String jsonParam = JSON.toJSONString(returnDome);
            return AjaxResult.success(jsonParam);
        }
        return AjaxResult.error("单据类型有问题");
    }

    private AjaxResult receiptBack(TaskHeader taskHeader, List<TaskDetail> taskDetails) {
        String referCode = "";
        Integer receiptHeaderId = 0;

        List<PositionEntity> positionList = new ArrayList<>();
        List<ReceiptDetail> updates = new ArrayList<>();
        for (TaskDetail taskDetail : taskDetails) {
            PositionEntity entity = new PositionEntity();
            ReceiptDetail receiptDetail = receiptDetailService.getById(taskDetail.getBillDetailId());
            entity.setDetailId(receiptDetail.getReferId());
            entity.setMaterialCode(receiptDetail.getMaterialCode());
            entity.setTotalQty(taskDetail.getQty());
            // 累加回传明细
            receiptDetail.setReturnedQty(receiptDetail.getReturnedQty().add(taskDetail.getQty()));
            referCode = receiptDetail.getReferCode();
            receiptHeaderId = receiptDetail.getReceiptId();
            updates.add(receiptDetail);
            positionList.add(entity);
        }
        MESBackResult mesBackResult = new MESBackResult();
        mesBackResult.setPositionList(positionList);
        mesBackResult.setReferCode(referCode);
        // 判断整个入库单是否完成
        boolean noCompletedQty = receiptDetailService.isNoCompletedQty(updates, receiptHeaderId);
        if (noCompletedQty) {
            mesBackResult.setReferState(QuantityConstant.MES_STATUS_COMPLETE);
        } else {
            mesBackResult.setReferState(QuantityConstant.MES_STATUS_UN_COMPLETE);
        }

        String url = addressService.selectAddress(QuantityConstant.MES_BACK_ADDRESS, QuantityConstant.DEFAULT_WAREHOUSE, "");

        String bodypost = OkHttpUtils.bodypost(url, JSON.toJSONString(mesBackResult));
        if (bodypost == null || "".equals(bodypost)) {
            taskHeader.setBack(-1);
            taskHeaderService.updateById(taskHeader);
            return AjaxResult.error("WMS请求发送成功,接收返回内容是空的");
        }
        AjaxResult ajaxResult = JSON.parseObject(bodypost, AjaxResult.class);
        if (ajaxResult.hasErr()) {
            taskHeader.setBack(-1);
            taskHeaderService.updateById(taskHeader);
            return AjaxResult.error(ajaxResult.getMsg());
        }
        receiptDetailService.saveOrUpdateBatch(updates);
        if (noCompletedQty) {
            ReceiptHeader receiptHeader = receiptHeaderService.getById(receiptHeaderId);
            receiptHeader.setFirstStatus(900);
            receiptHeader.setLastStatus(900);
            receiptHeader.setClosed(1);
            receiptHeaderService.updateById(receiptHeader);
        }
        taskHeader.setBack(1);
        taskHeaderService.updateById(taskHeader);
        return AjaxResult.success();
    }

    private AjaxResult shipmentBack(TaskHeader taskHeader, List<TaskDetail> taskDetails) {
        String referCode = "";
        Integer receiptHeaderId = 0;

        List<PositionEntity> positionList = new ArrayList<>();
        List<ShipmentDetail> updates = new ArrayList<>();
        for (TaskDetail taskDetail : taskDetails) {
            PositionEntity entity = new PositionEntity();
            ShipmentDetail shipmentDetail = shipmentDetailService.getById(taskDetail.getBillDetailId());
            entity.setDetailId(shipmentDetail.getReferId());
            entity.setMaterialCode(shipmentDetail.getMaterialCode());
            entity.setTotalQty(taskDetail.getQty());
            // 累加回传明细
            shipmentDetail.setReturnedQty(shipmentDetail.getReturnedQty().add(taskDetail.getQty()));
            referCode = shipmentDetail.getReferCode();
            receiptHeaderId = shipmentDetail.getShipmentId();
            updates.add(shipmentDetail);
            positionList.add(entity);
        }
        MESBackResult mesBackResult = new MESBackResult();
        mesBackResult.setPositionList(positionList);
        mesBackResult.setReferCode(referCode);
        // 判断整个入库单是否完成
        boolean noCompletedQty = shipmentDetailService.isNoCompletedQty(updates, receiptHeaderId);
        if (noCompletedQty) {
            mesBackResult.setReferState(QuantityConstant.MES_STATUS_COMPLETE);
        } else {
            mesBackResult.setReferState(QuantityConstant.MES_STATUS_UN_COMPLETE);
        }

        String url = addressService.selectAddress(QuantityConstant.MES_BACK_ADDRESS, QuantityConstant.DEFAULT_WAREHOUSE, "");

        String bodypost = OkHttpUtils.bodypost(url, JSON.toJSONString(mesBackResult));
        if (bodypost == null || "".equals(bodypost)) {
            taskHeader.setBack(-1);
            taskHeaderService.updateById(taskHeader);
            return AjaxResult.error("WMS请求发送成功,接收返回内容是空的");
        }
        AjaxResult ajaxResult = JSON.parseObject(bodypost, AjaxResult.class);
        if (ajaxResult.hasErr()) {
            taskHeader.setBack(-1);
            taskHeaderService.updateById(taskHeader);
            return AjaxResult.error(ajaxResult.getMsg());
        }
        shipmentDetailService.saveOrUpdateBatch(updates);
        if (noCompletedQty) {
            ShipmentHeader shipmentHeader = shipmentHeaderService.getById(receiptHeaderId);
            shipmentHeader.setFirstStatus(900);
            shipmentHeader.setLastStatus(900);
            shipmentHeader.setClosed(1);
            shipmentHeaderService.updateById(shipmentHeader);
        }
        taskHeader.setBack(1);
        taskHeaderService.updateById(taskHeader);
        return AjaxResult.success();
    }

    private List<MaterialInfo> getMaterialInfo(List<InventoryDetail> inventoryDetails) {
        List<MaterialInfo> materialInfos = new ArrayList<>();
        if (inventoryDetails == null || inventoryDetails.size() == 0) {
            return materialInfos;
        }
        for (InventoryDetail inventoryDetail : inventoryDetails) {
            MaterialInfo info = new MaterialInfo();
            info.setMaterialCode(inventoryDetail.getMaterialCode());
            info.setMaterialName(inventoryDetail.getMaterialName());
            info.setMaterialUnit(inventoryDetail.getMaterialUnit());
            info.setQty(inventoryDetail.getQty());
            info.setBatch(inventoryDetail.getBatch());
            info.setTaskQty(inventoryDetail.getTaskQty());
            materialInfos.add(info);
        }
        return materialInfos;
    }
}