重构商品管理模块,支持多规格

This commit is contained in:
2024-07-26 02:48:15 +08:00
parent b81f80971f
commit 37a5e01443
14 changed files with 236 additions and 177 deletions
@@ -1,13 +1,10 @@
package com.gxwebsoft.shop.controller;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.oa.entity.App;
import com.gxwebsoft.shop.entity.GoodsSku;
import com.gxwebsoft.shop.entity.GoodsSpec;
import com.gxwebsoft.shop.entity.SpecValue;
import com.gxwebsoft.shop.service.GoodsService;
import com.gxwebsoft.shop.entity.Goods;
import com.gxwebsoft.shop.param.GoodsParam;
@@ -21,22 +18,19 @@ import com.gxwebsoft.shop.service.GoodsSkuService;
import com.gxwebsoft.shop.service.GoodsSpecService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 商品记录表控制器
*
* @author 科技小王子
* @since 2024-03-30 20:00:50
* @since 2024-07-25 19:22:01
*/
@Api(tags = "商品记录表管理")
@RestController
@@ -73,36 +67,27 @@ public class GoodsController extends BaseController {
return success(data);
}
@ApiOperation("分页查询商品记录表")
@GetMapping("/page")
public ApiResult<PageResult<Goods>> page(GoodsParam param) {
// 使用关联查询
return success(goodsService.pageRel(param));
}
@ApiOperation("查询全部商品记录表")
@GetMapping()
public ApiResult<List<Goods>> list(GoodsParam param) {
// 使用关联查询
return success(goodsService.listRel(param));
}
@Transactional(rollbackFor = {Exception.class})
@ApiOperation("根据id查询商品记录表")
@GetMapping("/{id}")
public ApiResult<Goods> get(@PathVariable("id") Integer id) {
// 使用关联查询
final Goods goods = goodsService.getByIdRel(id);
final List<GoodsSpec> goodsSpecList = goodsSpecService.list(new LambdaQueryWrapper<GoodsSpec>().eq(GoodsSpec::getGoodsId, id));
final List<GoodsSku> goodsSkuList = goodsSkuService.list(new LambdaQueryWrapper<GoodsSku>().eq(GoodsSku::getGoodsId, id));
if(goodsSpecList != null){
goods.setGoodsSpecs(goodsSpecList);
@ApiOperation("分页查询商品记录表")
@GetMapping("/page")
public ApiResult<PageResult<Goods>> page(GoodsParam param) {
// 使用关联查询
return success(goodsService.pageRel(param));
}
if(goodsSkuList != null){
goods.setGoodsSkus(goodsSkuList);
@ApiOperation("查询全部商品记录表")
@GetMapping()
public ApiResult<List<Goods>> list(GoodsParam param) {
// 使用关联查询
return success(goodsService.listRel(param));
}
@PreAuthorize("hasAuthority('shop:goods:list')")
@ApiOperation("根据id查询商品记录表")
@GetMapping("/{id}")
public ApiResult<Goods> get(@PathVariable("id") Integer id) {
// 使用关联查询
return success(goodsService.getByIdRel(id));
}
return success(goods);
}
@PreAuthorize("hasAuthority('shop:goods:save')")
@ApiOperation("添加商品记录表")
@@ -18,7 +18,7 @@ import lombok.EqualsAndHashCode;
* 商品记录表
*
* @author 科技小王子
* @since 2024-03-30 20:00:50
* @since 2024-07-25 19:22:01
*/
@Data
@EqualsAndHashCode(callSuper = false)
@@ -37,7 +37,7 @@ public class Goods implements Serializable {
@ApiModelProperty(value = "商品编码")
private String code;
@ApiModelProperty(value = "商品名称")
@ApiModelProperty(value = "商品标题")
private String goodsName;
@ApiModelProperty(value = "商品封面图")
@@ -46,10 +46,7 @@ public class Goods implements Serializable {
@ApiModelProperty(value = "商品详情")
private String content;
@ApiModelProperty(value = "商品附件")
private String files;
@ApiModelProperty(value = "分类ID")
@ApiModelProperty(value = "商品分类ID")
private Integer categoryId;
@ApiModelProperty(value = "一级分类")
@@ -64,31 +61,46 @@ public class Goods implements Serializable {
@ApiModelProperty(value = "货架")
private String position;
@ApiModelProperty(value = "单位名称")
private String unitName;
@ApiModelProperty(value = "消费赚取积分")
private BigDecimal gainIntegral;
@ApiModelProperty(value = "进货价格")
private BigDecimal price;
@ApiModelProperty(value = "销售价格")
private BigDecimal salePrice;
@ApiModelProperty(value = "库存计算方式(10下单减库存 20付款减库存)")
private Integer deductStockType;
@ApiModelProperty(value = "封面图")
private String files;
@ApiModelProperty(value = "销量")
private Integer sales;
@ApiModelProperty(value = "库存")
private Integer stock;
@ApiModelProperty(value = "库存计算方式(10下单减库存 20付款减库存)")
private Integer deductStockType;
@ApiModelProperty(value = "商品重量")
private Integer goodsWeight;
@ApiModelProperty(value = "推荐")
private Integer recommend;
@ApiModelProperty(value = "1上架 0下架")
@ApiModelProperty(value = "商户ID")
private Integer merchantId;
@ApiModelProperty(value = "商户名称")
private String merchantName;
@ApiModelProperty(value = "状态(0:未上架,1:上架)")
private Integer isShow;
@ApiModelProperty(value = "状态, 0上架 1下架 2待审核 3审核不通过")
@ApiModelProperty(value = "状态, 0上架 1待上架 2待审核 3审核不通过")
private Integer status;
@ApiModelProperty(value = "备注")
@@ -97,15 +109,9 @@ public class Goods implements Serializable {
@ApiModelProperty(value = "排序号")
private Integer sortNumber;
@ApiModelProperty(value = "所有人")
@ApiModelProperty(value = "用户ID")
private Integer userId;
@ApiModelProperty(value = "商户ID")
private Integer merchantId;
@ApiModelProperty(value = "商户名称")
private String merchantName;
@ApiModelProperty(value = "是否删除, 0否, 1是")
@TableLogic
private Integer deleted;
@@ -39,7 +39,7 @@ public class GoodsSpec implements Serializable {
private String specName;
@ApiModelProperty(value = "规格值")
private String specValues;
private String specValue;
@ApiModelProperty(value = "规格值")
@TableField(exist = false)
@@ -12,7 +12,7 @@ import java.util.List;
* 商品记录表Mapper
*
* @author 科技小王子
* @since 2024-03-30 20:00:50
* @since 2024-07-25 19:22:01
*/
public interface GoodsMapper extends BaseMapper<Goods> {
@@ -2,103 +2,121 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gxwebsoft.shop.mapper.GoodsMapper">
<!-- 关联查询sql -->
<sql id="selectSql">
SELECT a.*
FROM shop_goods a
<where>
<if test="param.goodsId != null">
AND a.goods_id = #{param.goodsId}
</if>
<if test="param.type != null">
AND a.type = #{param.type}
</if>
<if test="param.code != null">
AND a.code LIKE CONCAT('%', #{param.code}, '%')
</if>
<if test="param.goodsName != null">
AND a.goods_name LIKE CONCAT('%', #{param.goodsName}, '%')
</if>
<if test="param.content != null">
AND a.content LIKE CONCAT('%', #{param.content}, '%')
</if>
<if test="param.categoryId != null">
AND a.category_id = #{param.categoryId}
</if>
<if test="param.parentId != null">
AND a.category_id IN (SELECT category_id FROM shop_goods_category WHERE parent_id=#{param.parentId})
</if>
<if test="param.categoryParent != null">
AND a.category_parent LIKE CONCAT('%', #{param.categoryParent}, '%')
</if>
<if test="param.categoryChildren != null">
AND a.category_children LIKE CONCAT('%', #{param.categoryChildren}, '%')
</if>
<if test="param.specs != null">
AND a.specs LIKE CONCAT('%', #{param.specs}, '%')
</if>
<if test="param.position != null">
AND a.position LIKE CONCAT('%', #{param.position}, '%')
</if>
<if test="param.price != null">
AND a.price = #{param.price}
</if>
<if test="param.salePrice != null">
AND a.sale_price = #{param.salePrice}
</if>
<if test="param.recommend != null">
AND a.recommend = #{param.recommend}
</if>
<if test="param.isShow != null">
AND a.is_show = #{param.isShow}
</if>
<if test="param.status != null">
AND a.status = #{param.status}
</if>
<if test="param.stock != null">
AND a.stock = #{param.stock}
</if>
<if test="param.comments != null">
AND a.comments LIKE CONCAT('%', #{param.comments}, '%')
</if>
<if test="param.sortNumber != null">
AND a.sort_number = #{param.sortNumber}
</if>
<if test="param.userId != null">
AND a.user_id = #{param.userId}
</if>
<if test="param.merchantId != null">
AND a.merchant_id = #{param.merchantId}
</if>
<if test="param.deleted != null">
AND a.deleted = #{param.deleted}
</if>
<if test="param.deleted == null">
AND a.deleted = 0
</if>
<if test="param.createTimeStart != null">
AND a.create_time &gt;= #{param.createTimeStart}
</if>
<if test="param.createTimeEnd != null">
AND a.create_time &lt;= #{param.createTimeEnd}
</if>
<if test="param.keywords != null">
AND (a.comments LIKE CONCAT('%', #{param.keywords}, '%')
OR a.goods_name = #{param.keywords}
OR a.code = #{param.keywords}
)
</if>
</where>
</sql>
<!-- 关联查询sql -->
<sql id="selectSql">
SELECT a.*
FROM shop_goods a
<where>
<if test="param.goodsId != null">
AND a.goods_id = #{param.goodsId}
</if>
<if test="param.type != null">
AND a.type = #{param.type}
</if>
<if test="param.code != null">
AND a.code LIKE CONCAT('%', #{param.code}, '%')
</if>
<if test="param.goodsName != null">
AND a.goods_name LIKE CONCAT('%', #{param.goodsName}, '%')
</if>
<if test="param.image != null">
AND a.image LIKE CONCAT('%', #{param.image}, '%')
</if>
<if test="param.content != null">
AND a.content LIKE CONCAT('%', #{param.content}, '%')
</if>
<if test="param.categoryId != null">
AND a.category_id = #{param.categoryId}
</if>
<if test="param.categoryParent != null">
AND a.category_parent LIKE CONCAT('%', #{param.categoryParent}, '%')
</if>
<if test="param.categoryChildren != null">
AND a.category_children LIKE CONCAT('%', #{param.categoryChildren}, '%')
</if>
<if test="param.specs != null">
AND a.specs = #{param.specs}
</if>
<if test="param.position != null">
AND a.position LIKE CONCAT('%', #{param.position}, '%')
</if>
<if test="param.price != null">
AND a.price = #{param.price}
</if>
<if test="param.salePrice != null">
AND a.sale_price = #{param.salePrice}
</if>
<if test="param.deductStockType != null">
AND a.deduct_stock_type = #{param.deductStockType}
</if>
<if test="param.files != null">
AND a.files LIKE CONCAT('%', #{param.files}, '%')
</if>
<if test="param.sales != null">
AND a.sales = #{param.sales}
</if>
<if test="param.stock != null">
AND a.stock = #{param.stock}
</if>
<if test="param.goodsWeight != null">
AND a.goods_weight = #{param.goodsWeight}
</if>
<if test="param.recommend != null">
AND a.recommend = #{param.recommend}
</if>
<if test="param.merchantId != null">
AND a.merchant_id = #{param.merchantId}
</if>
<if test="param.parentId != null">
AND a.category_id IN (SELECT category_id FROM shop_goods_category WHERE parent_id=#{param.parentId})
</if>
<if test="param.merchantName != null">
AND a.merchant_name LIKE CONCAT('%', #{param.merchantName}, '%')
</if>
<if test="param.isShow != null">
AND a.is_show = #{param.isShow}
</if>
<if test="param.status != null">
AND a.status = #{param.status}
</if>
<if test="param.comments != null">
AND a.comments LIKE CONCAT('%', #{param.comments}, '%')
</if>
<if test="param.sortNumber != null">
AND a.sort_number = #{param.sortNumber}
</if>
<if test="param.userId != null">
AND a.user_id = #{param.userId}
</if>
<if test="param.deleted != null">
AND a.deleted = #{param.deleted}
</if>
<if test="param.deleted == null">
AND a.deleted = 0
</if>
<if test="param.createTimeStart != null">
AND a.create_time &gt;= #{param.createTimeStart}
</if>
<if test="param.createTimeEnd != null">
AND a.create_time &lt;= #{param.createTimeEnd}
</if>
<if test="param.keywords != null">
AND (a.comments LIKE CONCAT('%', #{param.keywords}, '%')
OR a.goods_name = #{param.keywords}
OR a.code = #{param.keywords}
)
</if>
</where>
</sql>
<!-- 分页查询 -->
<select id="selectPageRel" resultType="com.gxwebsoft.shop.entity.Goods">
<include refid="selectSql"></include>
</select>
<!-- 分页查询 -->
<select id="selectPageRel" resultType="com.gxwebsoft.shop.entity.Goods">
<include refid="selectSql"></include>
</select>
<!-- 查询全部 -->
<select id="selectListRel" resultType="com.gxwebsoft.shop.entity.Goods">
<include refid="selectSql"></include>
</select>
<!-- 查询全部 -->
<select id="selectListRel" resultType="com.gxwebsoft.shop.entity.Goods">
<include refid="selectSql"></include>
</select>
</mapper>
@@ -19,8 +19,8 @@
<if test="param.specName != null">
AND a.spec_name LIKE CONCAT('%', #{param.specName}, '%')
</if>
<if test="param.valueList != null">
AND a.value_list LIKE CONCAT('%', #{param.valueList}, '%')
<if test="param.specValue != null">
AND a.spec_value LIKE CONCAT('%', #{param.specValue}, '%')
</if>
<if test="param.type != null">
AND a.type = #{param.type}
@@ -16,7 +16,7 @@ import lombok.EqualsAndHashCode;
* 商品记录表查询参数
*
* @author 科技小王子
* @since 2024-03-30 20:00:50
* @since 2024-07-25 19:22:01
*/
@Data
@EqualsAndHashCode(callSuper = false)
@@ -36,13 +36,16 @@ public class GoodsParam extends BaseParam {
@ApiModelProperty(value = "商品编码")
private String code;
@ApiModelProperty(value = "商品名称")
@ApiModelProperty(value = "商品标题")
private String goodsName;
@ApiModelProperty(value = "商品封面图")
private String image;
@ApiModelProperty(value = "商品详情")
private String content;
@ApiModelProperty(value = "分类ID")
@ApiModelProperty(value = "商品分类ID")
@QueryField(type = QueryType.EQ)
private Integer categoryId;
@@ -52,7 +55,8 @@ public class GoodsParam extends BaseParam {
@ApiModelProperty(value = "二级分类")
private String categoryChildren;
@ApiModelProperty(value = "规格")
@ApiModelProperty(value = "商品规格 0单规格 1多规格")
@QueryField(type = QueryType.EQ)
private Integer specs;
@ApiModelProperty(value = "货架")
@@ -66,22 +70,44 @@ public class GoodsParam extends BaseParam {
@QueryField(type = QueryType.EQ)
private BigDecimal salePrice;
@ApiModelProperty(value = "推荐")
@ApiModelProperty(value = "库存计算方式(10下单减库存 20付款减库存)")
@QueryField(type = QueryType.EQ)
private Integer recommend;
private Integer deductStockType;
@ApiModelProperty(value = "1上架 0下架")
@QueryField(type = QueryType.EQ)
private Integer isShow;
@ApiModelProperty(value = "封面图")
private String files;
@ApiModelProperty(value = "状态, 0上架 1下架 2待审核 3审核不通过")
@ApiModelProperty(value = "销量")
@QueryField(type = QueryType.EQ)
private Integer status;
private Integer sales;
@ApiModelProperty(value = "库存")
@QueryField(type = QueryType.EQ)
private Integer stock;
@ApiModelProperty(value = "商品重量")
@QueryField(type = QueryType.EQ)
private Integer goodsWeight;
@ApiModelProperty(value = "推荐")
@QueryField(type = QueryType.EQ)
private Integer recommend;
@ApiModelProperty(value = "商户ID")
@QueryField(type = QueryType.EQ)
private Integer merchantId;
@ApiModelProperty(value = "商户名称")
private String merchantName;
@ApiModelProperty(value = "状态(0:未上架,1:上架)")
@QueryField(type = QueryType.EQ)
private Integer isShow;
@ApiModelProperty(value = "状态, 0上架 1待上架 2待审核 3审核不通过")
@QueryField(type = QueryType.EQ)
private Integer status;
@ApiModelProperty(value = "备注")
private String comments;
@@ -89,7 +115,7 @@ public class GoodsParam extends BaseParam {
@QueryField(type = QueryType.EQ)
private Integer sortNumber;
@ApiModelProperty(value = "所有人")
@ApiModelProperty(value = "用户ID")
@QueryField(type = QueryType.EQ)
private Integer userId;
@@ -39,7 +39,7 @@ public class GoodsSpecParam extends BaseParam {
private String specName;
@ApiModelProperty(value = "规格值")
private String valueList;
private String specValue;
@ApiModelProperty(value = "活动类型 0=商品,1=秒杀,2=砍价,3=拼团")
@QueryField(type = QueryType.EQ)
@@ -11,7 +11,7 @@ import java.util.List;
* 商品记录表Service
*
* @author 科技小王子
* @since 2024-03-30 20:00:50
* @since 2024-07-25 19:22:01
*/
public interface GoodsService extends IService<Goods> {
@@ -20,13 +20,15 @@ import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* 商品记录表Service实现
*
* @author 科技小王子
* @since 2024-03-30 20:00:50
* @since 2024-07-25 19:22:01
*/
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
@@ -40,6 +42,18 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
PageParam<Goods, GoodsParam> page = new PageParam<>(param);
page.setDefaultOrder("sort_number asc, create_time desc");
List<Goods> list = baseMapper.selectPageRel(page, param);
final Set<Integer> goodsIds = list.stream().map(Goods::getGoodsId).collect(Collectors.toSet());
if (!CollectionUtils.isEmpty(goodsIds)) {
final List<GoodsSpec> specs = goodsSpecService.list(new LambdaQueryWrapper<GoodsSpec>().in(GoodsSpec::getGoodsId, goodsIds));
final List<GoodsSku> skus = goodsSkuService.list(new LambdaQueryWrapper<GoodsSku>().in(GoodsSku::getGoodsId, goodsIds));
final Map<Integer, List<GoodsSpec>> collectSpecs = specs.stream().collect(Collectors.groupingBy(GoodsSpec::getGoodsId));
final Map<Integer, List<GoodsSku>> collectSkus = skus.stream().collect(Collectors.groupingBy(GoodsSku::getGoodsId));
list.forEach(d -> {
d.setGoodsSpecs(collectSpecs.get(d.getGoodsId()));
d.setGoodsSkus(collectSkus.get(d.getGoodsId()));
});
}
return new PageResult<>(list, page.getTotal());
}
@@ -66,11 +80,21 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
if (!CollectionUtils.isEmpty(goodsSpecs)) {
goodsSpecs.forEach(d -> {
d.setGoodsId(goods.getGoodsId());
final List<String> collect = d.getValueList().stream().map(SpecValue::getSpecValue).collect(Collectors.toList());
d.setSpecValues(StringUtils.join(collect, ','));
});
goodsSpecService.saveBatch(goodsSpecs);
// final GoodsSpec spec = goodsSpecs.get(0);
// if (spec != null) {
// final GoodsSpec goodsSpec = new GoodsSpec();
// goodsSpec.setGoodsId(goods.getGoodsId());
// goodsSpec.setSpecId(spec.getSpecId());
// goodsSpec.setSpecName(spec.getSpecName());
// goodsSpec.setSpecValue(spec.getSpecValue());
// goodsSpec.setTenantId(goods.getTenantId());
// System.out.println("goodsSpec = " + goodsSpec);
// goodsSpecService.save(goodsSpec);
// }
}
// 保存商品sku表
final List<GoodsSku> goodsSkus = goods.getGoodsSkus();
if (!CollectionUtils.isEmpty(goodsSkus)) {
@@ -23,7 +23,7 @@ public class GoodsSpecServiceImpl extends ServiceImpl<GoodsSpecMapper, GoodsSpec
@Override
public PageResult<GoodsSpec> pageRel(GoodsSpecParam param) {
PageParam<GoodsSpec, GoodsSpecParam> page = new PageParam<>(param);
page.setDefaultOrder("create_time desc");
page.setDefaultOrder("id desc");
List<GoodsSpec> list = baseMapper.selectPageRel(page, param);
return new PageResult<>(list, page.getTotal());
}
@@ -33,7 +33,7 @@ public class GoodsSpecServiceImpl extends ServiceImpl<GoodsSpecMapper, GoodsSpec
List<GoodsSpec> list = baseMapper.selectListRel(param);
// 排序
PageParam<GoodsSpec, GoodsSpecParam> page = new PageParam<>();
page.setDefaultOrder("create_time desc");
page.setDefaultOrder("id desc");
return page.sortRecords(list);
}
@@ -34,7 +34,7 @@ public class OrderInfoTask extends BaseController {
/**
* 自动处理过期订单任务
*/
@Scheduled(cron="*/10 * * * * *")
// @Scheduled(cron="*/10 * * * * *")
@Transactional(rollbackFor = {Exception.class})
public void settledExpirationOrder() {
final Order order = new Order();
@@ -57,7 +57,7 @@ public class OrderInfoTask extends BaseController {
* 自动处理过期订单任务(游泳馆专用)
* Scheduled(cron="0 30 21 * * ?")
*/
@Scheduled(cron="*/30 * * * * *")
// @Scheduled(cron="*/30 * * * * *")
@Transactional(rollbackFor = {Exception.class})
public void settledExpirationOrderBy3028() {
final Order order = new Order();
@@ -32,7 +32,7 @@ public class OrderTask extends BaseController {
/**
* 自动处理过期订单任务
*/
@Scheduled(fixedDelay = 2000, initialDelay = 2000)
// @Scheduled(fixedDelay = 2000, initialDelay = 2000)
@Transactional(rollbackFor = {Exception.class})
public void settledExpirationOrder() {
final OrderParam p = new OrderParam();
@@ -48,9 +48,9 @@ public class ShopGenerator {
private static final String MODULE_NAME = "shop";
// 需要生成的表
private static final String[] TABLE_NAMES = new String[]{
"shop_spec",
// "shop_spec",
// "shop_spec_value",
// "shop_goods",
"shop_goods",
// "shop_goods_spec",
// "shop_goods_sku",
// "shop_goods_category",