爲校園超市系統增加購物車與訂單功能

SSM實現校園超市管理系統

之前寒假寫的一個小項目,idea寫的,用了maven工程,Spring+SpringMVC+MyBatis框架技術實現校園超市系統。

之前加的人有點多,源碼+sql+jar已上傳到    https://download.csdn.net/download/qq_38663663/11076831

裏邊的sql放錯了,正確的:鏈接:https://pan.baidu.com/s/1rhUfNyA4LmHpjTsxzVbvcw
提取碼:3760

這是之前發佈的小項目,還差購物車和訂單頁面沒有實現,這次將會補齊,新增添加收貨地址,新增支付寶和微信支付。

1、數據庫表新增購物車、訂單、訂單類目、收貨信息表,sql文件會給出

2.1、POJO層新加 Cart

public class Cart {

    /*購物車主鍵id*/
    private Integer id;
    /*客戶id*/
    private Integer customerId;
    /*商品對象*/
    private Product product;
    /*商品數量*/
    private Integer productNum;
    /*商品加入購物車的時間*/
    private Date createTime;
    /*商品總價*/
    private Double totalPrice;
    private Integer status;
}

2.2、Oder

public class Order {
    /*訂單id*/
    private Integer id;
    /*訂單編號*/
    private String orderNumber;
    /*客戶對象*/
    private Customer customer;
    /*商品總價*/
    private Double price;
    /*訂單的創建時間*/
    private Date createDate;
    /*商品數量*/
    private Integer productNumber;
    /**
     *功能描述:訂單的狀態
     * 0 表示未支付
     * 1 表示已經付未發貨
     * 2 表示已支付已發貨
     * 3 表示已發貨未收貨
     * 4 表示交易完成
     * 5 表示客戶刪除的訂單,設置爲無效
     */
    private Integer status;
    /*收貨地址*/
    private String address;
}

2.3、OderItem

public class OrderItem implements Serializable{
    /*訂單類目id*/
    private Integer id;
    /*商品數量*/
    private Integer num;
    /*商品小計*/
    private Double price;
    /*商品對象*/
    private Product product;
    /*所屬於哪個訂單,訂單對象*/
    private Order order;
}

2.4、Shipping

public class Shipping {
    /*主鍵id*/
    private Integer id;
    /*客戶id*/
    private Integer customerId;
    /*收貨人姓名*/
    private String receiverName;
    /*收貨人的座機號碼*/
    private String receiverPhone;
    /*收貨人的手機號碼*/
    private String receiverMobile;
    /*省份名稱*/
    private String receiverProvince;
    /*城市名稱*/
    private String receiverCity;
    /*區 縣*/
    private String receiverDistrict;
    /*郵政編碼*/
    private String zipCode;
    /*詳細地址內容*/
    private String addressDetail;
    /*創建時間*/
    private Date createTime;
    /*更新時間*/
    private Date updateTime;
    /*收貨地址的狀態:默認爲0,如果設置爲默認地址後則修改爲1*/
    private Integer status;
}

3.1、CartDao

public interface CartDao {
    
    int insertCart(Cart cart);

    List<Cart> selectAllCartByCustomerId(Integer customerId);

    Cart selectCartByCustomerIdAndCartId(@Param("customerId") Integer customerId,@Param("cartId") Integer cartId);

    Cart selectCartByCustomerIdAndProductId(@Param("customerId") Integer customerId,@Param("productId") Integer productId);

    Cart selectRedirectCartByCustomerIdAndProductId(@Param("customerId") Integer customerId,@Param("productId") Integer productId);

    int updateCartNumAndTotalPriceById(@Param("id") Integer id, @Param("productNum") Integer num,@Param("totalPrice") Double price);

    int deleteCartById(Integer id);

    int updateCartStatusByCustomerId(@Param("customerId") Integer id,@Param("status") Integer status);

    int updateCartStatusByCartIdAndCustomerId(@Param("cartId") Integer cartId,
                                              @Param("customerId") Integer id,
                                              @Param("status")Integer status);

