fix:空木箱移库加并发锁

This commit is contained in:
zhouz
2025-09-28 10:10:45 +08:00
parent f2fa6fbb09
commit 7c2e6d7d7d

View File

@@ -5,11 +5,13 @@ import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nl.b_lms.sch.point.dao.SchBasePoint;
@@ -37,12 +39,15 @@ import org.nl.wms.ext.mes.service.LmsToMesService;
import org.nl.wms.pda.mps.eum.RegionTypeEnum;
import org.nl.wms.pdm.bi.service.SubpackagerelationService;
import org.nl.wms.sch.manage.TaskStatusEnum;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
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.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
@@ -91,6 +96,9 @@ public class InBoxManageServiceImpl implements InBoxManageService {
@Autowired
private InBussManageService inBussManageService;
@Autowired
private RedissonClient redissonClient;
@Override
@Transactional(rollbackFor = Exception.class)
public void inBox(JSONObject whereJson) {
@@ -677,6 +685,7 @@ public class InBoxManageServiceImpl implements InBoxManageService {
return result;
}
/**
* 找一个空仓位(木箱区)
*
@@ -689,6 +698,7 @@ public class InBoxManageServiceImpl implements InBoxManageService {
* }
* @return JSONObject 仓位对象
*/
@SneakyThrows
public JSONObject getStruct(JSONObject jsonParam) {
// 仓位表
WQLObject attrTab = WQLObject.getWQLObject("st_ivt_structattr");
@@ -699,6 +709,12 @@ public class InBoxManageServiceImpl implements InBoxManageService {
*/
String block_num = jsonParam.getString("move_block_num");
JSONObject jsonAttr = new JSONObject();
RLock lock = redissonClient.getLock("getEmptyBox-----"+block_num);
boolean tryLock = lock.tryLock(0, TimeUnit.SECONDS);
try {
if (tryLock) {
JSONArray structArray = new JSONArray();
String height = jsonParam.getString("height");
@@ -745,7 +761,7 @@ public class InBoxManageServiceImpl implements InBoxManageService {
// 确定仓位: 找到仓位
jsonParam.put("row_num", row_num);
JSONObject jsonAttr = queryStruct(jsonParam);
jsonAttr = queryStruct(jsonParam);
// 如果是移库空并且仓位为空则报错
if (ObjectUtil.isNotEmpty(block_num) && ObjectUtil.isEmpty(jsonAttr)) {
@@ -788,6 +804,15 @@ public class InBoxManageServiceImpl implements InBoxManageService {
}
notInRowList.clear();
notInBlockList.clear();
} else {
throw new BadRequestException("存在正在分配的空木箱任务,请稍后再试!");
}
} finally {
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
return jsonAttr;
}