package com.zy.core.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.core.common.Cools; import com.core.exception.CoolException; import com.zy.asrs.entity.BasCrnp; import com.zy.asrs.entity.BasDualCrnp; import com.zy.asrs.entity.BasStation; import com.zy.asrs.entity.HttpRequestLog; import com.zy.asrs.entity.WrkMast; import com.zy.asrs.service.BasCrnpService; import com.zy.asrs.service.BasDualCrnpService; import com.zy.asrs.service.BasStationService; import com.zy.asrs.service.HttpRequestLogService; import com.zy.asrs.service.WrkMastService; import com.zy.asrs.utils.Utils; import com.zy.common.entity.FindCrnNoResult; import com.zy.common.service.CommonService; import com.zy.common.utils.HttpHandler; import com.zy.common.utils.RedisUtil; import com.zy.core.News; import com.zy.core.enums.RedisKeyType; import com.zy.core.enums.SlaveType; import com.zy.core.plugin.store.InTaskApplyRequest; import com.zy.system.entity.Config; import com.zy.system.service.ConfigService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; @Component public class WmsOperateUtils { private static final int APPLY_IN_TASK_TIMEOUT_SECONDS = 5; @Autowired private ConfigService configService; @Autowired private HttpRequestLogService httpRequestLogService; @Autowired private WrkMastService wrkMastService; @Autowired private CommonService commonService; @Autowired private BasCrnpService basCrnpService; @Autowired private BasDualCrnpService basDualCrnpService; @Autowired private BasStationService basStationService; @Autowired private RedisUtil redisUtil; // 申请入库任务 public String applyInTask(String barcode, Integer sourceStaNo, Integer locType1) { InTaskApplyRequest request = new InTaskApplyRequest(); request.setBarcode(barcode); request.setSourceStaNo(sourceStaNo); request.setLocType1(locType1); return applyInTask(request); } public String applyInTask(InTaskApplyRequest request) { Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key); if (systemConfigMapObj == null) { News.error("系统Config缓存失效"); return null; } HashMap systemConfigMap = (HashMap) systemConfigMapObj; String wmsUrl = systemConfigMap.get("wmsSystemUri"); if (wmsUrl == null) { News.error("未配置WMS系统URI,配置文件Code编码:wmsSystemUri"); return null; } String wmsSystemInUrl = systemConfigMap.get("wmsSystemInUrl"); if (wmsSystemInUrl == null) { News.error("未配置WMS入库接口地址,配置文件Code编码:wmsSystemInUrl"); return null; } Map requestParam = new LinkedHashMap<>(); String response = null; int result = 0; long startMs = System.currentTimeMillis(); try { BasStation basStation = basStationService .getOne(new QueryWrapper().eq("station_id", request.getSourceStaNo())); if (basStation == null) { News.error("站点{}不存在", request.getSourceStaNo()); return null; } String stationNo = String.valueOf(request.getSourceStaNo()); if (!Cools.isEmpty(basStation.getStationAlias())) { stationNo = basStation.getStationAlias(); } requestParam.put("barcode", request.getBarcode()); requestParam.put("sourceStaNo", stationNo); requestParam.put("locType1", request.getLocType1() == null ? 1 : request.getLocType1()); requestParam.put("row", Utils.getInTaskEnableRow(request.getSourceStaNo())); if (request.getExtraParams() != null && !request.getExtraParams().isEmpty()) { requestParam.putAll(request.getExtraParams()); } response = new HttpHandler.Builder() .setUri(wmsUrl) .setPath(wmsSystemInUrl) .setJson(JSON.toJSONString(requestParam)) .setTimeout(APPLY_IN_TASK_TIMEOUT_SECONDS, TimeUnit.SECONDS) .build() .doPost(); if (!Cools.isEmpty(response)) { JSONObject jsonObject = JSON.parseObject(response); if (jsonObject.getInteger("code") == 200) { result = 1; News.info("请求WMS入库接口成功!!!url:{};request:{};response:{};cost={}ms", wmsUrl + wmsSystemInUrl, JSON.toJSONString(requestParam), response, elapsedMs(startMs)); } else { News.info("请求WMS入库接口失败,接口返回Code异常!!!url:{};request:{};response:{};cost={}ms", wmsUrl + wmsSystemInUrl, JSON.toJSONString(requestParam), response, elapsedMs(startMs)); } } else { News.info("请求WMS入库接口失败,接口未响应!!!url:{};request:{};response:{};cost={}ms", wmsUrl + wmsSystemInUrl, JSON.toJSONString(requestParam), response, elapsedMs(startMs)); } } catch (Exception e) { News.error("请求WMS入库接口异常!!!url:{};request:{};response:{};cost={}ms", wmsUrl + wmsSystemInUrl, JSON.toJSONString(requestParam), response, elapsedMs(startMs), e); } finally { HttpRequestLog httpRequestLog = new HttpRequestLog(); httpRequestLog.setName(wmsUrl + wmsSystemInUrl); httpRequestLog.setRequest(JSON.toJSONString(requestParam)); httpRequestLog.setResponse(response); httpRequestLog.setCreateTime(new Date()); httpRequestLog.setResult(result); httpRequestLogService.save(httpRequestLog); } return response; } // 申请任务重新分配库位 public synchronized String applyReassignTaskLocNo(Integer taskNo, Integer stationId) { String wmsUrl = null; Config wmsSystemUriConfig = configService.getOne(new QueryWrapper().eq("code", "wmsSystemUri")); if (wmsSystemUriConfig != null) { wmsUrl = wmsSystemUriConfig.getValue(); } if (wmsUrl == null) { News.error("未配置WMS系统URI,配置文件Code编码:wmsSystemUri"); return null; } String wmsSystemReassignInTaskUrl = null; Config wmsSystemReassignInTaskUrlConfig = configService .getOne(new QueryWrapper().eq("code", "wmsSystemReassignInTaskUrl")); if (wmsSystemReassignInTaskUrlConfig != null) { wmsSystemReassignInTaskUrl = wmsSystemReassignInTaskUrlConfig.getValue(); } if (wmsSystemReassignInTaskUrl == null) { News.error("未配置WMS任务重新分配入库库位接口地址,配置文件Code编码:wmsSystemReassignInTaskUrl"); return null; } WrkMast wrkMast = wrkMastService.selectByWorkNo(taskNo); if (wrkMast == null) { News.info("无法找到对应任务,工作号={}", taskNo); return null; } HashMap requestParam = new HashMap<>(); String response = null; int result = 0; long startMs = System.currentTimeMillis(); try { List excludeCrnList = new ArrayList<>(); List excludeDualCrnList = new ArrayList<>(); if (!Cools.isEmpty(wrkMast.getCrnNo())) { excludeCrnList.add(wrkMast.getCrnNo()); } if (!Cools.isEmpty(wrkMast.getDualCrnNo())) { excludeDualCrnList.add(wrkMast.getDualCrnNo()); } requestParam.put("taskNo", wrkMast.getWmsWrkNo()); requestParam.put("row", Utils.getInTaskEnableRow(stationId, excludeCrnList, excludeDualCrnList, false)); response = new HttpHandler.Builder() .setUri(wmsUrl) .setPath(wmsSystemReassignInTaskUrl) .setJson(JSON.toJSONString(requestParam)) .setTimeout(30, TimeUnit.SECONDS) .build() .doPost(); if (!Cools.isEmpty(response)) { JSONObject jsonObject = JSON.parseObject(response); if (jsonObject.getInteger("code") == 200) { result = 1; News.info("请求申请任务重新分配入库接口成功!!!url:{};request:{};response:{};cost={}ms", wmsUrl + wmsSystemReassignInTaskUrl, JSON.toJSONString(requestParam), response, elapsedMs(startMs)); } else { News.info("请求申请任务重新分配入库接口失败,接口返回Code异常!!!url:{};request:{};response:{};cost={}ms", wmsUrl + wmsSystemReassignInTaskUrl, JSON.toJSONString(requestParam), response, elapsedMs(startMs)); } } else { News.info("请求申请任务重新分配入库接口失败,接口未响应!!!url:{};request:{};response:{};cost={}ms", wmsUrl + wmsSystemReassignInTaskUrl, JSON.toJSONString(requestParam), response, elapsedMs(startMs)); } } catch (Exception e) { News.error("请求申请任务重新分配入库接口异常!!!url:{};request:{}; response:{};cost={}ms", wmsUrl + wmsSystemReassignInTaskUrl, JSON.toJSONString(requestParam), response, elapsedMs(startMs), e); } finally { HttpRequestLog httpRequestLog = new HttpRequestLog(); httpRequestLog.setName(wmsUrl + wmsSystemReassignInTaskUrl); httpRequestLog.setRequest(JSON.toJSONString(requestParam)); httpRequestLog.setResponse(response); httpRequestLog.setCreateTime(new Date()); httpRequestLog.setResult(result); httpRequestLogService.save(httpRequestLog); } return response; } // 申请在库库位更换库位 public synchronized String applyChangeLocNo(String locNo) { String wmsUrl = null; Config wmsSystemUriConfig = configService.getOne(new QueryWrapper().eq("code", "wmsSystemUri")); if (wmsSystemUriConfig != null) { wmsUrl = wmsSystemUriConfig.getValue(); } if (wmsUrl == null) { News.error("未配置WMS系统URI,配置文件Code编码:wmsSystemUri"); return null; } String wmsSystemChangeLocNoUrl = null; Config wmsSystemChangeLocNoUrlConfig = configService .getOne(new QueryWrapper().eq("code", "wmsSystemChangeLocNoUrl")); if (wmsSystemChangeLocNoUrlConfig != null) { wmsSystemChangeLocNoUrl = wmsSystemChangeLocNoUrlConfig.getValue(); } if (wmsSystemChangeLocNoUrl == null) { News.error("未配置申请在库库位更换库位接口地址,配置文件Code编码:wmsSystemChangeLocNoUrl"); return null; } FindCrnNoResult findCrnNoResult = commonService.findCrnNoByLocNo(locNo); if (findCrnNoResult == null) { return null; } Integer crnNo = findCrnNoResult.getCrnNo(); List crnRows = new ArrayList<>(); if (findCrnNoResult.getCrnType().equals(SlaveType.Crn)) { BasCrnp basCrnp = basCrnpService.getOne(new QueryWrapper().eq("crn_no", crnNo)); if (basCrnp == null) { return null; } List> rowList = basCrnp.getControlRows$(); for (List list : rowList) { crnRows.addAll(list); } } else if (findCrnNoResult.getCrnType().equals(SlaveType.DualCrn)) { BasDualCrnp basDualCrnp = basDualCrnpService .getOne(new QueryWrapper().eq("crn_no", crnNo)); if (basDualCrnp == null) { return null; } List> rowList = basDualCrnp.getControlRows$(); for (List list : rowList) { crnRows.addAll(list); } } else { throw new CoolException("未知设备类型"); } HashMap requestParam = new HashMap<>(); String response = null; int result = 0; long startMs = System.currentTimeMillis(); try { requestParam.put("locNo", locNo); requestParam.put("row", crnRows); response = new HttpHandler.Builder() .setUri(wmsUrl) .setPath(wmsSystemChangeLocNoUrl) .setJson(JSON.toJSONString(requestParam)) .setTimeout(30, TimeUnit.SECONDS) .build() .doPost(); if (!Cools.isEmpty(response)) { JSONObject jsonObject = JSON.parseObject(response); if (jsonObject.getInteger("code") == 200) { result = 1; News.info("请求WMS申请更换库位接口成功!!!url:{};request:{};response:{};cost={}ms", wmsUrl + wmsSystemChangeLocNoUrl, JSON.toJSONString(requestParam), response, elapsedMs(startMs)); } else { News.info("请求WMS申请更换库位接口失败,接口返回Code异常!!!url:{};request:{};response:{};cost={}ms", wmsUrl + wmsSystemChangeLocNoUrl, JSON.toJSONString(requestParam), response, elapsedMs(startMs)); } } else { News.info("请求WMS申请更换库位接口失败,接口未响应!!!url:{};request:{};response:{};cost={}ms", wmsUrl + wmsSystemChangeLocNoUrl, JSON.toJSONString(requestParam), response, elapsedMs(startMs)); } } catch (Exception e) { News.error("请求WMS申请更换库位接口异常!!!url:{};request:{};response:{};cost={}ms", wmsUrl + wmsSystemChangeLocNoUrl, JSON.toJSONString(requestParam), response, elapsedMs(startMs), e); } finally { HttpRequestLog httpRequestLog = new HttpRequestLog(); httpRequestLog.setName(wmsUrl + wmsSystemChangeLocNoUrl); httpRequestLog.setRequest(JSON.toJSONString(requestParam)); httpRequestLog.setResponse(response); httpRequestLog.setCreateTime(new Date()); httpRequestLog.setResult(result); httpRequestLogService.save(httpRequestLog); } return response; } private long elapsedMs(long startMs) { return System.currentTimeMillis() - startMs; } }