12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace App\Models;
- use App\Traits\ModelHelpers;
- use Carbon\Carbon;
- use Illuminate\Database\Eloquent\Model;
- class CardRidingUserBags extends Model
- {
- use ModelHelpers;
- //
- protected $table = 'card_riding_user_bags';
- protected $guarded = [];
- const STATUS_OK = 1;
- const STATUS_NO = 0;
- public $statusMaps = [
- self::STATUS_OK => '骑行卡有效',
- self::STATUS_NO => '骑行卡失效'
- ];
- const LIMIT_TIMES_YES = 1;
- const LIMIT_TIMES_NO = 0;
- public static $limitTimesMaps = [
- self::LIMIT_TIMES_NO => '不限次',
- self::LIMIT_TIMES_YES => '限次',
- ];
- public function users(){
- return $this->belongsTo(User::class,'user_id','id');
- }
- public function cardRidingOrder(){
- return $this->belongsTo(CardRidingOrder::class,'card_riding_order_id','id');
- }
- public function cardRiding(){
- return $this->belongsTo(CardRiding::class,'riding_card_id','id');
- }
- /**
- * 判断用户是否存在有效骑行卡 有的话返回该卡 没有的话 返回[]
- *
- * @param $user_id integer
- *
- * @return mixed
- *
- * User:Fx
- * */
- public static function isExist($user_id,$order_start_time=''){
- $cardRidingUser = self::query()
- ->where('status',self::STATUS_OK)
- ->where('user_id',$user_id)
- ->first();
- if(empty($cardRidingUser)){
- // 不存在
- return [];
- }
- $t1 = Carbon::make($cardRidingUser->expiration_time);
- $t2 = Carbon::now(); // 对比过期时间
- if(!empty($order_start_time)){
- $t2 = Carbon::make($order_start_time);
- }
- if($t1->lt($t2)){
- // 已过期
- $cardRidingUser->status = self::STATUS_NO;
- $cardRidingUser->save();
- return [];
- }
- if($cardRidingUser->is_limit_times == self::LIMIT_TIMES_YES){
- // 限次卡 判断是否次数归0
- if($cardRidingUser->can_ridding_times <= 0){
- // 次数已经归0 更新卡失效
- $cardRidingUser->status = self::STATUS_NO;
- $cardRidingUser->save();
- return [];
- }
- }
- // 存在
- return $cardRidingUser;
- }
- }
|