use of cn.e3mall.manager.pojo.TbItem in project e3mall by colg-cloud.
the class TbItemServiceImpl method updateItem.
@Override
public E3Result updateItem(Long id, TbItem tbItem, String desc) {
TbItem db_tbItem = tbItemMapper.selectByPrimaryKey(id);
tbItem.setStatus(db_tbItem.getStatus());
tbItem.setCreated(db_tbItem.getCreated());
tbItem.setUpdated(new Date());
tbItemMapper.updateByPrimaryKey(tbItem);
TbItemDesc tbItemDesc = tbItemDescMapper.selectByPrimaryKey(tbItem.getId());
tbItemDesc.setItemDesc(desc);
tbItemDesc.setUpdated(new Date());
tbItemDescMapper.updateByPrimaryKey(tbItemDesc);
// 发布 "商品添加" 消息
jmsTemplate.send(topicDestination, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage(id + "");
return textMessage;
}
});
return E3Result.ok();
}
use of cn.e3mall.manager.pojo.TbItem in project e3mall by colg-cloud.
the class PageHelperTest method testPageHelper.
@Test
public void testPageHelper() {
applicationContext = new ClassPathXmlApplicationContext("classpath:/spring/applicationContext-dao.xml");
// 从容器中获得Mapper代理对象
TbItemMapper tbItemMapper = applicationContext.getBean(TbItemMapper.class);
// 执行sql语句之前设置分页信息,使用PageHelper的startPatge方法
PageHelper.startPage(1, 10);
// 执行查询
List<TbItem> list = tbItemMapper.selectAll();
// 取分页信息,PageInfo,1:总记录数,2:总页数,当前页码
PageInfo<TbItem> pageInfo = new PageInfo<>(list);
System.out.println(pageInfo.getTotal());
System.out.println(pageInfo.getPages());
System.out.println(list.size());
}
use of cn.e3mall.manager.pojo.TbItem in project e3mall by colg-cloud.
the class CartController method showCartList.
/**
* 展示购物车列表, 返回购物车列表视图
*
* @param request
* @return
*/
@GetMapping("/cart")
public String showCartList(HttpServletRequest request, HttpServletResponse response) {
/*
* 1. 从cookie中获取购物车列表
* 2. 判断用户是否登录
* 登录状态:
* 1). 把客户端(cookie)中的购物车商品和服务端(redis)中的购物车商品合并
* 2). 删除cookie中的购物车
* 3). 从redis中获取购物车列表, 传递到页面, 展示
* 未登录: 把cookie中列表传递到页面,展示
*/
// 从cookie中取购物车列表
List<TbItem> cartList = this.getCartListFromCookie(request);
// 判断用户是否为登录状态
TbUser tbUser = (TbUser) request.getAttribute("user");
if (tbUser != null) {
// 合并
Long userId = tbUser.getId();
cartService.mergeCart(userId, cartList);
// 删除cookie中的购物车
CookieUtils.deleteCookie(request, response, COOKIE_CART);
// 从redis中获取购物车列表
cartList = cartService.getCartList(userId);
}
// 把列表传递给页面
request.setAttribute("cartList", cartList);
// 返回逻辑视图
return "cart";
}
use of cn.e3mall.manager.pojo.TbItem in project e3mall by colg-cloud.
the class CartController method deleteCartItem.
/**
* 删除购物车商品
*
* @return 购物车列表视图
*/
@GetMapping("/delete/{itemId}")
public String deleteCartItem(@PathVariable Long itemId, HttpServletRequest request, HttpServletResponse response) {
// 判断用户是否登录
TbUser tbUser = (TbUser) request.getAttribute("user");
if (tbUser != null) {
cartService.deleteCartItem(tbUser.getId(), itemId);
return "redirect:/cart/cart.html";
}
// 从cookie中取购物车列表
List<TbItem> cartList = this.getCartListFromCookie(request);
// 遍历列表,找到要删除的商品
Iterator<TbItem> iterator = cartList.iterator();
while (iterator.hasNext()) {
TbItem tbItem = iterator.next();
if (tbItem.getId() == itemId.longValue()) {
// 删除商品
iterator.remove();
break;
}
}
// 把购物车列表写入cookie
CookieUtils.setCookie(request, response, COOKIE_CART, JSON.toJSONString(cartList), COOKIE_CART_EXPIRE, true);
// 重定向到逻辑视图, 绝对路径 在项目后面拼接 /cart/cart.html
return "redirect:/cart/cart.html";
}
use of cn.e3mall.manager.pojo.TbItem in project e3mall by colg-cloud.
the class CartServiceImpl method addCart.
@Override
public E3Result addCart(Long userId, Long itemId, Integer num) {
/*
* 向redis中添加购物车, 数据类型是 hash, key:用户id, field:商品id, value:商品信息
*
* 1. 判断商品是否存在
* 存在: 商品数量相加
* 不存在: 根据商品id取商品信息, 添加到购物车列表
*
* 2. 返回成功
*/
String key = CART_LIST_PRE + ":" + userId;
String field = itemId + "";
Boolean hexists = jedisClient.hexists(key, field);
// 商品存在
if (hexists) {
String jsonString = jedisClient.hget(key, field);
// 把jsonString转换成tbItem
TbItem tbItem = JSON.parseObject(jsonString, TbItem.class);
tbItem.setNum(tbItem.getNum() + num);
// 写回redis
jedisClient.hset(key, field, JSON.toJSONString(tbItem));
return E3Result.ok();
}
// 商品不存在, 根据商品id取商品信息
TbItem tbItem = tbItemMapper.selectByPrimaryKey(itemId);
// 设置购物车里商品属性
tbItem.setNum(num);
String image = tbItem.getImage();
if (StringUtils.isNotBlank(image)) {
tbItem.setImage(image.split(",")[0]);
}
// 添加到购物车列表
jedisClient.hset(key, field, JSON.toJSONString(tbItem));
return E3Result.ok();
}
Aggregations