157 lines
5.4 KiB
PHP
157 lines
5.4 KiB
PHP
<?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'];
|
||
|
||
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 $fields = ['*']): Collection|array
|
||
{
|
||
return self::with(['Coupon'])
|
||
->whereHas('Coupon' , function($query){
|
||
$query->where("coupon_client",1)->where("coupon_status",1);
|
||
})
|
||
->where($params)->get($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);
|
||
}
|
||
|
||
/**
|
||
* 获取患者问诊可用的优惠卷
|
||
* @param string|int $user_id 用户id
|
||
* @param string|int $inquiry_type 关联问诊类型,application_scope=问诊时存在生效,逗号分隔(1:专家问诊 2:快速问诊 3:公益问诊 4:问诊购药 5:检测)
|
||
* @param array $fields 字段
|
||
* @return Collection|array
|
||
*/
|
||
public static function getUserInquiryUsableCoupon(string|int $user_id, string|int $inquiry_type,array $fields = ['*']): Collection|array
|
||
{
|
||
$params = array();
|
||
$params[] = ['user_id', '=', $user_id];
|
||
$params[] = ['user_coupon_status', '=', 0];// 状态(0:未使用 1:已使用 3:已过期)
|
||
$params[] = ['valid_start_time', '<', date('Y-m-d H:i:s', time())]; // 有效使用时间
|
||
$params[] = ['valid_end_time', '>', date('Y-m-d H:i:s', time())]; // 过期使用时间
|
||
|
||
return self::with(['Coupon'])
|
||
->whereHas('Coupon', function ($query) use ($inquiry_type) {
|
||
$query->where("coupon_client",1)
|
||
->where("coupon_status",1)
|
||
->where("application_scope",2)
|
||
->where(function ($query) use ($inquiry_type) {
|
||
$query->orwhere("inquiry_type","like",'%' . $inquiry_type+1 . '%');
|
||
$query->orwhere("inquiry_type","like",'%1%');
|
||
});
|
||
})
|
||
->where($params)
|
||
->get($fields);
|
||
}
|
||
|
||
/**
|
||
* 获取患者购药可用的优惠卷
|
||
* @param string|int $user_id 用户id
|
||
* @param array $coupon_product_datas
|
||
* @param array $fields 字段
|
||
* @return Collection|array
|
||
*/
|
||
public static function getUserProductUsableCoupon(string|int $user_id,array $coupon_product_datas,array $fields = ['*']): Collection|array
|
||
{
|
||
$params = array();
|
||
$params[] = ['user_id', '=', $user_id];
|
||
$params[] = ['user_coupon_status', '=', 0];// 状态(0:未使用 1:已使用 3:已过期)
|
||
$params[] = ['valid_start_time', '<', date('Y-m-d H:i:s', time())]; // 有效使用时间
|
||
$params[] = ['valid_end_time', '>', date('Y-m-d H:i:s', time())]; // 过期使用时间
|
||
|
||
return self::with(['Coupon'])
|
||
->whereHas('Coupon', function ($query) use ($coupon_product_datas) {
|
||
$query->where("coupon_client",1)
|
||
->where("coupon_status",1)
|
||
->whereIn("application_scope",[1,3,4,5])
|
||
->where(function ($query) use ($coupon_product_datas) {
|
||
foreach ($coupon_product_datas as $coupon_product_data){
|
||
$query->orwhere("product_id","like",'%' . $coupon_product_data['product_id'] . '%');
|
||
}
|
||
});
|
||
})
|
||
->where($params)
|
||
->get($fields);
|
||
}
|
||
}
|