批量切割规格去掉名称

This commit is contained in:
liyj 2025-10-17 15:01:04 +08:00
parent e11cf54167
commit 555771a0bb
15 changed files with 274 additions and 78 deletions

View File

@ -48,6 +48,10 @@ public class ProductMapping implements Serializable {
@ApiModelProperty(value = "店铺名称") @ApiModelProperty(value = "店铺名称")
private String storeName; private String storeName;
@TableField(value ="product_number",updateStrategy=NOT_EMPTY)
@ApiModelProperty(value = "商品货架号")
private String productNumber;
@TableField(value ="spec_value",updateStrategy=NOT_EMPTY) @TableField(value ="spec_value",updateStrategy=NOT_EMPTY)
@ApiModelProperty("规格数据") @ApiModelProperty("规格数据")
@NotNull(message="规格数据不能为空") @NotNull(message="规格数据不能为空")

View File

@ -366,6 +366,31 @@ public class CommonUtil {
return t -> seen.add(keyExtractor.apply(t)); return t -> seen.add(keyExtractor.apply(t));
} }
/**
* 双字段去重工具
* @param list
* @param keyExtractor1
* @param keyExtractor2
* @return
* @param <T>
*/
public static <T> List<T> distinctByTwoFields(
List<T> list,
Function<T, ?> keyExtractor1,
Function<T, ?> keyExtractor2) {
Set<List<Object>> seen = new HashSet<>();
return list.stream()
.filter(item -> {
List<Object> keys = Arrays.asList(
keyExtractor1.apply(item),
keyExtractor2.apply(item)
);
return seen.add(keys);
})
.collect(Collectors.toList());
}
/** /**
* 按照指定比例计算分账金额 * 按照指定比例计算分账金额
* 分配优先级: 商家 > 平台 > 代理商 * 分配优先级: 商家 > 平台 > 代理商

View File

@ -37,4 +37,8 @@ public interface ShopNumberSeqService extends IBaseService<ShopNumberSeq> {
List<Integer> getBatchSpecId(int batchSize); List<Integer> getBatchSpecId(int batchSize);
void clearKeyStoreSepcId(); void clearKeyStoreSepcId();
List<Integer> getBatchLibraryProductId(int batchSize);
void clearKeyLibraryProductId();
} }

View File

@ -4,11 +4,13 @@ import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.suisung.mall.common.feignService.AccountService; import com.suisung.mall.common.feignService.AccountService;
import com.suisung.mall.common.modules.base.ShopBaseProductSpec; import com.suisung.mall.common.modules.base.ShopBaseProductSpec;
import com.suisung.mall.common.modules.library.LibraryProduct;
import com.suisung.mall.common.modules.number.ShopNumberSeq; import com.suisung.mall.common.modules.number.ShopNumberSeq;
import com.suisung.mall.common.modules.product.ShopProductSpecItem; import com.suisung.mall.common.modules.product.ShopProductSpecItem;
import com.suisung.mall.core.web.service.RedisService; import com.suisung.mall.core.web.service.RedisService;
import com.suisung.mall.core.web.service.impl.BaseServiceImpl; import com.suisung.mall.core.web.service.impl.BaseServiceImpl;
import com.suisung.mall.shop.base.service.ShopBaseProductSpecService; import com.suisung.mall.shop.base.service.ShopBaseProductSpecService;
import com.suisung.mall.shop.library.service.LibraryProductService;
import com.suisung.mall.shop.number.mapper.ShopNumberSeqMapper; import com.suisung.mall.shop.number.mapper.ShopNumberSeqMapper;
import com.suisung.mall.shop.number.service.ShopNumberSeqService; import com.suisung.mall.shop.number.service.ShopNumberSeqService;
import com.suisung.mall.shop.product.service.ShopProductSpecItemService; import com.suisung.mall.shop.product.service.ShopProductSpecItemService;
@ -52,6 +54,9 @@ public class ShopNumberSeqServiceImpl extends BaseServiceImpl<ShopNumberSeqMappe
@Autowired @Autowired
private AccountService accountService; private AccountService accountService;
@Autowired
private LibraryProductService libraryProductService;
public static void main(String[] args) { public static void main(String[] args) {
System.out.printf(IntStream.rangeClosed(1, 1).boxed().collect(Collectors.toList()).toString()); System.out.printf(IntStream.rangeClosed(1, 1).boxed().collect(Collectors.toList()).toString());
} }
@ -337,4 +342,39 @@ public class ShopNumberSeqServiceImpl extends BaseServiceImpl<ShopNumberSeqMappe
return IntStream.rangeClosed(start + 1, start + batchSize).boxed().collect(Collectors.toList()); return IntStream.rangeClosed(start + 1, start + batchSize).boxed().collect(Collectors.toList());
} }
/**
* 存的是最大值取的是范围如存最大值1批量是2则取范围1+11+2,如果没有则从1开始算即取范围[0+1,0+2]
*
* @param batchSize
* @return
*/
@Override
public synchronized List<Integer> getBatchLibraryProductId(int batchSize) {
int start = 0;
if (null != redisService.get(RedisKey.STOREDATALIBRARYID)) {
start = (Integer) redisService.get(RedisKey.STOREDATALIBRARYID);
redisService.set(RedisKey.STOREDATALIBRARYID, start + batchSize);
return IntStream.rangeClosed(start + 1, start + batchSize).boxed().collect(Collectors.toList());
}
QueryWrapper<LibraryProduct> queryWrapper = new QueryWrapper<>();
queryWrapper.select("max(id) as id");
LibraryProduct libraryProduct = libraryProductService.getOne(queryWrapper);
if (null != libraryProduct) {
start = Math.toIntExact(libraryProduct.getId());
redisService.set(RedisKey.STOREDATALIBRARYID, start + batchSize);
}
if (start == 0) {
redisService.set(RedisKey.STOREDATALIBRARYID, start + batchSize);
return IntStream.rangeClosed(start + 1, start + batchSize).boxed().collect(Collectors.toList());
}
return IntStream.rangeClosed(start + 1, start + batchSize).boxed().collect(Collectors.toList());
}
@Override
public void clearKeyLibraryProductId() {
redisService.del(RedisKey.STOREDATALIBRARYID);
}
} }

