首页>代码>vue+maven+ssh的网上花店系统>/flowers/src/main/java/com/jack/controller/CartController.java
package com.jack.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jack.entity.Cart;
import com.jack.entity.CartItem;
import com.jack.entity.Product;
import com.jack.service.CartService;
import com.util.controller.BaseController;
import com.util.entity.ResponseEntity;

@RestController
@RequestMapping("cart")
public class CartController extends BaseController{

	private static final Logger LOGGER=Logger.getLogger(CartController.class);
	
	@Autowired
	private CartService cartService;
	
	@GetMapping
	public ResponseEntity getCartList(HttpServletRequest request){
		ResponseEntity re=new ResponseEntity();
		List<CartItem> cartItemList=new ArrayList<CartItem>();
		Cart cart=(Cart) super.getSessionParams(request, "cart");
		try{	
			Map<String, Object> retMap=new HashMap<String, Object>();
			if(cart!=null){
				for ( Map<String,CartItem> cartItemMap : cart.getMapList()) {
					for (Map.Entry<String,CartItem> entry : cartItemMap.entrySet()) {
						CartItem item=entry.getValue();
						cartItemList.add(item);
					}
				}
				retMap.put("list", cartItemList);
				retMap.put("totalPrice", cart.getTotalPrice());
				retMap.put("totalNum", cart.getTotalNum());
				re.setData(retMap);
			    re.setStatusCode("200");
			}else{
				re.setData(null);
			    re.setStatusCode("200");
			}
		}catch(Exception e){
			e.getStackTrace();
			String errorMsg=e.getMessage();
		    re.setStatusCode("500");
			re.setErrorCode("500");
			re.setErrorMsg(errorMsg);
			LOGGER.info(errorMsg);
		}
		return re;
	}
	
	@PostMapping
	public ResponseEntity addProductToCart(Product product,HttpServletRequest request){
		ResponseEntity re=new ResponseEntity();
		Cart cart=(Cart) super.getSessionParams(request, "cart");
		List<CartItem> cartItemList=new ArrayList<CartItem>();
		try{
			Map<String, Object> retMap=new HashMap<String, Object>();
			if(cart==null){
				Cart cart1=new Cart();
				request.getSession().setAttribute("cart", cart1);
				cart=cartService.addProductToCart(product, cart1);
				for ( Map<String,CartItem> cartItemMap : cart.getMapList()) {
					for (Map.Entry<String,CartItem> entry : cartItemMap.entrySet()) {
						CartItem item=entry.getValue();
						cartItemList.add(item);
					}
				}
				retMap.put("list", cartItemList);
				retMap.put("totalPrice", cart.getTotalPrice());
				retMap.put("totalNum", cart.getTotalNum());
				re.setData(retMap);
				re.setStatusCode("200");
			}else{
				cart=cartService.addProductToCart(product, cart);
				for ( Map<String,CartItem> cartItemMap : cart.getMapList()) {
					for (Map.Entry<String,CartItem> entry : cartItemMap.entrySet()) {
						CartItem item=entry.getValue();
						cartItemList.add(item);
					}
				}
				retMap.put("list", cartItemList);
				retMap.put("totalPrice", cart.getTotalPrice());
				retMap.put("totalNum", cart.getTotalNum());
				re.setData(retMap);
			    re.setStatusCode("200");
			}	
		}catch(Exception e){
			e.printStackTrace();
			String errorMsg=e.getMessage();
		    re.setStatusCode("500");
			re.setErrorCode("500");
			re.setErrorMsg(errorMsg);
			LOGGER.info(errorMsg);
		}
		return re;
	}
	
