package com.zy.asrs.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.core.common.Cools; import com.zy.asrs.domain.enums.TaskStatusType; import com.zy.asrs.domain.enums.WorkNoType; import com.zy.asrs.entity.LocMast; import com.zy.asrs.entity.TaskWrk; import com.zy.asrs.entity.TaskWrkReport; import com.zy.asrs.entity.wms.StorageEscalationParam; import com.zy.asrs.entity.wms.WmsResult; import com.zy.asrs.service.*; import com.zy.common.service.CommonService; import com.zy.common.utils.HttpHandler; import com.zy.common.utils.Synchro; 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 java.io.IOException; import java.util.Date; @Slf4j @Service public class ToWmsServiceImpl implements ToWmsService { @Autowired private TaskWrkService taskWrkService; @Autowired private TaskWrkReportService taskWrkReportService; @Autowired private CommonService commonService; @Autowired private LocMastService locMastService; @Autowired private ApiLogService apiLogService; @Value("${wms.url}") private String wmsUrl; /** * 申请入库 */ @Value("${wms.inboundTaskApplyPath}") private String inboundTaskApplyPath; /** * 任务开始时,WCS回调WMS */ @Value("${wms.taskExecCallback}") private String taskExecCallback; /** * 任务完成结束时,WCS回调WMS */ @Value("${wms.taskStatusFeedbackPath}") private String taskStatusFeedbackPath; @Value("${wms.code}") private String code; @Value("${wms.successCode}") private String successCode; @Value("${wms.msg}") private String msg; @Value("${wms.data}") private String data; @Override public void addReportLog(TaskWrk taskWrk) { TaskWrkReport taskWrkReport = new TaskWrkReport(); Synchro.Copy(taskWrk, taskWrkReport); taskWrkReport.setCreateTime(new Date()); taskWrkReportService.insert(taskWrkReport); } @Override public TaskWrk getLocNoFromWms(StorageEscalationParam wmsParam) { String response = ""; Boolean success = false; try { response = new HttpHandler.Builder() .setUri(wmsUrl) .setPath(inboundTaskApplyPath) .setJson(JSON.toJSONString(wmsParam)) .build() .doPost(); if (!Cools.isEmpty(response)) { JSONObject jsonObject = JSON.parseObject(response); if (!Cools.isEmpty(jsonObject.get(code)) && jsonObject.get(code).equals(successCode)) { //有些三方wms系统不能及时返回库位号,这时候就需要 //ZWmsResult result = JSON.parseObject(jsonObject.get(data).toString(), WmsResult.class); TaskWrk taskWrk = null; int i = 1; while (i < 10) { taskWrk = taskWrkService.selectByBarcode(wmsParam.getBarcode()); if (taskWrk != null) { break; } try { i++; Thread.sleep(500L); } catch (InterruptedException e) { throw new RuntimeException(e); } } return taskWrk; } } } catch (IOException e) { e.printStackTrace(); log.info("请求wms报错,{}", e.getMessage()); } finally { addApiLog("入库任务请求获取库位", wmsUrl + inboundTaskApplyPath, JSON.toJSONString(wmsParam), response, success); } return null; } private TaskWrk createInTask(WmsResult result, String barcode, Integer startPoint) { //String locNo = Utils.Fusion(result.getRow(), result.getFloor(), result.getColumn()); String locNo = result.getLocNo(); Date now = new Date(); TaskWrk taskWrk = new TaskWrk(); int workNo1 = commonService.getWorkNo(WorkNoType.PAKIN.type);//获取入库工作号 taskWrk.setTaskNo(result.getTaskNo());//任务号 taskWrk.setWrkNo(workNo1); taskWrk.setStatus(TaskStatusType.DISTRIBUTE.id);//任务状态:派发 taskWrk.setCreateTime(now); taskWrk.setIoType(1);//任务类型 taskWrk.setIoPri(13);//优先级 taskWrk.setBarcode(barcode);//条码 LocMast locMast = locMastService.selectByLocNo(locNo); taskWrk.setCrnNo(locMast.getCrnNo()); taskWrk.setTargetPoint(locNo); taskWrk.setStartPoint(startPoint + ""); taskWrk.setCrnNo(result.getCrnNo()); if (taskWrk.getIoType() == 1) { taskWrk.setWrkSts(2); if (!Cools.isEmpty(taskWrk.getTargetPoint())) { taskWrk.setOriginTargetPoint(taskWrk.getTargetPoint()); } } return taskWrk; } private void addApiLog(String nameSpace, String url, String param, String response, Boolean success) { apiLogService.save(nameSpace, url, null, "127.0.0.1", param, response, success); } }