View File

@ -36,4 +36,10 @@ public interface ShopProductBaseMapper extends BaseMapper<ShopProductBase> {
*/ */
Map getProductBasicInfo(@Param("product_id") Long product_id, @Param("store_id") Integer store_id); Map getProductBasicInfo(@Param("product_id") Long product_id, @Param("store_id") Integer store_id);
int countFindMapping(@Param("storeId") Integer storeId);
List<ShopProductBase> findPageMapping(@Param("storeId") Integer storeId, @Param("offset") Integer offset, @Param("limit") Integer limit);
} }

View File

@ -301,4 +301,7 @@ public interface ShopProductBaseService extends IBaseService<ShopProductBase> {
Map<String, Long> getProductBasicIdByStore(Integer store_id, List<String> productNumberList); Map<String, Long> getProductBasicIdByStore(Integer store_id, List<String> productNumberList);
int countFindMapping(Integer store_id);
List<ShopProductBase> findPageMapping(Integer store_id, Integer pageNum, Integer pageSize);
} }

View File

@ -6449,4 +6449,16 @@ public class ShopProductBaseServiceImpl extends BaseServiceImpl<ShopProductBaseM
return true; return true;
} }
@Override
public int countFindMapping(Integer store_id) {
return shopProductBaseMapper.countFindMapping(store_id);
}
@Override
public List<ShopProductBase> findPageMapping(Integer store_id, Integer pageNum, Integer pageSize) {
return shopProductBaseMapper.findPageMapping(store_id,(pageNum-1)*pageSize,pageSize);
}
} }

View File

@ -42,7 +42,7 @@ public class ShopProductSpecItemServiceImpl extends BaseServiceImpl<ShopProductS
queryWrapper.eq("store_id",storeId); queryWrapper.eq("store_id",storeId);
List<ShopProductSpecItem> list= list(queryWrapper); List<ShopProductSpecItem> list= list(queryWrapper);
for(ShopProductSpecItem shopProductSpecItem:list){ for(ShopProductSpecItem shopProductSpecItem:list){
map.put(shopProductSpecItem.getSpec_item_name(),shopProductSpecItem.getSpec_item_id()); map.put(shopProductSpecItem.getSpec_item_name()+"_"+shopProductSpecItem.getCategory_id(),shopProductSpecItem.getSpec_item_id());
} }
return map; return map;

View File