    int updateCartStatusByCartIdAndCustomerIds(@Param("cartIds") Integer[] cartIds,
                                               @Param("customerId") Integer customerId,
                                               @Param("status") Integer status);

    int updateProductNumAndPriceByCartIdAndCustomerIdAndStatus(@Param("cartId") Integer cartId,
                                                       @Param("productNum") Integer productNum,
                                                       @Param("customerId") Integer id,
                                                       @Param("status") int status,
                                                       @Param("totalPrice")Double totalPrice);

    List<Cart> selectCartByCartIdsAndCustomerId(@Param("cartIds") Integer[] orderCartIds,
                                                @Param("customerId") Integer id,
                                                @Param("status") int status);

    List<Cart> selectRedirectCartByCartIdsAndCustomerId(@Param("cartIds") Integer[] orderCartIds,
                                                @Param("customerId") Integer id,
                                                @Param("status") int status);
}

3.2、OrderDao

public interface OrderDao {

    int insertOrder(Order order);
    Order selectOrderIdByOrderNoAndCustomerId(@Param("orderNo") String orderNo, @Param("customerId") Integer id);
    List<Order> selectAllOrderByCustomerId(@Param("customerId") Integer id);
    int updateOrderStatusByCustomerIdAndOrderId(@Param("customerId") Integer id,
                                                @Param("orderId") Integer orderId,
                                                @Param("status") Integer status);

    List<Order> selectOrdersByCustomerId(@Param("customerId") Integer id,@Param("status") Integer status);

    int updateOrderStatusByCustomerIdAndOrderNo(@Param("customerId") Integer id,
                                                @Param("orderNumber") String out_trade_no,
                                                @Param("status") Integer status);

    int updateOrderStatusByOrderNo(@Param("orderNumber") String out_trade_no,
                                   @Param("status") Integer status);

    Order selectOrderByOutTradeNo(String outTradeNo);
}

3.3、OrderItemDao

public interface OrderItemDao {

    int insertOrderItem(OrderItem orderItem);
    int insertOrderItemByOrderItems(@Param("orderItemList") List<OrderItem> orderItemList);
    List<OrderItem> selectOrderItemsByOrder(Integer orderId);

    List<OrderItem> selectOrderItemsByOrderIds();
}

3.4、ShippingDao

public interface ShippingDao {

    Shipping selectShippingByCustomerIdAndShippingId(@Param("customerId") Integer customerId,
                                                     @Param("shippingId") Integer shippingId);

    List<Shipping> selectAllShippings(@Param("customerId") Integer customerId , @Param("status") Integer status);

    int insertShipping(Shipping shipping);

    int deleteShippingByIdAndCustomerId(@Param("shippingId") Integer shippingId,
                                        @Param("customerId") Integer customerId,
                                        @Param("status") int status,
                                        @Param("updateTime") Date updateTime);

    int updateByShipping(Shipping shipping);
}

4.1、CartMapper

【Mybatis 配置文件各類參數】

A、resultType和resultMap的區別

如果你搜索只是返回一個值,比如說String ,或者是int,那你直接用resultType就行了。 但是你如果是返回一個複雜的對象,就必須定義好這個對象的resultMap。

其實reultType也可以返回對象,但是須返回這個對象所有信息了,適用用普通的完整返回。 因爲resultmap那段是我們自己指定的,可能指定的屬性只是User的一部分,而且還可以設置默認值,這是result type做不到的。

B、parameterType爲輸入參數,parameterType有基本數據類型和複雜的數據類型配置。

1.基本數據類型,如輸入參數只有一個,其數據類型可以是基本的數據類型,也可以是
自己定的類類型。包括int,String,Integer,Date,如下:

2.複雜數據類型:包含java實體類,map。

3.另外MyBatis還提供了一個使用註解來傳入多個參數的方式。這種方式需要在接口的參數上添加@Param註解

