RentOrder.php 7.0 KB

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