|
@@ -8,13 +8,10 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.gree.mall.miniapp.bean.material.MyDataCountBean;
|
|
|
import com.gree.mall.miniapp.bean.material.parts.PartsRefundRecordDTO;
|
|
|
import com.gree.mall.miniapp.bean.material.parts.SalesItemRecordDTO;
|
|
|
-import com.gree.mall.miniapp.bean.material.stock.WorkerStockAccBean;
|
|
|
-import com.gree.mall.miniapp.bean.material.stock.WorkerStockBean;
|
|
|
-import com.gree.mall.miniapp.bean.material.stock.WorkerStockDTO;
|
|
|
-import com.gree.mall.miniapp.bean.material.stock.WorkerStockDetailBean;
|
|
|
+import com.gree.mall.miniapp.bean.material.stock.*;
|
|
|
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
|
|
|
import com.gree.mall.miniapp.commonmapper.MaterialMapper;
|
|
|
-import com.gree.mall.miniapp.commonmapper.stock.WorkerStockCMapper;
|
|
|
+import com.gree.mall.miniapp.commonmapper.stock.MaterialGoodsStockCMapper;
|
|
|
import com.gree.mall.miniapp.constant.Constant;
|
|
|
import com.gree.mall.miniapp.enums.material.PartsPushFlagEnum;
|
|
|
import com.gree.mall.miniapp.exception.RemoteServiceException;
|
|
@@ -45,18 +42,20 @@ import java.util.stream.Collectors;
|
|
|
@Slf4j
|
|
|
@RequiredArgsConstructor
|
|
|
@Service
|
|
|
-public class WorkerStockLogic {
|
|
|
+public class MaterialGoodsStockLogic {
|
|
|
|
|
|
private final CommonLogic commonLogic;
|
|
|
private final WorkerStockService workerStockService;
|
|
|
private final WorkerStockAccService workerStockAccService;
|
|
|
private final WebsitPartsSalesItemService websitPartsSalesItemService;
|
|
|
- private final WorkerStockCMapper workerStockCMapper;
|
|
|
+ private final MaterialGoodsStockCMapper materialGoodsStockCMapper;
|
|
|
private final WebsitPartsRetService websitPartsRetService;
|
|
|
private final WebsitNormRecordService websitNormRecordService;
|
|
|
private final WebsitGoodsService websitGoodsService;
|
|
|
private final RedisLockRegistry redisLockRegistry;
|
|
|
private final MaterialMapper materialMapper;
|
|
|
+ private final WebsitStockService websitStockService;
|
|
|
+ private final WebsitStockAccService websitStockAccService;
|
|
|
|
|
|
/**
|
|
|
* 我的库存分类
|
|
@@ -67,7 +66,7 @@ public class WorkerStockLogic {
|
|
|
*/
|
|
|
public List<WebsitGoodsCategory> websitGoodsCategoryList(String webistId, String goodsType) {
|
|
|
CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat();
|
|
|
- List<WebsitGoodsCategory> websitGoodsCategories = workerStockCMapper.queryWebsitGoodsCategoryList(webistId, currentCompanyWechat.getUser().getWorkerNumber(), goodsType);
|
|
|
+ List<WebsitGoodsCategory> websitGoodsCategories = materialGoodsStockCMapper.queryWebsitGoodsCategoryList(webistId, currentCompanyWechat.getUser().getWorkerNumber(), goodsType);
|
|
|
return websitGoodsCategories;
|
|
|
}
|
|
|
|
|
@@ -82,7 +81,7 @@ public class WorkerStockLogic {
|
|
|
*/
|
|
|
public IPage<WorkerStockBean> workerStockList(String websitId, String goodsType, String categoryId, Integer pageNum, Integer pageSize) {
|
|
|
CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat();
|
|
|
- IPage<WorkerStockBean> workerStockBeanIPage = workerStockCMapper
|
|
|
+ IPage<WorkerStockBean> workerStockBeanIPage = materialGoodsStockCMapper
|
|
|
.queryWorkerStockList(new Page(pageNum, pageSize), websitId, currentCompanyWechat.getUser().getWorkerNumber(), goodsType, categoryId);
|
|
|
|
|
|
return workerStockBeanIPage;
|
|
@@ -183,6 +182,106 @@ public class WorkerStockLogic {
|
|
|
return bean;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 单个网点库存处理
|
|
|
+ * @param stockList
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public void handleWebsitStock(List<WebsitStockDTO> stockList) throws Exception {
|
|
|
+ if(!TransactionSynchronizationManager.isSynchronizationActive()) {
|
|
|
+ throw new RemoteServiceException("请先开启事务");
|
|
|
+ }
|
|
|
+// System.out.println("程序入口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
|
|
|
+ if (CollectionUtil.isEmpty(stockList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+// System.out.println("检查传入的参数入口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
|
|
|
+ // 检查传入的参数
|
|
|
+ for (int i = 0; i < stockList.size(); i++) {
|
|
|
+ WebsitStockDTO websitStockDTO = stockList.get(i);
|
|
|
+ try {
|
|
|
+ ValidateUtil.validate(websitStockDTO);
|
|
|
+ if (websitStockDTO.getChangeQty().compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ throw new RuntimeException("变动数量不能少于0");
|
|
|
+ }
|
|
|
+ } catch (ValidationException ve) {
|
|
|
+ throw new RemoteServiceException("执行数据第" + (i+1) + "行" + ve.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+// System.out.println("检查传入的参数出口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
|
|
|
+ // 锁定网点库存
|
|
|
+// System.out.println("锁定网点库存入口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
|
|
|
+ WebsitStockDTO websitStockDTO = stockList.get(0);
|
|
|
+ Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.MATERIAL_STOCK_LOCK +
|
|
|
+ websitStockDTO.getCompanyWechatId() + ":" +
|
|
|
+ websitStockDTO.getWebsitId() + ":" +
|
|
|
+ websitStockDTO.getGoodsType() + ":" +
|
|
|
+ websitStockDTO.getStorageId());
|
|
|
+ if(!obtain.tryLock(10, TimeUnit.SECONDS)){
|
|
|
+ throw new RemoteServiceException("库存已冻结:" + websitStockDTO.getWebsitName());
|
|
|
+ }
|
|
|
+// System.out.println("锁定网点库存出口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
|
|
|
+ // 处理网点库存
|
|
|
+ try{
|
|
|
+ this.saveWebsitStock(websitStockDTO, stockList);
|
|
|
+
|
|
|
+ } finally {
|
|
|
+ this.txCallUnlock(obtain);
|
|
|
+// System.out.println("程序出口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveWebsitStock(WebsitStockDTO websitStockDTO, List<WebsitStockDTO> stockList) {
|
|
|
+ // System.out.println("处理网点库存入口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
|
|
|
+ // 查询存在的配件库存
|
|
|
+ List<WebsitStock> existWebsitStockList = materialGoodsStockCMapper.queryExistWebsitStockList(websitStockDTO.getCompanyWechatId(),
|
|
|
+ websitStockDTO.getWebsitId(), websitStockDTO.getStorageId(), stockList);
|
|
|
+
|
|
|
+// System.out.println("计算库存入口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
|
|
|
+ // 全部商品库存
|
|
|
+ Map<String, WebsitStock> existShopStockMap = this.createNotExistStock(stockList, existWebsitStockList);
|
|
|
+ // 计算商品库存
|
|
|
+// List<PartsShopStock> shopStockList = new ArrayList<>();
|
|
|
+ List<WebsitStockAcc> shopStockAccList = new ArrayList<>();
|
|
|
+ this.computeWebsitStock(stockList, existShopStockMap, null, shopStockAccList);
|
|
|
+
|
|
|
+ if (CollectionUtil.isEmpty(shopStockAccList)) {
|
|
|
+ throw new RemoteServiceException("商品库存处理失败");
|
|
|
+ }
|
|
|
+// System.out.println("计算商品库存出口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
|
|
|
+// System.out.println("保存商品库存入口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
|
|
|
+// partsShopStockService.saveOrUpdateBatch(shopStockList);
|
|
|
+ websitStockService.saveOrUpdateBatch(existShopStockMap.values());
|
|
|
+// System.out.println("保存商品库存出口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
|
|
|
+// System.out.println("保存商品库存三级帐入口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
|
|
|
+ websitStockAccService.saveBatch(shopStockAccList);
|
|
|
+// System.out.println("保存商品库存三级帐出口:" + DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss.SSS"));
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, WebsitStock> createNotExistStock(List<WebsitStockDTO> stockList, List<WebsitStock> existWebsitStockList) {
|
|
|
+ // 已存在的配件库存转成Map
|
|
|
+ Map<String, WebsitStock> existStockMap = existWebsitStockList.stream()
|
|
|
+ .collect(Collectors.toMap(WebsitStock::getGoodsId, Function.identity()));
|
|
|
+
|
|
|
+ // 找出不存在的配件库存并存入新配件库存List
|
|
|
+ for (WebsitStockDTO websitStockDTO : stockList) {
|
|
|
+ WebsitStock shopStock = existStockMap.get(websitStockDTO.getGoodsId());
|
|
|
+ if (Objects.isNull(shopStock)) {
|
|
|
+ shopStock = websitStockDTO.createPartsStock();
|
|
|
+ existStockMap.put(websitStockDTO.getGoodsId(), shopStock);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return existStockMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void computeWebsitStock(List<WebsitStockDTO> stockList, Map<String, WebsitStock> existShopStockMap, Object o, List<WebsitStockAcc> shopStockAccList) {
|
|
|
+ for (WebsitStockDTO shopStockDTO : stockList) {
|
|
|
+ WebsitStock shopStock = existShopStockMap.get(shopStockDTO.getGoodsId());
|
|
|
+ WebsitStockAcc shopStockAcc = new WebsitStockAcc();
|
|
|
+ shopStockDTO.computeWebsitStock(shopStock, shopStockAcc);
|
|
|
+ shopStockAccList.add(shopStockAcc);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 单个师傅库存处理
|
|
@@ -228,7 +327,7 @@ public class WorkerStockLogic {
|
|
|
|
|
|
private void saveWorkerStock(WorkerStockDTO workerStockDTO, List<WorkerStockDTO> stockList) throws Exception {
|
|
|
// 查询存在的商品库存
|
|
|
- List<WorkerStock> existWorkerStockList = workerStockCMapper.queryExistWorkerStockList(workerStockDTO.getCompanyWechatId()
|
|
|
+ List<WorkerStock> existWorkerStockList = materialGoodsStockCMapper.queryExistWorkerStockList(workerStockDTO.getCompanyWechatId()
|
|
|
, workerStockDTO.getWorkerId(), stockList);
|
|
|
// 全部商品库存
|
|
|
Map<String, WorkerStock> existWorkerStockMap = this.createNotExistWorkerStock(stockList, existWorkerStockList);
|
|
@@ -377,6 +476,28 @@ public class WorkerStockLogic {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void txCallUnlock(Lock obtain){
|
|
|
+ //事务提交完成后才释放锁,此方法禁止执行mysql相关操作,否则将导致重大问题。
|
|
|
+ if (TransactionSynchronizationManager.isActualTransactionActive()) {
|
|
|
+ TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
|
|
|
+ @Override
|
|
|
+ public void afterCompletion(int status) {
|
|
|
+
|
|
|
+ if(TransactionSynchronization.STATUS_COMMITTED == status){
|
|
|
+ log.info("=========【网点商品库存】======事务提交==============");
|
|
|
+ }else if(TransactionSynchronization.STATUS_ROLLED_BACK == status){
|
|
|
+ log.info("========【网点商品库存】=========事务回滚============");
|
|
|
+ }
|
|
|
+
|
|
|
+ obtain.unlock();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ log.info("【网点商品库存】====没有事务===释放锁");
|
|
|
+ obtain.unlock();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public void txCallUnlockSalesItem(Lock obtain){
|
|
|
//事务提交完成后才释放锁,此方法禁止执行mysql相关操作,否则将导致重大问题。
|
|
|
if (TransactionSynchronizationManager.isActualTransactionActive()) {
|
|
@@ -398,4 +519,5 @@ public class WorkerStockLogic {
|
|
|
obtain.unlock();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
}
|