package com.mmall.controller.portal;
import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.ICartService;
import com.mmall.vo.CartVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/cart")
public class CartController {
Logger logger = LoggerFactory.getLogger(CartController.class);
@Resource
private ICartService iCartService;
/**
* 查看当前用户的购物车商品
* @param session
* @return
*/
@RequestMapping("list.do")
@ResponseBody
public ServerResponse<CartVo> list(HttpSession session){
User user=(User)session.getAttribute(Const.CURRENT_USER);
if(user==null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iCartService.list(user.getId()) ;
}
/**
* 新增购物车商品
* @param session
* @param count
* @param productId
* @return
*/
@RequestMapping("add.do")
@ResponseBody
public ServerResponse<CartVo> add(HttpSession session, Integer productId, Integer count){
User user=(User)session.getAttribute(Const.CURRENT_USER);
if(user==null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请以管理员账号登录");
}
return iCartService.add(user.getId(),productId,count);
}
/**
* 更新购物车商品的数量
* @param session
* @param count
* @param productId
* @return
*/
@RequestMapping("update.do")
@ResponseBody
public ServerResponse<CartVo> update(HttpSession session, Integer count, Integer productId){
User user=(User)session.getAttribute(Const.CURRENT_USER);
if(user==null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iCartService.update(user.getId(),productId,count);
}
/**
* 删除购物车中的产品
* @param session
* @param productIds
* @return
*/
@RequestMapping("delete_product.do")
@ResponseBody
public ServerResponse<CartVo> deleteProduct(HttpSession session, String productIds){
User user=(User)session.getAttribute(Const.CURRENT_USER);
if(user==null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iCartService.deleteProduct(user.getId(),productIds);
}
//全选
@RequestMapping("select_all.do")
@ResponseBody
public ServerResponse<CartVo> selectAll(HttpSession session){
User user=(User)session.getAttribute(Const.CURRENT_USER);
if(user==null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iCartService.selectOrUnSelect(user.getId(),null,Const.Cart.CHECKED);
}
//全反选
@RequestMapping("un_select_all.do")
@ResponseBody
public ServerResponse<CartVo> unSelectAll(HttpSession session){
User user=(User)session.getAttribute(Const.CURRENT_USER);
if(user==null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iCartService.selectOrUnSelect(user.getId(),null,Const.Cart.UN_CHECKED);
}
//单选
@RequestMapping("select.do")
@ResponseBody
public ServerResponse<CartVo> Select(HttpSession session,Integer productId){
User user=(User)session.getAttribute(Const.CURRENT_USER);
if(user==null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iCartService.selectOrUnSelect(user.getId(),null,Const.Cart.CHECKED);
}
//单反选
@RequestMapping("un_select.do")
@ResponseBody
public ServerResponse<CartVo> unSelect(HttpSession session,Integer productId){
User user=(User)session.getAttribute(Const.CURRENT_USER);
if(user==null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
}
return iCartService.selectOrUnSelect(user.getId(),productId,Const.Cart.UN_CHECKED);
}
/**
* 购物车商品数量
* @param session
* @param productId
* @return
*/
@RequestMapping("get_cart_product_count.do")
@ResponseBody
public ServerResponse<Integer> getCartProductCount(HttpSession session,Integer productId){
User user=(User)session.getAttribute(Const.CURRENT_USER);
if(user==null){
return ServerResponse.createBySuccess(0);
}
return iCartService.getCartProductCount(user.getId());
}
}