Order.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 RIDING_CARD_OK = 1;
  54. const RIDING_CARD_NO = 0;
  55. public static $ridingCardMaps = [
  56. self::RIDING_CARD_NO => '未使用骑行卡',
  57. self::RIDING_CARD_OK => '已使用骑行卡',
  58. ];
  59. const PREFERENTIAL_NO = 0;
  60. const PREFERENTIAL_CARD_RIDING = 1;
  61. const PREFERENTIAL_COUPON = 2;
  62. const PREFERENTIAL_FREE_MINUTE = 3;
  63. public static $preferentialTypeMaps = [
  64. self::PREFERENTIAL_NO => '无优惠',
  65. self::PREFERENTIAL_CARD_RIDING => '骑行卡优惠',
  66. self::PREFERENTIAL_COUPON => '优惠券优惠',
  67. self::PREFERENTIAL_FREE_MINUTE => '时长优惠',
  68. ];
  69. const PAY_TYPE_NO = 0;
  70. const PAY_TYPE_WECHAT = 1;
  71. const PAY_TYPE_ACCOUNT = 2;
  72. public static $payTypeMaps = [
  73. self::PAY_TYPE_NO => '待支付',
  74. self::PAY_TYPE_WECHAT => '微信支付',
  75. self::PAY_TYPE_ACCOUNT => '余额支付'
  76. ];
  77. const IS_ADMIN_SETTLE_ORDER_USER = 0;
  78. const IS_ADMIN_SETTLE_ORDER_ADMIN = 1;
  79. const IS_ADMIN_SETTLE_ORDER_SYSTEM = 2;
  80. public static $isAdminSettleOrderMaps = [
  81. self::IS_ADMIN_SETTLE_ORDER_USER => '用户结算',
  82. self::IS_ADMIN_SETTLE_ORDER_ADMIN => '管理员结算',
  83. self::IS_ADMIN_SETTLE_ORDER_SYSTEM => '系统自动结算',
  84. ];
  85. const TRADE_STATE_SUCCESS = 'SUCCESS';
  86. const TRADE_STATE_REFUND = 'REFUND';
  87. const TRADE_STATE_NOTPAY = 'NOTPAY';
  88. const TRADE_STATE_CLOSED = 'CLOSED';
  89. const TRADE_STATE_REVOKED = 'REVOKED';
  90. const TRADE_STATE_USERPAYING = 'USERPAYING';
  91. const TRADE_STATE_PAYERROR = 'PAYERROR';
  92. public static $tradeStateMaps = [
  93. self::TRADE_STATE_SUCCESS => '支付成功',
  94. self::TRADE_STATE_REFUND => '转入退款',
  95. self::TRADE_STATE_NOTPAY => '未支付',
  96. self::TRADE_STATE_CLOSED => '已关闭',
  97. self::TRADE_STATE_REVOKED => '已撤销(刷卡支付)',
  98. self::TRADE_STATE_USERPAYING => '用户支付中',
  99. self::TRADE_STATE_PAYERROR => '支付失败(其他原因,如银行返回失败)',
  100. ];
  101. public function bike()
  102. {
  103. return $this->belongsTo(Bike::class);
  104. }
  105. public function getUseBikeTimeLengthTextAttribute()
  106. {
  107. if ($this->attributes['use_bike_time_length']) {
  108. return format_minutes($this->attributes['use_bike_time_length']);
  109. }
  110. return '暂无';
  111. }
  112. public function getPauseBikeTimeLengthTextAttribute()
  113. {
  114. if ($this->attributes['pause_bike_time_length']) {
  115. return format_minutes($this->attributes['use_bike_time_length']);
  116. }
  117. return '暂无';
  118. }
  119. public function getUseBikeDistanceLengthTextAttribute()
  120. {
  121. if ($this->attributes['use_bike_distance_length']) {
  122. return $this->attributes['use_bike_distance_length'] . 'km';
  123. }
  124. return '暂无';
  125. }
  126. public function getEndUseBikeTimeTimestampAttribute()
  127. {
  128. return $this->end_use_bike_time->timestamp;
  129. }
  130. public function setPreferentialTypeAttribute($value)
  131. {
  132. $this->attributes['preferential_type'] = '-' . $value . '-';
  133. }
  134. public function getPreferentialTypeAttribute()
  135. {
  136. return trim($this->attributes['preferential_type'], '-');
  137. }
  138. public function getPreferentialTypeNameAttribute()
  139. {
  140. if ($this->attributes['preferential_type'] == '-0-') {
  141. return '无优惠';
  142. } else {
  143. $str = '';
  144. $preferential_type_arr = str2arr(trim($this->attributes['preferential_type'], '-'), '-');
  145. foreach ($preferential_type_arr as $v) {
  146. $str .= self::$preferentialTypeMaps[$v] . ',';
  147. }
  148. return rtrim($str, ',');
  149. }
  150. }
  151. // public function locations()
  152. // {
  153. // return $this->hasMany(LocationLogMongodb::class);
  154. // }
  155. /**
  156. * 生成订单号
  157. * @return bool|string
  158. * User: Mead
  159. */
  160. public static function makeNo()
  161. {
  162. // 订单流水号前缀
  163. $prefix = config('bike.no_tag') . self::NO_TAG . date('YmdHis');
  164. for ($i = 0; $i < 10; $i++) {
  165. // 随机生成 6 位的数字
  166. $no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
  167. // 判断是否已经存在
  168. if (!static::query()->where('no', $no)->exists()) {
  169. return $no;
  170. }
  171. }
  172. Log::warning('find order no failed');
  173. return false;
  174. }
  175. public function pay_order_callback()
  176. {
  177. $order = $this->attributes;
  178. //添加钱包记录
  179. 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);
  180. 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);
  181. //修改订单记录
  182. $this->pay_status = Order::PAY_STATUS_OK;
  183. $this->pay_time = now();
  184. $this->pay_type = Order::PAY_TYPE_WECHAT;
  185. $this->status = Order::STATUS_COMPLETE_ORDER;
  186. if ($this->attributes['is_coupon'] == self::COUPON_OK) {
  187. CouponsUserBag::useCoupon($this->attributes['id'], $this->attributes['coupon_user_bags_id']);
  188. //优惠方式
  189. if ($this->preferential_type == 0) {
  190. $this->preferential_type = arr2str([Order::PREFERENTIAL_COUPON], '-');
  191. } else {
  192. $arr = str2arr($this->preferential_type, '-');
  193. $this->preferential_type = arr2str(array_merge($arr, [Order::PREFERENTIAL_COUPON]), '-');
  194. }
  195. // 更新订单得优惠金额
  196. $this->preferential_money = bcsub(bcadd($this->attributes['time_money'], $this->attributes['dispatch_money'], 2), $this->attributes['pay_money'], 2);
  197. // 更新优惠券优惠金额
  198. $this->coupon_preferential_money = bcsub($this->attributes['preferential_money'], $this->attributes['card_preferential_money'], 2);
  199. }
  200. $this->save();
  201. return true;
  202. }
  203. /**
  204. * 退款回调
  205. * User: Mead
  206. */
  207. public function refund_order_callback()
  208. {
  209. 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([
  210. 'status' => 1
  211. ]);
  212. }
  213. }