Order.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Support\Facades\Log;
  4. class Order extends Model
  5. {
  6. protected $guarded = [];
  7. protected $hidden = [
  8. 'id',
  9. 'bike_id',
  10. 'user_id',
  11. 'pause_bike_time',
  12. 'updated_at',
  13. 'pay_time'
  14. ];
  15. protected $dates = ['start_use_bike_time', 'end_use_bike_time'];
  16. // protected $appends = ['use_bike_time_length_text', 'pause_bike_time_length_text', 'use_bike_distance_length_text'];
  17. protected $casts = [
  18. 'start_use_bike_location' => 'array',
  19. 'end_use_bike_location' => 'array',
  20. ];
  21. const REDIS_BIKE_LOCATION_TAG = 'bike_locations';
  22. const NO_TAG = 'O';
  23. // 骑行中
  24. const STATUS_RIDE_BIKE = 1;
  25. // 骑车结束,待支付
  26. const STATUS_CLOSE_BIKE = 2;
  27. // 订单完成
  28. const STATUS_COMPLETE_ORDER = 3;
  29. // 订单关闭(不够计费直接关闭)
  30. const STATUS_CLOSE_ORDER = 4;
  31. // 临时停车中
  32. const STATUS_PAUSE_BIKE = 0;
  33. public static $statusMaps = [
  34. self::STATUS_PAUSE_BIKE => '临时停车',
  35. self::STATUS_RIDE_BIKE => '骑行中',
  36. self::STATUS_CLOSE_BIKE => '骑行结束,待支付',
  37. self::STATUS_COMPLETE_ORDER => '已完成',
  38. self::STATUS_CLOSE_ORDER => '订单关闭'
  39. ];
  40. const PAY_STATUS_OK = 1;
  41. const PAY_STATUS_NO = 0;
  42. public static $payStatusMaps = [
  43. self::PAY_STATUS_NO => '未支付',
  44. self::PAY_STATUS_OK => '已支付'
  45. ];
  46. const COUPON_OK = 1;
  47. const COUPON_NO = 0;
  48. public static $couponMaps = [
  49. self::COUPON_NO => '未使用优惠券',
  50. self::COUPON_OK => '使用优惠券'
  51. ];
  52. const RIDING_CARD_OK = 1;
  53. const RIDING_CARD_NO = 0;
  54. public static $ridingCardMaps = [
  55. self::RIDING_CARD_NO => '未使用骑行卡',
  56. self::RIDING_CARD_OK => '已使用骑行卡',
  57. ];
  58. const PREFERENTIAL_NO = 0;
  59. const PREFERENTIAL_CARD_RIDING = 1;
  60. const PREFERENTIAL_COUPON = 2;
  61. const PREFERENTIAL_FREE_MINUTE = 3;
  62. public static $preferentialTypeMaps = [
  63. self::PREFERENTIAL_NO => '无优惠',
  64. self::PREFERENTIAL_CARD_RIDING => '骑行卡优惠',
  65. self::PREFERENTIAL_COUPON => '优惠券优惠',
  66. self::PREFERENTIAL_FREE_MINUTE => '时长优惠',
  67. ];
  68. const PAY_TYPE_NO = 0;
  69. const PAY_TYPE_WECHAT = 1;
  70. const PAY_TYPE_ACCOUNT = 2;
  71. const PAY_TYPE_ALIPAYMINI = 3;
  72. public static $payTypeMaps = [
  73. self::PAY_TYPE_NO => '待支付',
  74. self::PAY_TYPE_WECHAT => '微信支付',
  75. self::PAY_TYPE_ACCOUNT => '余额支付',
  76. self::PAY_TYPE_ALIPAYMINI=> '支付宝支付',
  77. ];
  78. const IS_ADMIN_SETTLE_ORDER_USER = 0;
  79. const IS_ADMIN_SETTLE_ORDER_ADMIN = 1;
  80. const IS_ADMIN_SETTLE_ORDER_SYSTEM = 2;
  81. public static $isAdminSettleOrderMaps = [
  82. self::IS_ADMIN_SETTLE_ORDER_USER => '用户结算',
  83. self::IS_ADMIN_SETTLE_ORDER_ADMIN => '管理员结算',
  84. self::IS_ADMIN_SETTLE_ORDER_SYSTEM => '系统自动结算',
  85. ];
  86. const TRADE_STATE_SUCCESS = 'SUCCESS';
  87. const TRADE_STATE_REFUND = 'REFUND';
  88. const TRADE_STATE_NOTPAY = 'NOTPAY';
  89. const TRADE_STATE_CLOSED = 'CLOSED';
  90. const TRADE_STATE_REVOKED = 'REVOKED';
  91. const TRADE_STATE_USERPAYING = 'USERPAYING';
  92. const TRADE_STATE_PAYERROR = 'PAYERROR';
  93. public static $tradeStateMaps = [
  94. self::TRADE_STATE_SUCCESS => '支付成功',
  95. self::TRADE_STATE_REFUND => '转入退款',
  96. self::TRADE_STATE_NOTPAY => '未支付',
  97. self::TRADE_STATE_CLOSED => '已关闭',
  98. self::TRADE_STATE_REVOKED => '已撤销(刷卡支付)',
  99. self::TRADE_STATE_USERPAYING => '用户支付中',
  100. self::TRADE_STATE_PAYERROR => '支付失败(其他原因,如银行返回失败)',
  101. ];
  102. public function bike()
  103. {
  104. return $this->belongsTo(Bike::class);
  105. }
  106. public function getUseBikeTimeLengthTextAttribute()
  107. {
  108. if ($this->attributes['use_bike_time_length']) {
  109. return format_minutes($this->attributes['use_bike_time_length']);
  110. }
  111. return '暂无';
  112. }
  113. public function getPauseBikeTimeLengthTextAttribute()
  114. {
  115. if ($this->attributes['pause_bike_time_length']) {
  116. return format_minutes($this->attributes['use_bike_time_length']);
  117. }
  118. return '暂无';
  119. }
  120. public function getUseBikeDistanceLengthTextAttribute()
  121. {
  122. if ($this->attributes['use_bike_distance_length']) {
  123. return $this->attributes['use_bike_distance_length'] . 'km';
  124. }
  125. return '暂无';
  126. }
  127. public function getEndUseBikeTimeTimestampAttribute()
  128. {
  129. return $this->end_use_bike_time->timestamp;
  130. }
  131. public function setPreferentialTypeAttribute($value)
  132. {
  133. $this->attributes['preferential_type'] = '-' . $value . '-';
  134. }
  135. public function getPreferentialTypeAttribute()
  136. {
  137. return trim($this->attributes['preferential_type'], '-');
  138. }
  139. public function getPreferentialTypeNameAttribute()
  140. {
  141. if ($this->attributes['preferential_type'] == '-0-') {
  142. return '无优惠';
  143. } else {
  144. $str = '';
  145. $preferential_type_arr = str2arr(trim($this->attributes['preferential_type'], '-'), '-');
  146. foreach ($preferential_type_arr as $v) {
  147. $str .= self::$preferentialTypeMaps[$v] . ',';
  148. }
  149. return rtrim($str, ',');
  150. }
  151. }
  152. // public function locations()
  153. // {
  154. // return $this->hasMany(LocationLogMongodb::class);
  155. // }
  156. /**
  157. * 生成订单号
  158. * @return bool|string
  159. * User: Mead
  160. */
  161. public static function makeNo($tag = null)
  162. {
  163. // 订单流水号前缀
  164. $prefix = $tag . self::NO_TAG . date('YmdHis');
  165. for ($i = 0; $i < 10; $i++) {
  166. // 随机生成 6 位的数字
  167. $no = $prefix . str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
  168. // 判断是否已经存在
  169. if (!static::query()->where('no', $no)->exists()) {
  170. return $no;
  171. }
  172. }
  173. Log::warning('find order no failed');
  174. return false;
  175. }
  176. public function pay_order_callback()
  177. {
  178. $order = $this->attributes;
  179. //添加钱包记录
  180. 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);
  181. 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);
  182. //修改订单记录
  183. // $this->pay_status = Order::PAY_STATUS_OK;
  184. // $this->pay_time = now();
  185. // $this->pay_type = Order::PAY_TYPE_WECHAT;
  186. $this->status = Order::STATUS_COMPLETE_ORDER;
  187. if ($this->attributes['is_coupon'] == self::COUPON_OK) {
  188. CouponsUserBag::useCoupon($this->attributes['id'], $this->attributes['coupon_user_bags_id']);
  189. //优惠方式
  190. if ($this->preferential_type == 0) {
  191. $this->preferential_type = arr2str([Order::PREFERENTIAL_COUPON], '-');
  192. } else {
  193. $arr = str2arr($this->preferential_type, '-');
  194. $this->preferential_type = arr2str(array_merge($arr, [Order::PREFERENTIAL_COUPON]), '-');
  195. }
  196. // 更新订单得优惠金额
  197. $this->preferential_money = bcsub(bcadd($this->attributes['time_money'], $this->attributes['dispatch_money'], 2), $this->attributes['pay_money'], 2);
  198. // 更新优惠券优惠金额
  199. $this->coupon_preferential_money = bcsub($this->attributes['preferential_money'], $this->attributes['card_preferential_money'], 2);
  200. }
  201. $this->save();
  202. return true;
  203. }
  204. /**
  205. * 退款回调
  206. * User: Mead
  207. */
  208. public function refund_order_callback()
  209. {
  210. 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([
  211. 'status' => 1
  212. ]);
  213. }
  214. }