新增库存扣减中间表的消费,保证库存的扣减一致性

This commit is contained in:
liyj 2025-10-11 14:23:57 +08:00
parent ef36db0b44
commit 62eb843629
3 changed files with 56 additions and 12 deletions

View File

@ -3,16 +3,25 @@ package com.small.client.dao;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.google.gson.Gson;
import com.small.client.Utils.BigDecimalFormatter;
import com.small.client.Utils.CommonUtil;
import com.small.client.Utils.HttpUtils;
import com.small.client.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.*;
import java.util.*;
import java.util.stream.Collectors;
/**
* 考虑到每个思迅软件都是自己的数据所以采用动态获取的方式获取数据
@ -22,7 +31,10 @@ import java.util.*;
@Service
@Slf4j
public class SxDataDao extends BaseDao{
@Autowired
private RestTemplate restTemplate;
@Value("${remoteIp}")
private String remoteIp;
private final static String T_BD_ITEM_CLS="t_bd_item_cls";//商品分类
private final static String T_BD_ITEM_INFO="t_bd_item_info b";//商品表
private final static String T_RM_VIP_INFO="t_rm_vip_info";//会员表
@ -458,7 +470,8 @@ public class SxDataDao extends BaseDao{
* @param dataBaseInfo
* @param map
*/
public void updateStoreData(DataBaseInfo dataBaseInfo, Map map){
public void updateStoreData(DataBaseInfo dataBaseInfo, Map map,List<ProductQuantityConsumptionDto> productQuantityConsumptionDtoList,
CommentModel commentModel){
if(CollectionUtil.isEmpty(map)){
log.info("同步数据为空");
return;
@ -482,8 +495,12 @@ public class SxDataDao extends BaseDao{
int count = 0;
Set<Map.Entry> sme=map.entrySet();
for (Map.Entry entry : sme) {
BigDecimal stock_qty= (BigDecimal) entry.getValue();
if(stock_qty.compareTo(BigDecimal.ZERO)==0){
continue;
}
ps.setString(1, (String) entry.getKey());
ps.setDouble(2, (double) entry.getValue());
ps.setDouble(2, stock_qty.doubleValue());
ps.setTimestamp(3, timestamp);
ps.addBatch(); // 添加至批处理
count++;
@ -495,22 +512,37 @@ public class SxDataDao extends BaseDao{
}
// 执行剩余未满 batchSize 的批次
int[] remainingCounts = ps.executeBatch();
List<String> consumIds=productQuantityConsumptionDtoList
.stream()
.map(ProductQuantityConsumptionDto::getConsumeId)
.collect(Collectors.toList());
Gson gson=new Gson();
String jsonString=gson.toJson(consumIds);
JSONArray jsonArray = JSONUtil.parseArray(jsonString);
String sign= CommonUtil.generateOpenSign(jsonString,commentModel.getAppKey(),commentModel.getAppId());
String code= HttpUtils.postData(restTemplate,remoteIp+HttpUtils.URL_SYNC_POST_STORE_DATA_RESPONSE
+"?appKey="+commentModel.getAppKey()
+"&sign="+sign, jsonArray);
if (!HttpUtils.SUCCESSCODE.equals(code)) {
throw new Exception("服务器异常");
}
log.info("剩余批次更新数: {}", Arrays.toString(remainingCounts));
conn.commit(); // 最终提交事务
log.info("批量更新完成,总记录数: {}" , count);
//baseUpdateImBrancStock(dataBaseInfo);
} catch (SQLException e) {
} catch (Exception e) {
conn.rollback(); // 出错时回滚整个事务
e.printStackTrace();
log.info("业务失败:: {}", e.getMessage());
}
} catch (SQLException e) {
e.printStackTrace();
log.info("sql失败: {}", e.getMessage());
}finally {
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
log.info("最后sql失败: {}", e.getMessage());
}
}
}

View File

@ -23,9 +23,6 @@ public class ProductQuantityConsumptionDto {
@ApiModelProperty("商品编号")
private String productNumber;
@ApiModelProperty("商品单价,如果是切割商品要算回来每公斤多少")
private BigDecimal unitPrice;
@ApiModelProperty("数量(正数表示入库/增加,负数表示出库/减少)")
private BigDecimal quantity;

View File

@ -218,6 +218,7 @@ public class SxDataServiceImp extends SxDataAbstService implements SxDataService
String code= HttpUtils.postData(restTemplate,remoteIp+HttpUtils.URL_SYNC_MEMBER
+"?appKey="+commentModel.getAppKey()
+"&sign="+sign,memberList);//todo 后期改为文件传输
if (!HttpUtils.SUCCESSCODE.equals(code)) {
continue;
}
@ -739,8 +740,22 @@ public class SxDataServiceImp extends SxDataAbstService implements SxDataService
+"?appKey="+commentModel.getAppKey()
+"&sign="+commentModel.getAppId(),JSONObject.class);
if(null!=jsonObject.get("result")){
Map map=(Map)jsonObject.get("result");
sxDataDao.updateStoreData(dataBaseInfo,map);
// Map map=(Map)jsonObject.get("result");
List<ProductQuantityConsumptionDto> productQuantityConsumptionDtoList =
JSONUtil.toList(jsonObject.getStr("result"),ProductQuantityConsumptionDto.class);
if(!productQuantityConsumptionDtoList.isEmpty()){
Map map = productQuantityConsumptionDtoList.stream()
.collect(Collectors.groupingBy(
ProductQuantityConsumptionDto::getProductNumber,
Collectors.reducing(
BigDecimal.ZERO,
ProductQuantityConsumptionDto::getQuantity,
BigDecimal::add
)
));
sxDataDao.updateStoreData(dataBaseInfo,map,productQuantityConsumptionDtoList,commentModel);
}
}
}