<?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 $user_coupon_id 主键id
 * @property int $user_id 用户id
 * @property int $patient_id 患者id
 * @property int $coupon_id 优惠卷id
 * @property int $user_coupon_status 状态（0:未使用 1:已使用 3:已过期）
 * @property string $coupon_use_date 使用时间
 * @property string $valid_start_time 开始使用时间
 * @property string $valid_end_time 过期使用时间
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 修改时间
 * @property-read Coupon $Coupon 
 */
class UserCoupon extends Model
{
    use Snowflake;

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

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['user_coupon_id', 'user_id', 'patient_id', 'coupon_id', 'user_coupon_status', 'coupon_use_date', 'valid_start_time', 'valid_end_time', 'created_at', 'updated_at'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['user_coupon_id' => 'string', 'user_id' => 'string', 'patient_id' => 'string', 'coupon_id' => 'string', 'user_coupon_status' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    protected string $primaryKey = "user_coupon_id";

    /**
     * 关联医院表
     */
    public function Coupon(): HasOne
    {
        return $this->hasOne(Coupon::class, 'coupon_id', 'coupon_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 $coupon_params
     * @param array $fields
     * @return Collection|array
     */
    public static function getWithCouponList(array $params,array $coupon_params,array $application_scope_params, array $fields = ['*']): Collection|array
    {
        return self::with(['Coupon'])
            ->whereHas('Coupon' , function($query) use ($coupon_params,$application_scope_params){
                $query->where($coupon_params)->whereIn('application_scope',$application_scope_params);
            })
            ->where($params)->get($fields);
    }

    /**
     * 获取优惠卷信息-单条
     * @param array $params
     * @param array $coupon_params
     * @param array $fields
     * @return object|null
     */
    public static function getWithCouponOne(array $params,array $coupon_params, array $application_scope_params,array $fields = ['*']): object|null
    {
        return self::with(['Coupon'])
            ->whereHas('Coupon' , function($query) use ($coupon_params,$application_scope_params){
                $query->where($coupon_params)->whereIn('application_scope',$application_scope_params);
            })
            ->where($params)
            ->first($fields);
    }

    /**
     * 新增用户优惠卷
     * @param array $data
     * @return \Hyperf\Database\Model\Model|UserCoupon
     */
    public static function addUserCoupon(array $data): \Hyperf\Database\Model\Model|UserCoupon
    {
        return self::create($data);
    }

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