CartOrderTrait.php 3.6 KB
<?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);
            }
        }
    }
}