Commit 3d40fb333eda68c17d1d36b4695ef4b4494a3a5f

Authored by 易文鹏
1 parent 7aa98c40

fix: 动态分配库位调整:方法加上事物,数据库操作加上对数据结果判断,清除魔法值,查询相同标记不为4就抛异常

src/main/java/com/huaheng/api/wcs/service/warecellAllocation/LocationAllocationServiceImpl.java
@@ -20,6 +20,7 @@ import com.huaheng.pc.config.zone.domain.Zone; @@ -20,6 +20,7 @@ import com.huaheng.pc.config.zone.domain.Zone;
20 import com.huaheng.pc.config.zone.service.ZoneService; 20 import com.huaheng.pc.config.zone.service.ZoneService;
21 import com.huaheng.pc.task.taskHeader.service.TaskHeaderService; 21 import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
22 import org.springframework.stereotype.Service; 22 import org.springframework.stereotype.Service;
  23 +import org.springframework.transaction.annotation.Transactional;
23 import org.springframework.util.StopWatch; 24 import org.springframework.util.StopWatch;
24 25
25 import javax.annotation.Resource; 26 import javax.annotation.Resource;
@@ -28,6 +29,7 @@ import java.time.format.DateTimeFormatter; @@ -28,6 +29,7 @@ import java.time.format.DateTimeFormatter;
28 import java.util.*; 29 import java.util.*;
29 import java.util.stream.Collectors; 30 import java.util.stream.Collectors;
30 31
  32 +import static com.huaheng.common.constant.QuantityConstant.*;
31 import static java.util.stream.Collectors.toList; 33 import static java.util.stream.Collectors.toList;
32 34
33 @Service 35 @Service
@@ -48,20 +50,14 @@ public class LocationAllocationServiceImpl implements LocationAllocationService @@ -48,20 +50,14 @@ public class LocationAllocationServiceImpl implements LocationAllocationService
48 @Resource 50 @Resource
49 private ConfigService configService; 51 private ConfigService configService;
50 52
  53 + @Transactional
