Order.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\Log;
  5. class Order extends Model
  6. {
  7. protected $guarded = [];
  8. protected $hidden = [
  9. 'id',
  10. 'bike_id',
  11. 'user_id',
  12. 'pause_bike_time',
  13. 'updated_at',
  14. 'pay_time'
  15. ];
  16. protected $dates = ['start_use_bike_time', 'end_use_bike_time'];
  17. // protected $appends = ['use_bike_time_length_text', 'pause_bike_time_length_text', 'use_bike_distance_length_text'];
  18. protected $casts = [
  19. 'start_use_bike_location' => 'array',
  20. 'end_use_bike_location' => 'array',
  21. ];
  22. const REDIS_BIKE_LOCATION_TAG = 'bike_locations';
  23. const NO_TAG = 'O';
  24. // 骑行中
  25. const STATUS_RIDE_BIKE = 1;
  26. // 骑车结束,待支付
  27. const STATUS_CLOSE_BIKE = 2;
  28. // 订单完成
  29. const STATUS_COMPLETE_ORDER = 3;
  30. // 订单关闭(不够计费直接关闭)
  31. const STATUS_CLOSE_ORDER = 4;
  32. // 临时停车中
  33. const STATUS_PAUSE_BIKE = 0;
  34. public static $statusMaps = [
  35. self::STATUS_PAUSE_BIKE => '临时停车',
  36. self::STATUS_RIDE_BIKE => '骑行中',
  37. self::STATUS_CLOSE_BIKE => '骑行结束,待支付',
  38. self::STATUS_COMPLETE_ORDER => '已完成',
  39. self::STATUS_CLOSE_ORDER => '订单关闭'
  40. ];
  41. const PAY_STATUS_OK = 1;
  42. const PAY_STATUS_NO = 0;
  43. public static $payStatusMaps = [
  44. self::PAY_STATUS_NO => '已支付',
  45. self::PAY_STATUS_OK => '未支付'
  46. ];
  47. const COUPON_OK = 1;
  48. const COUPON_NO = 0;
  49. public static $couponMaps = [
  50. self::COUPON_NO => '未使用优惠券',
  51. self::COUPON_OK => '使用优惠券'
  52. ];
  53. const PREFERENTIAL_NO = 0;
  54. const PREFERENTIAL_CARD_RIDING = 1;
  55. public static $preferentialTypeMaps = [
  56. self::PREFERENTIAL_NO => '无优惠',
  57. self::PREFERENTIAL_CARD_RIDING => '骑行卡优惠',
  58. ];
  59. const PAY_TYPE_NO = 0;
  60. const PAY_TYPE_WECHAT = 1;
  61. const PAY_TYPE_ACCOUNT = 2;
  62. public static $payTypeMaps = [
  63. self::PAY_TYPE_NO => '待支付',
  64. self::PAY_TYPE_WECHAT => '微信支付',
  65. self::PAY_TYPE_ACCOUNT => '余额支付'
  66. ];
  67. const IS_ADMIN_SETTLE_ORDER_USER = 0;
  68. const IS_ADMIN_SETTLE_ORDER_ADMIN = 1;
  69. const IS_ADMIN_SETTLE_ORDER_SYSTEM = 2;
  70. public static $isAdminSettleOrderMaps = [
  71. self::IS_ADMIN_SETTLE_ORDER_USER => '用户结算',
  72. self::IS_ADMIN_SETTLE_ORDER_ADMIN => '管理员结算',
  73. self::IS_ADMIN_SETTLE_ORDER_SYSTEM => '系统自动结算',
  74. ];
  75. public function bike()
  76. {
  77. return $this->belongsTo(Bike::class);
  78. }
  79. public function getUseBikeTimeLengthTextAttribute()
  80. {
  81. if ($this->attributes['use_bike_time_length']) {
  82. return format_minutes($this->attributes['use_bike_time_length']);
  83. }
  84. return '暂无';
  85. }
  86. public function getPauseBikeTimeLengthTextAttribute()
  87. {
  88. if ($this->attributes['pause_bike_time_length']) {
  89. return format_minutes($this->attributes['use_bike_time_length']);
  90. }
  91. return '暂无';
  92. }
  93. public function getUseBikeDistanceLengthTextAttribute()
  94. {
  95. if ($this->attributes['use_bike_distance_length']) {
  96. return $this->attributes['use_bike_distance_length'] . 'km';
  97. }
  98. return '暂无';
  99. }
  100. public function getEndUseBikeTimeTimestampAttribute()
  101. {
  102. return $this->end_use_bike_time->timestamp;
  103. }
  104. public function getPreferentialTypeNameAttribute()
  105. {
  106. return self::$preferentialTypeMaps[$this->attributes['preferential_type']];
  107. }
  108. // public function locations()
  109. // {
  110. // return $this->hasMany(LocationLogMongodb::class);
  111. // }
  112. /**
  113. * 生成订单号
  114. * @return bool|string
  115. * User: Mead
  116. */
  117. public static function makeNo()
  118. {
  119. // 订单流水号前缀
  120. $prefix = self::NO_TAG . date('YmdHis');
  121. for ($i = 0; $i < 10; $i++) {
  122. // 随机生成 6 位的数字
  123. $no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
  124. // 判断是否已经存在
  125. if (!static::query()->where('no', $no)->exists()) {
  126. return $no;
  127. }
  128. }
  129. Log::warning('find order no failed');
  130. return false;
  131. }
  132. public function pay_order_callback()
  133. {
  134. $order = $this->attributes;
  135. //添加钱包记录
  136. WalletLog::log(WalletLog::OPERATE_TYPE_ADD, $this->attributes['pay_money'], $this->attributes['user_id'], WalletLog::TYPE_ADD_WECHAT_PAY_ORDER, $this->attributes['area_id'], $this->attributes['id'], Order::class);
  137. WalletLog::log(WalletLog::OPERATE_TYPE_SUB, $this->attributes['pay_money'], $this->attributes['user_id'], WalletLog::TYPE_SUB_WECHAT_PAY_ORDER, $this->attributes['area_id'], $this->attributes['id'], Order::class);
  138. //修改订单记录
  139. $this->pay_status = Order::PAY_STATUS_OK;
  140. $this->pay_time = now();
  141. $this->pay_type = Order::PAY_TYPE_WECHAT;
  142. $this->status = Order::STATUS_COMPLETE_ORDER;
  143. if ($this->attributes['is_coupon'] == self::COUPON_OK) {
  144. CouponsUserBag::useCoupon($this->attributes['id'], $this->attributes['coupon_user_bags_id']);
  145. // 更新订单得优惠金额
  146. $this->preferential_money = bcsub(bcadd($this->attributes['time_money'], $this->attributes['dispatch_money'], 2), $this->attributes['pay_money'], 2);
  147. // 更新优惠券优惠金额
  148. $this->coupon_preferential_money = bcsub($this->attributes['preferential_money'], $this->attributes['card_preferential_money'], 2);
  149. }
  150. $this->save();
  151. return true;
  152. }
  153. /**
  154. * 退款回调
  155. * User: Mead
  156. */
  157. public function refund_order_callback()
  158. {
  159. WalletLog::where('type', WalletLog::TYPE_SUB_ORDER_MONEY_PAY_WECHAT)->where('operate_type', WalletLog::OPERATE_TYPE_SUB)->where('log_id', $this->attributes['id'])->where('user_id', $this->attributes['user_id'])->where('log_type', Order::class)->update([
  160. 'status' => 1
  161. ]);
  162. }
  163. }