| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 | 
							- <?php
 
- namespace app\common\Service;
 
- use app\common\model\Carts;
 
- use think\Exception;
 
- use app\common\Enum\GoodsEnum;
 
- class CartService
 
- {
 
-         /**
 
-      * 将购物车数据转换为标准的商品列表格式
 
-      * @param array $cart_ids 购物车ID列表
 
-      * @param int $user_id 用户ID
 
-      * @return array
 
-      * @throws Exception
 
-      */
 
-     public static function convertCartToGoodsList($cart_ids, $user_id)
 
-     {
 
-         // 查询购物车数据
 
-         $cartItems = Carts::where('id', 'in', $cart_ids)
 
-             ->where('user_id', $user_id)
 
-             ->select();
 
-             
 
-         if (empty($cartItems)) {
 
-             throw new Exception("购物车数据不存在");
 
-         }
 
-         
 
-         $goods_list = [];
 
-         foreach ($cartItems as $cart) {
 
-             $goods_list[] = [
 
-                 'goods_id' => $cart->goods_id,
 
-                 'goods_sku_id' => $cart->goods_sku_id,
 
-                 'nums' => $cart->nums
 
-             ];
 
-         }
 
-         
 
-         return $goods_list;
 
-     }
 
-        /**
 
-      * @ DateTime 2021-05-31
 
-      * @ 获取购物车商品列表
 
-      * @param string  $ids
 
-      * @param integer $user_id
 
-      * @param integer $sceneval
 
-      * @return array
 
-      */
 
-     public static function getGoodsList($ids, $user_id)
 
-     {
 
-         return Carts::field("c.*,GROUP_CONCAT(sp.name,':',sv.value order by sp.id asc) sku_attr")
 
-             ->with([
 
-                 'Goods' => function ($query) {
 
-                     $query->where('status', GoodsEnum::STATUS_ON_SALE);
 
-                 },
 
-                 'Sku'
 
-             ])
 
-             ->alias('c')
 
-             ->join('shop_goods_sku sku', 'c.goods_sku_id=sku.id', 'LEFT')
 
-             ->join('shop_goods_sku_spec p', "FIND_IN_SET(p.id,sku.spec_value_ids)", 'LEFT')
 
-             ->join('shop_spec sp', 'sp.id=p.spec_id', 'LEFT')
 
-             ->join('shop_spec_value sv', 'sv.id=p.spec_value_id', 'LEFT')
 
-             ->where(function ($query) use ($ids) {
 
-                 if ($ids) {
 
-                     $query->where('c.id', 'in', $ids);
 
-                 }
 
-             })
 
-             ->where('c.user_id', $user_id)
 
-             ->order('c.createtime desc')
 
-             ->group('c.id')
 
-             ->select();
 
-     }
 
-     /**
 
-      * 添加商品到购物车
 
-      *
 
-      * @param string $goods_id     商品ID
 
-      * @param string $goods_sku_id 商品SKUID
 
-      * @param int    $nums         数量
 
-      * @param int    $user_id      会员ID
 
-      * @return mixed
 
-      */
 
-     public static function push($goods_id, $goods_sku_id, $nums = 1, $user_id = 0)
 
-     {
 
-         $row = Carts::where('goods_id', $goods_id)
 
-             ->where('goods_sku_id', $goods_sku_id)
 
-             ->where('user_id', $user_id)
 
-             ->find();
 
-         //已存在,数量加
 
-         if ($row) {
 
-             $row->setInc('nums', $nums);
 
-         } else {
 
-             $row = new Carts();
 
-             $row->save([
 
-                 'goods_id'     => $goods_id,
 
-                 'goods_sku_id' => $goods_sku_id,
 
-                 'user_id'      => $user_id,
 
-                 'nums'         => $nums
 
-             ]);
 
-         }
 
-         return $row->id;
 
-     }
 
-     /**
 
-      * 清空购物车
 
-      */
 
-     public static function clear($cart_ids)
 
-     {
 
-         Carts::where('id', 'IN', $cart_ids)->delete();
 
-     }
 
- }
 
 
  |