增加 user 端的商品分类接口
增加 user 端的商品 spu 接口
This commit is contained in:
@@ -3,6 +3,8 @@ package cn.iocoder.mall.product.convert;
|
||||
import cn.iocoder.mall.product.api.bo.*;
|
||||
import cn.iocoder.mall.product.api.dto.ProductAttrAddDTO;
|
||||
import cn.iocoder.mall.product.api.dto.ProductAttrUpdateDTO;
|
||||
import cn.iocoder.mall.product.api.dto.ProductAttrValueAddDTO;
|
||||
import cn.iocoder.mall.product.api.dto.ProductAttrValueUpdateDTO;
|
||||
import cn.iocoder.mall.product.dataobject.ProductAttrDO;
|
||||
import cn.iocoder.mall.product.dataobject.ProductAttrValueDO;
|
||||
import org.mapstruct.Mapper;
|
||||
@@ -40,10 +42,17 @@ public interface ProductAttrConvert {
|
||||
@Mappings({})
|
||||
ProductAttrDO convert(ProductAttrUpdateDTO productAttrUpdateDTO);
|
||||
|
||||
@Mappings({})
|
||||
ProductAttrValueDO convert(ProductAttrValueAddDTO productAttrValueAddDTO);
|
||||
|
||||
@Mappings({})
|
||||
ProductAttrValueDO convert(ProductAttrValueUpdateDTO productAttrValueUpdateDTO);
|
||||
|
||||
@Mappings({})
|
||||
ProductAttrBO convert(ProductAttrDO productAttrDO);
|
||||
|
||||
@Mappings({})
|
||||
ProductAttrValueBO convert2(ProductAttrValueDO productAttrValueDO);
|
||||
|
||||
|
||||
}
|
||||
@@ -18,4 +18,12 @@ public interface ProductAttrValueMapper {
|
||||
|
||||
List<ProductAttrValueDO> selectListByAttrIds(@Param("attrIds") Collection<Integer> attrIds);
|
||||
|
||||
ProductAttrValueDO selectByAttrIdAndName(@Param("attrId") Integer attrId,
|
||||
@Param("name") String name);
|
||||
|
||||
|
||||
void insert(ProductAttrValueDO productAttrValueDO);
|
||||
|
||||
void update(ProductAttrValueDO productAttrValueDO);
|
||||
|
||||
}
|
||||
@@ -15,10 +15,15 @@ public interface ProductSpuMapper {
|
||||
|
||||
void update(ProductSpuDO productSpuDO);
|
||||
|
||||
// TODO 芋艿,需要捉摸下,怎么优化下。参数有点多
|
||||
List<ProductSpuDO> selectListByNameLikeOrderBySortAsc(@Param("name") String name,
|
||||
@Param("cid") Integer cid,
|
||||
@Param("visible") Boolean visible,
|
||||
@Param("offset") Integer offset,
|
||||
@Param("limit") Integer limit);
|
||||
|
||||
Integer selectCountByNameLike(@Param("name") String name);
|
||||
Integer selectCountByNameLike(@Param("name") String name,
|
||||
@Param("cid") Integer cid,
|
||||
@Param("visible") Boolean visible);
|
||||
|
||||
}
|
||||
@@ -57,6 +57,20 @@ public class ProductSpuDO extends BaseDO {
|
||||
*/
|
||||
private Integer sort;
|
||||
|
||||
// ========== Sku 相关字段 =========
|
||||
/**
|
||||
* 价格
|
||||
*
|
||||
* 目前的计算方式是,以 Sku 最小价格为准
|
||||
*/
|
||||
private Integer price;
|
||||
/**
|
||||
* 库存数量
|
||||
*
|
||||
* 目前的计算方式是,以 Sku 库存累加为准
|
||||
*/
|
||||
private Integer quantity;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
@@ -128,4 +142,23 @@ public class ProductSpuDO extends BaseDO {
|
||||
this.sort = sort;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public ProductSpuDO setPrice(Integer price) {
|
||||
this.price = price;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public ProductSpuDO setQuantity(Integer quantity) {
|
||||
this.quantity = quantity;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -167,17 +167,58 @@ public class ProductAttrServiceImpl implements ProductAttrService {
|
||||
|
||||
@Override
|
||||
public CommonResult<ProductAttrValueBO> addProductAttrValue(Integer adminId, ProductAttrValueAddDTO productAttrValueAddDTO) {
|
||||
return null;
|
||||
// 校验规格名不重复
|
||||
if (productAttrValueMapper.selectByAttrIdAndName(productAttrValueAddDTO.getAttrId(), productAttrValueAddDTO.getName()) != null) {
|
||||
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_ATTR_VALUE_EXISTS.getCode());
|
||||
}
|
||||
// 插入到数据库
|
||||
ProductAttrValueDO productAttrValueDO = ProductAttrConvert.INSTANCE.convert(productAttrValueAddDTO)
|
||||
.setStatus(ProductAttrConstants.ATTR_VALUE_STATUS_ENABLE);
|
||||
productAttrValueDO.setCreateTime(new Date()).setDeleted(BaseDO.DELETED_NO);
|
||||
productAttrValueMapper.insert(productAttrValueDO);
|
||||
// 返回成功
|
||||
return CommonResult.success(ProductAttrConvert.INSTANCE.convert2(productAttrValueDO));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> updateProductAttrValue(Integer adminId, ProductAttrValueUpdateDTO productAttrValueUpdateDTO) {
|
||||
return null;
|
||||
// 校验存在
|
||||
ProductAttrValueDO productAttrValueDO = productAttrValueMapper.selectById(productAttrValueUpdateDTO.getId());
|
||||
if (productAttrValueDO == null) {
|
||||
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_ATTR_VALUE_NOT_EXIST.getCode());
|
||||
}
|
||||
// 校验规格名不重复
|
||||
ProductAttrValueDO existsAttrDO = productAttrValueMapper.selectByAttrIdAndName(productAttrValueDO.getAttrId(), productAttrValueUpdateDTO.getName());
|
||||
if (existsAttrDO != null && !existsAttrDO.getId().equals(productAttrValueUpdateDTO.getId())) {
|
||||
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_ATTR_VALUE_EXISTS.getCode());
|
||||
}
|
||||
// 更新到数据库
|
||||
ProductAttrValueDO updateProductValue = ProductAttrConvert.INSTANCE.convert(productAttrValueUpdateDTO);
|
||||
productAttrValueMapper.update(updateProductValue);
|
||||
// 返回成功
|
||||
return CommonResult.success(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> updateProductAttrValueStatus(Integer adminId, Integer productAttrValueId, Integer status) {
|
||||
return null;
|
||||
// 校验参数
|
||||
if (!isValidAttrValueStatus(status)) {
|
||||
return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "变更状态必须是开启(1)或关闭(2)"); // TODO 有点搓
|
||||
}
|
||||
// 校验存在
|
||||
ProductAttrValueDO productAttrValueDO = productAttrValueMapper.selectById(productAttrValueId);
|
||||
if (productAttrValueDO == null) {
|
||||
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_ATTR_VALUE_NOT_EXIST.getCode());
|
||||
}
|
||||
// 校验状态
|
||||
if (productAttrValueDO.getStatus().equals(status)) {
|
||||
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_ATTR_VALUE_STATUS_EQUALS.getCode());
|
||||
}
|
||||
// 更新到数据库
|
||||
ProductAttrValueDO updateProductAttrValue = new ProductAttrValueDO().setId(productAttrValueId).setStatus(status);
|
||||
productAttrValueMapper.update(updateProductAttrValue);
|
||||
// 返回成功
|
||||
return CommonResult.success(true);
|
||||
}
|
||||
|
||||
private boolean isValidAttrStatus(Integer status) {
|
||||
|
||||
@@ -92,6 +92,7 @@ public class ProductSpuServiceImpl implements ProductSpuService {
|
||||
.setPicUrls(StringUtil.join(productSpuAddDTO.getPicUrls(), ","))
|
||||
.setSort(0); // 排序为 0
|
||||
spu.setCreateTime(new Date()).setDeleted(BaseDO.DELETED_NO);
|
||||
initSpuFromSkus(spu, productSpuAddDTO.getSkus()); // 初始化 sku 相关信息到 spu 中
|
||||
productSpuMapper.insert(spu);
|
||||
// 保存 Sku
|
||||
List<ProductSkuDO> skus = productSpuAddDTO.getSkus().stream().map(productSkuAddDTO -> {
|
||||
@@ -141,6 +142,7 @@ public class ProductSpuServiceImpl implements ProductSpuService {
|
||||
// 更新 Spu
|
||||
ProductSpuDO updateSpu = ProductSpuConvert.INSTANCE.convert(productSpuUpdateDTO)
|
||||
.setPicUrls(StringUtil.join(productSpuUpdateDTO.getPicUrls(), ","));
|
||||
initSpuFromSkus(updateSpu, productSpuUpdateDTO.getSkus()); // 初始化 sku 相关信息到 spu 中
|
||||
productSpuMapper.update(updateSpu);
|
||||
// 修改 Sku
|
||||
List<ProductSkuDO> existsSkus = productSkuMapper.selectListBySpuIdAndStatus(productSpuUpdateDTO.getId(), ProductSpuConstants.SKU_STATUS_ENABLE);
|
||||
@@ -198,10 +200,11 @@ public class ProductSpuServiceImpl implements ProductSpuService {
|
||||
ProductSpuPageBO productSpuPage = new ProductSpuPageBO();
|
||||
// 查询分页数据
|
||||
int offset = productSpuPageDTO.getPageNo() * productSpuPageDTO.getPageSize();
|
||||
productSpuPage.setSpus(ProductSpuConvert.INSTANCE.convert(productSpuMapper.selectListByNameLikeOrderBySortAsc(productSpuPageDTO.getName(),
|
||||
productSpuPage.setSpus(ProductSpuConvert.INSTANCE.convert(productSpuMapper.selectListByNameLikeOrderBySortAsc(
|
||||
productSpuPageDTO.getName(), productSpuPageDTO.getCid(), productSpuPageDTO.getVisible(),
|
||||
offset, productSpuPageDTO.getPageSize())));
|
||||
// 查询分页总数
|
||||
productSpuPage.setCount(productSpuMapper.selectCountByNameLike(productSpuPageDTO.getName()));
|
||||
productSpuPage.setCount(productSpuMapper.selectCountByNameLike(productSpuPageDTO.getName(), productSpuPageDTO.getCid(), productSpuPageDTO.getVisible()));
|
||||
// 返回结果
|
||||
return CommonResult.success(productSpuPage);
|
||||
}
|
||||
@@ -250,4 +253,10 @@ public class ProductSpuServiceImpl implements ProductSpuService {
|
||||
return null;
|
||||
}
|
||||
|
||||
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()); // 求库存之和
|
||||
}
|
||||
|
||||
}
|
||||
@@ -93,4 +93,38 @@
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<select id="selectByAttrIdAndName" resultType="ProductAttrValueDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM product_attr_value
|
||||
WHERE name = #{name}
|
||||
AND deleted = 0
|
||||
LIMIT 1
|
||||
</select>
|
||||
|
||||
<insert id="insert" parameterType="ProductAttrValueDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
|
||||
INSERT INTO product_attr_value (
|
||||
attr_id, name, status, create_time, deleted
|
||||
) VALUES (
|
||||
#{attrId}, #{name}, #{status}, #{createTime}, #{deleted}
|
||||
)
|
||||
</insert>
|
||||
|
||||
<update id="update" parameterType="ProductAttrValueDO">
|
||||
UPDATE product_attr_value
|
||||
<set>
|
||||
<if test="name != null">
|
||||
name = #{name},
|
||||
</if>
|
||||
<if test="status != null">
|
||||
status = #{status},
|
||||
</if>
|
||||
<if test="deleted != null">
|
||||
deleted = #{deleted}
|
||||
</if>
|
||||
</set>
|
||||
WHERE id = #{id}
|
||||
</update>
|
||||
|
||||
|
||||
</mapper>
|
||||
@@ -4,7 +4,8 @@
|
||||
|
||||
<sql id="FIELDS">
|
||||
id, name, sell_point, description, cid,
|
||||
pic_urls, visible, sort, create_time
|
||||
pic_urls, visible, sort, price, quantity,
|
||||
create_time
|
||||
</sql>
|
||||
|
||||
<select id="selectById" parameterType="Integer" resultType="ProductSpuDO">
|
||||
@@ -18,10 +19,12 @@
|
||||
<insert id="insert" parameterType="ProductSpuDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
|
||||
INSERT INTO product_spu (
|
||||
name, sell_point, description, cid, pic_urls,
|
||||
visible, sort, deleted, create_time
|
||||
visible, sort, price, quantity,
|
||||
deleted, create_time
|
||||
) VALUES (
|
||||
#{name}, #{sellPoint}, #{description}, #{cid}, #{picUrls},
|
||||
#{visible}, #{sort}, #{deleted}, #{createTime}
|
||||
#{visible}, #{sort}, #{price}, #{quantity},
|
||||
#{deleted}, #{createTime}
|
||||
)
|
||||
</insert>
|
||||
|
||||
@@ -46,6 +49,12 @@
|
||||
<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>
|
||||
@@ -64,6 +73,12 @@
|
||||
<if test="name != null">
|
||||
name LIKE "%"#{name}"%"
|
||||
</if>
|
||||
<if test="cid != null">
|
||||
AND cid = #{cid}
|
||||
</if>
|
||||
<if test="visible != null">
|
||||
AND visible = #{visible}
|
||||
</if>
|
||||
AND deleted = 0
|
||||
</where>
|
||||
ORDER BY sort ASC
|
||||
@@ -78,6 +93,12 @@
|
||||
<if test="name != null">
|
||||
name LIKE "%"#{name}"%"
|
||||
</if>
|
||||
<if test="cid != null">
|
||||
AND cid = #{cid}
|
||||
</if>
|
||||
<if test="visible != null">
|
||||
AND visible = #{visible}
|
||||
</if>
|
||||
AND deleted = 0
|
||||
</where>
|
||||
</select>
|
||||
|
||||
Reference in New Issue
Block a user