进行商品添加的迁移
This commit is contained in:
@@ -10,10 +10,6 @@ import java.util.List;
|
||||
@Repository
|
||||
public interface ProductSpuMapper {
|
||||
|
||||
ProductSpuDO selectById(Integer id);
|
||||
|
||||
List<ProductSpuDO> selectByIds(@Param("ids") Collection<Integer> ids);
|
||||
|
||||
/**
|
||||
* 获得大于 id 的商品编号数组
|
||||
*
|
||||
@@ -24,21 +20,4 @@ public interface ProductSpuMapper {
|
||||
List<Integer> selectIdListByIdGt(@Param("id") Integer id,
|
||||
@Param("limit") Integer limit);
|
||||
|
||||
void insert(ProductSpuDO productSpuDO);
|
||||
|
||||
void update(ProductSpuDO productSpuDO);
|
||||
|
||||
// TODO 芋艿,需要捉摸下,怎么优化下。参数有点多
|
||||
List<ProductSpuDO> selectListByNameLikeOrderBySortAsc(@Param("name") String name,
|
||||
@Param("cid") Integer cid,
|
||||
@Param("visible") Boolean visible,
|
||||
@Param("hasQuantity") Boolean hasQuantity,
|
||||
@Param("offset") Integer offset,
|
||||
@Param("limit") Integer limit);
|
||||
|
||||
Integer selectCountByNameLike(@Param("name") String name,
|
||||
@Param("cid") Integer cid,
|
||||
@Param("hasQuantity") Boolean hasQuantity,
|
||||
@Param("visible") Boolean visible);
|
||||
|
||||
}
|
||||
|
||||
@@ -35,39 +35,6 @@ public class ProductAttrServiceImpl implements ProductAttrService {
|
||||
@Autowired
|
||||
private ProductAttrValueMapper productAttrValueMapper;
|
||||
|
||||
public List<ProductAttrAndValuePairBO> validProductAttrAndValue(Set<Integer> productAttrValueIds, boolean validStatus) {
|
||||
// 首先,校验规格值
|
||||
List<ProductAttrValueDO> attrValues = productAttrValueMapper.selectListByIds(productAttrValueIds);
|
||||
if (attrValues.size() != productAttrValueIds.size()) {
|
||||
throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_ATTR_VALUE_NOT_EXIST.getCode());
|
||||
}
|
||||
if (validStatus) {
|
||||
for (ProductAttrValueDO attrValue : attrValues) { // 同时,校验下状态
|
||||
if (ProductAttrConstants.ATTR_STATUS_DISABLE.equals(attrValue.getStatus())) {
|
||||
throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_ATTR_VALUE_NOT_EXIST.getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
// 然后,校验规格
|
||||
Set<Integer> attrIds = attrValues.stream().map(ProductAttrValueDO::getAttrId).collect(Collectors.toSet());
|
||||
List<ProductAttrDO> attrs = productAttrMapper.selectListByIds(attrIds);
|
||||
if (attrs.size() != attrIds.size()) {
|
||||
throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_ATTR_NOT_EXIST.getCode());
|
||||
}
|
||||
if (validStatus) {
|
||||
for (ProductAttrDO attr : attrs) { // 同时,校验下状态
|
||||
if (ProductAttrConstants.ATTR_VALUE_STATUS_DISABLE.equals(attr.getStatus())) {
|
||||
throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_ATTR_NOT_EXIST.getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
// 返回成功
|
||||
Map<Integer, ProductAttrDO> attrMap = attrs.stream().collect(Collectors.toMap(ProductAttrDO::getId, productAttrDO -> productAttrDO)); // ProductAttrDO 的映射,方便查找。
|
||||
return attrValues.stream().map(productAttrValueDO -> new ProductAttrAndValuePairBO()
|
||||
.setAttrId(productAttrValueDO.getAttrId()).setAttrName(attrMap.get(productAttrValueDO.getAttrId()).getName())
|
||||
.setAttrValueId(productAttrValueDO.getId()).setAttrValueName(productAttrValueDO.getName())).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProductAttrPageBO getProductAttrPage(ProductAttrPageDTO productAttrPageDTO) {
|
||||
ProductAttrPageBO productAttrPageBO = new ProductAttrPageBO();
|
||||
|
||||
@@ -96,45 +96,6 @@ public class ProductSpuServiceImpl implements ProductSpuService {
|
||||
return productSpuDetailBO;
|
||||
}
|
||||
|
||||
@SuppressWarnings("Duplicates")
|
||||
@Transactional
|
||||
public ProductSpuDetailBO addProductSpu0(Integer adminId, ProductSpuAddDTO productSpuAddDTO) {
|
||||
// 校验商品分类分类存在
|
||||
ProductCategoryDO category = productCategoryService.validProductCategory(productSpuAddDTO.getCid());
|
||||
if (ProductCategoryConstants.PID_ROOT.equals(category.getPid())) { // 商品只能添加到二级分类下
|
||||
throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_SPU_CATEGORY_MUST_BE_LEVEL2.getCode());
|
||||
}
|
||||
// 校验规格是否存在
|
||||
Set<Integer> productAttrValueIds = new HashSet<>();
|
||||
productSpuAddDTO.getSkus().forEach(productSkuAddDTO -> productAttrValueIds.addAll(productSkuAddDTO.getAttrs()));
|
||||
List<ProductAttrAndValuePairBO> attrAndValuePairList = productAttrService.validProductAttrAndValue(productAttrValueIds
|
||||
, true); // 读取规格时,需要考虑规格是否被禁用
|
||||
// 保存 Spu
|
||||
ProductSpuDO spu = ProductSpuConvert.INSTANCE.convert(productSpuAddDTO)
|
||||
.setPicUrls(StringUtil.join(productSpuAddDTO.getPicUrls(), ","))
|
||||
.setSort(0); // 排序为 0
|
||||
spu.setCreateTime(new Date());
|
||||
spu.setDeleted(DeletedStatusEnum.DELETED_NO.getValue());
|
||||
initSpuFromSkus(spu, productSpuAddDTO.getSkus()); // 初始化 sku 相关信息到 spu 中
|
||||
productSpuMapper.insert(spu);
|
||||
// 保存 Sku
|
||||
List<ProductSkuDO> skus = productSpuAddDTO.getSkus().stream().map(productSkuAddDTO -> {
|
||||
ProductSkuDO sku = ProductSpuConvert.INSTANCE.convert(productSkuAddDTO)
|
||||
.setSpuId(spu.getId())
|
||||
.setStatus(ProductSpuConstants.SKU_STATUS_ENABLE)
|
||||
.setAttrs(StringUtil.join(productSkuAddDTO.getAttrs(), ","));
|
||||
sku.setCreateTime(new Date());
|
||||
sku.setDeleted(DeletedStatusEnum.DELETED_NO.getValue());
|
||||
return sku;
|
||||
}).collect(Collectors.toList());
|
||||
// 校验 Sku 规格
|
||||
validProductSku(productSpuAddDTO.getSkus(), attrAndValuePairList);
|
||||
// 插入 SKU 到数据库
|
||||
productSkuMapper.insertList(skus);
|
||||
// 返回成功
|
||||
return ProductSpuConvert.INSTANCE.convert2(spu, skus, attrAndValuePairList, category);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateProductSpu(Integer adminId, ProductSpuUpdateDTO productSpuUpdateDTO) {
|
||||
// 更新商品
|
||||
@@ -276,43 +237,6 @@ public class ProductSpuServiceImpl implements ProductSpuService {
|
||||
return ProductSpuConvert.INSTANCE.convert3(skus, spus, attrAndValuePairList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验 sku 是否合法
|
||||
*
|
||||
* @param productSkuAddDTOs sku 添加或修改信息
|
||||
* @param productAttrDetailBOs 商品规格明细数组
|
||||
* @return 是否校验通过
|
||||
*/
|
||||
private Boolean validProductSku(List<ProductSkuAddOrUpdateDTO> productSkuAddDTOs, List<ProductAttrAndValuePairBO> productAttrDetailBOs) {
|
||||
// 创建 ProductAttrDetailBO 的映射。其中,KEY 为 ProductAttrDetailBO.attrValueId ,即规格值的编号
|
||||
Map<Integer, ProductAttrAndValuePairBO> productAttrDetailBOMap = productAttrDetailBOs.stream().collect(
|
||||
Collectors.toMap(ProductAttrAndValuePairBO::getAttrValueId, productAttrDetailBO -> productAttrDetailBO));
|
||||
// 1. 先校验,一个 Sku 下,没有重复的规格。校验方式是,遍历每个 Sku ,看看是否有重复的规格 attrId
|
||||
for (ProductSkuAddOrUpdateDTO sku : productSkuAddDTOs) {
|
||||
Set<Integer> attrIds = sku.getAttrs().stream().map(attrValueId -> productAttrDetailBOMap.get(attrValueId).getAttrId())
|
||||
.collect(Collectors.toSet());
|
||||
if (attrIds.size() != sku.getAttrs().size()) {
|
||||
throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_SKU_ATTR_CANT_NOT_DUPLICATE.getCode());
|
||||
}
|
||||
}
|
||||
// 2. 再校验,每个 Sku 的规格值的数量,是一致的。
|
||||
int attrSize = productSkuAddDTOs.get(0).getAttrs().size();
|
||||
for (int i = 1; i < productSkuAddDTOs.size(); i++) {
|
||||
if (attrSize != productSkuAddDTOs.get(i).getAttrs().size()) {
|
||||
throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_SPU_ATTR_NUMBERS_MUST_BE_EQUALS.getCode());
|
||||
}
|
||||
}
|
||||
// 3. 最后校验,每个 Sku 之间不是重复的
|
||||
Set<Set<Integer>> skuAttrValues = new HashSet<>(); // 每个元素,都是一个 Sku 的 attrValueId 集合。这样,通过最外层的 Set ,判断是否有重复的.
|
||||
for (ProductSkuAddOrUpdateDTO sku : productSkuAddDTOs) {
|
||||
if (!skuAttrValues.add(new HashSet<>(sku.getAttrs()))) { // 添加失败,说明重复
|
||||
throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_SPU_SKU__NOT_DUPLICATE.getCode());
|
||||
}
|
||||
}
|
||||
// 校验通过
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得 sku 数组中,指定规格的 sku
|
||||
*
|
||||
@@ -335,18 +259,6 @@ public class ProductSpuServiceImpl implements ProductSpuService {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 sku 数组,计算相关的字段到 spu 中。
|
||||
*
|
||||
* @param spu spu
|
||||
* @param skus sku 数组
|
||||
*/
|
||||
private void initSpuFromSkus(ProductSpuDO spu, List<ProductSkuAddOrUpdateDTO> skus) {
|
||||
assert skus.size() > 0; // 写个断言,避免下面警告
|
||||
spu.setPrice(skus.stream().min(Comparator.comparing(ProductSkuAddOrUpdateDTO::getPrice)).get().getPrice()); // 求最小价格
|
||||
spu.setQuantity(skus.stream().mapToInt(ProductSkuAddOrUpdateDTO::getQuantity).sum()); // 求库存之和
|
||||
}
|
||||
|
||||
private boolean sendProductUpdateMessage(Integer id) {
|
||||
// 创建 Message 对象
|
||||
ProductUpdateMessage message = new ProductUpdateMessage().setId(id);
|
||||
|
||||
@@ -8,25 +8,6 @@
|
||||
create_time
|
||||
</sql>
|
||||
|
||||
<select id="selectById" parameterType="Integer" resultType="ProductSpuDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM product_spu
|
||||
WHERE id = #{id}
|
||||
AND deleted = 0
|
||||
</select>
|
||||
|
||||
<select id="selectByIds" resultType="ProductSpuDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM product_spu
|
||||
WHERE id IN
|
||||
<foreach item="id" collection="ids" separator="," open="(" close=")" index="">
|
||||
#{id}
|
||||
</foreach>
|
||||
AND deleted = 0
|
||||
</select>
|
||||
|
||||
<select id="selectIdListByIdGt" parameterType="Integer" resultType="Integer">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
@@ -41,105 +22,4 @@
|
||||
LIMIT #{limit}
|
||||
</select>
|
||||
|
||||
<insert id="insert" parameterType="ProductSpuDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
|
||||
INSERT INTO product_spu (
|
||||
name, sell_point, description, cid, pic_urls,
|
||||
visible, sort, price, quantity,
|
||||
deleted, create_time
|
||||
) VALUES (
|
||||
#{name}, #{sellPoint}, #{description}, #{cid}, #{picUrls},
|
||||
#{visible}, #{sort}, #{price}, #{quantity},
|
||||
#{deleted}, #{createTime}
|
||||
)
|
||||
</insert>
|
||||
|
||||
<update id="update" parameterType="ProductSpuDO">
|
||||
UPDATE product_spu
|
||||
<set>
|
||||
<if test="name != null">
|
||||
name = #{name},
|
||||
</if>
|
||||
<if test="sellPoint != null">
|
||||
sell_point = #{sellPoint},
|
||||
</if>
|
||||
<if test="description != null">
|
||||
description = #{description},
|
||||
</if>
|
||||
<if test="cid != null">
|
||||
cid = #{cid},
|
||||
</if>
|
||||
<if test="picUrls != null">
|
||||
pic_urls = #{picUrls},
|
||||
</if>
|
||||
<if test="visible != null">
|
||||
visible = #{visible},
|
||||
</if>
|
||||
<if test="price != null">
|
||||
price = #{price},
|
||||
</if>
|
||||
<if test="quantity != null">
|
||||
quantity = #{quantity},
|
||||
</if>
|
||||
<if test="sort != null">
|
||||
sort = #{sort},
|
||||
</if>
|
||||
<if test="deleted != null">
|
||||
deleted = #{deleted}
|
||||
</if>
|
||||
</set>
|
||||
WHERE id = #{id}
|
||||
</update>
|
||||
|
||||
<select id="selectListByNameLikeOrderBySortAsc" resultType="ProductSpuDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM product_spu
|
||||
<where>
|
||||
<if test="name != null">
|
||||
name LIKE "%"#{name}"%"
|
||||
</if>
|
||||
<if test="cid != null">
|
||||
AND cid = #{cid}
|
||||
</if>
|
||||
<if test="visible != null">
|
||||
AND visible = #{visible}
|
||||
</if>
|
||||
<if test="hasQuantity == true">
|
||||
AND quantity > 0
|
||||
</if>
|
||||
<if test="hasQuantity == false">
|
||||
AND quantity = 0
|
||||
</if>
|
||||
AND deleted = 0
|
||||
</where>
|
||||
ORDER BY sort ASC
|
||||
<if test="offset != null and limit != null">
|
||||
LIMIT #{offset}, #{limit}
|
||||
</if>
|
||||
</select>
|
||||
|
||||
<select id="selectCountByNameLike" resultType="Integer">
|
||||
SELECT
|
||||
COUNT(1)
|
||||
FROM product_spu
|
||||
<where>
|
||||
<if test="name != null">
|
||||
name LIKE "%"#{name}"%"
|
||||
</if>
|
||||
<if test="cid != null">
|
||||
AND cid = #{cid}
|
||||
</if>
|
||||
<if test="visible != null">
|
||||
AND visible = #{visible}
|
||||
</if>
|
||||
<if test="hasQuantity == true">
|
||||
AND quantity > 0
|
||||
</if>
|
||||
<if test="hasQuantity == false">
|
||||
AND quantity = 0
|
||||
</if>
|
||||
AND deleted = 0
|
||||
</where>
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="cn.iocoder.mall.product.dao.UserProductSpuCollectionsMapper">
|
||||
|
||||
<sql id="FIELDS">
|
||||
id, user_id, nickname, spu_id, spu_name,
|
||||
spu_image,sell_point,price, create_time, update_time,
|
||||
deleted
|
||||
</sql>
|
||||
|
||||
<select id="selectById" parameterType="Integer" resultType="UserProductSpuCollectionsDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM user_spu_collections
|
||||
WHERE id = #{id}
|
||||
AND deleted = 0
|
||||
</select>
|
||||
|
||||
|
||||
<select id="selectListByUser" resultType="UserProductSpuCollectionsDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM user_spu_collections
|
||||
<where>
|
||||
user_id = #{userId} AND deleted = 0
|
||||
</where>
|
||||
<if test="offset != null and limit != null">
|
||||
LIMIT #{offset}, #{limit}
|
||||
</if>
|
||||
</select>
|
||||
|
||||
<select id="selectCountByUser" resultType="Integer">
|
||||
SELECT
|
||||
COUNT(1)
|
||||
FROM user_spu_collections
|
||||
<where>
|
||||
user_id = #{userId} AND deleted = 0
|
||||
</where>
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
@@ -1,19 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
|
||||
<configuration>
|
||||
|
||||
<settings>
|
||||
<!-- 使用驼峰命名法转换字段。 -->
|
||||
<setting name="mapUnderscoreToCamelCase" value="true"/>
|
||||
</settings>
|
||||
|
||||
<typeAliases>
|
||||
<typeAlias alias="Integer" type="java.lang.Integer"/>
|
||||
<typeAlias alias="Long" type="java.lang.Long"/>
|
||||
<typeAlias alias="HashMap" type="java.util.HashMap"/>
|
||||
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap"/>
|
||||
<typeAlias alias="ArrayList" type="java.util.ArrayList"/>
|
||||
<typeAlias alias="LinkedList" type="java.util.LinkedList"/>
|
||||
</typeAliases>
|
||||
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user