51 @Override 54 @Override
52 public String allocation(int high, String area, String roadWay, String containerCode, Integer frequencyLocation, 55 public String allocation(int high, String area, String roadWay, String containerCode, Integer frequencyLocation,
53 Integer emptyContainerTask, Integer isSelfCreated, boolean isFlammable) { 56 Integer emptyContainerTask, Integer isSelfCreated, boolean isFlammable) {
54 57
55 - //容器  
56 - Container container = containerService.getOne(new LambdaQueryWrapper<Container>().eq(Container::getCode, containerCode));  
57 - //容器类型  
58 - ContainerType containerType = containerTypeService.getOne(new LambdaQueryWrapper<ContainerType>().eq(ContainerType::getCode, container.getContainerType()));  
59 - if (containerType == null) {  
60 - throw new ServiceException("容器类型不存在");  
61 - }  
62 //二期5号巷道 58 //二期5号巷道
63 if (roadWay.equals("5")) { 59 if (roadWay.equals("5")) {
64 - return fiveStacker(high, containerType.getCode()); 60 + return fiveStacker(high, containerCode);
65 } else { 61 } else {
66 return doubleRk(area, roadWay, high, frequencyLocation, emptyContainerTask, isSelfCreated, isFlammable); 62 return doubleRk(area, roadWay, high, frequencyLocation, emptyContainerTask, isSelfCreated, isFlammable);
67 } 63 }
@@ -70,13 +66,15 @@ public class LocationAllocationServiceImpl implements LocationAllocationService @@ -70,13 +66,15 @@ public class LocationAllocationServiceImpl implements LocationAllocationService
70 66
71 //一厂二期5号堆垛机,大小托盘 67 //一厂二期5号堆垛机,大小托盘
72 //分配库位排序,(层-外侧-内侧-列) 68 //分配库位排序,(层-外侧-内侧-列)
73 - private String fiveStacker(int high, String containerTypeCode) { 69 + private String fiveStacker(int high, String containerCode) {
  70 + Container container = containerService.getOne(new LambdaQueryWrapper<Container>().eq(Container::getCode, containerCode));
  71 + String containerType = container.getContainerType();
74 //判断预留库位 72 //判断预留库位
75 int reserveNumber = getReserveNumber(); 73 int reserveNumber = getReserveNumber();
76 LambdaQueryWrapper<Location> wrapper = Wrappers.lambdaQuery(); 74 LambdaQueryWrapper<Location> wrapper = Wrappers.lambdaQuery();
77 wrapper 75 wrapper
78 .eq(Location::getRoadway, 5) 76 .eq(Location::getRoadway, 5)
79 - .eq(Location::getStatus, QuantityConstant.STATUS_LOCATION_EMPTY) 77 + .eq(Location::getStatus, STATUS_LOCATION_EMPTY)
80 .eq(Location::getHigh, high) 78 .eq(Location::getHigh, high)
81 .eq(Location::getContainerCode, "") 79 .eq(Location::getContainerCode, "")
82 .ne(Location::getIColumn, "26") 80 .ne(Location::getIColumn, "26")
@@ -91,17 +89,17 @@ public class LocationAllocationServiceImpl implements LocationAllocationService @@ -91,17 +89,17 @@ public class LocationAllocationServiceImpl implements LocationAllocationService
91 } 89 }
92 90
93 //大托盘占两个库位 91 //大托盘占两个库位
94 - if (containerTypeCode.equals("X") || containerTypeCode.equals("W")) { 92 + if (containerType.equals(CONTAINER_TYPE_X) || containerType.equals(CONTAINER_TYPE_W)) {
95 for (Location location : list) { 93 for (Location location : list) {
96 - if (location.getIColumn() % 2 != 0 && location.getTemporaryType().charAt(0) != '1') { 94 + if (location.getIColumn() % 2 != 0 && location.getTemporaryType().startsWith(SMALL_CONTAINER_TYPE)) {
97 Location rightLocation = locationService.getRightEmptyLocation(location); 95 Location rightLocation = locationService.getRightEmptyLocation(location);
98 if (rightLocation != null) { 96 if (rightLocation != null) {
99 //禁用库位 97 //禁用库位
100 - int updateCount = locationService.updateStatusNew(rightLocation.getCode(), "CS0001", QuantityConstant.STATUS_LOCATION_DISABLE, QuantityConstant.STATUS_LOCATION_EMPTY); 98 + int updateCount = locationService.updateStatusNew(rightLocation.getCode(), DEFAULT_WAREHOUSE, STATUS_LOCATION_DISABLE, STATUS_LOCATION_EMPTY);
101 if (updateCount != 1) { 99 if (updateCount != 1) {
102 throw new ServiceException("禁用库位失败"); 100 throw new ServiceException("禁用库位失败");
103 } 101 }
104 - if (location.getTemporaryType().equals("0")) { 102 + if (location.getTemporaryType().equals(UNDEFINED_CONTAINER_TYPE)) {
105 //设置相邻临时库位类型 103 //设置相邻临时库位类型
106 setTemporaryLocationType(location, true); 104 setTemporaryLocationType(location, true);
107 } 105 }
@@ -113,12 +111,12 @@ public class LocationAllocationServiceImpl implements LocationAllocationService @@ -113,12 +111,12 @@ public class LocationAllocationServiceImpl implements LocationAllocationService
113 } else { 111 } else {
114 //小托盘不分配25列 112 //小托盘不分配25列
115 list = list.stream().filter(location -> location.getIColumn() != 25 113 list = list.stream().filter(location -> location.getIColumn() != 25
116 - && location.getTemporaryType().charAt(0) != '2').collect(toList()); 114 + && !String.valueOf(location.getTemporaryType()).startsWith(BIG_CONTAINER_TYPE)).collect(Collectors.toList());
117 if (list.isEmpty()) { 115 if (list.isEmpty()) {
118 return null; 116 return null;
119 } 117 }
120 Location location = list.get(0); 118 Location location = list.get(0);
121 - if (location.getTemporaryType().equals("0")) { 119 + if (location.getTemporaryType().equals(UNDEFINED_CONTAINER_TYPE)) {
122 //设置相邻临时库位类型 120 //设置相邻临时库位类型
123 setTemporaryLocationType(location, false); 121 setTemporaryLocationType(location, false);
124 } 122 }
@@ -129,7 +127,7 @@ public class LocationAllocationServiceImpl implements LocationAllocationService @@ -129,7 +127,7 @@ public class LocationAllocationServiceImpl implements LocationAllocationService
129 //设置相邻临时库位类型 127 //设置相邻临时库位类型
130 void setTemporaryLocationType(Location location, boolean isBigContainer) { 128 void setTemporaryLocationType(Location location, boolean isBigContainer) {
131 List<Location> setTemporaryTypeList = new ArrayList<>(); 129 List<Location> setTemporaryTypeList = new ArrayList<>();
132 - String prefix = isBigContainer ? "2-" : "1-"; 130 + String prefix = isBigContainer ? BIG_CONTAINER_TYPE : SMALL_CONTAINER_TYPE;
133 131
134 setTemporaryTypeList.add(location); 132 setTemporaryTypeList.add(location);
135 setTemporaryTypeList.add(getLocationByOffset(location, 0, 1)); 133 setTemporaryTypeList.add(getLocationByOffset(location, 0, 1));
@@ -138,8 +136,10 @@ public class LocationAllocationServiceImpl implements LocationAllocationService @@ -138,8 +136,10 @@ public class LocationAllocationServiceImpl implements LocationAllocationService
138 136
139 for (Location temporaryLocation : setTemporaryTypeList) { 137 for (Location temporaryLocation : setTemporaryTypeList) {
140 if (temporaryLocation != null) { 138 if (temporaryLocation != null) {
141 - temporaryLocation.setTemporaryType(prefix + location.getCode() + "-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH:mm:ss")));  
142 - locationService.updateById(temporaryLocation); 139 + temporaryLocation.setTemporaryType(prefix + "-" + location.getCode() + "-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH:mm:ss")));
  140 + if (locationService.updateById(temporaryLocation)) {
  141 + throw new ServiceException("设置临时库位类型失败");
  142 + }
143 } 143 }
144 } 144 }
145 } 145 }
@@ -164,7 +164,7 @@ public class LocationAllocationServiceImpl implements LocationAllocationService @@ -164,7 +164,7 @@ public class LocationAllocationServiceImpl implements LocationAllocationService
164 LambdaQueryWrapper<Location> wrapper = Wrappers.lambdaQuery(); 164 LambdaQueryWrapper<Location> wrapper = Wrappers.lambdaQuery();
165 wrapper.eq(Location::getArea, area) 165 wrapper.eq(Location::getArea, area)
166 .eq(Location::getRoadway, roadWay) 166 .eq(Location::getRoadway, roadWay)
167 - .eq(Location::getStatus, QuantityConstant.STATUS_LOCATION_EMPTY) 167 + .eq(Location::getStatus, STATUS_LOCATION_EMPTY)
168 .eq(Location::getHigh, high) 168 .eq(Location::getHigh, high)
169 .eq(Location::getContainerCode, "") 169 .eq(Location::getContainerCode, "")
170 .orderByAsc(Location::getILayer) 170 .orderByAsc(Location::getILayer)
@@ -196,9 +196,9 @@ public class LocationAllocationServiceImpl implements LocationAllocationService @@ -196,9 +196,9 @@ public class LocationAllocationServiceImpl implements LocationAllocationService
196 LambdaQueryWrapper<Location> locationLambda = Wrappers.lambdaQuery(); 196 LambdaQueryWrapper<Location> locationLambda = Wrappers.lambdaQuery();
197 locationLambda.eq(Location::getArea, area) 197 locationLambda.eq(Location::getArea, area)
198 .eq(Location::getRoadway, roadWay) 198 .eq(Location::getRoadway, roadWay)
199 - .eq(Location::getStatus, QuantityConstant.STATUS_LOCATION_EMPTY) 199 + .eq(Location::getStatus, STATUS_LOCATION_EMPTY)
200 .eq(Location::getHigh, high) 200 .eq(Location::getHigh, high)
201 - .eq(Location::getRowFlag, QuantityConstant.ROW_OUT)//外侧 201 + .eq(Location::getRowFlag, ROW_OUT)//外侧
202 .eq(Location::getContainerCode, "") 202 .eq(Location::getContainerCode, "")
203 .last(" limit 20"); 203 .last(" limit 20");
204 204
@@ -217,7 +217,7 @@ public class LocationAllocationServiceImpl implements LocationAllocationService @@ -217,7 +217,7 @@ public class LocationAllocationServiceImpl implements LocationAllocationService
217 } 217 }
218 218
219 //频繁库位 219 //频繁库位
220 - String str = ""; 220 + String str;
221 if (frequencyLocation == 1) { 221 if (frequencyLocation == 1) {
222 str = "frequencyLocation asc"; 222 str = "frequencyLocation asc";
223 //locationLambda.orderByAsc(Location::getFrequencyLocation); //升序 223 //locationLambda.orderByAsc(Location::getFrequencyLocation); //升序
@@ -258,9 +258,9 @@ public class LocationAllocationServiceImpl implements LocationAllocationService @@ -258,9 +258,9 @@ public class LocationAllocationServiceImpl implements LocationAllocationService
258 locationLambda = Wrappers.lambdaQuery(); 258 locationLambda = Wrappers.lambdaQuery();
259 locationLambda.eq(Location::getArea, area) 259 locationLambda.eq(Location::getArea, area)
260 .eq(StringUtils.isNotEmpty(roadWay), Location::getRoadway, roadWay) 260 .eq(StringUtils.isNotEmpty(roadWay), Location::getRoadway, roadWay)
261 - .eq(Location::getStatus, QuantityConstant.STATUS_LOCATION_EMPTY) 261 + .eq(Location::getStatus, STATUS_LOCATION_EMPTY)
262 .eq(Location::getHigh, high) 262 .eq(Location::getHigh, high)
263 - .eq(Location::getRowFlag, QuantityConstant.ROW_IN) 263 + .eq(Location::getRowFlag, ROW_IN)
264 .eq(Location::getContainerCode, "") 264 .eq(Location::getContainerCode, "")
265 .last(" limit 20"); 265 .last(" limit 20");
266 if (emptyContainerTask == 1) { 266 if (emptyContainerTask == 1) {
@@ -318,7 +318,7 @@ public class LocationAllocationServiceImpl implements LocationAllocationService @@ -318,7 +318,7 @@ public class LocationAllocationServiceImpl implements LocationAllocationService
318 318
319 //获取双伸位预留库位数 319 //获取双伸位预留库位数
320 private int getReserveNumber() { 320 private int getReserveNumber() {
321 - String value = configService.getKey(QuantityConstant.DOUBLE_FORK_RESERVE_LOCATION); 321 + String value = configService.getKey(DOUBLE_FORK_RESERVE_LOCATION);
322 return StringUtils.isNotEmpty(value) ? Integer.parseInt(value) : 0; 322 return StringUtils.isNotEmpty(value) ? Integer.parseInt(value) : 0;
323 } 323 }
324 324
src/main/java/com/huaheng/common/constant/QuantityConstant.java
@@ -545,4 +545,19 @@ public class QuantityConstant { @@ -545,4 +545,19 @@ public class QuantityConstant {
545 public static final String DEFAULT_DEPT = "01030101"; 545 public static final String DEFAULT_DEPT = "01030101";
546 public static final String DEFAULT_STOCK = "035"; 546 public static final String DEFAULT_STOCK = "035";
547 547
  548 +
  549 + /**
  550 + * 动态分配库位
  551 + */
  552 + public static final String CONTAINER_TYPE_X = "X";
  553 + public static final String CONTAINER_TYPE_W = "W";
  554 +
  555 + /**
  556 + * 库位临时托盘类型
  557 + */
  558 + public static final String UNDEFINED_CONTAINER_TYPE = "0";
  559 + public static final String SMALL_CONTAINER_TYPE = "1";
  560 + public static final String BIG_CONTAINER_TYPE = "2";
  561 +
  562 +
548 } 563 }
src/main/java/com/huaheng/pc/config/location/service/LocationServiceImpl.java
@@ -33,6 +33,8 @@ import com.huaheng.pc.task.taskHeader.service.TaskHeaderService; @@ -33,6 +33,8 @@ import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
33 import io.swagger.models.auth.In; 33 import io.swagger.models.auth.In;
34 import org.springframework.stereotype.Service; 34 import org.springframework.stereotype.Service;
35 35
  36 +import static com.huaheng.common.constant.QuantityConstant.*;
  37 +
36 import javax.annotation.Resource; 38 import javax.annotation.Resource;
37 import java.text.MessageFormat; 39 import java.text.MessageFormat;
38 import java.util.*; 40 import java.util.*;
@@ -783,36 +785,39 @@ public class LocationServiceImpl extends ServiceImpl&lt;LocationMapper, Location&gt; i @@ -783,36 +785,39 @@ public class LocationServiceImpl extends ServiceImpl&lt;LocationMapper, Location&gt; i
783 @Override 785 @Override
784 public void unbanRightLocationAndUnmark(String containerType, String locationCode) { 786 public void unbanRightLocationAndUnmark(String containerType, String locationCode) {
785 //解禁用右侧库位 787 //解禁用右侧库位
786 - Location location = locationService.getLocationByCode(locationCode, "CS0001");  
787 - if (("X".equals(containerType) || "W".equals(containerType)) && "5".equals(location.getRoadway())) { 788 + Location location = locationService.getLocationByCode(locationCode, DEFAULT_WAREHOUSE);
  789 + if ((CONTAINER_TYPE_X.equals(containerType) || CONTAINER_TYPE_W.equals(containerType)) && "5".equals(location.getRoadway())) {
788 Location rightLocation = locationService.getRightDisableLocation(location); 790 Location rightLocation = locationService.getRightDisableLocation(location);
789 if (rightLocation != null) { 791 if (rightLocation != null) {
790 - int updateCount = locationService.updateStatusNew(rightLocation.getCode(), "CS0001", QuantityConstant.STATUS_LOCATION_EMPTY, QuantityConstant.STATUS_LOCATION_DISABLE); 792 + int updateCount = locationService.updateStatusNew(rightLocation.getCode(), DEFAULT_WAREHOUSE, STATUS_LOCATION_EMPTY, STATUS_LOCATION_DISABLE);
791 if (updateCount != 1) { 793 if (updateCount != 1) {
792 throw new ServiceException("重复分配库位时,解锁禁用失败!"); 794 throw new ServiceException("重复分配库位时,解锁禁用失败!");
793 } 795 }
794 } 796 }
795 } 797 }
796 798
797 - //清除库位的上下左右库位标记  
798 - if (location.getTemporaryType().equals("0")) { 799 + //清除就近4个库位的标记
  800 + if (location.getTemporaryType().equals(UNDEFINED_CONTAINER_TYPE)) {
799 throw new ServiceException("库位未被标记异常!"); 801 throw new ServiceException("库位未被标记异常!");
800 } 802 }
801 List<Location> list = locationService.list(new LambdaQueryWrapper<Location>().eq(Location::getTemporaryType, location.getTemporaryType())); 803 List<Location> list = locationService.list(new LambdaQueryWrapper<Location>().eq(Location::getTemporaryType, location.getTemporaryType()));
802 - if (!list.isEmpty()) {  
803 - boolean flat = true;  
804 - for (Location l : list) {  
805 - if (StringUtils.isNotEmpty(l.getContainerCode()) || !l.getStatus().equals(QuantityConstant.STATUS_LOCATION_EMPTY)) {  
806 - flat = false;  
807 - break;  
808 - }  
809 - }  
810 - if (flat) {  
811 - locationService.update(new LambdaUpdateWrapper<Location>()  
812 - .set(Location::getTemporaryType, "0").in(Location::getCode, list.stream()  
813 - .map(Location::getCode).collect(Collectors.toList()))); 804 + if (list.size() != 4) {
  805 + throw new ServiceException("库位标记异常,相同标记未达4个:" + list.size());
  806 + }
  807 +
  808 + //4个库位都是空的,并且状态位空,才能清除标记
  809 + boolean flat = true;
  810 + for (Location l : list) {
  811 + if (StringUtils.isNotEmpty(l.getContainerCode()) || !l.getStatus().equals(STATUS_LOCATION_EMPTY)) {
  812 + flat = false;
  813 + break;
814 } 814 }
815 } 815 }
  816 + if (flat) {
  817 + locationService.update(new LambdaUpdateWrapper<Location>()
  818 + .set(Location::getTemporaryType, UNDEFINED_CONTAINER_TYPE).in(Location::getCode, list.stream()
  819 + .map(Location::getCode).collect(Collectors.toList())));
  820 + }
816 821
817 } 822 }
818 823