C、 useGeneratedKeys 參數只針對 insert 語句生效,默認爲 false。當設置爲 true 時,表示如果插入的表以自增列爲主鍵,則允許 JDBC 支持自動生成主鍵,並可將自動生成的主鍵返回。

D、根據ids(多個id)查詢用戶信息。比如查詢id爲16 /22 /26/28 /29這五個id的用戶信息。

使用in+foreach來處理

<foreach collection="list" item="item" open="(" close=")" separator="," index="">
    #{item.studentId}
</foreach>

解釋含義:

foreach的主要用在構建in條件中,它可以在SQL語句中進行迭代一個集合。
foreach元素的屬性主要有 item,index,collection,open,separator,close。
item集合中每一個元素進行迭代時的別名,
index表示在迭代過程中,每次迭代到的位置,
open該語句以什麼開始,
separator在每次進行迭代之間以什麼符號作爲分隔 符,
close以什麼結束,
在使用foreach的時候最關鍵的也是最容易出錯的就是collection屬性,
該屬性是必須指定的,但是在不同情況 下,該屬性的值是不一樣的,
主要有一下3種情況:
1.     如果傳入的是單參數且參數類型是一個List的時候,collection屬性值爲list
2.     如果傳入的是單參數且參數類型是一個array數組的時候,collection的屬性值爲array
3.     如果傳入的參數是多個的時候,我們就需要把它們封裝成一個Map了

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--設置dao接口-->
<mapper namespace="com.xmlvhy.shop.dao.CartDao">

    <resultMap id="CartMap" type="Cart">
        <id column="id" property="id"/>
        <result column="customer_id" property="customerId"/>
        <result column="product_num" property="productNum"/>
        <result column="total_price" property="totalPrice"/>
        <result column="status" property="status"/>
        <result column="create_time" property="createTime"/>
        <association property="product" javaType="Product" column="product_id">
            <id property="id" column="p.id"/>
            <result property="name" column="name"/>
            <result property="price" column="price"/>
            <result property="image" column="image"/>
        </association>
    </resultMap>

    <sql id="CartColumn">
        id,
        customer_id,
        product_id,
        product_num,
        total_price,
        status,
        create_time
    </sql>

    <select id="selectCartByCustomerIdAndCartId" resultMap="CartMap">
        select t.id,t.customer_id,t.product_num,t.total_price,t.status, t.create_time,p.id 'p.id',p.name,p.image,p.price
        from t_cart t
          left join t_product p
        on t.product_id = p.id
        where t.id = #{cartId} and customer_id = #{customerId} and status = 1
    </select>

    <select id="selectCartByCustomerIdAndProductId" resultType="Cart">
        select
        <include refid="CartColumn"/>
        from t_cart
        where customer_id = #{customerId} and product_id = #{productId} and status = 1
    </select>

    <select id="selectRedirectCartByCustomerIdAndProductId" resultType="Cart">
        select
        <include refid="CartColumn"/>
        from t_cart
        where customer_id = #{customerId} and product_id = #{productId} and status = 2
    </select>

    <insert id="insertCart" parameterType="Cart" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        insert into t_cart
          (customer_id, product_id, product_num,total_price,status,create_time)
        values
          (#{customerId},#{product.id},#{productNum},#{totalPrice},#{status},#{createTime})
    </insert>

    <select id="selectAllCartByCustomerId" parameterType="integer" resultMap="CartMap">
        select t.id,t.customer_id,t.product_num,t.total_price,t.status, t.create_time,p.id 'p.id',p.name,p.image,p.price
        from t_cart t
          left join t_product p
        on t.product_id = p.id
        where customer_id = #{customerId} and status = 1
    </select>

    <select id="selectCartByCartIdsAndCustomerId" resultMap="CartMap">
        select t.id,t.customer_id,t.product_num,t.total_price,t.status, t.create_time,p.id 'p.id',p.name,p.image,p.price
        from t_cart t
          left join t_product p
        on t.product_id = p.id
        where t.id in
          <if test="cartIds != null and cartIds.length > 0">
            <foreach collection="cartIds" item="cartId" open="(" separator="," close=")">
                #{cartId}
            </foreach>
          </if>
        and customer_id = #{customerId} and status = #{status}
    </select>

    <select id="selectRedirectCartByCartIdsAndCustomerId" resultMap="CartMap">
        select t.id,t.customer_id,t.product_num,t.total_price,t.status, t.create_time,p.id 'p.id',p.name,p.image,p.price
        from t_cart t
        left join t_product p
        on t.product_id = p.id
        where t.id in
        <if test="cartIds != null and cartIds.length > 0">
            <foreach collection="cartIds" item="cartId" open="(" separator="," close=")">
                #{cartId}
            </foreach>
        </if>
        and customer_id = #{customerId} and status = #{status}
    </select>

    <update id="updateCartNumAndTotalPriceById">
        update t_cart
          set product_num = #{productNum},
              total_price = #{totalPrice}
        where id = #{id}
    </update>

    <update id="updateCartStatusByCustomerId">
        update t_cart
          set status = #{status}
        where customer_id = #{customerId}
    </update>

    <update id="updateCartStatusByCartIdAndCustomerId">
        update t_cart
          set status = #{status}
        where id = #{cartId} and customer_id = #{customerId}
    </update>

    <!--TODO: 更新數據,當條件是一個整型的數組的時候,使用 foreach 語句-->
    <update id="updateCartStatusByCartIdAndCustomerIds">
        update t_cart
        set status = #{status}
        where id in
        <if test="cartIds != null and cartIds.length > 0">
            <foreach collection="cartIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        and customer_id = #{customerId}
    </update>

    <update id="updateProductNumAndPriceByCartIdAndCustomerIdAndStatus">
        update t_cart
          set product_num = #{productNum},
              total_price = #{totalPrice}
        where id = #{cartId} and customer_id = #{customerId} and status = #{status}
    </update>

    <delete id="deleteCartById" parameterType="integer">
        delete from t_cart
        where id = #{id}
    </delete>
</mapper>

5.1、CartServiceImpl

/**
 * Author: jx
 * Date: 2019-03-19 16:07
 * Description: 購物車業務實現類
 */
@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class CartServiceImpl implements CartService {

    @Autowired
    private CartDao cartDao;
    @Autowired
    private ProductDao productDao;

    /**
     *功能描述: 添加商品到購物車
     * @Author jx
     * @Date 17:09 2019/03/19
     * @Param [cartVo]
     * @return java.lang.Boolean
     */
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public Boolean saveToCart(CartVo cartVo) {

        Cart cartResult = cartDao.selectCartByCustomerIdAndProductId(cartVo.getCustomerId(), cartVo.getProductId());
        //先查詢一下購物車中是否有此商品,沒有則插入保存,有的話就更新購物車的商品數量就可以了
        if (cartResult == null) {
            Cart cart = new Cart();
            Product product = productDao.selectProductById(cartVo.getProductId());
            //計算總價
            Double totalPrice = product.getPrice() * cartVo.getProductNum();

            BeanUtils.copyProperties(cartVo,cart);
            cart.setTotalPrice(totalPrice);
            cart.setProduct(product);
            //設置狀態,默認是有效的
            cart.setStatus(CartConstant.CART_PRODUCT_STATUS_VALID);
            cart.setCreateTime(new Date());

            int rows = cartDao.insertCart(cart);
            if (rows >= 1) {
                return true;
            }else{
                return false;
            }
        }

        //更新購物車的商品數量
        int productSums = cartResult.getProductNum() + cartVo.getProductNum();
        Product pd = productDao.selectProductById(cartVo.getProductId());

        //更新商品總價格
        Double priceSum = pd.getPrice() * productSums;

        int rows = cartDao.updateCartNumAndTotalPriceById(cartResult.getId(), productSums,priceSum);
        if (rows >= 1) {
            return true;
        }
        return false;
    }
    /**
     *功能描述: 根據客戶 id 查找他所有購物車信息
     * @Author jx
     * @Date 19:54 2019/03/19
     * @Param [customerId]
     * @return java.util.List<com.xmlvhy.shop.pojo.Cart>
     */
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public List<Cart> findCustomerAllCarts(Integer customerId) {
        return cartDao.selectAllCartByCustomerId(customerId);
    }

    /**
     *功能描述: 清空用戶的購物車
     * @Author jx
     * @Date 19:22 2019/03/20
     * @Param [id]
     * @return java.lang.Boolean
     */
    @Override
    public Boolean modifyCartStatus(Integer id) {
        int rows = cartDao.updateCartStatusByCustomerId(id, CartConstant.CART_PRODUCT_STATUS_ISVALID);
        if (rows >= 1) {
            return true;
        }
        return false;
    }

    /**
     *功能描述: 從購物車中移除某一商品
     * @Author jx
     * @Date 10:42 2019/03/21
     * @Param [cartId, id]
     * @return java.lang.Boolean
     */
    @Override
    public Boolean modifyCartStatusByCartIdAndCustomerId(Integer cartId, Integer id) {
        int rows = cartDao.updateCartStatusByCartIdAndCustomerId(cartId, id, CartConstant.CART_PRODUCT_STATUS_ISVALID);
        if (rows >= 1) {
            return true;
        }
        return false;
    }

    /**
     *功能描述: 從購物車中移除選中的商品
     * @Author jx
     * @Date 13:01 2019/03/21
     * @Param [cartIds, customerId]
     * @return java.lang.Boolean
     */
    @Override
    public Boolean modifyCartStatusByCartIdAndCustomerIds(Integer[] cartIds, Integer customerId) {
        int rows = cartDao.updateCartStatusByCartIdAndCustomerIds(cartIds, customerId, CartConstant.CART_PRODUCT_STATUS_ISVALID);
        if (rows >= 1) {
            return true;
        }
        return false;
    }

    /**
     *功能描述: 購物車頁面修改商品數量 以及總價格更新
     * @Author jx
     * @Date 15:25 2019/03/22
     * @Param [cartId, productNum, id]
     * @return java.lang.Boolean
     */
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public Boolean modifyNumAndPriceByCartIdAndCustomerIdAndStatus(Integer cartId, Integer productNum, Integer id) {

        //拿到該商品信息,計算修改數量後的總價格
        Cart cart = cartDao.selectCartByCustomerIdAndCartId(id, cartId);
        Double totalPrice = (cart.getProduct().getPrice()) * productNum;

        int rows = cartDao.updateProductNumAndPriceByCartIdAndCustomerIdAndStatus(cartId, productNum,
                id, CartConstant.CART_PRODUCT_STATUS_VALID,totalPrice);
        if (rows >= 1) {
            return true;
        }
        return false;
    }

    /**
     *功能描述: 根據客戶選中購物車多個物品項進行查詢
     * @Author jx
     * @Date 15:25 2019/03/22
     * @Param [orderCartIds, id]
     * @return java.util.List<com.xmlvhy.shop.pojo.Cart>
     */
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public List<Cart> findCartByCartIdsAndCustomerId(Integer[] orderCartIds, Integer id) throws OrderCartNotFoundException {

        List<Cart> cartList = cartDao.selectCartByCartIdsAndCustomerId(orderCartIds, id, CartConstant.CART_PRODUCT_STATUS_VALID);

        if (cartList.size() == 0) {
            throw new OrderCartNotFoundException("購物車信息不存在");
        }
        return cartList;
    }

    @Override
    public List<Cart> findRedirectCartByCartIdsAndCustomerId(Integer[] orderCartIds, Integer id) {
        List<Cart> cartList = cartDao.selectRedirectCartByCartIdsAndCustomerId(orderCartIds, id, CartConstant.CART_PRODUCT_REDIRECT_TO_CART);

        if (cartList.size() == 0) {
            throw new OrderCartNotFoundException("購物車信息不存在");
        }
        return cartList;
    }

    /**
     *功能描述: 直接購買,產生一個購物車,返回購物車id
     * @Author jx
     * @Date 10:42 2019/04/03
     * @Param [cart]
     * @return int
     */
    @Override
    public int redirectToCart(CartVo cartVo) {

        Cart cartResult = cartDao.selectRedirectCartByCustomerIdAndProductId(cartVo.getCustomerId(), cartVo.getProductId());
        //先查詢一下購物車中是否有此商品,沒有則插入保存,有的話就更新購物車的商品數量就可以了
        if (cartResult == null) {
            Cart cart = new Cart();
            Product product = productDao.selectProductById(cartVo.getProductId());
            //計算總價
            Double totalPrice = product.getPrice() * cartVo.getProductNum();

            BeanUtils.copyProperties(cartVo,cart);
            cart.setTotalPrice(totalPrice);
            cart.setProduct(product);
            //直接購買放入購物車
            cart.setStatus(CartConstant.CART_PRODUCT_REDIRECT_TO_CART);
            cart.setCreateTime(new Date());

            int rows = cartDao.insertCart(cart);
            if (rows >= 1) {
                return cart.getId();
            }else{
                return 0;
            }
        }

        //直接購買,這裏沒有疊加,更新購物車的商品數量
        int productSums = cartVo.getProductNum();
        Product pd = productDao.selectProductById(cartVo.getProductId());

        //更新商品總價格
        Double priceSum = pd.getPrice() * productSums;

        int rows = cartDao.updateCartNumAndTotalPriceById(cartResult.getId(), productSums,priceSum);
        if (rows >= 1) {
            return cartResult.getId();
        }
        return 0;
    }
}

6.1、CarControlelr

/**
 * Author: jx
 * Date: 2019-03-13 0:24
 * Description:<描述>
 */
@Controller
@RequestMapping("/front/cart")
@Slf4j
public class CarController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CartService cartService;

    /**
     * 功能描述: 清空購物車後展示此頁面
     *
     * @return java.lang.String
     * @Author jx
     * @Date 19:50 2019/03/20
     * @Param []
     */
    @RequestMapping("showEmptyCart")
    public String showEmptyCart() {
        return "emptyCart";
    }

    /**
     * 功能描述: 購物車展示
     *
     * @return java.lang.String
     * @Author jx
     * @Date 21:14 2019/03/19
     * @Param [session, model]
     */
    @RequestMapping("myCarts")
    public String myCars(HttpSession session, Model model) {
        Customer customer = (Customer) session.getAttribute("customer");
        if (customer != null) {
            List<Cart> cartList = cartService.findCustomerAllCarts(customer.getId());
            model.addAttribute("cartList", cartList);
        }
        return "car";
    }

    /**
     * 功能描述: 添加商品到購物車
     *
     * @return com.xmlvhy.shop.common.utils.ResponseResult
     * @Author jx
     * @Date 16:02 2019/03/19
     * @Param [id, session]
     */
    @RequestMapping("addToCart")
    @ResponseBody
    public ResponseResult addToCart(Integer id, Integer textBox, HttpSession session) {
        Customer customer = (Customer) session.getAttribute("customer");
        if (ObjectUtils.isEmpty(customer)) {
            //用戶沒有登錄,則提示讓他登錄
            return ResponseResult.deny("還請客官先登錄哦~");
        } else {

            CartVo cartVo = new CartVo();
            cartVo.setCustomerId(customer.getId());
            cartVo.setProductId(id);
            cartVo.setProductNum(textBox);

            if (cartService.saveToCart(cartVo)) {
                //此session用於標誌購物車非空
                //session.setAttribute("emptyCart",0);
                return ResponseResult.success("商品成功加入購物車");
            } else {
                return ResponseResult.fail("商品加入購物車失敗");
            }
        }
    }

    /**
     * 功能描述: 清空購物車操作
     *
     * @return com.xmlvhy.shop.common.utils.ResponseResult
     * @Author jx
     * @Date 19:24 2019/03/20
     * @Param [session]
     */
    @RequestMapping("clearAllProductFromCart")
    @ResponseBody
    public ResponseResult clearAllProductFromCart(HttpSession session) {
        Customer customer = (Customer) session.getAttribute("customer");
        if (!ObjectUtils.isEmpty(customer)) {
            if (cartService.modifyCartStatus(customer.getId())) {
                //此session用於標誌購物車爲空
                //session.setAttribute("emptyCart",null);
                return ResponseResult.success("購物車已清空");
            }
        } else {
            return ResponseResult.fail("請您先登錄");
        }
        return ResponseResult.fail("商品移除失敗");
    }

    /**
     * 功能描述: 從購物車中移除某一商品
     *
     * @return com.xmlvhy.shop.common.utils.ResponseResult
     * @Author jx
     * @Date 10:49 2019/03/21
     * @Param [cartId, session]
     */
    @RequestMapping("removeOneProduct")
    @ResponseBody
    public ResponseResult removeOneProduct(Integer cartId, HttpSession session) {
        Customer customer = (Customer) session.getAttribute("customer");
        if (ObjectUtils.isEmpty(customer)) {
            return ResponseResult.fail("請您先登錄");
        }
        if (cartService.modifyCartStatusByCartIdAndCustomerId(cartId, customer.getId())) {
            return ResponseResult.success("該商品移除成功");
        }
        return ResponseResult.fail();
    }

    /**
     * 功能描述: 從購物車中移除選中的商品
     *
     * @return com.xmlvhy.shop.common.utils.ResponseResult
     * @Author jx
     * @Date 11:49 2019/03/21
     * @Param [cartIds, session]
     */
    @RequestMapping("removeMoreProductFromCart")
    @ResponseBody
    public ResponseResult removeMoreProductFromCart(Integer[] cartIds, HttpSession session) {
        Customer customer = (Customer) session.getAttribute("customer");
        if (ObjectUtils.isEmpty(customer)) {
            return ResponseResult.fail("請您先登錄");
        }
        if (cartService.modifyCartStatusByCartIdAndCustomerIds(cartIds,customer.getId())) {
            return ResponseResult.success("商品移除成功");
        }
        return ResponseResult.fail("商品移除失敗");
    }

    /**
     *功能描述: 購物車頁面修改商品的數量
     * @Author jx
     * @Date 17:15 2019/03/21
     * @Param [cartId, productNum, session]
     * @return com.xmlvhy.shop.common.utils.ResponseResult
     */
    @RequestMapping("inputModifyProductNum")
    @ResponseBody
    public ResponseResult inputModifyProductNum(Integer cartId, Integer productNum, HttpSession session){
        Customer customer = (Customer) session.getAttribute("customer");
        if (ObjectUtils.isEmpty(customer)) {
            return ResponseResult.fail("客官,還請先登錄");
        }
        if (cartService.modifyNumAndPriceByCartIdAndCustomerIdAndStatus(cartId,productNum,customer.getId())) {
            return ResponseResult.success("商品數量已修改");
        }
        return ResponseResult.fail("商品數量修改失敗");
    }

    /**
     *功能描述: 臨時將前端發送過來的數據存到 session中去
     * @Author jx
     * @Date 11:48 2019/03/22
     * @Param [count, price, orderCartIds, session]
     * @return com.xmlvhy.shop.common.utils.ResponseResult
     */
    @RequestMapping("addTempOrderItem")
    @ResponseBody
    public ResponseResult addTempOrderItem(Integer count, String price, Integer[]orderCartIds, HttpSession session){
        Customer customer = (Customer) session.getAttribute("customer");
        if (ObjectUtils.isEmpty(customer)) {
            return ResponseResult.fail("客官還請先登錄");
        }
        session.setAttribute("count",count);

        String[] strings = price.split("¥");

        double newPrice = Double.parseDouble(strings[1]);

        session.setAttribute("price",newPrice);
        session.setAttribute("orderCartIds",orderCartIds);
        return ResponseResult.success(session);
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

未完成。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章