|
@@ -1,6 +1,7 @@
|
|
|
package com.yc.ship.module.product.service.voyagestockdistribute;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.yc.ship.framework.common.exception.ServiceException;
|
|
|
import com.yc.ship.framework.common.pojo.PageResult;
|
|
|
import com.yc.ship.framework.common.util.collection.CollectionUtils;
|
|
|
import com.yc.ship.framework.common.util.object.BeanUtils;
|
|
@@ -10,11 +11,15 @@ import com.yc.ship.module.product.dal.dataobject.voyagestockdistribute.VoyageSto
|
|
|
import com.yc.ship.module.product.dal.dataobject.voyagestockdistribute.VoyageStockDistributeRoomDO;
|
|
|
import com.yc.ship.module.product.dal.mysql.voyagestockdistribute.VoyageStockDistributeNewMapper;
|
|
|
import com.yc.ship.module.product.enums.DistributorOrStoreEnum;
|
|
|
+import com.yc.ship.module.product.framework.lock.ProductRedisKeyConstants;
|
|
|
import com.yc.ship.module.product.service.voyagestockdetail.VoyageStockDetailService;
|
|
|
import com.yc.ship.module.resource.api.room.RoomApi;
|
|
|
import com.yc.ship.module.resource.api.room.dto.RoomRespDTO;
|
|
|
import com.yc.ship.module.resource.dal.dataobject.roommodel.ResourceRoomModelDO;
|
|
|
import com.yc.ship.module.resource.service.roommodel.ResourceRoomModelService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.redisson.api.RLock;
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
@@ -24,6 +29,7 @@ import java.math.BigDecimal;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
@@ -34,13 +40,12 @@ import java.util.stream.Collectors;
|
|
|
*/
|
|
|
@Service
|
|
|
@Validated
|
|
|
+@Slf4j
|
|
|
public class VoyageStockDistributeNewServiceImpl implements VoyageStockDistributeNewService {
|
|
|
|
|
|
-
|
|
|
@Resource
|
|
|
private VoyageStockDistributeNewMapper voyageStockDistributeNewMapper;
|
|
|
|
|
|
-
|
|
|
@Resource
|
|
|
private VoyageStockDistributeRoomService voyageStockDistributeRoomService;
|
|
|
|
|
@@ -52,7 +57,8 @@ public class VoyageStockDistributeNewServiceImpl implements VoyageStockDistribut
|
|
|
|
|
|
@Resource
|
|
|
private VoyageStockDetailService voyageStockDetailService;
|
|
|
-
|
|
|
+ @Resource
|
|
|
+ private RedissonClient redissonClient;
|
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@@ -60,55 +66,68 @@ public class VoyageStockDistributeNewServiceImpl implements VoyageStockDistribut
|
|
|
public void createVoyageStockDistributeNew(List<VoyageStockDistributeNewSaveReqVO> createReqVO) {
|
|
|
BigDecimal zero = BigDecimal.ZERO;
|
|
|
Long voyageId = createReqVO.get(0).getVoyageId();
|
|
|
- Integer type = createReqVO.get(0).getType();
|
|
|
- //删除分配的房间
|
|
|
- voyageStockDistributeRoomService.deleteByVoyageId(voyageId, type);
|
|
|
- // 分配的房间列表
|
|
|
- List<VoyageStockDistributeRoomDO> roomList = new ArrayList<>();
|
|
|
- createReqVO.stream().forEach(item -> {
|
|
|
- if(!CollectionUtils.isAnyEmpty(item.getRoomList())) {
|
|
|
- roomList.addAll(BeanUtils.toBean(item.getRoomList(), VoyageStockDistributeRoomDO.class));
|
|
|
- }
|
|
|
- });
|
|
|
- voyageStockDistributeRoomService.createVoyageStockDistributeRoomBatch(roomList);
|
|
|
- //修改或插入库存表
|
|
|
- List<VoyageStockDistributeNewDO> list = voyageStockDistributeNewMapper.selectListByVoyageId(voyageId, type);
|
|
|
- if(CollectionUtils.isAnyEmpty(list)) { // 为空则新增
|
|
|
- list = BeanUtils.toBean(createReqVO, VoyageStockDistributeNewDO.class);
|
|
|
- list.stream().forEach(item -> {
|
|
|
- item.setBookNum(zero);
|
|
|
- });
|
|
|
- if(!CollectionUtils.isAnyEmpty(list)) {
|
|
|
- voyageStockDistributeNewMapper.insertBatch(list);
|
|
|
- }
|
|
|
- }else {
|
|
|
- Map<String, BigDecimal> map = CollectionUtils.convertMap(createReqVO, item -> item.getObjectId() + "_" + item.getRoomModelId() + "_" + item.getFloor(), item -> item.getNum());
|
|
|
- Map<String, VoyageStockDistributeNewSaveReqVO> mapVO = CollectionUtils.convertMap(createReqVO, item -> item.getObjectId() + "_" + item.getRoomModelId() + "_" + item.getFloor());
|
|
|
- List<String> existList = CollectionUtils.convertList(list, item -> item.getObjectId() + "_" + item.getRoomModelId() + "_" + item.getFloor());
|
|
|
- Map<String, VoyageStockDistributeNewDO> existMap = CollectionUtils.convertMap(list, item -> item.getObjectId() + "_" + item.getRoomModelId() + "_" + item.getFloor());
|
|
|
- List<VoyageStockDistributeNewDO> insertList = new ArrayList<>();// 插入数据
|
|
|
- List<VoyageStockDistributeNewDO> updateList = new ArrayList<>(); // 修改数据
|
|
|
- map.keySet().stream().forEach(key -> {
|
|
|
- if(existList.contains(key)) { //存在,做修改
|
|
|
- VoyageStockDistributeNewDO voyageStockDistributeNewDO = existMap.get(key);
|
|
|
- voyageStockDistributeNewDO.setNum(map.get(key));
|
|
|
- voyageStockDistributeNewDO.setRoomModelNum(mapVO.get(key).getRoomModelNum());
|
|
|
- updateList.add(voyageStockDistributeNewDO);
|
|
|
- }else { //不存在,做插入
|
|
|
- VoyageStockDistributeNewDO voyageStockDistributeNewDO = BeanUtils.toBean(mapVO.get(key), VoyageStockDistributeNewDO.class);
|
|
|
- insertList.add(voyageStockDistributeNewDO);
|
|
|
+ //现在给整个航次的库存加锁
|
|
|
+ RLock lock = redissonClient.getLock(String.format(ProductRedisKeyConstants.STOCK_REDIS_KEY_PREFIX, voyageId));
|
|
|
+ try {
|
|
|
+ if(lock.tryLock(60,90, TimeUnit.SECONDS)) {
|
|
|
+ Integer type = createReqVO.get(0).getType();
|
|
|
+ //删除分配的房间
|
|
|
+ voyageStockDistributeRoomService.deleteByVoyageId(voyageId, type);
|
|
|
+ // 分配的房间列表
|
|
|
+ List<VoyageStockDistributeRoomDO> roomList = new ArrayList<>();
|
|
|
+ createReqVO.stream().forEach(item -> {
|
|
|
+ if(!CollectionUtils.isAnyEmpty(item.getRoomList())) {
|
|
|
+ roomList.addAll(BeanUtils.toBean(item.getRoomList(), VoyageStockDistributeRoomDO.class));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ voyageStockDistributeRoomService.createVoyageStockDistributeRoomBatch(roomList);
|
|
|
+ //修改或插入库存表
|
|
|
+ List<VoyageStockDistributeNewDO> list = voyageStockDistributeNewMapper.selectListByVoyageId(voyageId, type);
|
|
|
+ if(CollectionUtils.isAnyEmpty(list)) { // 为空则新增
|
|
|
+ list = BeanUtils.toBean(createReqVO, VoyageStockDistributeNewDO.class);
|
|
|
+ list.stream().forEach(item -> {
|
|
|
+ item.setBookNum(zero);
|
|
|
+ });
|
|
|
+ if(!CollectionUtils.isAnyEmpty(list)) {
|
|
|
+ voyageStockDistributeNewMapper.insertBatch(list);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ Map<String, BigDecimal> map = CollectionUtils.convertMap(createReqVO, item -> item.getObjectId() + "_" + item.getRoomModelId() + "_" + item.getFloor(), item -> item.getNum());
|
|
|
+ Map<String, VoyageStockDistributeNewSaveReqVO> mapVO = CollectionUtils.convertMap(createReqVO, item -> item.getObjectId() + "_" + item.getRoomModelId() + "_" + item.getFloor());
|
|
|
+ List<String> existList = CollectionUtils.convertList(list, item -> item.getObjectId() + "_" + item.getRoomModelId() + "_" + item.getFloor());
|
|
|
+ Map<String, VoyageStockDistributeNewDO> existMap = CollectionUtils.convertMap(list, item -> item.getObjectId() + "_" + item.getRoomModelId() + "_" + item.getFloor());
|
|
|
+ List<VoyageStockDistributeNewDO> insertList = new ArrayList<>();// 插入数据
|
|
|
+ List<VoyageStockDistributeNewDO> updateList = new ArrayList<>(); // 修改数据
|
|
|
+ map.keySet().stream().forEach(key -> {
|
|
|
+ if(existList.contains(key)) { //存在,做修改
|
|
|
+ VoyageStockDistributeNewDO voyageStockDistributeNewDO = existMap.get(key);
|
|
|
+ voyageStockDistributeNewDO.setNum(map.get(key));
|
|
|
+ voyageStockDistributeNewDO.setRoomModelNum(mapVO.get(key).getRoomModelNum());
|
|
|
+ updateList.add(voyageStockDistributeNewDO);
|
|
|
+ }else { //不存在,做插入
|
|
|
+ VoyageStockDistributeNewDO voyageStockDistributeNewDO = BeanUtils.toBean(mapVO.get(key), VoyageStockDistributeNewDO.class);
|
|
|
+ insertList.add(voyageStockDistributeNewDO);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if(!CollectionUtils.isAnyEmpty(updateList)) {
|
|
|
+ voyageStockDistributeNewMapper.updateBatch(updateList);
|
|
|
+ }
|
|
|
+ if(!CollectionUtils.isAnyEmpty(insertList)) {
|
|
|
+ voyageStockDistributeNewMapper.insertBatch(insertList);
|
|
|
+ }
|
|
|
}
|
|
|
- });
|
|
|
- if(!CollectionUtils.isAnyEmpty(updateList)) {
|
|
|
- voyageStockDistributeNewMapper.updateBatch(updateList);
|
|
|
+ BigDecimal distributeNum = getDistributeNum(voyageId);
|
|
|
+ voyageStockDetailService.handleShareNum(voyageId, distributeNum);
|
|
|
}
|
|
|
- if(!CollectionUtils.isAnyEmpty(insertList)) {
|
|
|
- voyageStockDistributeNewMapper.insertBatch(insertList);
|
|
|
+ }catch (ServiceException e) {
|
|
|
+ log.error("库存分配失败:",e);
|
|
|
+ }catch (Exception e) {
|
|
|
+ log.error("库存分配失败:",e);
|
|
|
+ }finally {
|
|
|
+ if(lock.isHeldByCurrentThread() && lock.isLocked()){
|
|
|
+ lock.unlock();
|
|
|
}
|
|
|
}
|
|
|
- BigDecimal distributeNum = getDistributeNum(voyageId);
|
|
|
- voyageStockDetailService.handleShareNum(voyageId, distributeNum);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
@Override
|