Explorar o código

【微浦MES-EIS 镭雕获取接口增加工单号:synchronized 同步锁,支持多台机器同时镭雕一个工单获取条码号】

xiaost hai 4 días
pai
achega
42275d4b2c

+ 17 - 5
src/main/java/com/uas/eis/serviceImpl/MESHelperServiceImpl.java

@@ -42,6 +42,7 @@ import javax.xml.parsers.ParserConfigurationException;
 
 import org.xml.sax.InputSource;
 import java.io.StringReader;
+import java.util.concurrent.ConcurrentHashMap;
 
 @Service
 public class MESHelperServiceImpl implements MESHelperService {
@@ -2025,6 +2026,7 @@ public class MESHelperServiceImpl implements MESHelperService {
 		}
 		return map;
 	}
+	private final ConcurrentHashMap<String, Object> lockMap = new ConcurrentHashMap<>();
 	/**
 	 * 微浦MES获取镭雕序列号
 	 * @param
@@ -2034,7 +2036,6 @@ public class MESHelperServiceImpl implements MESHelperService {
 	 */
 	@Override
 	public Result<Map<String,Object>> getRadiumSN(String macode ,int combineqty) {
-		Map<String,Object> remap = new HashMap<>();
 		if(!StringUtil.hasText(macode)){
 			return ApiResponse.failRspResult("输入参数macode(工单号)不能为空!");
 		}
@@ -2044,7 +2045,18 @@ public class MESHelperServiceImpl implements MESHelperService {
 		/*if(combineqty>20){
 			return ApiResponse.failRspResult("输入参数combineqty(拼版数):"+combineqty+"值不合理!");
 		}*/
-		//判断工单号是否存在
+		// 按工单号获取锁对象
+		Object lock = lockMap.computeIfAbsent(macode, k -> new Object());
+
+		synchronized (lock) {
+			// 执行业务逻辑
+			return doGetRadiumSN(macode, combineqty);
+		}
+	}
+
+	private Result<Map<String,Object>> doGetRadiumSN(String macode ,int combineqty){
+		Map<String,Object> remap = new HashMap<>();
+          //判断工单号是否存在
 		SqlRowList rs = baseDao.queryForRowSet("select * from make where ma_code=?",macode);
 		if(rs.next()){
 			int cn = baseDao.getJdbcTemplate().queryForObject("select count(1) from MAKESNRULEDETAIL where msd_makecode=? and msd_type='before'",Integer.class,macode);
@@ -2055,9 +2067,9 @@ public class MESHelperServiceImpl implements MESHelperService {
 			rs = baseDao.queryForRowSet("select * from (select MSD_SNCODE sncode from MAKESNRULEDETAIL where nvl(msd_isget,0)=0 " +
 					" and msd_makecode=? and msd_type='before' order by msd_id) where rownum<=?",macode,combineqty);
 			if(rs.next()){
-               if(rs.getResultList().size()<combineqty){
-				   return ApiResponse.failRspResult("工单号:"+macode+",剩余可获取镭雕条码数为:"+rs.getResultList().size()+",不足拼版数:"+combineqty+"!");
-			   }
+				if(rs.getResultList().size()<combineqty){
+					return ApiResponse.failRspResult("工单号:"+macode+",剩余可获取镭雕条码数为:"+rs.getResultList().size()+",不足拼版数:"+combineqty+"!");
+				}
 				remap.put("macode",macode);
 				remap.put("combineqty",combineqty);
 				List<Map<String, Object>> list = new ArrayList<>();