ZarshService.java 20.2 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 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
package com.huaheng.pc.sap.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.api.sap.domain.ZarDomain;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.bean.BeanUtils;
import com.huaheng.framework.web.domain.AjaxResult;
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.zone.service.ZoneService;
import com.huaheng.pc.monitor.apilog.domain.ApiLog;
import com.huaheng.pc.monitor.apilog.service.IApiLogService;
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.sap.domain.Zarsh;
import com.huaheng.pc.sap.domain.Zarsi;
import com.huaheng.pc.sap.mapper.ZarshMapper;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
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.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import com.huaheng.pc.task.taskHeader.service.WorkTaskService;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Enzo Cotter on 2022/5/11.
 *
 * @author zhouhong
 */
@Service
public class ZarshService extends ServiceImpl<ZarshMapper, Zarsh> {
    @Resource
    private ZarsiService zarsiService;
    @Resource
    private ReceiptContainerHeaderService receiptContainerHeaderService;
    @Resource
    private ShipmentContainerHeaderService shipmentContainerHeaderService;
    @Resource
    private ReceiptHeaderService receiptHeaderService;
    @Resource
    private ShipmentHeaderService shipmentHeaderService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private ContainerService containerService;
    @Resource
    private LocationService locationService;
    @Resource
    private IApiLogService apiLogService;
    @Autowired
    private BackSapStatusService backSapStatusService;
    @Autowired
    private ZoneService zoneService;
    @Resource
    private WorkTaskService workTaskService;

    public boolean saveZarsh(Zarsh zarsh) {
        Date date = new Date();
        String uniqueId = UUID.randomUUID().toString();
        zarsh.setUniqueId(uniqueId);
        zarsh.setCreated(date);
        zarsh.setCreateBy("wms");
        zarsh.setLastUpdatedBy("wms");
        zarsh.setLastUpdated(date);
        return this.save(zarsh);
    }

    public boolean editZarsh(Zarsh zarsh) {
        Date date = new Date();
        zarsh.setLastUpdatedBy("wms");
        zarsh.setLastUpdated(date);
        return this.updateById(zarsh);
    }

    /**
     * 生成任务保存同步表
     *
     * @param task
     * @param taskDetailList
     * @param container
     */
    public void saveZarshByTask(TaskHeader task, List<TaskDetail> taskDetailList, Container container) {
        Zarsh zarsh = saveZarshTask(task, taskDetailList, container);
        //保存子表
        saveZarsiTask(taskDetailList, zarsh);
    }

    public Zarsh saveZarshTask(TaskHeader task, List<TaskDetail> taskDetailList, Container container) {
        Zarsh zarsh = new Zarsh();
        zarsh.setMandt("2");
        zarsh.setWerks("3");
        zarsh.setDrunId(task.getContainerCode());
        if (container.getContainerType().equals("D1") ||
                container.getContainerType().equals("E1")) {
            //片状托架
            zarsh.setPlType(1);
        } else if (container.getContainerType().equals("D2") ||
                container.getContainerType().equals("E2") ||
                container.getContainerType().equals("D3") ||
                container.getContainerType().equals("E3")) {
            //卷状托架
            zarsh.setPlType(0);
        } else {
            //特殊托架
            zarsh.setPlType(2);
        }

        if (taskDetailList != null && taskDetailList.size() > 0) {
            //有料
            zarsh.setInKind(1);
        } else {
            //无料
            zarsh.setInKind(2);
        }
        zarsh.setInitDate(new Date());
        zarsh.setCFlag("R");
        int taskType = task.getTaskType();
        if (taskType == QuantityConstant.TASK_TYPE_WHOLERECEIPT ||
                taskType == QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT ||
                taskType == QuantityConstant.TASK_TYPE_EMPTYRECEIPT ||
                taskType == QuantityConstant.TASK_TYPE_MANY_EMPTYRECEIPT) {
            //入库
            zarsh.setMFlag(2);
            zarsh.setFromPos(task.getPort());
            if (StringUtils.isNotEmpty(task.getFromLocation())) {
                zarsh.setLocation(task.getFromLocation());
            } else {
                zarsh.setLocation(task.getToLocation());
            }
        } else {
            //出库
            zarsh.setMFlag(3);
            zarsh.setToPos(task.getPort());
            if (StringUtils.isNotEmpty(task.getFromLocation())) {
                zarsh.setLocation(task.getFromLocation());
            } else {
                zarsh.setLocation(task.getToLocation());
            }
        }
        zarsh.setTaskId(task.getId());
        if (saveZarsh(zarsh)) {
            return zarsh;
        } else {
            throw new ServiceException("zarsh保存失败");
        }
    }

