先看一下页面新增商品是什么样的:
后台页面,商品列表页面,单击新增商品,弹出个新增商品的窗口。
我们选择商品分类之后,会发送一个请求,查询对应商品分类下的所有品牌,并返回到 所属品牌 中供选择。
从页面请求信息中可以看出:
请求方式:GET
请求路径:/brand/cid/{cid}
请求参数:cid
响应数据:品牌集合
下面来是实现一下 新增商品 时的 商品品牌查询:
1.编写Controller
2.在service中实现 queryBrandsByCid方法
3.编写sql语句
(因为根据分类查询品牌 有中间表,所有需要中间编写Sql)
重启 leyou-item 服务,去页面查看:
品牌信息就可以查询出来并选择了。
然后继续往后看:
填写商品基本信息,并下一步:
填写商品描述,并下一步:
(这里已经帮我们实现好了富文本编辑器。 介绍以及项目中的实现点这里:富文本编辑器使用 )
再下一步,填写规格参数:
填写规格参数这个需要我们去实现。
我们之前已经写过一个 根据 gid(规格组id) 查询规格参数的接口。
所以这里我们只需改造一下这个 查询规格参数的接口,添加一个参数 商品分类 cid。
不过,我们也会可能还会根据 是否搜素、是否为通用属性 等条件来过滤,不如我们这次多添加几个过滤条件。
这样后面需要使用的时候,就不用再又回来修改添加属性了。
我们看下页面请求,其实一开始点击新增商品的时候,就开始请求规格参数了:
下面来实现新增商品时 查询规格参数:
1.修改controller中的查询规格参数的方法
2.修改service中的queryParams方法:
重启 leyou-item 服务,然后去页面测试看看:
规格参数查询成功了,填写之后 下一步。
然后填写SKU属性 并保存:
我们要去实现这个 保存商品信息功能
(页面表单提交)
先看一下请求的信息:
可以看到 请求方式是POST,请求路径是item/goods
提交的数据整个是一个 json格式数据,包含Spu表所有数据:
-
brandId:品牌id
-
cid1、cid2、cid3:商品分类id
-
subTitle:副标题
-
title:标题
-
spuDetail:是一个json对象,代表商品详情表数据
-
afterService:售后服务
-
description:商品描述
-
packingList:包装列表
-
specialSpec:sku规格属性模板
-
genericSpec:通用规格参数
-
-
skus:spu下的所有sku数组,元素是每个sku对象:
-
title:标题
-
images:图片
-
price:价格
-
stock:库存
-
ownSpec:特有规格参数
-
indexes:特有规格参数的下标
-
下面我们去编写一下保存商品信息功能的代码
1.添加实体类
SPU和SpuDetail实体类已经添加过了,我们只需再添加 Sku 和 Stock 类即可:
Sku类:
@Table(name = "tb_sku")
public class Sku {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long spuId;
private String title;
private String images;
private Long price;
private String ownSpec;// 商品特殊规格的键值对
private String indexes;// 商品特殊规格的下标
private Boolean enable;// 是否有效,逻辑删除用
private Date createTime;// 创建时间
private Date lastUpdateTime;// 最后修改时间
@Transient
private Integer stock;// 库存
//记得生成getter、setter 方法
}
Stock类
@Table(name = "tb_stock")
public class Stock {
@Id
private Long skuId;
private Integer seckillStock;// 秒杀可用库存
private Integer seckillTotal;// 已秒杀数量
private Integer stock;// 正常库存
//记得生成 getter、setter方法
}
修改 SpuBo 类:
(记得生成getter、setter方法)
2.添加通用mapper方法
import tk.mybatis.mapper.common.Mapper;
public interface StockMapper extends Mapper<Stock> {
}
import tk.mybatis.mapper.common.Mapper;
public interface SpuDetailMapper extends Mapper<SpuDetail> {
}
import tk.mybatis.mapper.common.Mapper;
public interface SkuMapper extends Mapper<Sku> {
}
3.在GoodsController中添加新增商品的方法:
3. 在GoodsService中实现savaGoods方法
为了方便看,这里直接贴出GoodsService全部代码
@Service
public class GoodsService {
@Autowired
private SpuMapper spuMapper;
@Autowired
private CategoryService categoryService;
@Autowired
private BrandMapper brandMapper;
@Autowired
private SpuDetailMapper spuDetailMapper;
@Autowired
private StockMapper stockMapper;
@Autowired
private SkuMapper skuMapper;
public PageResult<SpuBo> querySpuBoByPage(String key, Boolean saleable, Integer page, Integer rows) {
Example example = new Example(Spu.class);
Example.Criteria criteria = example.createCriteria();
// 搜索条件
if (StringUtils.isNotBlank(key)) {
criteria.andLike("title", "%" + key + "%");
}
if (saleable != null) {
criteria.andEqualTo("saleable", saleable);
}
// 分页条件
PageHelper.startPage(page, rows);
// 执行查询
List<Spu> spus = this.spuMapper.selectByExample(example);
PageInfo<Spu> pageInfo = new PageInfo<>(spus);
List<SpuBo> spuBos = new ArrayList<>();
spus.forEach(spu->{
SpuBo spuBo = new SpuBo();
// copy共同属性的值到新的对象
BeanUtils.copyProperties(spu, spuBo);
// 查询分类名称
List<String> names = this.categoryService.queryNamesByIds(Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));
spuBo.setCname(StringUtils.join(names, "/"));
// 查询品牌的名称
spuBo.setBname(this.brandMapper.selectByPrimaryKey(spu.getBrandId()).getName());
spuBos.add(spuBo);
});
return new PageResult<>(pageInfo.getTotal(), spuBos);
}
/**
* 新增商品
* @param spuBo
*/
@Transactional
public void saveGoods(SpuBo spuBo) {
// 新增spu
// 设置默认字段
spuBo.setId(null);
spuBo.setSaleable(true);
spuBo.setValid(true);
spuBo.setCreateTime(new Date());
spuBo.setLastUpdateTime(spuBo.getCreateTime());
this.spuMapper.insertSelective(spuBo);
// 新增spuDetail
SpuDetail spuDetail = spuBo.getSpuDetail();
spuDetail.setSpuId(spuBo.getId());
this.spuDetailMapper.insertSelective(spuDetail);
saveSkuAndStock(spuBo);
}
private void saveSkuAndStock(SpuBo spuBo) {
spuBo.getSkus().forEach(sku -> {
// 新增sku
sku.setSpuId(spuBo.getId());
sku.setCreateTime(new Date());
sku.setLastUpdateTime(sku.getCreateTime());
this.skuMapper.insertSelective(sku);
// 新增库存
Stock stock = new Stock();
stock.setSkuId(sku.getId());
stock.setStock(sku.getStock());
this.stockMapper.insertSelective(stock);
});
}
}
重启 leyou-item 服务,去页测试看看:
保存成功了。
商品的 修改,删除,上架,下架,这里就不实现了。有兴趣的可以去实现一下。