@ -11,28 +11,32 @@ public class ProductMappingExcel {
@ExcelProperty(value = "商品名称", index = 0) @ExcelProperty(value = "商品名称", index = 0)
private String productName; private String productName;
@ExcelProperty(value = "店铺编号", index = 1) @ExcelProperty(value = "商品货号", index = 1)
private String productNumber;
@ExcelProperty(value = "店铺编号", index = 2)
private Integer storeId; private Integer storeId;
@ExcelProperty(value = "店铺名称", index = 2) @ExcelProperty(value = "店铺名称", index = 3)
private String storeName; private String storeName;
@ExcelProperty(value = "规格数据", index = 3) @ExcelProperty(value = "规格数据", index = 4)
private BigDecimal specValue; private BigDecimal specValue;
@ExcelProperty(value = "规格单位", index = 4) @ExcelProperty(value = "规格单位", index = 5)
private String specUnit; private String specUnit;
@ExcelProperty(value = "商品描述", index = 5) @ExcelProperty(value = "商品描述", index = 6)
private String description; private String description;
@ExcelProperty(value = "排序值", index = 6) @ExcelProperty(value = "排序值", index = 7)
private Integer sortOrder; private Integer sortOrder;
// 转换为实体对象 // 转换为实体对象
public ProductMapping toEntity() { public ProductMapping toEntity() {
ProductMapping entity = new ProductMapping(); ProductMapping entity = new ProductMapping();
entity.setProductName(this.productName); entity.setProductName(this.productName);
entity.setProductNumber(this.productNumber);
entity.setStoreId(this.storeId); entity.setStoreId(this.storeId);
entity.setStoreName(this.storeName); entity.setStoreName(this.storeName);
entity.setSpecValue(this.specValue); entity.setSpecValue(this.specValue);
@ -46,6 +50,7 @@ public class ProductMappingExcel {
public static ProductMappingExcel fromEntity(ProductMapping entity) { public static ProductMappingExcel fromEntity(ProductMapping entity) {
ProductMappingExcel excel = new ProductMappingExcel(); ProductMappingExcel excel = new ProductMappingExcel();
excel.setProductName(entity.getProductName()); excel.setProductName(entity.getProductName());
excel.setProductNumber(entity.getProductNumber());
excel.setStoreId(entity.getStoreId()); excel.setStoreId(entity.getStoreId());
excel.setStoreName(entity.getStoreName()); excel.setStoreName(entity.getStoreName());
excel.setSpecValue(entity.getSpecValue()); excel.setSpecValue(entity.getSpecValue());
@ -59,8 +64,9 @@ public class ProductMappingExcel {
* 生成唯一键productName + storeId + specValue + specUnit * 生成唯一键productName + storeId + specValue + specUnit
*/ */
public String getUniqueKey() { public String getUniqueKey() {
return String.format("%s|%d|%s|%s", return String.format("%s|%s|%d|%s|%s",
productName, productName,
productNumber,
storeId, storeId,
specValue.stripTrailingZeros().toPlainString(), specValue.stripTrailingZeros().toPlainString(),
specUnit); specUnit);

View File

@ -23,4 +23,7 @@ public class RedisKey {
public static final String STOREDATASPECID="storedata:SpecId"; public static final String STOREDATASPECID="storedata:SpecId";
public static final String STOREDATALIBRARYID="storedata:libraryId";
} }

View File

@ -22,7 +22,7 @@ public interface ProductMappingService extends IBaseService<ProductMapping> {
* @param isUpdate * @param isUpdate
*/ */
void computeProductMapping(List<ShopProductBase> shopProductBaseList,Integer storeId, void computeProductMapping(List<ShopProductBase> shopProductBaseList,Integer storeId,
Map shopProductSpecItemMap,Map ShopBaseProductSpecMap, Map productMappingMap, Map shopProductSpecItemMap,Map ShopBaseProductSpecMap, Map<String,ProductMapping> productMappingMap,
boolean isUpdate,String isPubish); boolean isUpdate,String isPubish);
Map getProductMapping(Integer storeId); Map getProductMapping(Integer storeId);

View File

@ -67,9 +67,6 @@ import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.*; import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -113,6 +110,7 @@ public class ProductMappingServiceImpl extends BaseServiceImpl<ProductMappingMap
@Autowired @Autowired
private RedisService redisService; private RedisService redisService;
@Override @Override
public CommonResult findPageProductMapping(ProductMapping productMapping,Integer pageNum,Integer pageSize) { public CommonResult findPageProductMapping(ProductMapping productMapping,Integer pageNum,Integer pageSize) {
QueryWrapper<ProductMapping> queryWrapper = new QueryWrapper<>(); QueryWrapper<ProductMapping> queryWrapper = new QueryWrapper<>();
@ -129,7 +127,7 @@ public class ProductMappingServiceImpl extends BaseServiceImpl<ProductMappingMap
} }
@Override @Override
public void computeProductMapping(List<ShopProductBase> shopProductBaseList,Integer storeId,Map shopProductSpecItemMap,Map ShopBaseProductSpecMap, Map productMappingMap,boolean isUpdate,String isPubish) { public void computeProductMapping(List<ShopProductBase> shopProductBaseList, Integer storeId, Map shopProductSpecItemMap, Map ShopBaseProductSpecMap, Map<String,ProductMapping> productMappingMap, boolean isUpdate, String isPubish) {
shopProductBaseList= shopProductBaseList.stream().filter(base ->ObjectUtil.isNotEmpty(base.getUnit_name())).collect(Collectors.toList()); shopProductBaseList= shopProductBaseList.stream().filter(base ->ObjectUtil.isNotEmpty(base.getUnit_name())).collect(Collectors.toList());
if (CollUtil.isEmpty(shopProductBaseList)) { if (CollUtil.isEmpty(shopProductBaseList)) {
log.info("没有规格数据要处理"); log.info("没有规格数据要处理");
@ -146,11 +144,59 @@ public class ProductMappingServiceImpl extends BaseServiceImpl<ProductMappingMap
queryWrapper.and(m->m.eq("store_id",storeId).or().eq("store_id",0)).orderByDesc("sort_order"); queryWrapper.and(m->m.eq("store_id",storeId).or().eq("store_id",0)).orderByDesc("sort_order");
List<ProductMapping> list= this.list(queryWrapper); List<ProductMapping> list= this.list(queryWrapper);
for (ProductMapping productMapping:list){ for (ProductMapping productMapping:list){
map.put(productMapping.getProductName(),productMapping); map.put(productMapping.getProductNumber(),productMapping);
} }
return map; return map;
} }
/**
* 处理规格数据
* @param shopProductSpecItemMap
* @param ShopBaseProductSpecMap
* @param productMappingMap
*/
public void saveProductSpecItem(Map shopProductSpecItemMap,Map ShopBaseProductSpecMap,Map<String,ProductMapping> productMappingMap,Map<String,Integer> shopProductItemCategoryIdMap){
List<ShopProductSpecItem> addShopProductSpecItemList=new ArrayList<>();
List<ShopProductSpecItem> finalAddShopProductSpecItemList = addShopProductSpecItemList;
productMappingMap.forEach((k, productMapping)->{
Integer categoryId=shopProductItemCategoryIdMap.get(k);
if(categoryId==null){
return;
}
ShopBaseProductSpec shopBaseProductSpec= (ShopBaseProductSpec) ShopBaseProductSpecMap.get(categoryId);
ShopProductSpecItem addShopProductSpecItem=new ShopProductSpecItem();
if(null==shopBaseProductSpec){
shopBaseProductSpec= (ShopBaseProductSpec) ShopBaseProductSpecMap.get(String.valueOf(categoryId));
}
String Spec_item_name=BigDecimalFormatter.formatWithoutTrailingZeros(productMapping.getSpecValue())+productMapping.getSpecUnit();
String Spec_item_name_categoryId=Spec_item_name+"_"+categoryId;//存在的item
if(ObjectUtil.isNotEmpty(shopProductSpecItemMap.get(Spec_item_name_categoryId))){
return;
}
Integer specId= shopBaseProductSpec.getSpec_id();
addShopProductSpecItem.setStore_id(productMapping.getStoreId());
addShopProductSpecItem.setCategory_id(categoryId);
addShopProductSpecItem.setSpec_id(specId);//根据规格获取
addShopProductSpecItem.setSpec_item_name(Spec_item_name);
addShopProductSpecItem.setSpec_item_enable(1);//上架
addShopProductSpecItem.setSpec_item_order(String.valueOf(productMapping.getSortOrder()));
finalAddShopProductSpecItemList.add(addShopProductSpecItem);
});
if(!finalAddShopProductSpecItemList.isEmpty()){
//去重
addShopProductSpecItemList=com.suisung.mall.common.utils.CommonUtil.distinctByTwoFields(addShopProductSpecItemList,ShopProductSpecItem::getSpec_item_name,ShopProductSpecItem::getCategory_id);
List<Integer> integers= shopNumberSeqService.getBatchSpecItemId(addShopProductSpecItemList.size());
for (int i = 0; i < addShopProductSpecItemList.size(); i++){
addShopProductSpecItemList.get(i).setSpec_item_id(integers.get(i));
}
if(!addShopProductSpecItemList.isEmpty()){
shopProductSpecItemService.saveBatch(addShopProductSpecItemList,addShopProductSpecItemList.size());
}
}
}
/** /**
* 处理数据生成规格列表 * 处理数据生成规格列表
* @param shopProductBaseList * @param shopProductBaseList
@ -158,10 +204,18 @@ public class ProductMappingServiceImpl extends BaseServiceImpl<ProductMappingMap
* @param ShopBaseProductSpecMap * @param ShopBaseProductSpecMap
* @param productMappingMap * @param productMappingMap
*/ */
public void dealData(List<ShopProductBase> shopProductBaseList,Map shopProductSpecItemMap,Map ShopBaseProductSpecMap, Map productMappingMap,boolean isUpdate,String isPublish){ @Transactional
public void dealData(List<ShopProductBase> shopProductBaseList,Map shopProductSpecItemMap,Map ShopBaseProductSpecMap, Map<String,ProductMapping> productMappingMap,boolean isUpdate,String isPublish){
List<ShopProductItem> shopProductItems=findShopProductItemsList(shopProductBaseList); List<ShopProductItem> shopProductItems=findShopProductItemsList(shopProductBaseList);
Map<String,Integer> shopProductItemCategoryIdMap=new HashMap<>();
shopProductItemCategoryIdMap=shopProductItems.stream().collect(Collectors.toMap(ShopProductItem::getItem_number,ShopProductItem::getCategory_id,
(existingValue,newValue)->existingValue));
saveProductSpecItem(shopProductSpecItemMap, ShopBaseProductSpecMap, productMappingMap,shopProductItemCategoryIdMap);//保存规格数据
Set<String> item_keys = redisService.keys(ConstantRedis.Cache_NameSpace +"shop_product_spec_item"+ "*");
redisService.del(item_keys);
shopProductSpecItemMap= shopProductSpecItemService.getExistItem(shopProductBaseList.get(0).getStore_id());
List<ShopProductInfo> shopProductInfoList=findShopProductInfoList(shopProductBaseList); List<ShopProductInfo> shopProductInfoList=findShopProductInfoList(shopProductBaseList);
List<ShopProductSpecItem> addShopProductSpecItemList=new ArrayList<>(); // List<ShopProductSpecItem> addShopProductSpecItemList=new ArrayList<>();
List<ShopProductSpecItem> updateShopProductSpecItemList=new ArrayList<>(); List<ShopProductSpecItem> updateShopProductSpecItemList=new ArrayList<>();
List<ShopProductBase> updateShopProductBaseList=new ArrayList<>(); List<ShopProductBase> updateShopProductBaseList=new ArrayList<>();
@ -171,7 +225,8 @@ public class ProductMappingServiceImpl extends BaseServiceImpl<ProductMappingMap
//找出需要更新的列表 //找出需要更新的列表
for (int i = 0; i < shopProductBaseList.size(); i++){ for (int i = 0; i < shopProductBaseList.size(); i++){
ShopProductSpecItem shopProductSpecItem=processShopProductSpecItem(shopProductBaseList.get(i),shopProductItems.get(i).getCategory_id(),shopProductSpecItemMap,ShopBaseProductSpecMap,productMappingMap,null); ShopProductBase shopProductBase=shopProductBaseList.get(i);
ShopProductSpecItem shopProductSpecItem=processShopProductSpecItem(shopProductBase,shopProductItemCategoryIdMap.get(shopProductBase.getProduct_number()),shopProductSpecItemMap,ShopBaseProductSpecMap,productMappingMap,null);
if(shopProductSpecItem!=null){ if(shopProductSpecItem!=null){
ShopProductIndex shopProductIndex=new ShopProductIndex(); ShopProductIndex shopProductIndex=new ShopProductIndex();
shopProductIndex.setProduct_id(shopProductBaseList.get(i).getProduct_id()); shopProductIndex.setProduct_id(shopProductBaseList.get(i).getProduct_id());
@ -197,11 +252,12 @@ public class ProductMappingServiceImpl extends BaseServiceImpl<ProductMappingMap
if(shopProductSpecItem.isUpdate()){ if(shopProductSpecItem.isUpdate()){
shopProductBaseList.get(i).setProduct_unit_price(shopProductSpecItem.getItemPrice()); shopProductBaseList.get(i).setProduct_unit_price(shopProductSpecItem.getItemPrice());
updateShopProductSpecItemList.add(shopProductSpecItem); updateShopProductSpecItemList.add(shopProductSpecItem);
}else {
shopProductBaseList.get(i).setProduct_unit_price(shopProductSpecItem.getItemPrice());
shopProductBaseList.get(i).setProduct_market_price(shopProductSpecItem.getItemPrice());
addShopProductSpecItemList.add(shopProductSpecItem);
} }
// else {
// shopProductBaseList.get(i).setProduct_unit_price(shopProductSpecItem.getItemPrice());
// shopProductBaseList.get(i).setProduct_market_price(shopProductSpecItem.getItemPrice());
// addShopProductSpecItemList.add(shopProductSpecItem);
// }
proccessItemAndInfo(shopProductItems.get(i),shopProductInfoList.get(i),shopProductSpecItem,isUpdate); proccessItemAndInfo(shopProductItems.get(i),shopProductInfoList.get(i),shopProductSpecItem,isUpdate);
shopProductBaseList.get(i).setProduct_unit_price(shopProductItems.get(i).getItem_unit_price()); shopProductBaseList.get(i).setProduct_unit_price(shopProductItems.get(i).getItem_unit_price());
updateShopProductBaseList.add(shopProductBaseList.get(i)); updateShopProductBaseList.add(shopProductBaseList.get(i));
@ -212,27 +268,29 @@ public class ProductMappingServiceImpl extends BaseServiceImpl<ProductMappingMap
} }
} }
//再次变量根据addShopProductSpecItemList算出id,这样防止多次链接redis //再次变量根据addShopProductSpecItemList算出id,这样防止多次链接redis
if(!addShopProductSpecItemList.isEmpty()){ // if(!addShopProductSpecItemList.isEmpty()){
List<Integer> integers= shopNumberSeqService.getBatchSpecItemId(addShopProductSpecItemList.size()); // //去重
int index=0; // addShopProductSpecItemList= addShopProductSpecItemList.stream().filter(com.suisung.mall.common.utils.CommonUtil.distinctByKey(ShopProductSpecItem::getSpec_item_name)).collect(Collectors.toList());
for (int i = 0; i < shopProductBaseList.size(); i++){ // List<Integer> integers= shopNumberSeqService.getBatchSpecItemId(addShopProductSpecItemList.size());
if(index==integers.size()){ // int index=0;
break; // for (int i = 0; i < shopProductBaseList.size(); i++){
} // if(index==integers.size()){
ShopProductSpecItem shopProductSpecItem=processShopProductSpecItem(shopProductBaseList.get(i),shopProductItems.get(i).getCategory_id(),shopProductSpecItemMap,ShopBaseProductSpecMap,productMappingMap,integers.get(index)); // break;
if(shopProductSpecItem!=null){ // }
if(!shopProductSpecItem.isUpdate()){ // ShopProductSpecItem shopProductSpecItem=processShopProductSpecItem(shopProductBaseList.get(i),shopProductItems.get(i).getCategory_id(),shopProductSpecItemMap,ShopBaseProductSpecMap,productMappingMap,integers.get(index));
addShopProductSpecItemList.set(index,shopProductSpecItem); // if(shopProductSpecItem!=null){
index++; // if(!shopProductSpecItem.isUpdate()){
} // addShopProductSpecItemList.set(index,shopProductSpecItem);
proccessItemAndInfo(shopProductItems.get(i),shopProductInfoList.get(i),shopProductSpecItem,isUpdate); // index++;
} // }
} // proccessItemAndInfo(shopProductItems.get(i),shopProductInfoList.get(i),shopProductSpecItem,isUpdate);
} // }
// }
// }
if(CollUtil.isNotEmpty(addShopProductSpecItemList)){ // if(CollUtil.isNotEmpty(addShopProductSpecItemList)){
shopProductSpecItemService.saveBatch(addShopProductSpecItemList,addShopProductSpecItemList.size()); // shopProductSpecItemService.saveBatch(addShopProductSpecItemList,addShopProductSpecItemList.size());
} // }
synchronized (this){ synchronized (this){
if(CollUtil.isNotEmpty(updateShopProductSpecItemList)){ if(CollUtil.isNotEmpty(updateShopProductSpecItemList)){
shopProductSpecItemService.updateBatchById(updateShopProductSpecItemList,updateShopProductSpecItemList.size()); shopProductSpecItemService.updateBatchById(updateShopProductSpecItemList,updateShopProductSpecItemList.size());
@ -351,29 +409,32 @@ public class ProductMappingServiceImpl extends BaseServiceImpl<ProductMappingMap
* @return * @return
*/ */
private ShopProductSpecItem processShopProductSpecItem(ShopProductBase base,Integer categoryId,Map shopProductSpecItemMap,Map ShopBaseProductSpecMap, Map productMappingMap,Integer specItemId){ private ShopProductSpecItem processShopProductSpecItem(ShopProductBase base,Integer categoryId,Map shopProductSpecItemMap,Map ShopBaseProductSpecMap, Map productMappingMap,Integer specItemId){
String productName=base.getProduct_name(); String productNumber=base.getProduct_number();
if(null!=productMappingMap.get(productName)){ if(null!=productMappingMap.get(productNumber)){
ShopBaseProductSpec shopBaseProductSpec= (ShopBaseProductSpec) ShopBaseProductSpecMap.get(categoryId); ShopBaseProductSpec shopBaseProductSpec= (ShopBaseProductSpec) ShopBaseProductSpecMap.get(categoryId);
if(null==shopBaseProductSpec){ if(null==shopBaseProductSpec){
shopBaseProductSpec= (ShopBaseProductSpec) ShopBaseProductSpecMap.get(String.valueOf(categoryId)); shopBaseProductSpec= (ShopBaseProductSpec) ShopBaseProductSpecMap.get(String.valueOf(categoryId));
} }
Integer specId= shopBaseProductSpec.getSpec_id(); Integer specId= shopBaseProductSpec.getSpec_id();
ShopProductSpecItem addShopProductSpecItem=new ShopProductSpecItem(); ShopProductSpecItem addShopProductSpecItem=new ShopProductSpecItem();
ProductMapping productMapping= (ProductMapping) productMappingMap.get(productName); ProductMapping productMapping= (ProductMapping) productMappingMap.get(productNumber);
String Spec_item_name=productMapping.getProductName()+ BigDecimalFormatter.formatWithoutTrailingZeros(productMapping.getSpecValue())+productMapping.getSpecUnit(); //String Spec_item_name=productMapping.getProductName()+ BigDecimalFormatter.formatWithoutTrailingZeros(productMapping.getSpecValue())+productMapping.getSpecUnit();
String Spec_item_name=BigDecimalFormatter.formatWithoutTrailingZeros(productMapping.getSpecValue())+productMapping.getSpecUnit();
//计算价格 //计算价格
BigDecimal[] bigDecimals= ProductPriceCalculator.calculatePriceAndQuantity(base.getUnit_price(),base.getShop_weight(),productMapping.getSpecValue(),productMapping.getSpecUnit()); BigDecimal[] bigDecimals= ProductPriceCalculator.calculatePriceAndQuantity(base.getUnit_price(),base.getShop_weight(),productMapping.getSpecValue(),productMapping.getSpecUnit());
addShopProductSpecItem.setItemPrice(bigDecimals[0]); addShopProductSpecItem.setItemPrice(bigDecimals[0]);
addShopProductSpecItem.setItemQuantity(bigDecimals[1]); addShopProductSpecItem.setItemQuantity(bigDecimals[1]);
Integer Spec_item_id = null; Integer Spec_item_id = null;
addShopProductSpecItem.setUpdate(true); addShopProductSpecItem.setUpdate(true);
if(ObjectUtil.isNotEmpty(shopProductSpecItemMap.get(Spec_item_name))){ String Spec_item_name_categoryId=Spec_item_name+"_"+categoryId;//存在的item
Spec_item_id= (Integer) shopProductSpecItemMap.get(Spec_item_name); if(ObjectUtil.isNotEmpty(shopProductSpecItemMap.get(Spec_item_name_categoryId))){
Spec_item_id= (Integer) shopProductSpecItemMap.get(Spec_item_name_categoryId);
}else { }else {
if(ObjectUtil.isNotEmpty(specItemId)){ return null;
Spec_item_id=specItemId; // if(ObjectUtil.isNotEmpty(specItemId)){
} // Spec_item_id=specItemId;
addShopProductSpecItem.setUpdate(false); // }
// addShopProductSpecItem.setUpdate(false);
} }
addShopProductSpecItem.setStore_id(base.getStore_id()); addShopProductSpecItem.setStore_id(base.getStore_id());
addShopProductSpecItem.setCategory_id(categoryId); addShopProductSpecItem.setCategory_id(categoryId);
@ -405,6 +466,7 @@ public class ProductMappingServiceImpl extends BaseServiceImpl<ProductMappingMap
@Override @Override
public CommonResult syncAllProductMapping(Integer storeId,String isPublish) { public CommonResult syncAllProductMapping(Integer storeId,String isPublish) {
clearCacheData();
if(ObjectUtil.isEmpty(storeId)){ if(ObjectUtil.isEmpty(storeId)){
storeId= Integer.valueOf(Objects.requireNonNull(ContextUtil.getCurrentUser()).getStore_id()); storeId= Integer.valueOf(Objects.requireNonNull(ContextUtil.getCurrentUser()).getStore_id());
} }
@ -419,39 +481,44 @@ public class ProductMappingServiceImpl extends BaseServiceImpl<ProductMappingMap
return CommonResult.failed("请设置优先方式为自动更新"); return CommonResult.failed("请设置优先方式为自动更新");
} }
//找出范围内的规格产品 //找出范围内的规格产品
QueryWrapper<ShopProductBase> queryWrapper= new QueryWrapper<>(); int total = shopProductBaseService.countFindMapping(storeId);
queryWrapper.eq("product_state_id", StateCode.PRODUCT_STATE_OFF_THE_SHELF_UNCHECK);
queryWrapper.eq("store_id", storeId);
queryWrapper.gt("unit_price", BigDecimal.ZERO);
queryWrapper.orderByAsc("product_id");
long total=shopProductBaseService.count(queryWrapper);
int pages= CommonUtil.getPagesCount(Math.toIntExact(total),SHOPBASEPAGE); int pages= CommonUtil.getPagesCount(total,SHOPBASEPAGE);
//ExecutorService executor = Executors.newFixedThreadPool(6);
//List<Future<?>> futures = new ArrayList<>();
ExecutorService executor = Executors.newFixedThreadPool(6);
List<Future<?>> futures = new ArrayList<>();
Map<String,Integer> shopProductSpecItemMap = shopProductSpecItemService.getExistItem(storeId);
Map<String,ProductMapping> productMappingMap = this.getProductMapping(storeId); Map<String,ProductMapping> productMappingMap = this.getProductMapping(storeId);
Map<String,ShopBaseProductSpec> ShopBaseProductSpecMap = baseProductSpecService.getShopBaseProductSpecMap(storeId); Map<String,ShopBaseProductSpec> ShopBaseProductSpecMap = baseProductSpecService.getShopBaseProductSpecMap(storeId);
boolean isUpDatePrice=ObjectUtil.isNotEmpty(storeDbConfig.getRefreshTime()); boolean isUpDatePrice=ObjectUtil.isNotEmpty(storeDbConfig.getRefreshTime());
for (int i=1;i<=pages;i++){ for (int i=1;i<=pages;i++){
int finalI = i; //int finalI = i;
Integer finalStoreId = storeId; // Integer finalStoreId = storeId;
List<ShopProductBase> shopProductBaseList=shopProductBaseService.lists(queryWrapper, finalI,SHOPBASEPAGE).getRecords(); Map<String,Integer> shopProductSpecItemMap = shopProductSpecItemService.getExistItem(storeId);
futures.add(executor.submit(() -> { List<ShopProductBase> shopProductBaseList=shopProductBaseService.findPageMapping(storeId,i,SHOPBASEPAGE);
this.computeProductMapping(shopProductBaseList, finalStoreId,shopProductSpecItemMap,ShopBaseProductSpecMap, productMappingMap,isUpDatePrice,isPublish); //List<ShopProductBase> shopProductBaseList=shopProductBaseService.lists(queryWrapper, i,SHOPBASEPAGE).getRecords();
return "成功" + finalI; // futures.add(executor.submit(() -> {
})); this.computeProductMapping(shopProductBaseList, i,shopProductSpecItemMap,ShopBaseProductSpecMap, productMappingMap,isUpDatePrice,isPublish);
// return "成功" + finalI;
// }));
} }
// 等待所有任务完成 // 等待所有任务完成
for (Future<?> future : futures) { // for (Future<?> future : futures) {
try { // try {
System.out.println("任务结果: " + future.get()); // System.out.println("任务结果: " + future.get());
} catch (Exception e) { // } catch (Exception e) {
System.err.println("任务执行异常: " + e.getMessage()); // System.err.println("任务执行异常: " + e.getMessage());
} // }
} // }
executor.shutdown(); // executor.shutdown();
log.info("--------批量匹配规格结束-----");
clearCacheData();
return CommonResult.success(true);
}
/**
* 清除缓存数据
*/
private void clearCacheData(){
shopNumberSeqService.clearKeyStoreItemSepcId(); shopNumberSeqService.clearKeyStoreItemSepcId();
Set<String> item_keys = redisService.keys(ConstantRedis.Cache_NameSpace +"shop_product_item"+ "*"); Set<String> item_keys = redisService.keys(ConstantRedis.Cache_NameSpace +"shop_product_item"+ "*");
redisService.del(item_keys); redisService.del(item_keys);
@ -461,7 +528,7 @@ public class ProductMappingServiceImpl extends BaseServiceImpl<ProductMappingMap
redisService.del(index_keys); redisService.del(index_keys);
Set<String> info_keys = redisService.keys(ConstantRedis.Cache_NameSpace +"shop_product_info"+ "*"); Set<String> info_keys = redisService.keys(ConstantRedis.Cache_NameSpace +"shop_product_info"+ "*");
redisService.del(info_keys); redisService.del(info_keys);
return CommonResult.success(true);
} }
@Override @Override

View File

@ -2,6 +2,17 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.suisung.mall.shop.product.mapper.ShopProductBaseMapper"> <mapper namespace="com.suisung.mall.shop.product.mapper.ShopProductBaseMapper">
<resultMap id="shopProductBaseMap" type="com.suisung.mall.common.modules.product.ShopProductBase">
<id property="product_id" column="product_id" />
<result property="product_name" column="product_name" />
<result property="product_number" column="product_number" />
<result property="store_id" column="store_id" />
<result property="store_name" column="store_name" />
<result property="unit_price" column="unit_price" />
<result property="shop_weight" column="shop_weight" />
<result property="unit_name" column="unit_name" />
</resultMap>
<select id="listItem" resultType="java.lang.Long"> <select id="listItem" resultType="java.lang.Long">
SELECT SELECT
@ -74,4 +85,18 @@
limit 1 limit 1
</select> </select>
<select id="countFindMapping" resultType="java.lang.Integer">
SELECT count(1) from shop_product_base spb
inner join
product_mapping pm on spb.product_number =pm.product_number and spb.store_id=pm.store_id
where spb.store_id =#{storeId} and spb.product_state_id=1003 and unit_price>0
</select>
<select id="findPageMapping" resultMap="shopProductBaseMap">
SELECT spb.* from shop_product_base spb
inner join
product_mapping pm on spb.product_number =pm.product_number and spb.store_id=pm.store_id
where spb.store_id =#{storeId} and spb.product_state_id=1003 and unit_price>0 order by spb.product_id asc limit #{offset},#{limit}
</select>
</mapper> </mapper>

View File

@ -229,7 +229,7 @@
</update> </update>
<select id="getLimitFirstByProductId" resultType="com.suisung.mall.common.modules.product.ShopProductItem"> <select id="getLimitFirstByProductId" resultType="com.suisung.mall.common.modules.product.ShopProductItem">
SELECT product_id,store_id,item_id,item_unit_price,item_unit_price,item_market_price,category_id FROM ( SELECT product_id,store_id,item_id,item_unit_price,item_unit_price,item_market_price,category_id,item_number FROM (
SELECT ROW_NUMBER() OVER(partition by s.product_id order by s.product_id asc) rn,s.* SELECT ROW_NUMBER() OVER(partition by s.product_id order by s.product_id asc) rn,s.*
from shop_product_item s from shop_product_item s
)t where t.rn=1 )t where t.rn=1

View File

@ -0,0 +1 @@
alter table product_mapping add column product_number varchar(50) not null default 1 comment '商品货架号';