Helius
2021-04-14 1e4e6e8aa4b738320bf8b5a0ee43aa62ca2c13e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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);
    }
 
 
}