12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace App\Modules\Mini\Services;
- use App\Base\BaseService;
- use App\Models\Cart;
- use App\Models\Goods\Goods;
- use App\Models\Order\Order;
- use App\Models\User\User;
- class CartService extends BaseService
- {
- public function paginate(User $user, $params)
- {
- $p = Cart::where("user_id", $user->id)->orderByDesc("id")->paginate($params['page_size']);
- return [
- "total" => $p->total(),
- "page_total" => $p->lastPage(),
- "list" => array_map(function (Cart $model) {
- return [
- "id" => $model->id,
- "sn" => $model->sn,
- "name" => $model->goods->name,
- "en_name" => $model->goods->en_name,
- "thumb" => $model->thumb,
- "count" => $model->count,
- "custom" => $model->custom,
- ];
- }, $p->items()),
- ];
- }
- public function add(User $user, $params): Cart
- {
- $gid = $params['id'];
- $goods = Goods::findOrFail($gid);
- $cart = new Cart();
- $cart->user_id = $user->id;
- $cart->goods_id = $goods->id;
- $cart->count = $params['count'];
- $cart->extra = [
- "goods_name" => $goods->name,
- "goods_en_name" => $goods->en_name,
- "thumb" => $goods->thumb,
- "sn" => $goods->sn,
- "custom" => $params['custom'],
- ];
- $cart->save();
- return $cart;
- }
- public function update(User $user, $params)
- {
- $cart = Cart::where("id", $params['id'])->where("user_id", $user->id)->firstOrFail();
- if ($params['count']) {
- $cart->count = $params['count'];
- $cart->save();
- } else {
- $cart->delete();
- }
- return $cart;
- }
- public function ask(User $user, $params)
- {
- $params['source'] = Order::SOURCE_CART;
- return app(OrderService::class)->askByCart($user, $params);
- }
- }
|