	@DeleteMapping("{id}")
	public ResponseEntity deleteCartItemById(@PathVariable String id,HttpServletRequest request){
		ResponseEntity re=new ResponseEntity();
		Cart cart=(Cart) super.getSessionParams(request, "cart");
		List<CartItem> cartItemList=new ArrayList<CartItem>();
		try{
			cart=cartService.deleteCartItemById(id, cart);
			for ( Map<String,CartItem> cartItemMap : cart.getMapList()) {
				for (Map.Entry<String,CartItem> entry : cartItemMap.entrySet()) {
					CartItem item=entry.getValue();
					cartItemList.add(item);
				}
			}
			Map<String, Object> retMap=new HashMap<String, Object>();
			retMap.put("list", cartItemList);
			retMap.put("totalPrice", cart.getTotalPrice());
			retMap.put("totalNum", cart.getTotalNum());
			re.setData(retMap);
			re.setStatusCode("200");
		}catch(Exception e){
			e.printStackTrace();
			String errorMsg=e.getMessage();
		    re.setStatusCode("500");
			re.setErrorCode("500");
			re.setErrorMsg(errorMsg);
			LOGGER.info(errorMsg);
		}
		return re;
	}
	
	@DeleteMapping
	public ResponseEntity clearCart(HttpServletRequest request){
		ResponseEntity re =new ResponseEntity();
		Cart cart=(Cart) super.getSessionParams(request, "cart");
		List<CartItem> cartItemList=new ArrayList<CartItem>();
		try{
			cart=cartService.clearCart(cart);
			for ( Map<String,CartItem> cartItemMap : cart.getMapList()) {
				for (Map.Entry<String,CartItem> entry : cartItemMap.entrySet()) {
					CartItem item=entry.getValue();
					cartItemList.add(item);
				}
			}
			Map<String, Object> retMap=new HashMap<String, Object>();
			retMap.put("list", cartItemList);
			retMap.put("totalPrice", cart.getTotalPrice());
			retMap.put("totalNum", cart.getTotalNum());
			re.setData(retMap);
			re.setStatusCode("200");
		}catch (Exception e) {
			e.printStackTrace();
			String errorMsg=e.getMessage();
		    re.setStatusCode("500");
			re.setErrorCode("500");
			re.setErrorMsg(errorMsg);
			LOGGER.info(errorMsg);
		}
		return re;
	}
	
	@PutMapping
	public ResponseEntity changeCartItemNum(Product product,String quantity,HttpServletRequest request){
		ResponseEntity re=new ResponseEntity();
		Cart cart=(Cart) super.getSessionParams(request, "cart");
		List<CartItem> cartItemList=new ArrayList<CartItem>();
		try{
			cart=cartService.changeItemQuantity(product.getId(), quantity, cart);
			for ( Map<String,CartItem> cartItemMap : cart.getMapList()) {
				for (Map.Entry<String,CartItem> entry : cartItemMap.entrySet()) {
					CartItem item=entry.getValue();
					cartItemList.add(item);
				}
			}
			Map<String, Object> retMap=new HashMap<String, Object>();
			retMap.put("list", cartItemList);
			retMap.put("totalPrice", cart.getTotalPrice());
			retMap.put("totalNum", cart.getTotalNum());
			re.setData(retMap);
			re.setStatusCode("200");
		}catch(Exception e){
			e.printStackTrace();
			String errorMsg=e.getMessage();
		    re.setStatusCode("500");
			re.setErrorCode("500");
			re.setErrorMsg(errorMsg);
			LOGGER.info(errorMsg);
		}
		return re;
	}
}
最近下载更多
20041226  LV3 6月19日
543666826  LV34 2024年12月28日
wuni1105  LV2 2024年5月16日
阿九11111  LV4 2024年5月9日
微信网友_6505237310967808  LV1 2023年6月5日
hbsoft2008  LV16 2023年2月17日
张三helisi  LV4 2022年6月11日
对方说到底是  LV2 2022年5月29日
gch666  LV6 2022年5月23日
最近浏览更多
hk4663016 11月6日
暂无贡献等级
不爱吃香菜  LV1 11月5日
wj3679  LV13 10月21日
20041226  LV3 6月18日
jhkhk313  LV1 6月8日
周 敬博  LV2 6月6日
chunnnn 6月4日
暂无贡献等级
两杆大烟枪 5月26日
暂无贡献等级
lilimyeclipse  LV6 5月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友