Order.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Models;
  3. use App\Traits\ModelHelpers;
  4. use Carbon\Carbon;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * 普通订单
  9. * */
  10. class Order extends Model
  11. {
  12. use ModelHelpers;
  13. const NO_TAG = 'O';
  14. const ORDER_TYPE = 0;
  15. protected $guarded = [];
  16. //
  17. // 骑行中
  18. const STATUS_RIDE_BIKE = 1;
  19. // 骑车结束,待支付
  20. const STATUS_CLOSE_BIKE = 2;
  21. // 订单完成
  22. const STATUS_COMPLETE_ORDER = 3;
  23. // 订单关闭(不够计费直接关闭)
  24. const STATUS_CLOSE_ORDER = 4;
  25. // 临时停车中
  26. const STATUS_PAUSE_BIKE = 0;
  27. public static $statusMaps = [
  28. self::STATUS_PAUSE_BIKE => '临时停车',
  29. self::STATUS_RIDE_BIKE => '骑行中',
  30. self::STATUS_CLOSE_BIKE => '待支付',
  31. self::STATUS_COMPLETE_ORDER => '已完成',
  32. self::STATUS_CLOSE_ORDER => '订单关闭'
  33. ];
  34. const ADMIN_SETTLE_ORDER_AUTO_CLOSE = 2;
  35. const ADMIN_SETTLE_ORDER_ADMIN = 1;
  36. const ADMIN_SETTLE_ORDER_USER = 0;
  37. public static $adminSettleOrderMaps = [
  38. self::ADMIN_SETTLE_ORDER_USER => '正常结算',
  39. self::ADMIN_SETTLE_ORDER_ADMIN => '管理员结算',
  40. self::ADMIN_SETTLE_ORDER_AUTO_CLOSE => '系统自动结算',
  41. ];
  42. const PAY_STATUS_OK = 1;
  43. const PAY_STATUS_NO = 0;
  44. public static $payStatusMaps = [
  45. self::PAY_STATUS_NO => '已支付',
  46. self::PAY_STATUS_OK => '未支付'
  47. ];
  48. const REFUND_MONEY_OK = 1;
  49. const REFUND_MONEY_NO = 0;
  50. public static $refundMoneyMaps = [
  51. self::REFUND_MONEY_NO => '未返还',
  52. self::REFUND_MONEY_OK => '已返还'
  53. ];
  54. const PAY_TYPE_NO = 0;
  55. const PAY_TYPE_WECHAT = 1;
  56. const PAY_TYPE_ACCOUNT = 2;
  57. public static $payTypeMaps = [
  58. self::PAY_TYPE_NO => '待支付',
  59. self::PAY_TYPE_WECHAT => '微信支付',
  60. self::PAY_TYPE_ACCOUNT => '余额支付'
  61. ];
  62. const PREFERENTIAL_NO = 0;
  63. const PREFERENTIAL_CARD_RIDING = 1;
  64. public static $preferentialTypeMaps = [
  65. self::PREFERENTIAL_NO => '无优惠',
  66. self::PREFERENTIAL_CARD_RIDING => '骑行卡优惠',
  67. ];
  68. public function users()
  69. {
  70. return $this->belongsTo(User::class, 'user_id', 'id');
  71. }
  72. public function bikes()
  73. {
  74. return $this->belongsTo(Bike::class, 'bike_id', 'id');
  75. }
  76. public function order_bike_operates()
  77. {
  78. return $this->hasMany(OrderBikeOperate::class, 'order_id', 'id');
  79. }
  80. public function area()
  81. {
  82. return $this->belongsTo(Area::class, 'area_id', 'id');
  83. }
  84. public function locationsLog()
  85. {
  86. return $this->hasMany(LocationsLog::class, 'order_id', 'id');
  87. }
  88. public function walletLogs()
  89. {
  90. return $this->morphMany(WalletLog::class, 'log');
  91. }
  92. public function getOrderTypeAttribute(){
  93. return self::ORDER_TYPE;
  94. }
  95. /**
  96. * 生成订单号
  97. * @return bool|string
  98. * User: Mead
  99. */
  100. public static function makeNo()
  101. {
  102. // 订单流水号前缀
  103. $prefix = self::NO_TAG . date('YmdHis');
  104. for ($i = 0; $i < 10; $i++) {
  105. // 随机生成 6 位的数字
  106. $no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
  107. // 判断是否已经存在
  108. if (!static::query()->where('no', $no)->exists()) {
  109. return $no;
  110. }
  111. }
  112. Log::warning('find order no failed');
  113. return false;
  114. }
  115. /**
  116. * 根据用户id 查询今日订单次数
  117. * @param $user_id
  118. *
  119. * */
  120. public static function getTodayOrderNum($user_id){
  121. return self::query()->where('created_at','>',Carbon::today())->where('user_id',$user_id)->count();
  122. }
  123. }