品优购day04--商品的添加---回显---修改

商品的添加 回显 修改

大家看下这个表看看能否看明白
品优购day04--商品的添加---回显---修改

  1. 添加–
    看到这张表 我们首先想到的是:
    这张表 含有多张表 所以我们要 封装一个bean 这样从前端接收的到值,用别的bean 都是接收不了的,
    封装一个这样的表
    public class PageGoods implements Serializable {
    //商品–spu也就是商品信息
    private Goods goods;
    //商品扩展信息
    private GoodsDesc goodsDesc;
    //商品属性信息 sku
    private List itemList;
    那么在业务层 我们如何添加呢,
    GoodsController层
    //获取商家id
    String name = SecurityContextHolder.getContext().getAuthentication().getName();
    goods.getGoods().setSellerId(name);//商户id
    try {
    goodsService.add(goods);
    return new Result(true,“添加成功”);
    } catch (Exception e) {
    e.printStackTrace();
    return new Result(false,“添加失败”);
    }
    GoodsService接口层
    //添加商品信息
    void add(PageGoods goods);

. GoodsServiceImpl层实现层

//设置商品状态
    goods.getGoods().setAuditStatus("0");//设置未申请状态
    //插入商品表
    goodsDao.insertSelective(goods.getGoods());
    //封装的商品表.获取商品属性表 赋值商品id (封装商品表获取商品信息,获取商品id)
    goods.getGoodsDesc().setGoodsId(goods.getGoods().getId());

    //插入sku表'   goods_id'

    goodsDescDao.insertSelective(goods.getGoodsDesc());
    //判断是否启用规格
    if ("1".equals(goods.getGoods().getIsEnableSpec())) {
     //判断商品详情是否为空
        if (goods.getItemList() != null) {

            for (Item item : goods.getItemList()) {
                //设置item对象中的属性值
                **item = setItem(item, goods);**---为什么用到setItem这个方法是因为代码都是公共的,都要用到,把他提取出来
                //商品标题 =商品名称+规格属性
                String title = goods.getGoods().getGoodsName();
                //获取规格json格式字符串, 格式例如 {网络:移动3G, 机身内存: 64G}
                String specJsonStr = item.getSpec();
                //规格属性
                Map<String, String> specMap = JSON.parseObject(specJsonStr, Map.class);
                if (specMap != null) {
                    for (String key : specMap.keySet()) {
                        title += " " + specMap.get(key);
                    }
                }
                item.setTitle(title);//标题 商品名称

                itemDao.insertSelective(item);

            }
        }
        } else {
            //没有启用规格
            //商品信息
            Item item = new Item();
            //赋值标题
            item.setTitle(goods.getGoods().getGoodsName());//商品KPU+规格描述串作为SKU名称
            //赋值价格
            item.setPrice(goods.getGoods().getPrice());//价格
            //赋值状态
            item.setStatus("1");//状态
            item.setIsDefault("1");//是否默认
            item.setNum(0);//库存数量
            //item.setMarketPrice(new BigDecimal(99999));//默认价格
            //JSON空值默认为 {}
            item.setSpec("{}");
          **item=setItem(item,goods);**
            itemDao.insertSelective(item);

        }

//私有方法
private Item setItem(Item item,PageGoods goods){

    item.setGoodsId(goods.getGoods().getId());//商品id
    item.setSellerId(goods.getGoods().getSellerId());//商家编号
    //审核状态, 默认为0未审核
    item.setStatus("0");
    item.setCreateTime(new Date());//创建时间
    item.setUpdateTime(new Date());//修改时间
    //品牌名称
    Brand brand = brandDao.selectByPrimaryKey(goods.getGoods().getBrandId());
    item.setBrand(brand.getName());
    //分类名称
    ItemCat itemCat = itemCatDao.selectByPrimaryKey(goods.getGoods().getCategory3Id());//分类

    item.setCategory(itemCat.getName());
    //商家id
    item.setCategoryid(goods.getGoods().getCategory3Id());
    //商家名称
    Seller seller = sellerDao.selectByPrimaryKey(goods.getGoods().getSellerId());
    item.setSeller(seller.getNickName());

    //图片地址(取spu的第一个图片)
    List<Map> imageList = JSON.parseArray(goods.getGoodsDesc().getItemImages(), Map.class);
    if (imageList.size() > 0) {
        item.setImage((String) imageList.get(0).get("url"));
    }
  return  item;
}
  1. 回显
    GoodsController层
    //回显
    @RequestMapping(“findOne”)
    public PageGoods findOneShop(Long id){

    return goodsService.findOneShop(id);
    }
    GoodsService接口层
    //商户商品信息回显
    PageGoods findOneShop(Long id);
    GoodsServiceImpl实现层
    根据id回显商品信息
    PageGoods goodEntity = new PageGoods();
    //根据ID封装goods
    Goods goods = goodsDao.selectByPrimaryKey(id);
    goodEntity.setGoods(goods);//赋值
    //商品扩展表
    GoodsDesc goodsDesc = goodsDescDao.selectByPrimaryKey(id);
    goodEntity.setGoodsDesc(goodsDesc);

     /**
      * 商品id,同时也是商品编号
      */
    //商品详情 根据条件查询商品属性表
     ItemQuery itemQuery = new ItemQuery();
     ItemQuery.Criteria criteria = itemQuery.createCriteria();
     criteria.andIdEqualTo(id);
     List<Item> items = itemDao.selectByExample(itemQuery);
    
      goodEntity.setItemList(items);
    
     return goodEntity;
    
  2. 修改
    GoodsController层
    //修改商品信息
    @RequestMapping(“update”)
    public Result update(@RequestBody PageGoods goods){
    try {
    goodsService.updateGoods(goods);
    return new Result(true,“修改成功”);
    } catch (Exception e) {
    e.printStackTrace();
    return new Result(false,“修改失败”);
    }
    }
    GoodsService接口层
    //修改商品信息
    void updateGoods(PageGoods goods);
    GoodsServiceImpl实现层
    //修改商品信息
    @Override
    public void updateGoods(PageGoods goods) {
    //重新赋值
    Goods goods1 = goods.getGoods();
    //商品信息
    goodsDao.updateByPrimaryKeySelective(goods1);
    // 商品spu
    GoodsDesc goodsDesc = goods.getGoodsDesc();
    goodsDescDao.updateByPrimaryKeySelective(goodsDesc);
    //商品sku
    List itemList = goods.getItemList();
    for (Item item : itemList) {
    itemDao.updateByPrimaryKeySelective(item);
    }

    }