package com.matrix.system.hive.action;
|
|
import com.matrix.core.pojo.AjaxResult;
|
import com.matrix.core.tools.WebUtil;
|
import com.matrix.system.constance.Dictionary;
|
import com.matrix.system.constance.SystemConstance;
|
import com.matrix.system.hive.bean.ShoppingGoods;
|
import com.matrix.system.hive.bean.ShoppingGoodsAssemble;
|
import com.matrix.system.hive.dao.ShoppingGoodsAssembleDao;
|
import com.matrix.system.hive.pojo.ShoppingCarItem;
|
import com.matrix.system.hive.service.*;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import javax.annotation.Resource;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
|
/**
|
* 线下订单
|
*
|
* @author jiangyouyao
|
* @date 2016-09-08
|
*/
|
@Controller
|
@RequestMapping(value = "admin/shoppingCar")
|
public class ShoppingCarController extends BaseController {
|
@Resource
|
private SysOrderService currentService;
|
@Resource
|
private MoneyCardUseService cardUseService;
|
@Resource
|
private SysOrderService orderService; // 订单信息Service
|
|
|
|
@Resource
|
private SysProjUseService projUseService; // 产品使用Service
|
|
@Resource
|
private ShoppingGoodsService shoppingGoodsService;
|
|
@Resource
|
private ShoppingGoodsAssembleService shoppingGoodsAssembleService;
|
|
@Autowired
|
private ShoppingGoodsAssembleDao shoppingGoodsAssembleDao;
|
|
|
/**
|
* 在购物车中添加一个产品
|
*
|
* @author jiangyouyao
|
*/
|
@RequestMapping(value = "/addItemToCar")
|
@SuppressWarnings("unchecked")
|
public @ResponseBody
|
AjaxResult findProject(ShoppingCarItem carItem) {
|
List<ShoppingCarItem> carItems = null;
|
// 查看购物车是否存在
|
if (WebUtil.getSession().getAttribute(SystemConstance.SHOPPING_CAR) == null) {
|
WebUtil.getSession().setAttribute(SystemConstance.SHOPPING_CAR, new ArrayList<>());
|
}
|
carItems = (List<ShoppingCarItem>) WebUtil.getSession().getAttribute(
|
SystemConstance.SHOPPING_CAR);
|
// 根据类型不同设置不同的实体对象,并且去重
|
toAdd(carItem, carItems);
|
|
return new AjaxResult(AjaxResult.STATUS_SUCCESS, "添加成功!");
|
}
|
|
/**
|
* 原来存在的商品则只是增加数量,原来存在在购物车则只改变数量
|
*
|
* @param carItem
|
* @param carItems
|
* @author jiangyouyao
|
*/
|
private void toAdd(ShoppingCarItem carItem, List<ShoppingCarItem> carItems) {
|
ShoppingGoods shopGoods = shoppingGoodsService.findById(carItem.getGoodsId());
|
carItem.setShoppingGoods(shopGoods);
|
boolean flag = true;
|
// 去重
|
for (ShoppingCarItem shoppingCarItem : carItems) {
|
Long existId = shoppingCarItem.getGoodsId();
|
Long getId = carItem.getGoodsId();
|
boolean idIsSame = existId.equals(getId);
|
boolean bothIsFree = shoppingCarItem.getIsFree().equals(carItem.getIsFree());
|
if (idIsSame && bothIsFree) {
|
shoppingCarItem.setCount(shoppingCarItem.getCount() + carItem.getCount());
|
flag = false;
|
break;
|
}
|
}
|
if (flag) {
|
carItems.add(carItem);
|
}
|
}
|
|
/**
|
* 清空购物车
|
*/
|
@RequestMapping(value = "/clearShoppingCar")
|
public @ResponseBody
|
AjaxResult clearShoppingCar() {
|
WebUtil.getSession().removeAttribute(SystemConstance.SHOPPING_CAR);
|
return new AjaxResult(AjaxResult.STATUS_SUCCESS, "购物车已清空");
|
}
|
|
|
/**
|
* 查询购物车产品
|
*
|
* @author 姜友瑶
|
*/
|
@RequestMapping(value = "/showCar")
|
public @ResponseBody
|
AjaxResult showCar() {
|
@SuppressWarnings("unchecked")
|
List<ShoppingCarItem> carItems = (List<ShoppingCarItem>) WebUtil.getSession().getAttribute(
|
SystemConstance.SHOPPING_CAR);
|
return new AjaxResult(AjaxResult.STATUS_SUCCESS, carItems, 0);
|
}
|
|
|
}
|