<?php

namespace Modules\User\Models\Traits;


use Illuminate\Database\Eloquent\Relations\HasOne;
use Modules\Mall\Models\Order;
use Modules\Mall\Models\OrderItem;
use Modules\User\Models\Identity;
use Modules\User\Models\UserStock;
use Modules\User\Models\UserStockLog;

trait HasStock
{


    /**
     * Notes: 库存
     *
     * @Author: 玄尘
     * @Date: 2022/7/26 16:13
     * @return HasOne
     */
    public function userStock(): HasOne
    {
        return $this->hasOne(UserStock::class)->withDefault([
            'stock'   => 0,
            'hold'    => 0,
            'residue' => 0,
        ]);
    }

    /**
     * Notes: 获取库存
     *
     * @Author: 玄尘
     * @Date: 2022/7/29 13:58
     */
    public function getStockData(): array
    {
        $identity = $this->identityFirst();
        $min      = 1;

        $deliver = OrderItem::query()
            ->whereHas('order', function ($q) {
                $q->TypeSample()->ByUser($this)->paid();
            })->sum('qty');

        $deliverd = OrderItem::query()
            ->whereHas('order', function ($q) {
                $q->TypeSample()->ByUser($this)->whereIn('state', [
                    Order::STATUS_SIGNED,
                    Order::STATUS_DELIVERED,
                ]);
            })->sum('qty');

        return [
            'case_id'  => $this->case ? $this->case->id : 0,
            'stock'    => $this->userStock->stock,//总库存
            'hold'     => $this->userStock->hold,//已发货
            'residue'  => (int) bcsub($this->userStock->residue, $deliver),//未提货
            'deliver'  => $deliver,//待发货
            'deliverd' => $deliverd,//已发货
            'min_pick' => $min,//最少提货数量
        ];
    }

    /**
     * Notes: 增加库存
     *
     * @Author: 玄尘
     * @Date: 2022/7/26 16:18
     */
    public function addStock($identity_id, $type = null, $addStock = 0)
    {
        try {
            $identity = Identity::find($identity_id);

            $stock = $identity->stock;
            if ($addStock) {
                $stock = $addStock;
            }

            $userStock = $this->userStock;
            if (! $type) {
                $type = UserStockLog::TYPE_IN;
            }

            if (isset($userStock->id)) {
                $userStock->increment('stock', $stock);
            } else {
                $userStock = UserStock::query()->updateOrCreate([
                    'user_id' => $this->id,
                ], [
                    'stock' => $stock,
                ]);
            }

            $this->addStockLog($type, $stock, $identity_id);

            return true;
        } catch (\Exception $exception) {
            return new \Exception($exception->getMessage());
        }
    }


    /**
     * Notes: 增加销量
     *
     * @Author: 玄尘
     * @Date: 2022/7/29 14:44
     */
    public function addHold($number)
    {
        $this->userStock->increment('hold', $number);

        $this->addStockLog(UserStockLog::TYPE_OUT, -$number);

    }

    /**
     * Notes: 增加日志
     *
     * @Author: 玄尘
     * @Date: 2022/8/2 14:55
     * @param $type
     * @param $variable
     * @param  int  $identity_id
     */
    public function addStockLog($type, $variable, int $identity_id = 0)
    {
        $userStock = UserStock::where('user_id', $this->id)->first();
        UserStockLog::query()
            ->create([
                'user_stock_id' => $userStock->id,
                'type'          => $type,
                'variable'      => $variable,
                'identity_id'   => $identity_id,
            ]);
    }


}