创建规格模板
分析
- item-param-list中新增规格参数指向的url为/item-param-add,所以在 item-param-add.jsp中添加规格模板。
![4.itemParamToolBar.png](https://img-blog.csdn.net/20180310154457480)
- 通过common.js中的函数初始化类目选择器。如果查询到商品规格存在,则提示重复。
![4.initItemParam.png](https://img-blog.csdn.net/20180310155020259)
- 底部js中通过ajax存储规格参数,创建规格模板。
![4.itemParamjson.png](https://img-blog.csdn.net/2018031015595488)
![4.saveItemParam.png](https://img-blog.csdn.net/20180310155720908)
需求
- 查询规格模板:
- 请求url:/item/param/query/itemcatid/{itemCatId}
- 请求参数: 商品分类id,url中获得。
- 响应参数:TaoTaoResult。
- 存储规格模板:
- 请求url:/item/param/save/{cid}
- 请求参数:
- 商品分类id,url中的cid。
- 商品参数模板的json字符串,post的实体paramData。
- 响应参数:TaoTaoResult。
查询模板
Service层
- 创建ItemParamService和对应实现类。
- 根据商品分类id在tb_item_param表中查询对应的模板,在taotaoResult的data中设置查找到的模板或者置为Null。
@Service
public class ItemParamServiceImpl implements ItemParamService {
@Autowired
private TbItemParamMapper itemParamMapper;
@Override
public TaotaoResult getItemParamByCid(long cid) {
TbItemParamExample example = new TbItemParamExample();
Criteria criteria = example.createCriteria();
criteria.andItemCatIdEqualTo(cid);
List<TbItemParam> list = itemParamMapper.selectByExampleWithBLOBs(example);
if (list != null && list.size() > 0) {
return TaotaoResult.ok(list.get(0));
}
return TaotaoResult.ok();
}
}
Controller层
- 创建ItemParamController,接收商品分类id,返回包含查询模板结果的TaoTaoResult的json。
@Controller
@RequestMapping("/item/param")
public class ItemParamController {
@Autowired
private ItemParamService itemParamService;
@RequestMapping("/query/itemcatid/{itemCatId}")
@ResponseBody
public TaotaoResult getItemParamByCid(@PathVariable Long itemCatId) {
TaotaoResult result = itemParamService.getItemParamByCid(itemCatId);
return result;
}
}
创建模板
Service层
- ItemParamServiceImpl中,接收模板对象,补全参数之后插入数据库,返回TaotaoResult。
public TaotaoResult insertItemParam(TbItemParam itemParam) {
itemParam.setCreated(new Date());
itemParam.setUpdated(new Date());
itemParamMapper.insert(itemParam);
return TaotaoResult.ok();
}
Controller层
- ItemParamController中,接收商品分类id和模板参数json,装入TbItemParam类中调用service接口,返回TaotaoResult的json。
@RequestMapping("/save/{cid}")
@ResponseBody
public TaotaoResult insertItemParam(@PathVariable Long cid, String paramData) {
TbItemParam itemParam = new TbItemParam();
itemParam.setItemCatId(cid);
itemParam.setParamData(paramData);
TaotaoResult result = itemParamService.insertItemParam(itemParam);
return result;
}
根据规格模板生成表单
分析
- item-add.jsp中通过调用changeItemParam在选择商品类别之后生成规格信息表单。data.data.paramData为模板的json。
![4.createItemParamForm.png](https://img-blog.csdn.net/2018031016392353)
![4.changeItemParam.png](https://img-blog.csdn.net/20180310164226358)
- 生成的表单如下:
![4.showItemParam.png](https://img-blog.csdn.net/20180310164740687)
需求
保存商品规格参数
分析
- item-add.jsp中将规格参数转化为json字符串,放入隐藏input表单itemParams中,通过ajax保存包括商品规格参数在内的商品信息。
![4.postParams.png](https://img-blog.csdn.net/20180310165825676)
![4.nameItemParam.png](https://img-blog.csdn.net/20180310165945729)
需求
- 请求url:/item/save
- 请求参数:
- 商品信息(TbItem,不包括商品规格)。
- 商品描述。
- 商品规格。
- 响应参数:TaotaoResult。
- 需要修改添加商品的接口。
Service层
- 修改ItemService和实现类的addItem方法,增加itemParamItem参数,并补全规格信息,将规格插入数据库,返回taotaoResult。
public TaotaoResult addItem(TbItem item, TbItemDesc itemDesc, TbItemParamItem itemParamItem) {
try {
Long itemId = IDUtils.genItemId();
item.setId(itemId);
item.setStatus((byte) 1);
item.setCreated(new Date());
item.setUpdated(new Date());
itemMapper.insert(item);
itemDesc.setItemId(itemId);
itemDesc.setCreated(new Date());
itemDesc.setUpdated(new Date());
itemDescMapper.insert(itemDesc);
itemParamItem.setItemId(itemId);
itemParamItem.setCreated(new Date());
itemParamItem.setUpdated(new Date());
itemParamItemMapper.insert(itemParamItem);
} catch (Exception e) {
e.printStackTrace();
return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
}
return TaotaoResult.ok();
}
Controller层
- ItemController中addItem方法增加itemParams入参,组织itemParamItem,调用对应service方法,返回TaotaoResult的json。
@RequestMapping(value = "/save", method = RequestMethod.POST)
@ResponseBody
public TaotaoResult addItem(TbItem item, String desc, String itemParams) {
TbItemDesc itemDesc = new TbItemDesc();
itemDesc.setItemDesc(desc);
TbItemParamItem itemParamItem = new TbItemParamItem();
itemParamItem.setParamData(itemParams);
TaotaoResult result = itemService.addItem(item, itemDesc, itemParamItem);
return result;
}
展示规格参数
分析
- item-param-list.jsp中利用data-options控件显示规格参数。
![4.itemparamlist.png](https://img-blog.csdn.net/20180310171446444)
需求
- 请求url:/item/param/list?page=xxx&rows=yyy。
- 请求参数:分页参数page和rows。
- 响应参数:EasyUIDataGridResult,list中的对象包含的属性为TbItemParam的变量和itemCatName。
Service层
- taotao-manager-pojo中新建ItemParamWithName,满足data-options控件需要的变量。
public class ItemParamWithName extends TbItemParam {
private String itemCatName;
public ItemParamWithName(TbItemParam itemParam, String itemCatName) {
this.itemCatName = itemCatName;
this.setUpdated(itemParam.getUpdated());
this.setCreated(itemParam.getCreated());
this.setId(itemParam.getId());
this.setItemCatId(itemParam.getItemCatId());
this.setParamData(itemParam.getParamData());
}
public String getItemCatName() {
return itemCatName;
}
public void setItemCatName(String itemCatName) {
this.itemCatName = itemCatName;
}
}
- ItemParamService和实现类中根据分页参数查询规格参数列表,在逐个查询规格对应的名称之后,封装到ItemParamWithName类的列表中,最后封装到EasyUIDataGridResult中。
public EasyUIDataGridResult getItemParamList(int page, int rows) {
PageHelper.startPage(page, rows);
List<TbItemParam> paramList = itemParamMapper.selectByExampleWithBLOBs(new TbItemParamExample());
List<ItemParamWithName> paramWithNameList = new ArrayList<>(paramList.size());
for (TbItemParam itemParam : paramList) {
String name = getItemCatName(itemParam.getItemCatId());
paramWithNameList.add(new ItemParamWithName(itemParam, name));
}
PageInfo<TbItemParam> pageInfo = new PageInfo<>(paramList);
long total = pageInfo.getTotal();
EasyUIDataGridResult result = new EasyUIDataGridResult();
result.setTotal(total);
result.setRows(paramWithNameList);
return result;
}
private String getItemCatName(long itemCatId) {
TbItemCatExample example = new TbItemCatExample();
TbItemCatExample.Criteria criteria = example.createCriteria();
criteria.andIdEqualTo(itemCatId);
List<TbItemCat> list = itemCatMapper.selectByExample(example);
if (list != null && !list.isEmpty()) {
return list.get(0).getName();
}
return null;
}
Controller层
- 根据分页参数调用返回查询规格参数列表的service方法,返回EasyUIDataGridResult的json。
@RequestMapping("/list")
@ResponseBody
public EasyUIDataGridResult getItemList(Integer page, Integer rows) {
return itemParamService.getItemParamList(page, rows);
}