<?php

namespace Modules\Mall\Models;

use App\Models\Model;
use App\Traits\HasCovers;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Modules\Mall\Models\Traits\GoodsAttribute;
use Overtrue\LaravelVersionable\Versionable;

class GoodsSku extends Model
{

    use HasCovers,
        Versionable,
        GoodsAttribute,
        SoftDeletes;

    protected $table = 'mall_goods_skus';

    /**
     * 不参与版本记录的字段
     *
     * @var array|string[]
     */
    protected array $dontVersionable = ['updated_at'];

    /**
     * Notes   : 所属品牌
     *
     * @Date   : 2021/5/11 11:00 上午
     * @Author : < Jason.C >
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function goods(): BelongsTo
    {
        return $this->belongsTo(Goods::class);
    }

    /**
     * Notes   : 当前规格是否可购买
     *
     * @Date   : 2021/5/14 10:08
     * @Author : Mr.wang
     * @param  int  $qty
     * @return bool
     */
    public function canBuy(int $qty = 1): bool
    {
        if ($this->goods->channel == Goods::CHANNEL_FREE) {
            return true;
        }

        if ($this->goods->status == Goods::STATUS_UP && $this->stock >= $qty) {
            return true;
        }

        return false;
    }

    /**
     * @Notes  : 检测商品价格，给出最低售价
     *
     * @Date   : 2022/6/30   11:41
     * @param  float  $cost
     * @param  float  $price
     * @return float
     * @author : Mr.wang
     */
    public static function verifyPrice(float $cost, float $price): float
    {
        return 0;
        $costPercent = app('Conf_mall')['cost_percent'] ?? 70;
        $up          = ceil(bcdiv($cost, bcdiv($costPercent, 100, 4), 2));
        if ($price < $up) {
            return $up;
        } else {
            return 0;
        }
    }

}
