CardRidingUserBags.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Models;
  3. use App\Traits\ModelHelpers;
  4. use Carbon\Carbon;
  5. use Illuminate\Database\Eloquent\Model;
  6. class CardRidingUserBags extends Model
  7. {
  8. use ModelHelpers;
  9. //
  10. protected $table = 'card_riding_user_bags';
  11. protected $guarded = [];
  12. const STATUS_OK = 1;
  13. const STATUS_NO = 0;
  14. public $statusMaps = [
  15. self::STATUS_OK => '骑行卡有效',
  16. self::STATUS_NO => '骑行卡失效'
  17. ];
  18. const LIMIT_TIMES_YES = 1;
  19. const LIMIT_TIMES_NO = 0;
  20. public static $limitTimesMaps = [
  21. self::LIMIT_TIMES_NO => '不限次',
  22. self::LIMIT_TIMES_YES => '限次',
  23. ];
  24. public function users(){
  25. return $this->belongsTo(User::class,'user_id','id');
  26. }
  27. public function cardRidingOrder(){
  28. return $this->belongsTo(CardRidingOrder::class,'card_riding_order_id','id');
  29. }
  30. public function cardRiding(){
  31. return $this->belongsTo(CardRiding::class,'riding_card_id','id');
  32. }
  33. /**
  34. * 判断用户是否存在有效骑行卡 有的话返回该卡 没有的话 返回[]
  35. *
  36. * @param $user_id integer
  37. *
  38. * @return mixed
  39. *
  40. * User:Fx
  41. * */
  42. public static function isExist($user_id,$order_start_time=''){
  43. $cardRidingUser = self::query()
  44. ->where('status',self::STATUS_OK)
  45. ->where('user_id',$user_id)
  46. ->first();
  47. if(empty($cardRidingUser)){
  48. // 不存在
  49. return [];
  50. }
  51. $t1 = Carbon::make($cardRidingUser->expiration_time);
  52. $t2 = Carbon::now(); // 对比过期时间
  53. if(!empty($order_start_time)){
  54. $t2 = Carbon::make($order_start_time);
  55. }
  56. if($t1->lt($t2)){
  57. // 已过期
  58. $cardRidingUser->status = self::STATUS_NO;
  59. $cardRidingUser->save();
  60. return [];
  61. }
  62. if($cardRidingUser->is_limit_times == self::LIMIT_TIMES_YES){
  63. // 限次卡 判断是否次数归0
  64. if($cardRidingUser->can_ridding_times <= 0){
  65. // 次数已经归0 更新卡失效
  66. $cardRidingUser->status = self::STATUS_NO;
  67. $cardRidingUser->save();
  68. return [];
  69. }
  70. }
  71. // 存在
  72. return $cardRidingUser;
  73. }
  74. }