购物车系列_控制器

package com.bw.controller;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpSession;

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 org.springframework.web.servlet.ModelAndView;

import com.bw.pojo.Ids;
import com.bw.pojo.Product;
import com.bw.pojo.ShoppingCar;
import com.bw.pojo.User;
import com.bw.service.ProductService;
import com.bw.service.ShoppingCarService;

@Controller
@RequestMapping("shop")
public class ShoppingCarController {

    @Autowired
    private ShoppingCarService ss;

    @Autowired
    private ProductService ps;

    @RequestMapping("addShoppingCar")
    public String addShoppingCar(Integer id, HttpSession session,
            ShoppingCar shoppingCar) {
        ShoppingCar selectById = ss.selectById(id);
        if (selectById == null) {
            Product product = ps.selectById(id);
            User user = (User) session.getAttribute("user");
            shoppingCar.setProduct(product);
            shoppingCar.setUser(user);
            shoppingCar.setCount(1);
            shoppingCar.setPriceCount(product.getPrice());
            ss.insertShoppingCar(shoppingCar);
        } else {
            selectById.setCount(selectById.getCount() + 1);
            ss.updateShoppingCar(selectById);
        }
        return null;
    }
/*
    @RequestMapping("addOrder")
    public String addOrder(Integer id, HttpSession session,
            ShoppingCar shoppingCar) {
        Product product = ps.selectById(id);
        User user = (User) session.getAttribute("user");
        shoppingCar.setProduct(product);
        shoppingCar.setUser(user);
        shoppingCar.setCount(1);
        shoppingCar.setPriceCount(product.getPrice());
        Integer integer = ss.insertShoppingCar(shoppingCar);
        System.out.println(integer);

        return null;

    }*/

    @RequestMapping("selectShop")
    @ResponseBody
    public List<ShoppingCar> selectShop(HttpSession session) {
        User user = (User) session.getAttribute("user");
        List<ShoppingCar> list = ss.selectShop(user);
        return list;
    }

    @RequestMapping("deleteShop")
    public String deleteShop(Ids ids, HttpSession session) {
        User user = (User) session.getAttribute("user");
        ids.setUid(user.getId());
        ss.deleteShop(ids);
        return "";
    }

    @RequestMapping("reduceShop")
    public String updateShoppingCar(ShoppingCar sc, HttpSession session) {
        User user = (User) session.getAttribute("user");
        ShoppingCar selectById = ss.selectById(sc.getProduct().getId());
        Integer count = selectById.getCount();
        Integer newcount =  --count;
        sc.setUser(user);
        sc.setCount(newcount);
        sc.setPriceCount(newcount*sc.getProduct().getPrice());
        ss.updateShop(sc);
        return "";
    }
    @RequestMapping("addShop")
    public String addShoppingCar(ShoppingCar sc, HttpSession session) {
        User user = (User) session.getAttribute("user");
        ShoppingCar selectById = ss.selectById(sc.getProduct().getId());
        Integer count = selectById.getCount();
        Integer newcount = ++count;
        sc.setUser(user);
        sc.setCount(newcount);
        sc.setPriceCount(newcount*sc.getProduct().getPrice());
        ss.updateShop(sc);
        return "";
    }
    @RequestMapping("buycheShop")
    public String buycheShop(String ids) {
        String[] split = ids.split(",");
        for (String string : split) {
            String trim = string.trim();
            ss.insertOeder(Integer.parseInt(trim));
        }
        return "shop";
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值