新增库存扣减中间表的消费,保证库存的扣减一致性
This commit is contained in:
parent
c1b872c8a2
commit
3e7c7f8cd9
@ -3,16 +3,25 @@ package com.small.client.dao;
|
|||||||
|
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.date.DateUtil;
|
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.BigDecimalFormatter;
|
||||||
|
import com.small.client.Utils.CommonUtil;
|
||||||
|
import com.small.client.Utils.HttpUtils;
|
||||||
import com.small.client.dto.*;
|
import com.small.client.dto.*;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.math.NumberUtils;
|
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.stereotype.Service;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 考虑到每个思迅软件都是自己的数据,所以采用动态获取的方式获取数据
|
* 考虑到每个思迅软件都是自己的数据,所以采用动态获取的方式获取数据
|
||||||
@ -22,7 +31,10 @@ import java.util.*;
|
|||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class SxDataDao extends BaseDao{
|
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_CLS="t_bd_item_cls";//商品分类
|
||||||
private final static String T_BD_ITEM_INFO="t_bd_item_info b";//商品表
|
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";//会员表
|
private final static String T_RM_VIP_INFO="t_rm_vip_info";//会员表
|
||||||
@ -458,7 +470,8 @@ public class SxDataDao extends BaseDao{
|
|||||||
* @param dataBaseInfo
|
* @param dataBaseInfo
|
||||||
* @param map
|
* @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)){
|
if(CollectionUtil.isEmpty(map)){
|
||||||
log.info("同步数据为空");
|
log.info("同步数据为空");
|
||||||
return;
|
return;
|
||||||
@ -482,8 +495,12 @@ public class SxDataDao extends BaseDao{
|
|||||||
int count = 0;
|
int count = 0;
|
||||||
Set<Map.Entry> sme=map.entrySet();
|
Set<Map.Entry> sme=map.entrySet();
|
||||||
for (Map.Entry entry : sme) {
|
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.setString(1, (String) entry.getKey());
|
||||||
ps.setDouble(2, (double) entry.getValue());
|
ps.setDouble(2, stock_qty.doubleValue());
|
||||||
ps.setTimestamp(3, timestamp);
|
ps.setTimestamp(3, timestamp);
|
||||||
ps.addBatch(); // 添加至批处理
|
ps.addBatch(); // 添加至批处理
|
||||||
count++;
|
count++;
|
||||||
@ -495,22 +512,37 @@ public class SxDataDao extends BaseDao{
|
|||||||
}
|
}
|
||||||
// 执行剩余未满 batchSize 的批次
|
// 执行剩余未满 batchSize 的批次
|
||||||
int[] remainingCounts = ps.executeBatch();
|
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));
|
log.info("剩余批次更新数: {}", Arrays.toString(remainingCounts));
|
||||||
conn.commit(); // 最终提交事务
|
conn.commit(); // 最终提交事务
|
||||||
log.info("批量更新完成,总记录数: {}" , count);
|
log.info("批量更新完成,总记录数: {}" , count);
|
||||||
//baseUpdateImBrancStock(dataBaseInfo);
|
//baseUpdateImBrancStock(dataBaseInfo);
|
||||||
} catch (SQLException e) {
|
} catch (Exception e) {
|
||||||
conn.rollback(); // 出错时回滚整个事务
|
conn.rollback(); // 出错时回滚整个事务
|
||||||
e.printStackTrace();
|
log.info("业务失败:: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
log.info("sql失败:: {}", e.getMessage());
|
||||||
}finally {
|
}finally {
|
||||||
if(conn!=null){
|
if(conn!=null){
|
||||||
try {
|
try {
|
||||||
conn.close();
|
conn.close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new RuntimeException(e);
|
log.info("最后sql失败:: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,42 @@
|
|||||||
|
package com.small.client.dto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同步消费中间表,同步数据时保存数据到这个表中,消费完更新已消费,证明数据已经同步给思迅客户端
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ApiModel("商品数量消费表")
|
||||||
|
public class ProductQuantityConsumptionDto {
|
||||||
|
@ApiModelProperty("自定义主键ID")
|
||||||
|
private String consumeId;
|
||||||
|
|
||||||
|
@ApiModelProperty("订单编号")
|
||||||
|
private String orderId;
|
||||||
|
|
||||||
|
@ApiModelProperty("商品编号")
|
||||||
|
private String productNumber;
|
||||||
|
|
||||||
|
@ApiModelProperty("数量(正数表示入库/增加,负数表示出库/减少)")
|
||||||
|
private BigDecimal quantity;
|
||||||
|
|
||||||
|
@ApiModelProperty("消费状态:0-未消费,1-已消费")
|
||||||
|
private Integer status;
|
||||||
|
|
||||||
|
@ApiModelProperty("店铺ID")
|
||||||
|
private Integer storeId;
|
||||||
|
|
||||||
|
@ApiModelProperty("创建时间")
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||||
|
private Date createTime;
|
||||||
|
|
||||||
|
@ApiModelProperty(value = "更新时间")
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||||
|
private Date updateTime;
|
||||||
|
}
|
||||||
@ -218,6 +218,7 @@ public class SxDataServiceImp extends SxDataAbstService implements SxDataService
|
|||||||
String code= HttpUtils.postData(restTemplate,remoteIp+HttpUtils.URL_SYNC_MEMBER
|
String code= HttpUtils.postData(restTemplate,remoteIp+HttpUtils.URL_SYNC_MEMBER
|
||||||
+"?appKey="+commentModel.getAppKey()
|
+"?appKey="+commentModel.getAppKey()
|
||||||
+"&sign="+sign,memberList);//todo 后期改为文件传输
|
+"&sign="+sign,memberList);//todo 后期改为文件传输
|
||||||
|
|
||||||
if (!HttpUtils.SUCCESSCODE.equals(code)) {
|
if (!HttpUtils.SUCCESSCODE.equals(code)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -739,8 +740,22 @@ public class SxDataServiceImp extends SxDataAbstService implements SxDataService
|
|||||||
+"?appKey="+commentModel.getAppKey()
|
+"?appKey="+commentModel.getAppKey()
|
||||||
+"&sign="+commentModel.getAppId(),JSONObject.class);
|
+"&sign="+commentModel.getAppId(),JSONObject.class);
|
||||||
if(null!=jsonObject.get("result")){
|
if(null!=jsonObject.get("result")){
|
||||||
Map map=(Map)jsonObject.get("result");
|
// Map map=(Map)jsonObject.get("result");
|
||||||
sxDataDao.updateStoreData(dataBaseInfo,map);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user