三、后台管理新增商品

三、后台管理新增商品_第1张图片
选择商品分类,同时加载品牌分类对应的品牌列表和该分类的规格参数信息:
三、后台管理新增商品_第2张图片
涉及到的实体类:
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表示该属性并非一个到数据库表的字段的映射
    @Transient
    private Integer stock;// 忽略库存
}

SpuDetail

@Table(name="tb_spu_detail")
public class SpuDetail {
    @Id
    private Long spuId;// 对应的SPU的id
    private String description;// 商品描述
    private String specialSpec;// 商品特殊规格的名称及可选值模板
    private String genericSpec;// 商品的全局规格属性
    private String packingList;// 包装清单
    private String afterService;// 售后服务
}

SpecParam

@Table(name = "tb_spec_param")
public class SpecParam {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private Long cid;
    private Long groupId;
    private String name;
    @Column(name = "`numeric`")
    private Boolean numeric;
    private String unit;
    private Boolean generic;
    private Boolean searching;
    private String segments;
}

根据cid查询品牌:

@Controller
@RequestMapping("brand")
public class BrandController {
    @Autowired
    private BrandService brandService;
    /**
     * 根据cid 查询品牌列表
     */
    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandsByCid(@PathVariable("cid")Long cid){
        List<Brand> brands = this.brandService.queryBrandsByCid(cid);
        if (CollectionUtils.isEmpty(brands)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brands);
    }
}
/**
 * 根据cid查询品牌列表
 */
public List<Brand> queryBrandsByCid(Long cid) {
    return this.brandMapper.selectBrandByCid(cid);
}
public interface BrandMapper extends Mapper<Brand> {
    @Insert("insert into tb_category_brand (category_id, brand_id) value (#{cid}, #{bid})")
    // 多个参数的时候通过@Param指定参数的名称
    void insertCategoryAndBrand(@Param("cid") Long cid, @Param("bid") Long bid);

    // 根据cid查询品牌列表
    @Select("SELECT b.* from tb_brand b INNER JOIN tb_category_brand cb on b.id=cb.brand_id where cb.category_id=#{cid}")
    List<Brand> selectBrandByCid(Long cid);
}

根据cid查询规格参数信息

@Controller
@RequestMapping("spec")
public class SpecificationController {
    @Autowired
    private SpecificationService specificationService;
    /**
     * 根据参数查询规格参数
     * @param gid
     * @return
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> queryParams(
            @RequestParam(value = "gid", required = false)Long gid,
            @RequestParam(value = "cid", required = false)Long cid,
            @RequestParam(value = "generic", required = false)Boolean generic,
            @RequestParam(value = "searching", required = false)Boolean searching
            ){
        List<SpecParam> params = this.specificationService.queryParams(gid, cid, generic, searching);
        if (CollectionUtils.isEmpty(params)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(params);
    }
}
/**
 * 根据传递的具体参数查询规格参数
 * @param gid,group_id,规格参数的规格组
 * @return
 */
public List<SpecParam> queryParams(Long gid, Long cid, Boolean generic, Boolean searching) {
    SpecParam record = new SpecParam();
    record.setGroupId(gid);
    record.setCid(cid);
    record.setGeneric(generic);
    record.setSearching(searching);
    return this.specParamMapper.select(record);
}

保存商品:
三、后台管理新增商品_第3张图片
三、后台管理新增商品_第4张图片
三、后台管理新增商品_第5张图片
三、后台管理新增商品_第6张图片
三、后台管理新增商品_第7张图片

/**
 * 新增商品
 */
@PostMapping("goods")
public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo){
    this.goodsService.saveGoods(spuBo);
    return ResponseEntity.status(HttpStatus.CREATED).build();
}
@Transactional// 加事物,新增spu,新增spuDetail
public void saveGoods(SpuBo spuBo) {
    //1.新增spu,spuBo继承了spu
    //设置默认字段
    spuBo.setId(null);
    spuBo.setSaleable(true);
    spuBo.setValid(true);
    spuBo.setCreateTime(new Date());
    spuBo.setLastUpdateTime(spuBo.getCreateTime());
    this.spuMapper.insertSelective(spuBo);
    //2.新增spuDetail
    SpuDetail spuDetail = spuBo.getSpuDetail();
    spuDetail.setSpuId(spuBo.getId());
    this.spuDetailMapper.insertSelective(spuDetail);
    //3.新增sku和库存
    saveSkuAndStock(spuBo);
    //4.发送新增商品的消息到消息队列:item.insert,就是rooting key
    sendMsg("insert", spuBo.getId());
}
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);
    });
}

你可能感兴趣的:(某商城项目)