    public void saveZarsiTask(List<TaskDetail> taskDetailList, Zarsh zarsh) {
        List<Zarsi> zarsilist = new ArrayList<>();
        for (TaskDetail taskDetail : taskDetailList) {
            Zarsi zarsi = new Zarsi();
            BeanUtils.copyBeanProp(zarsi, zarsh);
            zarsi.setId(null);
            zarsi.setMantr(taskDetail.getMaterialCode());
            zarsi.setVerme(taskDetail.getQty());
            //批号
            zarsi.setCharg(taskDetail.getLot());
            //项目号(板台上由多个位置得具体位置)
            zarsi.setPosnr(taskDetail.getContainerDetailNumber());
            zarsilist.add(zarsi);
        }
        if (zarsiService.saveBatch(zarsilist)) {
        } else {
            throw new ServiceException("zarsi保存失败");
        }
    }

    /**
     * TaskHeader task 修改状态
     *
     * @param task
     */
    public void updateZarshByTaskComplete(TaskHeader task, String cFlag) {
        int taskType = task.getTaskType();
        String location = null;
        LambdaQueryWrapper<Zarsh> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if (taskType == QuantityConstant.TASK_TYPE_WHOLERECEIPT ||
                taskType == QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT ||
                taskType == QuantityConstant.TASK_TYPE_EMPTYRECEIPT ||
                taskType == QuantityConstant.TASK_TYPE_MANY_EMPTYRECEIPT) {
            //入库
            location = task.getToLocation();
            lambdaQueryWrapper.eq(Zarsh::getMFlag, 2);
        } else {
            location = task.getFromLocation();
            lambdaQueryWrapper.eq(Zarsh::getMFlag, 3);
        }

        /*lambdaQueryWrapper.eq(Zarsh::getDrunId, task.getContainerCode())
                .eq(Zarsh::getLocation,location);*/
        lambdaQueryWrapper.eq(Zarsh::getTaskId, task.getId());
        lambdaQueryWrapper.orderByDesc(Zarsh::getCFlag);
        //.eq(Zarsh::getCFlag, "R");
        List<Zarsh> list = this.list(lambdaQueryWrapper);
        if (null != list && list.size() > 0) {
            Zarsh zarsh = list.get(0);
            zarsh.setCFlag(cFlag);
            zarsh.setLocation(task.getToLocation());
            zarsh.setFromPos(task.getPort());
            zarsh.setToPos(task.getPort());
            zarsh.setTaskId(task.getId());
            zarsh.setLastUpdatedBy("wms");
            zarsh.setLastUpdated(new Date());
            this.updateById(zarsh);
        } else {
            //throw new ServiceException("未找到Zarsh对象");
        }
    }

