自动化立体仓库 - WCS系统
野心家
2024-09-04 bfc2fee36897a0e6ca4d77701cf4d8f6a7281aec
src/main/java/com/zy/asrs/service/impl/WorkServiceImpl.java
@@ -1,10 +1,23 @@
package com.zy.asrs.service.impl;
import com.alibaba.fastjson.JSON;
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.LocMast;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.service.*;
import com.zy.common.utils.HttpHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
public class WorkServiceImpl implements WorkService {
@@ -20,146 +33,179 @@
    private WrkDetlLogService wrkDetlLogService;
    @Autowired
    private WrkDetlService wrkDetlService;
    @Autowired
    private ApiLogService apiLogService;
    @Value("${wms.url}")
    private String wmsUrl;
    @Value("${wms.inboundTaskApplyPath}")
    private String inboundTaskApplyPath;
    @Value("${wms.movePath}")
    private String movePath;
    @Value("${wms.taskStatusFeedbackPath}")
    private String taskStatusFeedbackPath;
    @Override
    @Transactional
    public void completeWrkMast(String workNo, Long userId) {
//        WrkMast wrkMast = wrkMastService.selectById(workNo);
//        WmsWrk wmsWrk = wmsWrkService.selectByTaskNo(wrkMast.getWmsWrkNo());
//        if (Cools.isEmpty(wrkMast, wmsWrk)) {
//            throw new CoolException(workNo + "任务不存在");
//        }
//        if (wrkMast.getWrkSts() == 4 || wrkMast.getWrkSts() == 14) {
//            throw new CoolException("当前任务已完成");
//        }
//        // 入库 + 库位转移
//        if (wrkMast.getWrkSts() < 4 || (wrkMast.getWrkSts() > 10 && wrkMast.getIoType()==11)) {
//            wrkMast.setWrkSts(4L);
//            // 出库
//        } else if (wrkMast.getWrkSts() > 10) {
//            wrkMast.setWrkSts(14L);
//        }
//        Date now = new Date();
//        wrkMast.setCrnStrTime(DateUtils.calculate(now, 1L, TimeUnit.SECONDS, true));
//        wrkMast.setCrnEndTime(now);
//        wrkMast.setModiTime(now);
//        wrkMast.setModiUser(userId);
//        // 完成操作人员记录
//        wrkMast.setManuType("手动完成");
//
//        wmsWrk.setStatus(WmsStatusType.COMPLETE.id);
//        wmsWrk.setModiUser(userId);
//        wmsWrk.setModiTime(now);
//        wmsWrk.setCompleteTime(now);
//        if (!wrkMastService.updateById(wrkMast) || !wmsWrkService.updateById(wmsWrk)) {
//            throw new CoolException("修改任务失败");
//        }
        WrkMast wrkMast = wrkMastService.selectById(workNo);
        if (Cools.isEmpty(wrkMast)) {
            throw new CoolException(workNo + "工作档不存在");
        }
        if (wrkMast.getWrkSts() == 4 || wrkMast.getWrkSts() == 14) {
            throw new CoolException("当前工作档已完成");
        }
        // 入库 + 库位转移
        if (wrkMast.getWrkSts() < 4 || (wrkMast.getWrkSts() > 10 && wrkMast.getIoType() == 11)) {
            wrkMast.setWrkSts(4L);
            // 出库
        } else if (wrkMast.getWrkSts() > 10) {
            wrkMast.setWrkSts(14L);
        }
        Date now = new Date();
        wrkMast.setCrnStrTime(DateUtils.calculate(now, 1L, TimeUnit.SECONDS, true));
        wrkMast.setCrnEndTime(now);
        wrkMast.setModiTime(now);
        wrkMast.setModiUser(userId);
        // 完成操作人员记录
        wrkMast.setManuType("手动完成");
        if (!wrkMastService.updateById(wrkMast)) {
            throw new CoolException("修改工作档失败");
        }
        HashMap<String,Object> headParam = new HashMap<>();
        headParam.put("TaskNo",wrkMast.getTaskNo());
        headParam.put("Result",0);//完成
        try {
            String response;
            log.error("wcs手动完结任务上报wms={}", wrkMast);
            response = new HttpHandler.Builder()
                    // .setHeaders(headParam)
                    .setUri(wmsUrl)
                    .setPath(taskStatusFeedbackPath)
                    .setJson(JSON.toJSONString(headParam))
                    .build()
                    .doPost();
            apiLogService.save("wcs手动完成任务上报wms"
                    , wmsUrl + taskStatusFeedbackPath
                    , null
                    , "127.0.0.1"
                    , JSON.toJSONString(headParam)
                    , response
                    , true
            );
        } catch (Exception e) {
            log.error("wcs手动完结任务上报wms失败={}", wrkMast);
            log.error("wcs手动完结任务上报wms失败,报错信息:", e);
//                        throw new CoolException("wcs派发入库任务上报wms失败");
        }
    }
    @Override
    @Transactional
    public void cancelWrkMast(String workNo, Long userId) {
//        Date now = new Date();
//        WrkMast wrkMast = wrkMastService.selectById(workNo);
//        WmsWrk wmsWrk = wmsWrkService.selectByWmsWrkNo(wrkMast.getWmsWrkNo());
//        if (Cools.isEmpty(wrkMast, wmsWrk)) {
//            throw new CoolException(workNo + "任务不存在");
//        }
//
//        String locNo = ""; // 待修改目标库位
//        String locSts = ""; // 待修改目标库位状态
//        // 入库取消(修改目标库位)
//        if (wrkMast.getWrkSts() < 4) {
//            locNo = wrkMast.getLocNo();
//            locSts = "O";
//
//            // 库位转移
//            if (wrkMast.getIoType() == 11) {
//                // 库位转移:源库位
//                LocMast locMast = locMastService.selectById(wrkMast.getSourceLocNo());
//                if (Cools.isEmpty(locMast)) {
//                    throw new CoolException("取消库位转移失败,源库位不存在:"+ wrkMast.getSourceLocNo());
//                }
//                locMast.setLocSts(wrkMast.getFullPlt().equalsIgnoreCase("N")?"D":"F");
//                locMast.setModiTime(now);
//                locMast.setModiUser(userId);
//                locMastService.updateById(locMast);
//            }
//            // 出库取消(修改源库位)
//        } else if (wrkMast.getWrkSts() > 10 && wrkMast.getWrkSts() != 14) {
//            locNo = wrkMast.getSourceLocNo();
//            // 出库 ===>> F.在库
//            if (wrkMast.getIoType() > 100 && wrkMast.getIoType() != 110) {
//                locSts = "F";
//                // 空板出库 ===>> D.空桶/空栈板
//            } else if (wrkMast.getIoType() == 110) {
//                locSts = "D";
//                // 库位转移 ===>> D.空桶/空栈板
//            } else if (wrkMast.getIoType() == 11) {
//                locSts = wrkMast.getFullPlt().equalsIgnoreCase("N")?"D":"F";
//                // 库位转移:目标库位
//                LocMast locMast = locMastService.selectById(wrkMast.getLocNo());
//                if (Cools.isEmpty(locMast)) {
//                    throw new CoolException("取消库位转移失败,目标库位不存在:"+ wrkMast.getSourceLocNo());
//                }
//                locMast.setLocSts("O");
//                locMast.setModiTime(now);
//                locMast.setModiUser(userId);
//                locMastService.updateById(locMast);
//            }
//        } else {
//            throw new CoolException("当前工作状态无法取消");
//        }
//
//        //取消入库工作档时,查询组托表,如果有将状态改为待处理
//        if(wrkMast.getIoType() == 1) {
//            List<WaitPakin> waitPakins = waitPakinService.selectList(new EntityWrapper<WaitPakin>().eq("zpallet", wrkMast.getBarcode()));
//            for (WaitPakin waitPakin:waitPakins){
//                if (!Cools.isEmpty(waitPakin)) {
//                    waitPakin.setIoStatus("N");
//                    waitPakin.setLocNo("");
//                    waitPakinService.update(waitPakin, new EntityWrapper<WaitPakin>()
//                            .eq("order_no", waitPakin.getOrderNo())
//                            .eq("matnr", waitPakin.getMatnr())
//                            .eq("batch", waitPakin.getBatch()));
//                }
//            }
//        }
//
//        // 取消操作人员记录
//        wrkMast.setManuType("手动取消");
//        wrkMast.setModiUser(userId);
//        wrkMast.setModiTime(now);
//        if (!wrkMastService.updateById(wrkMast)) {
//            throw new CoolException("取消任务失败");
//        }
//
//        wmsWrk.setStatus(WmsStatusType.CANCEL.id);
//        wmsWrk.setModiUser(userId);
//        wmsWrk.setModiTime(now);
//        wmsWrk.setCancelTime(now);
//        if (!wmsWrkService.updateById(wmsWrk)) {
//            throw new CoolException("取消任务失败");
//        }
//
//        // 保存工作主档历史档
//        if (!wrkMastLogService.save(wrkMast.getWrkNo())) {
//            throw new CoolException("保存任务历史档失败, workNo = " + wrkMast.getWrkNo());
//        }
//        // 删除工作主档
//        boolean wrkMastRes = wrkMastService.deleteById(wrkMast);
//
//        // 修改库位状态
//        LocMast locMast = locMastService.selectById(locNo);
//        if (Cools.isEmpty(locMast)) {
//            throw new CoolException("取消任务失败,库位不存在:"+ locNo);
//        }
//        locMast.setLocSts(locSts);
//        locMast.setModiTime(now);
//        locMast.setModiUser(userId);
//        boolean locMastRes = locMastService.updateById(locMast);
//        if (!wrkMastRes || !locMastRes) {
//            throw new CoolException("保存数据失败");
//        }
        Date now = new Date();
        WrkMast wrkMast = wrkMastService.selectById(workNo);
        String locNo = ""; // 待修改目标库位
        String locSts = ""; // 待修改目标库位状态
        // 入库取消(修改目标库位)
        if (wrkMast.getWrkSts() < 4) {
            locNo = wrkMast.getLocNo();
            locSts = "O";
            // 库位转移
            if (wrkMast.getIoType() == 11) {
                // 库位转移:源库位
                LocMast locMast = locMastService.selectById(wrkMast.getSourceLocNo());
                if (Cools.isEmpty(locMast)) {
                    throw new CoolException("取消库位转移失败,源库位不存在:" + wrkMast.getSourceLocNo());
                }
                locMast.setLocSts(wrkMast.getFullPlt().equalsIgnoreCase("N") ? "D" : "F");
                locMast.setModiTime(now);
                locMast.setModiUser(userId);
                locMastService.updateById(locMast);
            }
            // 出库取消(修改源库位)
        } else if (wrkMast.getWrkSts() > 10 && wrkMast.getWrkSts() != 14) {
            locNo = wrkMast.getSourceLocNo();
            // 出库 ===>> F.在库
            if (wrkMast.getIoType() > 100 && wrkMast.getIoType() != 110) {
                locSts = "F";
                // 空板出库 ===>> D.空桶/空栈板
            } else if (wrkMast.getIoType() == 110) {
                locSts = "D";
                // 库位转移 ===>> D.空桶/空栈板
            } else if (wrkMast.getIoType() == 11) {
                locSts = wrkMast.getFullPlt().equalsIgnoreCase("N") ? "D" : "F";
                // 库位转移:目标库位
                LocMast locMast = locMastService.selectById(wrkMast.getLocNo());
                if (Cools.isEmpty(locMast)) {
                    throw new CoolException("取消库位转移失败,目标库位不存在:" + wrkMast.getSourceLocNo());
                }
                locMast.setLocSts("O");
                locMast.setModiTime(now);
                locMast.setModiUser(userId);
                locMastService.updateById(locMast);
            }
        } else {
            throw new CoolException("当前工作状态无法取消");
        }
        // 取消操作人员记录
        wrkMast.setManuType("手动取消");
        wrkMast.setModiUser(userId);
        wrkMast.setModiTime(now);
        if (!wrkMastService.updateById(wrkMast)) {
            throw new CoolException("取消任务失败");
        }
        // 保存工作主档历史档
        if (!wrkMastLogService.save(wrkMast.getWrkNo())) {
            throw new CoolException("保存任务历史档失败, workNo = " + wrkMast.getWrkNo());
        }
        // 删除工作主档
        boolean wrkMastRes = wrkMastService.deleteById(wrkMast);
        // 修改库位状态
        LocMast locMast = locMastService.selectById(locNo);
        if (Cools.isEmpty(locMast)) {
            throw new CoolException("取消任务失败,库位不存在:" + locNo);
        }
        locMast.setLocSts(locSts);
        locMast.setModiTime(now);
        locMast.setModiUser(userId);
        boolean locMastRes = locMastService.updateById(locMast);
        if (!wrkMastRes || !locMastRes) {
            throw new CoolException("保存数据失败");
        }
        HashMap<String,Object> headParam = new HashMap<>();
        headParam.put("TaskNo",wrkMast.getTaskNo());
        headParam.put("Result",1);//取消
        try {
            String response;
            log.error("wcs手动取消任务上报wms={}", wrkMast);
            response = new HttpHandler.Builder()
                    // .setHeaders(headParam)
                    .setUri(wmsUrl)
                    .setPath(taskStatusFeedbackPath)
                    .setJson(JSON.toJSONString(headParam))
                    .build()
                    .doPost();
            apiLogService.save("wcs手动取消任务上报wms"
                    , wmsUrl + taskStatusFeedbackPath
                    , null
                    , "127.0.0.1"
                    , JSON.toJSONString(headParam)
                    , response
                    , true
            );
        } catch (Exception e) {
            log.error("wcs手动取消任务上报wms失败={}", wrkMast);
            log.error("wcs手动取消任务上报wms失败,报错信息:", e);
//                        throw new CoolException("wcs派发入库任务上报wms失败");
        }
    }
}