<?php

namespace Modules\Mall\Traits;

use Illuminate\Database\Eloquent\Relations\HasMany;
use Modules\Gout\Models\GoutCase;
use Modules\Mall\Models\Order;
use Modules\Mall\Models\OrderItem;

trait HasOrders
{

    /**
     * Notes   : 用户的订单列表
     *
     * @Date   : 2021/4/19 10:28 上午
     * @Author : < Jason.C >
     * @return HasMany
     */
    public function orders(): HasMany
    {
        return $this->hasMany(Order::class);
    }

    public function orderItems(): HasMany
    {
        return $this->hasMany(OrderItem::class);
    }

    /**
     * Notes: 是否可以申请提货
     *
     * @Author: 玄尘
     * @Date  : 2021/10/22 14:13
     * @return bool
     */
    public function canPick(): bool
    {
        return $this->case && $this->case->status == GoutCase::STATUS_PASS &&
            $this->identities()->where('order', '>', 1)->count() > 0 &&
            $this->userStock->stock > $this->userStock->hold;
    }

    /**
     * Notes: 提货
     *
     * @Author: 玄尘
     * @Date: 2022/7/27 16:18
     */
    public function pickGoods()
    {
        if (! $this->canPick()) {
            return '不可提货';
        }
    }

    /**
     * Notes: 获取订单数据
     *
     * @Author: 玄尘
     * @Date: 2022/12/30 20:57
     */
    public function getOrderCount(): array
    {
        return [
            //免费领取
            'sample' => [
                'all'       => $this->orders()->typesample()->common()->count(),
                'paid'      => $this->orders()->typesample()->paid()->count(),
                'unpay'     => $this->orders()->typesample()->unpay()->count(),
                'delivered' => $this->orders()->typesample()->delivered()->count(),
            ],
            //正常
            'normal' => [
                'all'       => $this->orders()->typenormal()->common()->count(),
                'paid'      => $this->orders()->typenormal()->paid()->count(),
                'unpay'     => $this->orders()->typenormal()->unpay()->count(),
                'delivered' => $this->orders()->typenormal()->delivered()->count(),
            ],
            //积分兑换
            'score'  => [
                'all'       => $this->orders()->typescore()->common()->count(),
                'paid'      => $this->orders()->typescore()->paid()->count(),
                'unpay'     => $this->orders()->typescore()->unpay()->count(),
                'delivered' => $this->orders()->typescore()->delivered()->count(),
            ],
        ];
    }

    /**
     * Notes: 获取捐赠数量
     *
     * @Author: 玄尘
     * @Date: 2022/12/31 17:14
     */
    public function getJzCount()
    {
        return OrderItem::query()
            ->whereHas('order', function ($q) {
                $q->where('type', Order::TYPE_NORMAL)
                    ->where('user_id', $this->id)
                    ->whereIn('state', [
                        Order::STATUS_PAID,
                        Order::STATUS_DELIVERED,
                        Order::STATUS_SIGNED,
                        Order::STATUS_COMPLETED,
                    ]);
            })->sum('qty');
    }


    /**
     * Notes: 获取所有捐赠数
     *
     * @Author: 玄尘
     * @Date: 2022/12/31 18:03
     * @return int|mixed
     */
    public function getALlJzCount()
    {
        return OrderItem::query()
            ->whereHas('order', function ($q) {
                $q->where('type', Order::TYPE_NORMAL)
                    ->whereIn('state', [
                        Order::STATUS_PAID,
                        Order::STATUS_DELIVERED,
                        Order::STATUS_SIGNED,
                        Order::STATUS_COMPLETED,
                    ]);
            })->sum('qty');
    }

    /**
     * Notes: 剩余
     *
     * @Author: 玄尘
     * @Date: 2023/1/3 10:55
     */
    public function getResidue()
    {
        $hasAllJzCOunt = $this->getALlJzCount();

        return bcsub(1000, $hasAllJzCOunt);
    }

}