    /**
     * RyTask扫描同步表保存逻辑
     *
     * @param zarsh
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult saveDataBySap(Zarsh zarsh) {
        //用主表UniqueId查找子表zarsi信息
        LambdaQueryWrapper<Zarsi> zarsilambdaQueryWrapper = Wrappers.lambdaQuery();
        zarsilambdaQueryWrapper.eq(Zarsi::getUniqueId, zarsh.getUniqueId());
        List<Zarsi> zarsiList = zarsiService.list(zarsilambdaQueryWrapper);
        //检测数据为空情况
//        checkDataBySap(zarsh, zarsiList);
        //判断出库、入库,然后在判断是否空托盘出入库
        if(StringUtils.isEmpty(zarsh.getLgnum())){
            throw new ServiceException("仓库号为空");
        }
        String zoneCode=zoneService.getZoneCodeByNick(zarsh.getLgnum());
        if(StringUtils.isEmpty(zoneCode)){
            throw new ServiceException("找不到对应的仓库");
        }
        zarsh.setLgnum(zoneCode);
        TaskHeader taskHeader = null;
        switch (zarsh.getMFlag()) {
            //整盘入库、补充入库
            case 2:
            case 8:
                //入库
                if(StringUtils.isEmpty(zarsh.getFromPos())){
                    throw new ServiceException("入库的站台为空");
                }
                taskHeader = receiptBySap(zarsiList, zarsh);
                break;
            case 1:
            case 3:
                //出库
                if(StringUtils.isEmpty(zarsh.getToPos())){
                    throw new ServiceException("出库的站台为空");
                }
                taskHeader = shipmentBySap(zarsiList, zarsh);
                break;
            default:
                throw new ServiceException("MFlag不支持的业务类型");
        }
        /*if(taskHeader!=null){
            //修改同步表C_FLAG命令状态
            this.updateZarshByTaskComplete(taskHeader,"R");
        }*/
        return AjaxResult.success("", taskHeader);
    }

    /**
     * RyTask扫描同步表保存逻辑
     *
     * @param zarDomain
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult saveSapDataByApi(ZarDomain zarDomain) {
        //用主表UniqueId查找子表zarsi信息

        List<Zarsi> zarsiList = zarDomain.getZarsiList();
        Zarsh zarsh=zarDomain.getZarsh();

        //判断出库、入库,然后在判断是否空托盘出入库
        if(StringUtils.isEmpty(zarsh.getLgnum())){
            throw new ServiceException("仓库号为空");
        }
        String zoneCode=zoneService.getZoneCodeByNick(zarsh.getLgnum());
        if(StringUtils.isEmpty(zoneCode)){
            throw new ServiceException("找不到对应的仓库");
        }
        if(StringUtils.isNull(zarsh.getMFlag())){
            throw new ServiceException("入库出库标志为空");
        }
        zarsh.setLgnum(zoneCode);
        TaskHeader taskHeader = null;
        switch (zarsh.getMFlag()) {
            //整盘入库、补充入库
            case 2:
            case 8:
                //入库
                if(StringUtils.isEmpty(zarsh.getFromPos())){
                    throw new ServiceException("入库的站台为空");
                }
                taskHeader = receiptBySap(zarsiList, zarsh);
                break;
            case 1:
            case 3:
                //出库
                if(StringUtils.isEmpty(zarsh.getToPos())){
                    throw new ServiceException("出库的站台为空");
                }
                taskHeader = shipmentBySap(zarsiList, zarsh);
                break;
            default:
                throw new ServiceException("MFlag不支持的业务类型");
        }
        return AjaxResult.success("", taskHeader);
    }

    //入库
    public TaskHeader receiptBySap(List<Zarsi> zarsiList, Zarsh zarsh) {
        TaskHeader taskHeader = null;
        //空托盘入库
        if (zarsiList == null || zarsiList.size() == 0) {
            //检测空容器状态
            checkEmptyContainerBySap(zarsh.getDrunId(), zarsh.getLocation());
            //创建任务表数据
            String area=zoneService.getZoneAreaByCode(zarsh.getLgnum());
            AjaxResult ajaxResult_taskheader =workTaskService.createEmptyIn(zarsh.getDrunId(), zarsh.getLocation(),area,zarsh.getFromPos());
            /*AjaxResult ajaxResult_taskheader = taskHeaderService.saveTaskBySap(null, null, zarsh, zarsiList, true);
             */
            if (ajaxResult_taskheader != null && ajaxResult_taskheader.hasErr()) {
                throw new ServiceException(ajaxResult_taskheader.getMsg());
            }
            Integer taskHeaderId = (Integer) ajaxResult_taskheader.getData();
            taskHeader=taskHeaderService.getById(taskHeaderId);
            taskHeader.setUniqueIds(zarsh.getUniqueId());
            taskHeaderService.updateById(taskHeader);
            //调用sap的api,传递任务状态
            backSapStatusService.saveBackSapStatus(zarsh.getUniqueId(), zarsh.getLgnum(), null, 1, "1", null,null);
            return taskHeader;
        }

        //保存入库信息
        ReceiptHeader receiptHeader = null;
        AjaxResult ajaxResult_receiptheader = receiptHeaderService.saveReceiptBySap(zarsh, zarsiList);
        if (ajaxResult_receiptheader != null && ajaxResult_receiptheader.hasErr()) {
            throw new ServiceException(ajaxResult_receiptheader.getMsg());
        }
        receiptHeader = (ReceiptHeader) ajaxResult_receiptheader.getData();
        //调用sap的api,传递任务状态
        backSapStatusService.saveBackSapStatus(zarsh.getUniqueId(), zarsh.getLgnum(), null, 1, "1", null,null);
        //判断有没有托盘号,有则生成组盘
        /*if(StringUtils.isNotEmpty(zarsh.getDrunId())){
            //保存组盘信息
            ReceiptContainerHeader receiptContainerHeader=null;
            AjaxResult ajaxResult_receiptcontainerH=receiptContainerHeaderService.saveReceiptContainerBySap(receiptHeader,zarsh,zarsiList);
            if (ajaxResult_receiptcontainerH != null && ajaxResult_receiptcontainerH.hasErr()) {
                throw new ServiceException(ajaxResult_receiptcontainerH.getMsg());
            }
            receiptContainerHeader=(ReceiptContainerHeader)ajaxResult_receiptcontainerH.getData();
            //创建任务表数据
            AjaxResult ajaxResult_taskheader=taskHeaderService.saveTaskBySap(receiptContainerHeader,null,zarsh,zarsiList,false);
            if (ajaxResult_taskheader != null && ajaxResult_taskheader.hasErr()) {
                throw new ServiceException(ajaxResult_taskheader.getMsg());
            }
            taskHeader=(TaskHeader) ajaxResult_taskheader.getData();
        }*/

        return taskHeader;
    }

    //出库
    public TaskHeader shipmentBySap(List<Zarsi> zarsiList, Zarsh zarsh) {
        TaskHeader taskHeader = null;
        //空托盘出库
        if (zarsiList == null || zarsiList.size() == 0) {
            //创建任务表数据
            checkEmptyContainerBySap(zarsh.getDrunId(), zarsh.getLocation());

            AjaxResult ajaxResult_taskheader =workTaskService.createEmptyOut(zarsh.getDrunId(), zarsh.getLocation());
            if (ajaxResult_taskheader != null && ajaxResult_taskheader.hasErr()) {
                throw new ServiceException(ajaxResult_taskheader.getMsg());
            }
            taskHeader = (TaskHeader) ajaxResult_taskheader.getData();
            //调用sap的api,传递任务状态
            backSapStatusService.saveBackSapStatus(zarsh.getUniqueId(), zarsh.getLgnum(), null, 2, "1", null,null);
            return taskHeader;
        }
        //保存出库信息
        ShipmentHeader shipmentHeader = null;
        AjaxResult ajaxResult = shipmentHeaderService.saveShipmentBySap(zarsh, zarsiList);
        if (ajaxResult != null && ajaxResult.hasErr()) {
            throw new ServiceException(ajaxResult.getMsg());
        }
        shipmentHeader = (ShipmentHeader) ajaxResult.getData();
        //调用sap的api,传递任务状态
        backSapStatusService.saveBackSapStatus(zarsh.getUniqueId(), zarsh.getLgnum(), null, 2, "1", null,null);
        //判断有没有库位号,有则生成组盘+任务
        if(StringUtils.isNotEmpty(zarsh.getLocation())){
            //保存组盘信息
            ShipmentContainerHeader shipmentContainerHeader=null;
            AjaxResult ajaxResult_shipCH=shipmentContainerHeaderService.saveShipmentContainerBySap(shipmentHeader,zarsh,zarsiList);
            if (ajaxResult_shipCH != null && ajaxResult_shipCH.hasErr()) {
                throw new ServiceException(ajaxResult_shipCH.getMsg());
            }
            shipmentContainerHeader=(ShipmentContainerHeader) ajaxResult_shipCH.getData();
            //创建任务表数据
            AjaxResult ajaxResult_taskheader=taskHeaderService.saveTaskBySapLK(shipmentContainerHeader);

        }
        return taskHeader;
    }

    //定时任务扫描时,检查数据是否合规,必填字段是否都有,保存进zarsh表
    public void checkDataBySap(Zarsh zarsh, List<Zarsi> zarsiList) {
        Assert.notNull(zarsh.getInKind(), "货物总类不能为空");
        if (StringUtils.isEmpty(zarsh.getCFlag()) ||
                StringUtils.isEmpty(zarsh.getDrunId()) ||
                StringUtils.isEmpty(zarsh.getLocation())) {
            throw new ServiceException("数据id:" + zarsh.getId() + ",命令状态,容器号,库位不能为空");
        }
        for (Zarsi zarsi : zarsiList) {
            if (StringUtils.isEmpty(zarsi.getMantr()) ||
                    StringUtils.isNull(zarsi.getVerme())) {
                throw new ServiceException("数据id:" + zarsi.getId() + ",物料、库存不能为空");
            }
        }
    }

    //空托盘入库出库检测容器和库位是否空闲
    public void checkEmptyContainerBySap(String containerCode, String locationCode) {
        /*if (StringUtils.isEmpty(containerCode)) {
            throw new ServiceException("容器号不存在");
        }*/
        if(StringUtils.isNotEmpty(containerCode)){
            Container container = containerService.getContainerByCode(containerCode, QuantityConstant.WAREHOUSECODE);
            if (StringUtils.isNull(container)) {
                throw new ServiceException("容器不存在");
            }
            if (StringUtils.isNotEmpty(container.getLocationCode())) {
                throw new ServiceException("容器已在库位" + container.getLocationCode() + "上");
            }
        }
        if(StringUtils.isNotEmpty(locationCode)){
            Location location = locationService.getLocationByCode(locationCode, QuantityConstant.WAREHOUSECODE);
            if (StringUtils.isNull(location)) {
                throw new ServiceException("目标库位不存在");
            }
            if (StringUtils.isNotEmpty(location.getContainerCode())) {
                throw new ServiceException(locationCode + "上已存在容器" + location.getContainerCode());
            }
            if (!QuantityConstant.STATUS_LOCATION_EMPTY.equals(location.getStatus())) {
                throw new ServiceException("目标库位非空闲");
            }
        }

    }


    public void saveApiLog(String url, String requestBody, String responseBody) {
        ApiLog apiLog = new ApiLog();
        apiLog.setApiName("审核回传ERP");
        apiLog.setApiMethod("POST");
        apiLog.setRequestFrom("WMS");
        apiLog.setResponseBy("ERP");
        apiLog.setUrl(url);
        apiLog.setRequestTime(new Date());
        apiLog.setResponseTime(new Date());
        apiLog.setRequestBody(requestBody);
        apiLog.setResponseBody(responseBody);
        apiLogService.save(apiLog);
    }
}