CartOrderTrait.php
3.6 KB
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
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* CartOrderTrait
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Models\Traits
* @package App\Models\Traits
* @author Richer <yangzi1028@163.com>
* @date 2022年6月23日15:00:43
* @copyright 2020-2021 Richer (http://www.pwyld.com/)
* @license http://www.pwyld.com/ License
* @link http://www.pwyld.com/
*/
namespace App\Models\Traits;
use App\Models\Cart\CartOrder;
use App\Models\EpGiftCard\EpGiftCard;
use App\Models\Gift\Gift;
use App\Models\GiftCard\GiftCard;
use App\Models\Goods\GoodsSku;
use App\Models\Groupon\Activity;
use App\Models\Order\Order;
use App\Models\Shop;
use App\Models\System\SystemSetting;
use App\Models\User\UserWalletRecord;
use Illuminate\Support\Facades\DB;
/**
* Trait CartOrderTrait
*
* @category App\Models\Traits
* @package App\Models\Traits
* @author Richer <yangzi1028@163.com>
* @date 2022年6月23日15:00:43
* @copyright 2020-2021 Richer (http://www.pwyld.com/)
* @license http://www.pwyld.com/ License
* @link http://www.pwyld.com/
*/
trait CartOrderTrait
{
/**
* 更新购物车订单
*
* @param $order
*/
protected function updateCartOrder($order)
{
if ($order::OBJ_NAME === CartOrder::OBJ_NAME) {
$order_ids = explode(',', $order->order_ids);
if ($order_ids) {
$data = [
'status' => Order::PAID,
'paid_type' => $order->paid_type,
'paid_at' => $order->paid_at
];
Order::whereIn('id', $order_ids)->update($data);
// 同时每个订单都要生成礼物
}
}
}
/**
* 生成礼物
*
* @param $order
*/
public function generateCardOrderGift($order)
{
$order_ids = explode(',', $order->order_ids);
if ($order_ids) {
$gifts = [];
$orders = Order::whereIn('id', $order_ids)->get()->each(function ($item) use (&$gifts) {
$gifts[] = [
'user_id' => $item->user_id,
'shop_id' => $item->shop_id ? : 0,
'can_recycled' => $item->can_recycled ? : 0, // add by Richer 于 2022年8月30日14:00:38 是否可以回收
'giftable_type' => Order::OBJ_NAME,
'giftable_id' => $item->id,
'number' => $item->number,
'goods_type' => $item->orderable_type,
'goods_id' => $item->orderable_id,
'quantity' => $item->quantity,
'type' => $item->orderable_type,
// 'giver_id' => $item->user_id,
'goods_name' => $item->goods_name,
'goods_price' => $item->goods_price ? : 0,
'goods_image' => substr_file_path($item->goods_image),
'sku_id' => $item->sku_id ? : 0,
'sku' => $item->sku ? json_encode($item->sku) : null,
'goods_quantity' => $item->quantity,
'price' => $item->total_amount,
'created_at' => now()->toDateTimeString(),
];
});
if ($gifts) {
DB::table(Gift::TABLE)->insert($gifts);
}
}
}
}