Order.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 RIDING_CARD_OK = 1;
  63. const RIDING_CARD_NO = 0;
  64. public static $ridingCardMaps = [
  65. self::RIDING_CARD_NO => '未使用骑行卡',
  66. self::RIDING_CARD_OK => '已使用骑行卡',
  67. ];
  68. const PREFERENTIAL_NO = 0;
  69. const PREFERENTIAL_CARD_RIDING = 1;
  70. const PREFERENTIAL_COUPON = 2;
  71. const PREFERENTIAL_FREE_MINUTE = 3;
  72. public static $preferentialTypeMaps = [
  73. self::PREFERENTIAL_NO => '无优惠',
  74. self::PREFERENTIAL_CARD_RIDING => '骑行卡优惠',
  75. self::PREFERENTIAL_COUPON => '优惠券优惠',
  76. self::PREFERENTIAL_FREE_MINUTE => '时长优惠',
  77. ];
  78. public function setPreferentialTypeAttribute($value){
  79. $this->attributes['preferential_type'] = '-'.$value.'-';
  80. }
  81. public function getPreferentialTypeAttribute(){
  82. return trim($this->attributes['preferential_type'],'-');
  83. }
  84. const COUPON_OK = 1;
  85. const COUPON_NO = 0;
  86. public static $couponMaps = [
  87. self::COUPON_OK => '优惠券优惠',
  88. self::COUPON_NO => '无优惠券优惠',
  89. ];
  90. public function getPreferentialTypeNameAttribute()
  91. {
  92. if($this->attributes['preferential_type'] == '-0-'){
  93. return '无优惠';
  94. }else{
  95. $str = '';
  96. $preferential_type_arr = str2arr(trim($this->attributes['preferential_type'],'-'),'-');
  97. foreach ($preferential_type_arr as $v){
  98. $str .= self::$preferentialTypeMaps[$v] . ',';
  99. }
  100. return rtrim($str,',');
  101. }
  102. }
  103. public function users()
  104. {
  105. return $this->belongsTo(User::class, 'user_id', 'id');
  106. }
  107. public function bikes()
  108. {
  109. return $this->belongsTo(Bike::class, 'bike_id', 'id');
  110. }
  111. public function order_bike_operates()
  112. {
  113. return $this->hasMany(OrderBikeOperate::class, 'order_id', 'id');
  114. }
  115. public function area()
  116. {
  117. return $this->belongsTo(Area::class, 'area_id', 'id');
  118. }
  119. public function locationsLog()
  120. {
  121. return $this->hasMany(LocationsLog::class, 'order_id', 'id');
  122. }
  123. public function walletLogs()
  124. {
  125. return $this->morphMany(WalletLog::class, 'log');
  126. }
  127. public function getOrderTypeAttribute(){
  128. return self::ORDER_TYPE;
  129. }
  130. /**
  131. * 生成订单号
  132. * @return bool|string
  133. * User: Mead
  134. */
  135. public static function makeNo()
  136. {
  137. // 订单流水号前缀
  138. $prefix = config('bike.no_tag') . self::NO_TAG . date('YmdHis');
  139. for ($i = 0; $i < 10; $i++) {
  140. // 随机生成 6 位的数字
  141. $no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
  142. // 判断是否已经存在
  143. if (!static::query()->where('no', $no)->exists()) {
  144. return $no;
  145. }
  146. }
  147. Log::warning('find order no failed');
  148. return false;
  149. }
  150. /**
  151. * 根据用户id 查询今日订单次数
  152. * @param $user_id
  153. *
  154. * */
  155. public static function getTodayOrderNum($user_id){
  156. return self::query()->where('created_at','>',Carbon::today())->where('user_id',$user_id)->count();
  157. }
  158. }