RentOrder.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace App\Models;
  3. use App\Handlers\BikeStatusInfoSyncHandler;
  4. use Carbon\Carbon;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. class RentOrder extends Model
  9. {
  10. protected $guarded = [];
  11. // protected $hidden = [
  12. // 'id',
  13. // 'bike_id',
  14. // 'user_id',
  15. // 'updated_at',
  16. // 'pay_time'
  17. // ];
  18. protected $dates = ['start_use_bike_time', 'end_use_bike_time', 'return_end_bike_time'];
  19. protected $casts = [
  20. 'start_use_bike_location' => 'array',
  21. 'end_use_bike_location' => 'array',
  22. 'setting' => 'json',
  23. ];
  24. const REDIS_BIKE_LOCATION_TAG = 'bike_locations';
  25. const NO_TAG = 'D';
  26. const NO_OVER_TAG = 'R';
  27. const TYPE_DAY_RENT = 0;
  28. public static $typeMaps = [
  29. self::TYPE_DAY_RENT => '日租'
  30. ];
  31. // 待支付租金
  32. const STATUS_WAIT_PAY_RENT_MONEY = 0;
  33. // 租车中
  34. const STATUS_RENT_BIKE = 1;
  35. // 租车结束,待支付
  36. const STATUS_CLOSE_RENT_BIKE = 2;
  37. // 已完成
  38. const STATUS_COMPLETE_ORDER = 3;
  39. // 订单关闭
  40. const STATUS_CLOSE_ORDER = 4;
  41. public static $statusMaps = [
  42. self::STATUS_WAIT_PAY_RENT_MONEY => '待支付租金',
  43. self::STATUS_RENT_BIKE => '租车中',
  44. self::STATUS_CLOSE_RENT_BIKE => '租车结束,待支付',
  45. self::STATUS_COMPLETE_ORDER => '已完成',
  46. self::STATUS_CLOSE_ORDER => '订单关闭'
  47. ];
  48. const PAY_STATUS_OK = 1;
  49. const PAY_STATUS_NO = 0;
  50. public static $payStatusMaps = [
  51. self::PAY_STATUS_NO => '已支付',
  52. self::PAY_STATUS_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 IS_ADMIN_SETTLE_ORDER_USER = 0;
  63. const IS_ADMIN_SETTLE_ORDER_ADMIN = 1;
  64. const IS_ADMIN_SETTLE_ORDER_SYSTEM = 2;
  65. public static $isAdminSettleOrderMaps = [
  66. self::IS_ADMIN_SETTLE_ORDER_USER => '用户结算',
  67. self::IS_ADMIN_SETTLE_ORDER_ADMIN => '管理员结算',
  68. self::IS_ADMIN_SETTLE_ORDER_SYSTEM => '系统自动结算',
  69. ];
  70. public function bike()
  71. {
  72. return $this->belongsTo(Bike::class);
  73. }
  74. public function getUseBikeTimeLengthTextAttribute()
  75. {
  76. if ($this->attributes['use_bike_time_length']) {
  77. return format_minutes($this->attributes['use_bike_time_length']);
  78. }
  79. return '暂无';
  80. }
  81. public function getPauseBikeTimeLengthTextAttribute()
  82. {
  83. if ($this->attributes['pause_bike_time_length']) {
  84. return format_minutes($this->attributes['use_bike_time_length']);
  85. }
  86. return '暂无';
  87. }
  88. public function getUseBikeDistanceLengthTextAttribute()
  89. {
  90. if ($this->attributes['use_bike_distance_length']) {
  91. return $this->attributes['use_bike_distance_length'] . 'km';
  92. }
  93. return '暂无';
  94. }
  95. public function getEndUseBikeTimeTimestampAttribute()
  96. {
  97. if ($this->attributes['end_use_bike_time']) {
  98. return $this->end_use_bike_time->timestamp;
  99. }
  100. return time();
  101. }
  102. /**
  103. * 生成订单号
  104. * @return bool|string
  105. * User: Mead
  106. */
  107. public static function makeNo()
  108. {
  109. // 订单流水号前缀
  110. $prefix = config('bike.no_tag') . self::NO_TAG . date('YmdHis');
  111. for ($i = 0; $i < 10; $i++) {
  112. // 随机生成 6 位的数字
  113. $no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
  114. // 判断是否已经存在
  115. if (!static::query()->where('no', $no)->exists()) {
  116. return $no;
  117. }
  118. }
  119. Log::warning('find order no failed');
  120. return false;
  121. }
  122. /**
  123. * 生成二次消费订单号
  124. * @return bool|string
  125. * User: Mead
  126. */
  127. public static function makeOverNo()
  128. {
  129. // 订单流水号前缀
  130. $prefix = config('bike.no_tag') . self::NO_OVER_TAG . date('YmdHis');
  131. for ($i = 0; $i < 10; $i++) {
  132. // 随机生成 6 位的数字
  133. $no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
  134. // 判断是否已经存在
  135. if (!static::query()->where('over_no', $no)->exists()) {
  136. return $no;
  137. }
  138. }
  139. Log::warning('find order no failed');
  140. return false;
  141. }
  142. public function pay_rent_order_callback()
  143. {
  144. $order = $this->attributes;
  145. //添加钱包记录
  146. WalletLog::log(WalletLog::OPERATE_TYPE_ADD, $this->attributes['rent_pay_money'], $this->attributes['user_id'], WalletLog::TYPE_ADD_WECHAT_PAY_RENT_ORDER, $this->attributes['area_id'], $this->attributes['id'], RentOrder::class);
  147. WalletLog::log(WalletLog::OPERATE_TYPE_SUB, $this->attributes['rent_pay_money'], $this->attributes['user_id'], WalletLog::TYPE_SUB_WECHAT_PAY_RENT_ORDER, $this->attributes['area_id'], $this->attributes['id'], RentOrder::class);
  148. Bike::where('id', $this->attributes['bike_id'])->update([
  149. 'is_rent' => Bike::RENT_YES,
  150. 'is_riding' => Bike::RIDING_YES
  151. ]);
  152. // 更新orders redis
  153. (new BikeStatusInfoSyncHandler())->toBikeRideStatus(BikeStatusInfoSyncHandler::ROLE_USER, $order['bike_no'], [
  154. 'id' => $this->attributes['id'],
  155. 'bike_id' => $order['bike_id'],
  156. 'area_id' => $order['area_id'],
  157. 'is_rent' => 1
  158. ]);
  159. return true;
  160. }
  161. public function pay_rent_over_order_callback()
  162. {
  163. $order = $this->attributes;
  164. //添加钱包记录
  165. WalletLog::log(WalletLog::OPERATE_TYPE_ADD, $this->attributes['pay_money'], $this->attributes['user_id'], WalletLog::TYPE_ADD_WECHAT_PAY_RENT_ORDER_MONEY, $this->attributes['area_id'], $this->attributes['id'], RentOrder::class);
  166. WalletLog::log(WalletLog::OPERATE_TYPE_SUB, $this->attributes['pay_money'], $this->attributes['user_id'], WalletLog::TYPE_SUB_WECHAT_PAY_RENT_ORDER_MONEY, $this->attributes['area_id'], $this->attributes['id'], RentOrder::class);
  167. $location['lat'] = $order['end_use_bike_location']['latitude'];
  168. $location['lng'] = $order['end_use_bike_location']['longitude'];
  169. (new BikeStatusInfoSyncHandler())->toBikeWaitRideStatus($order['bike_no'], $location['lng'], $location['lat']);
  170. return true;
  171. }
  172. /**
  173. * 退款回调
  174. * User: Mead
  175. */
  176. public function refund_order_callback()
  177. {
  178. return 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', RentOrder::class)->update([
  179. 'status' => 1
  180. ]);
  181. }
  182. }