723 lines
36 KiB
Java
723 lines
36 KiB
Java
package com.wms.bussiness;
|
||
|
||
import com.alibaba.fastjson2.JSON;
|
||
import com.alibaba.fastjson2.JSONObject;
|
||
import com.wms.constants.enums.*;
|
||
import com.wms.controller.BaseController;
|
||
import com.wms.entity.app.ResponseEntity;
|
||
import com.wms.entity.app.container.ContainerApiLocalResponse;
|
||
import com.wms.entity.app.container.CreateInstoreTaskRequest;
|
||
import com.wms.entity.app.container.SendContainerTaskRequest;
|
||
import com.wms.entity.app.wcs.WcsTask;
|
||
import com.wms.entity.table.*;
|
||
import com.wms.mapper.LocationMapper;
|
||
import com.wms.mapper.StockMapper;
|
||
import com.wms.mapper.TaskMapper;
|
||
import com.wms.mapper.VehicleMapper;
|
||
import com.wms.service.*;
|
||
import com.wms.utils.HttpUtils;
|
||
import com.wms.utils.StringUtils;
|
||
import com.wms.utils.storage.LocationUtils;
|
||
import lombok.RequiredArgsConstructor;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.scheduling.annotation.Scheduled;
|
||
import org.springframework.stereotype.Component;
|
||
import org.springframework.transaction.annotation.Isolation;
|
||
import org.springframework.transaction.annotation.Propagation;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
|
||
import java.util.*;
|
||
|
||
/**
|
||
* 定期任务类
|
||
*/
|
||
@Component
|
||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||
public class JobComponent extends BaseController {
|
||
/**
|
||
* 任务服务
|
||
*/
|
||
private final TaskService taskService;
|
||
|
||
/**
|
||
* 任务 Mapper
|
||
*/
|
||
private final TaskMapper taskMapper;
|
||
|
||
/**
|
||
* 载具/料箱 服务
|
||
*/
|
||
private final VehicleService vehicleService;
|
||
/**
|
||
* 库位服务
|
||
*/
|
||
private final LocationService locationService;
|
||
|
||
/**
|
||
* 库位 Mapper
|
||
*/
|
||
private final LocationMapper locationMapper;
|
||
|
||
/**
|
||
* 库位操作类
|
||
*/
|
||
private final LocationUtils locationUtils;
|
||
|
||
/**
|
||
* 站台服务
|
||
*/
|
||
private final StandService standService;
|
||
/**
|
||
* 配置服务
|
||
*/
|
||
private final ConfigService configService;
|
||
/**
|
||
* 库存服务
|
||
*/
|
||
private final StockService stockService;
|
||
private final TaskRecordService taskRecordService;
|
||
private final StockMapper stockMapper;
|
||
private final VehicleMapper vehicleMapper;
|
||
|
||
|
||
@Scheduled(fixedDelay = 1000)
|
||
public void sendTasksToWcs() {
|
||
try {
|
||
// 查找待下发的任务
|
||
Task taskForQuery = new Task();
|
||
taskForQuery.setTaskStatus(WmsTaskStatus.NEW.getCode());
|
||
List<Task> tasks = taskService.selTasks(taskForQuery);
|
||
if(tasks == null) {
|
||
logger.error("定时器下发任务发生异常,数据库查询任务列表失败");
|
||
return;
|
||
}
|
||
for(Task task : tasks){
|
||
if(task.getTaskType().equals(TaskType.IN.getCode())) {
|
||
sendTasksInToWcs(task);
|
||
return;
|
||
}
|
||
if(task.getTaskType().equals(TaskType.OUT.getCode()) || task.getTaskType().equals(TaskType.INVENTORY.getCode())){
|
||
sendTasksOutToWcs(task);
|
||
return;
|
||
}
|
||
if(task.getTaskType().equals(TaskType.MOVE.getCode())){
|
||
sendTasksMoveToWcs(task);
|
||
return;
|
||
}
|
||
}
|
||
}catch (Exception exception){
|
||
logger.error("定时器下发任务发生异常:{}", exception.toString());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 发送入库任务给WCS
|
||
* @param task 任务
|
||
*/
|
||
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
|
||
public void sendTasksInToWcs(Task task) {
|
||
try {
|
||
List<Location> thisLocations = locationService.selLocations(new Location(task.getDestination()));
|
||
if(thisLocations == null) {
|
||
logger.warn("下发入库任务查询库位详细信息失败,数据库网络连接异常,任务号:{}", task.getTaskId());
|
||
return;
|
||
}
|
||
if(thisLocations.isEmpty()) {
|
||
logger.warn("下发入库任务查询库位详细信息异常,不存在详细信息,该库位可能不存在,任务号:{}", task.getTaskId());
|
||
Task taskForUpdate = new Task();
|
||
taskForUpdate.setTaskId(task.getTaskId());
|
||
taskForUpdate.setTaskStatus(WmsTaskStatus.EXCEPTION.getCode());
|
||
taskForUpdate.setRemark1("该库位不存在");
|
||
taskService.executeTask(taskForUpdate);
|
||
return;
|
||
}
|
||
Location thisLocation = thisLocations.get(0);
|
||
if(thisLocation.getAreaId() == 1){
|
||
List<WcsTask> request = new ArrayList<>();
|
||
// 创建发送的任务
|
||
WcsTask tempTask = new WcsTask();
|
||
tempTask.setTaskId(task.getTaskId());
|
||
tempTask.setTaskType(TaskType.IN.getCode());
|
||
tempTask.setOrigin(task.getOrigin());
|
||
tempTask.setDestination(task.getDestination());
|
||
tempTask.setVehicleNo(task.getVehicleNo());
|
||
tempTask.setVehicleSize(1);
|
||
tempTask.setWeight(task.getWeight() == null ? 0 : task.getWeight());
|
||
tempTask.setPriority(task.getTaskPriority());
|
||
request.add(tempTask);
|
||
// 发送任务
|
||
ResponseEntity result = JSON.parseObject(HttpUtils.sendHttpPostWithoutToken(UrlEnums.URL_WMS_TO_WCS_SEND_TASK.getValue(), JSON.toJSONString(request)), ResponseEntity.class);
|
||
if (result != null && Objects.equals(ResponseCode.OK.getCode(), result.getCode())) {
|
||
logger.info("下发立库入库任务成功,任务ID:{},任务信息:{}", tempTask.getTaskId(), JSON.toJSONString(request));
|
||
Task taskForUpdate = new Task();
|
||
taskForUpdate.setTaskId(task.getTaskId());
|
||
taskForUpdate.setTaskStatus(WmsTaskStatus.WAIT.getCode());
|
||
taskService.executeTask(taskForUpdate);
|
||
return;
|
||
}
|
||
logger.info("下发立库入库任务失败,任务ID:{},信息:{}", tempTask.getTaskId(), JSON.toJSONString(result));
|
||
}
|
||
// else { //四向车库
|
||
// //List<WcsTask> request = new ArrayList<>();
|
||
// // 创建发送的任务
|
||
// String uuid = UUID.randomUUID().toString();
|
||
// CreateInstoreTaskRequest request = new CreateInstoreTaskRequest();
|
||
// request.setRequestId(uuid);
|
||
// request.setKey(StringUtils.containerMd5(uuid).toUpperCase());
|
||
// request.setPalletNo(task.getVehicleNo());
|
||
// request.setHeight("0");
|
||
// request.setWeight("0");
|
||
// request.setFromCellNo(task.getOrigin());
|
||
// // 发送任务
|
||
// String requestString = JSON.toJSONString(request);
|
||
// String responseString = HttpUtils.sendHttpPostWithoutToken(UrlEnums.URL_WMS_TO_WCS_CONTAINERIN_TASK.getValue(), requestString);
|
||
// if (responseString == null || responseString.isEmpty()) {
|
||
// logger.error("Response string is null or empty");
|
||
// }
|
||
// ContainerApiLocalResponse result = JSON.parseObject(responseString, ContainerApiLocalResponse.class);
|
||
// if (result != null && result.getCode().equals("200")) {
|
||
// logger.info("下发托盘库入库任务成功,任务ID:{}", task.getTaskId());
|
||
// Task taskForUpdate = new Task();
|
||
// taskForUpdate.setTaskId(task.getTaskId());
|
||
// taskForUpdate.setTaskStatus(WmsTaskStatus.WAIT.getCode());
|
||
// taskService.executeTask(taskForUpdate);
|
||
// return;
|
||
// }
|
||
// logger.info("下发托盘库入库任务失败,任务ID:{},信息:{}", task.getTaskId(), JSON.toJSONString(result));
|
||
// }
|
||
}catch (Exception exception){
|
||
logger.error("下发入库任务发生异常:{}", exception.toString());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 发送出库任务给WCS
|
||
* @param task 任务
|
||
*/
|
||
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
|
||
public void sendTasksOutToWcs(Task task) {
|
||
/* 检查该库位前一个深度是否存在库存,若存在库存则生成一个移库任务,此任务暂不下发 */
|
||
List<Location> thisLocations = locationService.selLocations(new Location(task.getOrigin()));
|
||
if(thisLocations == null) {
|
||
logger.warn("下发出库任务查询库位详细信息失败,数据库网络连接异常,任务号:{}", task.getTaskId());
|
||
logger.info("下发出库任务查询库位详细信息失败,数据库网络连接异常");
|
||
return;
|
||
}
|
||
if(thisLocations.isEmpty()) {
|
||
logger.warn("下发出库任务查询库位详细信息异常,不存在详细信息,该库位可能不存在,任务号:{}", task.getTaskId());
|
||
logger.info("下发出库任务查询库位详细信息异常,不存在详细信息,该库位可能不存在");
|
||
Task taskForUpdate = new Task();
|
||
taskForUpdate.setTaskId(task.getTaskId());
|
||
taskForUpdate.setTaskStatus(WmsTaskStatus.EXCEPTION.getCode());
|
||
taskForUpdate.setRemark1("该库位不存在");
|
||
taskService.executeTask(taskForUpdate);
|
||
return;
|
||
}
|
||
Location thisLocation = thisLocations.get(0); // 找出当前库位详细信息
|
||
int depth = 0;
|
||
while (depth < thisLocation.getDepth()-1) {
|
||
depth++;
|
||
/* 检查该库位有没有任务,若有则退出函数,若没有则检查有没有库存,若没有库存则继续,若有库存则生成一个移库任务,生成之后退出函数 */
|
||
Location beforLocationsQuery = new Location();
|
||
// logger.info("{},{},{},{},{}",thisLocation.getAreaId(),thisLocation.getQueue(),thisLocation.getLine(),thisLocation.getLayer(),depth);
|
||
beforLocationsQuery.setAreaId(thisLocation.getAreaId());
|
||
beforLocationsQuery.setTunnelId(thisLocation.getTunnelId());
|
||
beforLocationsQuery.setQueue(thisLocation.getQueue());
|
||
beforLocationsQuery.setLayer(thisLocation.getLayer());
|
||
beforLocationsQuery.setLocationType(1);
|
||
beforLocationsQuery.setDepth(depth);
|
||
List<Location> beforLocations = locationService.selLocations(beforLocationsQuery);
|
||
if(beforLocations == null) {
|
||
logger.warn("下发出库任务查询库位详细信息失败,数据库网络连接异常,任务号:{}", task.getTaskId());
|
||
logger.info("下发出库任务查询库位详细信息失败,数据库网络连接异常");
|
||
return;
|
||
}
|
||
if(beforLocations.isEmpty()) {
|
||
logger.info("beforLocations.isEmpty()");
|
||
return;
|
||
}
|
||
Location beforLocation = beforLocations.get(0); // 前一个库位
|
||
List<Task> notCompleteTasks = taskMapper.haveNotCompleteTask(beforLocation.getLocationId());
|
||
if(notCompleteTasks == null) {
|
||
logger.info("notCompleteTasks为null");
|
||
return;
|
||
}
|
||
if(!notCompleteTasks.isEmpty()) {
|
||
logger.info("发送出库任务时存在未完成的任务,退出函数");
|
||
return;
|
||
}
|
||
// 检查是否有库存,因为存在空框所以不在库存表中检验
|
||
Vehicle nextVehicle = new Vehicle();
|
||
nextVehicle.setCurrentLocation(beforLocation.getLocationId());
|
||
List<Vehicle> beforVehicleCheckIfEmpty = vehicleMapper.selVehicles(nextVehicle);
|
||
if(beforVehicleCheckIfEmpty == null) {
|
||
logger.info("beforLocationsCheckIfEmpty == null");
|
||
return;
|
||
}
|
||
if(beforVehicleCheckIfEmpty.isEmpty()) {
|
||
logger.info("位置{}没有框",beforLocation.getLocationId());
|
||
continue; // 没有库存,继续
|
||
}
|
||
/* 生成一个移库任务 */
|
||
/* 查找一个空库位 */
|
||
Location emptyLocation = new Location();
|
||
emptyLocation.setLocationStatus(LocationStatus.EMPTY.getCode());
|
||
emptyLocation.setAreaId(beforLocation.getAreaId());
|
||
|
||
// 查找同一巷道的用于移库交换区域
|
||
emptyLocation.setTunnelId(Integer.parseInt(String.valueOf(thisLocation.getTunnelId()).substring(0,1)));
|
||
emptyLocation.setLayer(thisLocation.getLayer());
|
||
emptyLocation.setIsChangeArea(1);
|
||
List<Location> emptyLocations = locationMapper.findChangeLocation(emptyLocation);
|
||
if(emptyLocations == null) {
|
||
logger.info("emptyLocations == null");
|
||
return;
|
||
}
|
||
if(emptyLocations.isEmpty()){
|
||
logger.warn("移库没有可用库位,任务号:{}", task.getTaskId());
|
||
logger.info("移库没有可用库位");
|
||
return; // 移库没有可用库位
|
||
}
|
||
Location emptyLocationItem = locationUtils.checkCanUse(emptyLocations); // 取得新库位
|
||
if(emptyLocationItem == null) {
|
||
logger.info("emptyLocationItem == null");
|
||
return; // 没有可用库位或者m库位存在干涉
|
||
}
|
||
// 锁定旧库位库存
|
||
stockMapper.updateStockStatusWithLocationId(beforLocation.getLocationId(), StockStatus.LOCK.getCode());
|
||
// 锁定新库位
|
||
Location locationForUpdate = new Location();
|
||
locationForUpdate.setLocationId(emptyLocationItem.getLocationId());
|
||
locationForUpdate.setLocationStatus(LocationStatus.OCCUPY.getCode());
|
||
locationMapper.modifyLocation(locationForUpdate);
|
||
|
||
//创建移库任务
|
||
Task moveTask = new Task();
|
||
moveTask.setTaskId(String.valueOf(Calendar.getInstance().getTimeInMillis()));
|
||
moveTask.setTaskType(TaskType.MOVE.getCode());
|
||
moveTask.setTaskGroup(task.getTaskGroup());
|
||
moveTask.setTaskStatus(OrderOutStatusEnum.CREATED.getCode());
|
||
moveTask.setOrigin(beforLocation.getLocationId());
|
||
moveTask.setDestination(emptyLocationItem.getLocationId());
|
||
moveTask.setPickStand(null);
|
||
moveTask.setWeight(0.0);
|
||
moveTask.setVehicleNo(beforVehicleCheckIfEmpty.get(0).getVehicleId());
|
||
moveTask.setCreateTime(new Date());
|
||
moveTask.setUserName("WMS");
|
||
moveTask.setTaskPriority(thisLocation.getDepth() - depth);
|
||
// if(depth == 2){
|
||
// moveTask.setTaskPriority(8);
|
||
// } else if (depth == 1) {
|
||
// moveTask.setTaskPriority(9);
|
||
// }
|
||
if (beforVehicleCheckIfEmpty.get(0).getIsEmpty() == 1){
|
||
moveTask.setRemark1("空框");
|
||
}else {
|
||
moveTask.setRemark1("带料");
|
||
}
|
||
int a = taskService.addTask(moveTask);
|
||
if (a == 1 && depth < thisLocation.getDepth()-1 ){
|
||
logger.info("生成移库任务成功,任务号:{},深度为1", moveTask.getTaskId());
|
||
continue;
|
||
} else if (a == 1 && depth == thisLocation.getDepth()-1) {
|
||
return;
|
||
} else {
|
||
logger.info("生成移库任务失败,任务号:{}", moveTask.getTaskId());
|
||
}
|
||
return;
|
||
}
|
||
if(thisLocation.getAreaId() == 1) { // 宝开立库
|
||
List<WcsTask> request = new ArrayList<>();
|
||
// 创建发送的任务
|
||
WcsTask tempTask = new WcsTask();
|
||
tempTask.setTaskId(task.getTaskId());
|
||
tempTask.setTaskType(TaskType.OUT.getCode());
|
||
tempTask.setOrigin(task.getOrigin());
|
||
tempTask.setDestination(task.getDestination());
|
||
tempTask.setVehicleNo(task.getVehicleNo());
|
||
tempTask.setVehicleSize(1);
|
||
tempTask.setWeight(task.getWeight() == null ? 0 : task.getWeight());
|
||
tempTask.setPriority(task.getTaskPriority());
|
||
request.add(tempTask);
|
||
// 发送任务
|
||
ResponseEntity result = JSON.parseObject(HttpUtils.sendHttpPostWithoutToken(UrlEnums.URL_WMS_TO_WCS_SEND_TASK.getValue(), JSON.toJSONString(request)), ResponseEntity.class);
|
||
if (result != null && Objects.equals(ResponseCode.OK.getCode(), result.getCode())) {
|
||
logger.info("下发立库出库任务成功,任务ID:{}", tempTask.getTaskId());
|
||
Task taskForUpdate = new Task();
|
||
taskForUpdate.setTaskId(task.getTaskId());
|
||
taskForUpdate.setTaskStatus(WmsTaskStatus.WAIT.getCode());
|
||
taskService.executeTask(taskForUpdate);
|
||
return;
|
||
}
|
||
logger.info("下发立库出库任务失败,任务ID:{},信息:{},result结果:{}", tempTask.getTaskId(), JSON.toJSONString(result),result.getCode());
|
||
} else { // 四向车库
|
||
String uuid = UUID.randomUUID().toString();
|
||
SendContainerTaskRequest request = new SendContainerTaskRequest();
|
||
request.setRequestid(uuid);
|
||
request.setKey(StringUtils.containerMd5(uuid).toUpperCase());
|
||
request.setWmstaskid(task.getTaskId());
|
||
request.setPalletno(task.getVehicleNo());
|
||
request.setTasktype("2"); // 出库
|
||
request.setFromcellno(task.getOrigin());
|
||
request.setTocellno("111"); // ???????
|
||
String requestString = JSON.toJSONString(request);
|
||
String responseString = HttpUtils.sendHttpPostWithoutToken(UrlEnums.URL_WMS_TO_WCS_CONTAINER_TASK.getValue(), requestString);
|
||
//JSONObject jsonResponse = JSON.parseObject(responseString);
|
||
ContainerApiLocalResponse result = JSON.parseObject(responseString, ContainerApiLocalResponse.class);
|
||
if(result != null && result.getCode().equals("200")) {
|
||
logger.info("下发四向车出库任务成功,任务ID:{}", task.getTaskId());
|
||
Task taskForUpdate = new Task();
|
||
taskForUpdate.setTaskId(task.getTaskId());
|
||
taskForUpdate.setTaskStatus(WmsTaskStatus.WAIT.getCode());
|
||
taskService.executeTask(taskForUpdate);
|
||
return;
|
||
}
|
||
logger.info("下发四向车出库任务失败,任务ID:{},信息:{}", task.getTaskId(), JSON.toJSONString(result));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 发送移库任务给WCS
|
||
* @param task 任务
|
||
*/
|
||
|
||
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
|
||
public void sendTasksMoveToWcs(Task task){
|
||
/* 检查该库位前一个深度是否存在库存,若存在库存则生成一个移库任务,此任务暂不下发 */
|
||
List<Location> thisLocations = locationService.selLocations(new Location(task.getOrigin()));
|
||
if(thisLocations == null) {
|
||
logger.info("下发移库任务查询库位详细信息失败,数据库网络连接异常");
|
||
return;
|
||
}
|
||
if(thisLocations.isEmpty()) {
|
||
logger.info("下发移库任务查询库位详细信息异常,不存在详细信息,该库位可能不存在");
|
||
Task taskForUpdate = new Task();
|
||
taskForUpdate.setTaskId(task.getTaskId());
|
||
taskForUpdate.setTaskStatus(WmsTaskStatus.EXCEPTION.getCode());
|
||
taskForUpdate.setRemark1("该库位不存在");
|
||
taskService.executeTask(taskForUpdate);
|
||
return;
|
||
}
|
||
Location thisLocation = thisLocations.get(0); // 找出当前库位详细信息
|
||
int depth = thisLocation.getDepth();
|
||
while (depth > 1) {
|
||
depth--;
|
||
/* 检查该库位有没有任务,若有则退出函数,若没有则检查有没有库存,若没有库存则继续,若有库存则生成一个移库任务,生成之后退出函数 */
|
||
Location beforLocationsQuery = new Location();
|
||
beforLocationsQuery.setAreaId(thisLocation.getAreaId());
|
||
beforLocationsQuery.setTunnelId(thisLocation.getTunnelId());
|
||
beforLocationsQuery.setQueue(thisLocation.getQueue());
|
||
beforLocationsQuery.setLayer(thisLocation.getLayer());
|
||
beforLocationsQuery.setLocationType(1);
|
||
beforLocationsQuery.setDepth(depth);
|
||
List<Location> beforLocations = locationService.selLocations(beforLocationsQuery);
|
||
if (beforLocations == null) {
|
||
logger.info("下发移库任务查询库位详细信息失败,数据库网络连接异常");
|
||
return;
|
||
}
|
||
if (beforLocations.isEmpty()) {
|
||
logger.info("前一个库位信息为空");
|
||
return;
|
||
}
|
||
Location beforLocation = beforLocations.get(0); // 前一个库位
|
||
List<Task> notCompleteTasks = taskMapper.haveNotCompleteTask(beforLocation.getLocationId());
|
||
if (notCompleteTasks == null) {
|
||
logger.info("查询任务异常");
|
||
return;
|
||
}
|
||
if (!notCompleteTasks.isEmpty()) {
|
||
logger.info("发送移库任务时存在未完成的任务,退出函数");
|
||
return;
|
||
}
|
||
}
|
||
logger.info("开始发送移库任务给WCS,任务号:{}", task.getTaskId());
|
||
String uuid = UUID.randomUUID().toString();
|
||
SendContainerTaskRequest request = new SendContainerTaskRequest();
|
||
request.setRequestid(uuid);
|
||
request.setKey(StringUtils.containerMd5(uuid).toUpperCase());
|
||
request.setWmstaskid(task.getTaskId());
|
||
request.setPalletno(task.getVehicleNo());
|
||
request.setTasktype("3"); // 移库
|
||
request.setFromcellno(task.getOrigin());
|
||
request.setTocellno(task.getDestination());
|
||
String requestString = JSON.toJSONString(request);
|
||
String responseString = HttpUtils.sendHttpPostWithoutToken(UrlEnums.URL_WMS_TO_WCS_CONTAINER_TASK.getValue(), requestString);
|
||
ContainerApiLocalResponse result = JSON.parseObject(responseString, ContainerApiLocalResponse.class);
|
||
if(result != null && result.getCode().equals("200")) {
|
||
logger.info("下发四向车移库任务成功,任务ID:{}", task.getTaskId());
|
||
logger.info("移库目标位置为:{}",task.getDestination());
|
||
Task taskForUpdate = new Task();
|
||
taskForUpdate.setTaskId(task.getTaskId());
|
||
taskForUpdate.setTaskStatus(WmsTaskStatus.WAIT.getCode());
|
||
taskService.executeTask(taskForUpdate);
|
||
return;
|
||
}
|
||
logger.info("下发四向车移库任务失败,任务ID:{},信息:{}", task.getTaskId(), JSON.toJSONString(result));
|
||
}
|
||
|
||
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
|
||
public void sendTasksMoveToWcs1(Task task) {
|
||
/* 检查该库位前一个深度是否存在库存,若存在库存则生成一个移库任务,此任务暂不下发 */
|
||
List<Location> thisLocations = locationService.selLocations(new Location(task.getOrigin()));
|
||
if(thisLocations == null) {
|
||
logger.warn("下发移库任务查询库位详细信息失败,数据库网络连接异常,任务号:{}", task.getTaskId());
|
||
return;
|
||
}
|
||
if(thisLocations.isEmpty()) {
|
||
logger.warn("下发移库任务查询库位详细信息异常,不存在详细信息,该库位可能不存在,任务号:{}", task.getTaskId());
|
||
Task taskForUpdate = new Task();
|
||
taskForUpdate.setTaskId(task.getTaskId());
|
||
taskForUpdate.setTaskStatus(WmsTaskStatus.EXCEPTION.getCode());
|
||
taskForUpdate.setRemark1("该库位不存在");
|
||
taskService.executeTask(taskForUpdate);
|
||
return;
|
||
}
|
||
Location thisLocation = thisLocations.get(0); // 找出当前库位详细信息
|
||
int depth = thisLocation.getDepth();
|
||
while (depth > 1) {
|
||
depth--;
|
||
/* 检查该库位有没有任务,若有则退出函数,若没有则检查有没有库存,若没有库存则继续,若有库存则生成一个移库任务,生成之后退出函数 */
|
||
Location beforLocationsQuery = new Location();
|
||
beforLocationsQuery.setAreaId(thisLocation.getAreaId());
|
||
beforLocationsQuery.setQueue(thisLocation.getQueue());
|
||
beforLocationsQuery.setLine(thisLocation.getLine());
|
||
beforLocationsQuery.setLayer(thisLocation.getLayer());
|
||
beforLocationsQuery.setDepth(depth);
|
||
List<Location> beforLocations = locationService.selLocations(beforLocationsQuery);
|
||
if(beforLocations == null) {
|
||
logger.warn("下发移库任务查询库位详细信息失败,数据库网络连接异常,任务号:{}", task.getTaskId());
|
||
return;
|
||
}
|
||
if(beforLocations.isEmpty()) {
|
||
return;
|
||
}
|
||
Location beforLocation = beforLocations.get(0); // 前一个库位
|
||
List<Task> notCompleteTasks = taskMapper.haveNotCompleteTask(beforLocation.getLocationId());
|
||
if(notCompleteTasks == null) {
|
||
return;
|
||
}
|
||
for(Task notCompleteTask : notCompleteTasks) {
|
||
String taskId = notCompleteTask.getTaskId();
|
||
if(!taskId.equals(task.getTaskId())) {
|
||
return;
|
||
}
|
||
}
|
||
// 检查是否有库存、
|
||
Stock stockQuery = new Stock();
|
||
stockQuery.setLocationId(beforLocation.getLocationId());
|
||
List<Stock> stocks = stockService.selStocks(stockQuery);
|
||
if(stocks == null) {
|
||
return;
|
||
}
|
||
if(stocks.isEmpty()) {
|
||
continue; // 没有库存,继续
|
||
}
|
||
/* 生成一个移库任务 */
|
||
/* 查找一个空库位 */
|
||
Location emptyLocation = new Location();
|
||
emptyLocation.setLocationStatus(LocationStatus.EMPTY.getCode());
|
||
emptyLocation.setAreaId(beforLocation.getAreaId());
|
||
List<Location> emptyLocations = locationMapper.selLocations(emptyLocation);
|
||
if(emptyLocations == null) {
|
||
return;
|
||
}
|
||
if(emptyLocations.isEmpty()){
|
||
return; // 移库没有可用库位
|
||
}
|
||
Location emptyLocationItem = locationUtils.checkCanUse(emptyLocations); // 取得新库位
|
||
if(emptyLocationItem == null) {
|
||
return; // 没有可用库位或者m库位存在干涉
|
||
}
|
||
// 锁定旧库位库存
|
||
stockMapper.updateStockStatusWithLocationId(beforLocation.getLocationId(), StockStatus.LOCK.getCode());
|
||
// 锁定新库位
|
||
Location locationForUpdate = new Location();
|
||
locationForUpdate.setLocationId(emptyLocationItem.getLocationId());
|
||
locationForUpdate.setLocationStatus(LocationStatus.OCCUPY.getCode());
|
||
locationMapper.modifyLocation(locationForUpdate);
|
||
// 构造移库任务
|
||
Task moveTask = new Task();
|
||
moveTask.setTaskId(UUID.randomUUID().toString());
|
||
moveTask.setTaskGroup(task.getTaskGroup());
|
||
moveTask.setTaskType(TaskType.MOVE.getCode());
|
||
moveTask.setOrigin(beforLocation.getLocationId());
|
||
moveTask.setDestination(emptyLocationItem.getLocationId());
|
||
moveTask.setVehicleNo(stocks.get(0).getVehicleId());
|
||
moveTask.setVehicleSize(0);
|
||
moveTask.setWeight(0.0);
|
||
moveTask.setTaskPriority(9);
|
||
moveTask.setTaskStatus(WmsTaskStatus.NEW.getCode());
|
||
taskService.addTask(moveTask);
|
||
return;
|
||
}
|
||
if(thisLocation.getAreaId() == 1) { // 宝开立库
|
||
List<WcsTask> request = new ArrayList<>();
|
||
// 创建发送的任务
|
||
WcsTask tempTask = new WcsTask();
|
||
tempTask.setTaskId(task.getTaskId());
|
||
tempTask.setTaskType(TaskType.MOVE.getCode());
|
||
tempTask.setOrigin(task.getOrigin());
|
||
tempTask.setDestination(task.getDestination());
|
||
tempTask.setVehicleNo(task.getVehicleNo());
|
||
tempTask.setVehicleSize(1);
|
||
tempTask.setWeight(task.getWeight() == null ? 0 : task.getWeight());
|
||
tempTask.setPriority(task.getTaskPriority());
|
||
request.add(tempTask);
|
||
// 发送任务
|
||
ResponseEntity result = JSON.parseObject(HttpUtils.sendHttpPostWithoutToken(UrlEnums.URL_WMS_TO_WCS_SEND_TASK.getValue(), JSON.toJSONString(request)), ResponseEntity.class);
|
||
if (result != null && Objects.equals(ResponseCode.OK.getCode(), result.getCode())) {
|
||
logger.info("下发立库移库任务成功,任务ID:{}", tempTask.getTaskId());
|
||
Task taskForUpdate = new Task();
|
||
taskForUpdate.setTaskId(task.getTaskId());
|
||
taskForUpdate.setTaskStatus(WmsTaskStatus.WAIT.getCode());
|
||
taskService.executeTask(taskForUpdate);
|
||
return;
|
||
}
|
||
logger.info("下发立库移库任务失败,任务ID:{},信息:{}", tempTask.getTaskId(), JSON.toJSONString(result));
|
||
} else {
|
||
String uuid = UUID.randomUUID().toString();
|
||
SendContainerTaskRequest request = new SendContainerTaskRequest();
|
||
request.setRequestid(uuid);
|
||
request.setKey(StringUtils.containerMd5(uuid).toUpperCase());
|
||
request.setWmstaskid(task.getTaskId());
|
||
request.setPalletno(task.getVehicleNo());
|
||
request.setTasktype("3"); // 移库
|
||
request.setFromcellno(task.getOrigin());
|
||
request.setTocellno(task.getDestination());
|
||
String requestString = JSON.toJSONString(request);
|
||
String responseString = HttpUtils.sendHttpPostWithoutToken(UrlEnums.URL_WMS_TO_WCS_CONTAINER_TASK.getValue(), requestString);
|
||
ContainerApiLocalResponse result = JSON.parseObject(responseString, ContainerApiLocalResponse.class);
|
||
if(result != null && result.getCode().equals("200")) {
|
||
// logger.info("下发四向车移库任务成功,任务ID:{}", task.getTaskId());
|
||
Task taskForUpdate = new Task();
|
||
taskForUpdate.setTaskId(task.getTaskId());
|
||
taskForUpdate.setTaskStatus(WmsTaskStatus.WAIT.getCode());
|
||
taskService.executeTask(taskForUpdate);
|
||
return;
|
||
}
|
||
// logger.info("下发四向车移库任务失败,任务ID:{},信息:{}", task.getTaskId(), JSON.toJSONString(result));
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
/**
|
||
* 每隔一秒检索一遍任务列表,同时向WCS下发任务
|
||
*/
|
||
// @Scheduled(fixedDelay = 1000)
|
||
public void executeTasks() {
|
||
try {
|
||
// 检索任务列表,查询状态为等待状态的任务
|
||
Task taskForQuery = new Task();
|
||
taskForQuery.setTaskStatus(WmsTaskStatus.NEW.getCode());
|
||
List<Task> tasks = taskService.selTasks(taskForQuery);
|
||
// 相同任务组的任务只发一次
|
||
List<String> taskGroupIds = new LinkedList<>();
|
||
List<WcsTask> request = new ArrayList<>();
|
||
if (!tasks.isEmpty()) {// 存在等待中的任务
|
||
for (Task task : tasks) {
|
||
if (taskGroupIds.contains(task.getTaskGroup())) {// 该taskGroup的任务已经发送过
|
||
task.setTaskStatus(WmsTaskStatus.WAIT.getCode());
|
||
continue;
|
||
}
|
||
// 创建发送的任务
|
||
WcsTask tempTask = new WcsTask();
|
||
tempTask.setTaskId(task.getTaskGroup());
|
||
if (TaskType.OUT.getCode().equals(task.getTaskType()) || TaskType.INVENTORY.getCode().equals(task.getTaskType())) {
|
||
tempTask.setTaskType(TaskType.OUT.getCode());
|
||
} else {
|
||
tempTask.setTaskType(TaskType.IN.getCode());
|
||
}
|
||
tempTask.setOrigin(task.getOrigin());
|
||
tempTask.setDestination(task.getDestination());
|
||
tempTask.setVehicleNo(task.getVehicleNo());
|
||
tempTask.setVehicleSize(1);
|
||
tempTask.setWeight(task.getWeight() == null ? 0 : task.getWeight());
|
||
tempTask.setPriority(task.getTaskPriority());
|
||
request.add(tempTask);
|
||
|
||
taskGroupIds.add(task.getTaskGroup());
|
||
task.setTaskStatus(WmsTaskStatus.WAIT.getCode());
|
||
}
|
||
if (request.isEmpty()) {
|
||
return;
|
||
}
|
||
// 发送任务
|
||
ResponseEntity result = JSON.parseObject(HttpUtils.sendHttpPostWithoutToken(UrlEnums.URL_WMS_TO_WCS_SEND_TASK.getValue(), JSON.toJSONString(request)), ResponseEntity.class);
|
||
if (result != null && Objects.equals(ResponseCode.OK.getCode(), result.getCode())) {
|
||
// 更新任务列表
|
||
for (Task task : tasks) {
|
||
// 更新任务
|
||
taskService.executeTask(task);
|
||
}
|
||
}
|
||
}
|
||
} catch (Exception exception) {
|
||
logger.error("发生异常:{}", exception.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 定期检查设备状态
|
||
* 5秒钟
|
||
*/
|
||
// @Scheduled(fixedDelay = 5000)
|
||
public void checkEquipmentStatus() {
|
||
|
||
}
|
||
|
||
/**
|
||
* 每天查询一次是否有过期的库存
|
||
* 每天晚上8点执行一次
|
||
*/
|
||
// @Scheduled(cron = "0 0 20 * * ?")
|
||
public void detectOutOfDateStock() {
|
||
logger.info("执行定时任务:查询过期库存");
|
||
List<Stock> outOfDateStocks = stockService.selStockOutOfDate();
|
||
if (!outOfDateStocks.isEmpty()) {
|
||
logger.info("过期库存数量不为0,准备更新过期库存");
|
||
for (Stock outOfDateStock : outOfDateStocks) {
|
||
try {
|
||
outOfDateStock.setGoodsStatus(GoodsStatus.OVERDUE.getCode());
|
||
stockService.modifyStock(outOfDateStock);
|
||
logger.info("过期库存更新成功");
|
||
} catch (Exception e) {
|
||
logger.error("过期库存更新异常:{}", e.getMessage());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 每天查询一次是否有入库后长期未使用的库存
|
||
* 每天晚上9点执行一次
|
||
*/
|
||
// @Scheduled(cron = "0 0 21 * * ?")
|
||
public void detectStockLongTimeNoUse() {
|
||
logger.info("执行定时任务:查询是否有入库后长期未使用的库存");
|
||
List<Stock> stocksLongTimeNoUse = stockService.selStockLongTimeNoUse(7);
|
||
if (!stocksLongTimeNoUse.isEmpty()) {
|
||
logger.info("有入库后长期未使用的库存, 准备更新库存状态");
|
||
for (Stock stockLongTimeNoUse : stocksLongTimeNoUse) {
|
||
try {
|
||
stockLongTimeNoUse.setGoodsStatus(GoodsStatus.SCRAP.getCode());
|
||
stockService.modifyStock(stockLongTimeNoUse);
|
||
logger.info("长时间未使用库存状态更新成功");
|
||
} catch (Exception e) {
|
||
logger.error("长时间未使用库存状态更新异常:{}", e.getMessage());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 每天查询一次是否有过期记录
|
||
* 每天晚上10点执行一次
|
||
*/
|
||
//@Scheduled(cron = "0 0 22 * * ?")
|
||
public void deleteOutOfDateData() {
|
||
logger.info("执行定时任务:删除过期数据");
|
||
taskRecordService.deleteTaskRecordRegularly();
|
||
}
|
||
} |