6商品编辑功能的实现
6.1 前端信息
- 返回的数据结构:TaotaoResult
- 请求的url:/item/save
- 请求类型与提交参数:$("#itemAddForm").serialize()将表单序列号为key-value形式的字符串以post 的形式将表单的内容提交。
6.2.ItemServiceImpl
@Override
public TaotaoResult updateItem(TbItem item, String desc, String itemParam) throws Exception {
TbItemExample example = new TbItemExample();
TbItemExample.Criteria criteria= example.createCriteria();
Long itemId = item.getId();
criteria.andIdEqualTo(itemId);
TbItem oldItem = itemMapper.selectByPrimaryKey(itemId);
//更新商品信息
item.setStatus((byte) 1);
item.setCreated(oldItem.getCreated());
item.setUpdated(new Date());
itemMapper.updateByExample(item,example);
//更新商品描述信息
TaotaoResult result = updateItemDesc(itemId,desc);
if(result.getStatus()!=200) {
throw new Exception();
}
//更新商品规格参数
result = updateItemParamItem(itemId,itemParam);
if(result.getStatus()!=200) {
throw new Exception();
}
return TaotaoResult.ok();
}
public TaotaoResult updateItemDesc(Long itemId, String desc) throws Exception {
TbItemDescExample example = new TbItemDescExample();
TbItemDescExample.Criteria criteria = example.createCriteria();
criteria.andItemIdEqualTo(itemId);
TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
itemDesc.setUpdated(new Date());
itemDesc.setItemDesc(desc);
itemDescMapper.updateByExample(itemDesc,example);
return TaotaoResult.ok();
}
public TaotaoResult updateItemParamItem(Long itemId, String param) throws Exception {
TbItemParamItem itemParamItem = itemParamItemMapper.selectByPrimaryKey(itemId);
if(itemParamItem==null) {
return TaotaoResult.ok();
}
itemParamItem.setUpdated(new Date());
itemParamItem.setParamData(param);
TbItemParamItemExample example = new TbItemParamItemExample();
TbItemParamItemExample.Criteria criteria = example.createCriteria();
criteria.andItemIdEqualTo(itemId);
itemParamItemMapper.updateByExample(itemParamItem, example);
return TaotaoResult.ok();
}
6.3 Controller
@RequestMapping(value = "/update",method = RequestMethod.POST)
@ResponseBody
public TaotaoResult updateItem(TbItem item,String desc,String itemParams) throws Exception{
TaotaoResult result = itemService.updateItem(item,desc,itemParams);
return result;
}
7商品删除功能的实现
7.1 前端信息
- 返回的数据结构:TaotaoResult
- 请求的url:/item/delete
- 请求类型与提交参数:post请求,ids
7.1.ItemServiceImpl
@Override
public TaotaoResult deleteItemById(long itemId) throws Exception {
itemMapper.deleteByPrimaryKey(itemId);
itemDescMapper.deleteByPrimaryKey(itemId);
itemParamItemMapper.deleteByPrimaryKey(itemId);
return TaotaoResult.ok();
}
7.2 Controller
@RequestMapping(value = "/delete",method = RequestMethod.POST)
@ResponseBody
public TaotaoResult updateItem(@RequestParam String ids) throws Exception{
String[] list = ids.split(",");
for(String i:list){
Long itemId = Long.valueOf(i);
TaotaoResult result = itemService.deleteItemById(itemId);
if(result.getStatus()!=200){
return TaotaoResult.build(500,"删除失败,一个或多个未被刪除");
}
}
return TaotaoResult.ok();
}
网友评论