Skip to content

Commit

Permalink
添加一种更新商品sku的方式。mysql replace into(先查找,如存在则删除,新增。如不存在新增)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeiGaoRobot committed May 19, 2022
1 parent f5718b4 commit f2b327c
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import cn.lili.modules.goods.entity.dos.GoodsSku;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
Expand All @@ -23,4 +25,78 @@ public interface GoodsSkuMapper extends BaseMapper<GoodsSku> {
@Select("SELECT id FROM li_goods_sku WHERE goods_id = #{goodsId}")
List<String> getGoodsSkuIdByGoodsId(String goodsId);


@Insert("replace into li_goods_sku (\n" +
"\tid,\n" +
"\tgoods_id,\n" +
"\tspecs,\n" +
"\tsimple_specs,\n" +
"\tfreight_template_id,\n" +
"\tgoods_name,\n" +
"\tsn,\n" +
"\tbrand_id,\n" +
"\tcategory_path,\n" +
"\tgoods_unit,\n" +
"\tselling_point,\n" +
"\tweight,\n" +
"\tmarket_enable,\n" +
"\tintro,\n" +
"\tprice,\n" +
"\tcost,\n" +
"\tquantity,\n" +
"\tgrade,\n" +
"\tthumbnail,\n" +
"\tsmall,\n" +
"\tstore_category_path,\n" +
"\tstore_id,\n" +
"\tstore_name,\n" +
"\tauth_flag,\n" +
"\tself_operated,\n" +
"\tmobile_intro,\n" +
"\trecommend,\n" +
"\tsales_model,\n" +
"\tgoods_type,\n" +
"\tcreate_by,\n" +
"\tcreate_time,\n" +
"\tupdate_time,\n" +
"\tdelete_flag \n" +
")\n" +
" VALUES\n" +
"(\n" +
"\t#{goodsSku.id},\n" +
"\t#{goodsSku.goodsId},\n" +
"\t#{goodsSku.specs},\n" +
"\t#{goodsSku.simpleSpecs},\n" +
"\t#{goodsSku.freightTemplateId},\n" +
"\t#{goodsSku.goodsName},\n" +
"\t#{goodsSku.sn},\n" +
"\t#{goodsSku.brandId},\n" +
"\t#{goodsSku.categoryPath},\n" +
"\t#{goodsSku.goodsUnit},\n" +
"\t#{goodsSku.sellingPoint},\n" +
"\t#{goodsSku.weight},\n" +
"\t#{goodsSku.marketEnable},\n" +
"\t#{goodsSku.intro},\n" +
"\t#{goodsSku.price},\n" +
"\t#{goodsSku.cost},\n" +
"\t#{goodsSku.quantity},\n" +
"\t#{goodsSku.grade},\n" +
"\t#{goodsSku.thumbnail},\n" +
"\t#{goodsSku.small},\n" +
"\t#{goodsSku.storeCategoryPath},\n" +
"\t#{goodsSku.storeId},\n" +
"\t#{goodsSku.storeName},\n" +
"\t#{goodsSku.authFlag},\n" +
"\t#{goodsSku.selfOperated},\n" +
"\t#{goodsSku.mobileIntro},\n" +
"\t#{goodsSku.recommend},\n" +
"\t#{goodsSku.salesModel},\n" +
"\t#{goodsSku.goodsType},\n" +
"\t#{goodsSku.createBy},\n" +
"\t#{goodsSku.createTime},\n" +
"\t#{goodsSku.updateTime},\n" +
"\t#{goodsSku.deleteFlag}\n" +
")")
int replaceGoodsSku(@Param("goodsSku") GoodsSku goodsSku);

}
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,6 @@ static String getStockCacheKey(String id) {
* @return 全部skuId的集合
*/
List<String> getSkuIdsByGoodsId(String goodsId);

boolean deleteAndInsertGoodsSkus(List<GoodsSku> goodsSkus);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import cn.lili.common.exception.ServiceException;
import cn.lili.common.properties.RocketmqCustomProperties;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.SnowFlake;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
Expand Down Expand Up @@ -149,9 +150,11 @@ public void update(List<Map<String, Object>> skuList, Goods goods, Boolean regen
oldSkuIds.add(goodsSkuVO.getId());
cache.remove(GoodsSkuService.getCacheKeys(goodsSkuVO.getId()));
}
this.removeByIds(oldSkuIds);

this.remove(new LambdaQueryWrapper<GoodsSku>().eq(GoodsSku::getGoodsId, goods.getId()));
//删除sku相册
goodsGalleryService.removeByGoodsId(goods.getId());
getGoodsListByGoodsId(goods.getId());
// 添加商品sku
newSkuList = this.addGoodsSku(skuList, goods);

Expand Down Expand Up @@ -179,6 +182,7 @@ public void update(List<Map<String, Object>> skuList, Goods goods, Boolean regen
this.clearCache(sku.getId());
}
}
this.remove(new LambdaQueryWrapper<GoodsSku>().eq(GoodsSku::getGoodsId, goods.getId()));
this.saveOrUpdateBatch(newSkuList);
}
this.updateStock(newSkuList);
Expand Down Expand Up @@ -393,7 +397,7 @@ public List<GoodsSku> getGoodsSkuByIdFromCache(List<String> ids) {

@Override
public List<GoodsSkuVO> getGoodsListByGoodsId(String goodsId) {
List<GoodsSku> list = this.list(new LambdaQueryWrapper<GoodsSku>().eq(GoodsSku::getGoodsId, goodsId));
List<GoodsSku> list = this.list(new LambdaQueryWrapper<GoodsSku>().eq(GoodsSku::getGoodsId, goodsId).orderByAsc(GoodsSku::getGoodsName));
return this.getGoodsSkuVOList(list);
}

Expand Down Expand Up @@ -597,6 +601,19 @@ public void generateEs(Goods goods) {
applicationEventPublisher.publishEvent(new GeneratorEsGoodsIndexEvent("生成商品", GoodsTagsEnum.GENERATOR_GOODS_INDEX.name(), goods.getId()));
}

@Override
@Transactional(rollbackFor = Exception.class)
public boolean deleteAndInsertGoodsSkus(List<GoodsSku> goodsSkus) {
int count = 0;
for (GoodsSku skus : goodsSkus) {
if (CharSequenceUtil.isEmpty(skus.getId())) {
skus.setId(SnowFlake.getIdStr());
}
count = this.baseMapper.replaceGoodsSku(skus);
}
return count > 0;
}

/**
* 修改库存
*
Expand Down

0 comments on commit f2b327c

Please sign in to comment.