<?php

declare(strict_types=1);

namespace App\Model;



use Hyperf\Database\Model\Collection;
use Hyperf\Database\Model\Relations\HasOne;
use Hyperf\Snowflake\Concern\Snowflake;

/**
 * @property int $product_item_id 主键id
 * @property int $order_product_id 订单-商品订单id
 * @property int $order_inquiry_id 订单-问诊id
 * @property int $order_prescription_id 订单-处方id
 * @property int $product_id 商品id
 * @property string $product_name 商品名称
 * @property string $product_price 商品价格
 * @property string $product_platform_code 商品处方平台编码
 * @property int $amount 数量
 * @property string $manufacturer 生产厂家
 * @property string $product_cover_img 商品封面图
 * @property string $product_spec 商品规格
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 修改时间
 * @property-read Product $Product 
 */
class OrderProductItem extends Model
{
    use Snowflake;

    /**
     * The table associated with the model.
     */
    protected ?string $table = 'order_product_item';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['product_item_id', 'order_product_id', 'order_inquiry_id', 'order_prescription_id', 'product_id', 'product_name', 'product_price', 'product_platform_code', 'amount', 'manufacturer', 'product_cover_img', 'product_spec', 'created_at', 'updated_at'];

    protected string $primaryKey = "product_item_id";

    /**
     * 关联商品表
     */
    public function Product(): HasOne
    {
        return $this->hasOne(Product::class, 'product_id', 'product_id');
    }

    /**
     * 获取信息-单条
     * @param array $params
     * @param array $fields
     * @return object|null
     */
    public static function getOne(array $params, array $fields = ['*']): object|null
    {
        return self::where($params)->first($fields);
    }

    /**
     * 获取数据-多
     * @param array $params
     * @param array $fields
     * @return Collection|array
     */
    public static function getList(array $params = [], array $fields = ['*']): Collection|array
    {
        return self::where($params)->get($fields);
    }

    /**
     * 获取是否存在
     * @param array $params
     * @return bool
     */
    public static function getExists(array $params): bool
    {
        return self::where($params)->exists();
    }

    /**
     * 获取数据-关联商品表
     * @param array $params
     * @param array $fields
     * @return Collection|array
     */
    public static function getWithProductList(array $params = [], array $fields = ['*']): Collection|array
    {
        return self::with([
            "Product"
        ])
            ->where($params)
            ->get($fields);
    }

    /**
     * 新增
     * @param array $data
     * @return \Hyperf\Database\Model\Model|OrderProductItem
     */
    public static function addOrderProductItem(array $data): \Hyperf\Database\Model\Model|OrderProductItem
    {
        return self::create($data);
    }

    public static function edit(array $params = [], array $data = []): int
    {
        return self::where($params)->update($data);
    }
}
