Order.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Models;
  3. use Auth;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Database\Eloquent\Model;
  6. /**
  7. * 订单
  8. *
  9. * @property int $oid
  10. * @property string $order_sn 订单编号
  11. * @property int $user_id 操作人
  12. * @property int $goods_id 商品ID
  13. * @property int $coupon_id 优惠券ID
  14. * @property int $origin_amount 订单原始总价,单位分
  15. * @property int $amount 订单总价,单位分
  16. * @property string|null $expire_at 过期时间
  17. * @property int $is_expire 是否已过期:0-未过期、1-已过期
  18. * @property string $pay_way 支付方式:balance、f2fpay、codepay、payjs、bitpayx等
  19. * @property int $status 订单状态:-1-已关闭、0-待支付、1-已支付待确认、2-已完成
  20. * @property \Illuminate\Support\Carbon|null $created_at 创建时间
  21. * @property \Illuminate\Support\Carbon|null $updated_at 最后一次更新时间
  22. * @property-read \App\Models\Coupon|null $coupon
  23. * @property-read mixed $pay_way_label
  24. * @property-read mixed $status_label
  25. * @property-read \App\Models\Goods|null $goods
  26. * @property-read \App\Models\Payment|null $payment
  27. * @property-read \App\Models\User|null $user
  28. * @method static Builder|Order newModelQuery()
  29. * @method static Builder|Order newQuery()
  30. * @method static Builder|Order query()
  31. * @method static Builder|Order uid()
  32. * @method static Builder|Order whereAmount($value)
  33. * @method static Builder|Order whereCouponId($value)
  34. * @method static Builder|Order whereCreatedAt($value)
  35. * @method static Builder|Order whereExpireAt($value)
  36. * @method static Builder|Order whereGoodsId($value)
  37. * @method static Builder|Order whereIsExpire($value)
  38. * @method static Builder|Order whereOid($value)
  39. * @method static Builder|Order whereOrderSn($value)
  40. * @method static Builder|Order whereOriginAmount($value)
  41. * @method static Builder|Order wherePayWay($value)
  42. * @method static Builder|Order whereStatus($value)
  43. * @method static Builder|Order whereUpdatedAt($value)
  44. * @method static Builder|Order whereUserId($value)
  45. * @mixin \Eloquent
  46. */
  47. class Order extends Model {
  48. protected $table = 'order';
  49. protected $primaryKey = 'oid';
  50. protected $appends = ['status_label'];
  51. function scopeUid($query) {
  52. return $query->whereUserId(Auth::id());
  53. }
  54. function user() {
  55. return $this->hasOne(User::class, 'id', 'user_id');
  56. }
  57. function goods() {
  58. return $this->hasOne(Goods::class, 'id', 'goods_id')->withTrashed();
  59. }
  60. function coupon() {
  61. return $this->hasOne(Coupon::class, 'id', 'coupon_id')->withTrashed();
  62. }
  63. function payment() {
  64. return $this->hasOne(Payment::class, 'oid', 'oid');
  65. }
  66. // 订单状态
  67. function getStatusLabelAttribute() {
  68. switch($this->attributes['status']){
  69. case -1:
  70. $status_label = trans('home.invoice_status_closed');
  71. break;
  72. case 1:
  73. $status_label = trans('home.invoice_status_wait_confirm');
  74. break;
  75. case 2:
  76. $status_label = trans('home.invoice_status_payment_confirm');
  77. break;
  78. case 0:
  79. $status_label = trans('home.invoice_status_wait_payment');
  80. break;
  81. default:
  82. $status_label = 'Unknown';
  83. }
  84. return $status_label;
  85. }
  86. function getOriginAmountAttribute($value) {
  87. return $value / 100;
  88. }
  89. function setOriginAmountAttribute($value) {
  90. return $this->attributes['origin_amount'] = $value * 100;
  91. }
  92. function getAmountAttribute($value) {
  93. return $value / 100;
  94. }
  95. function setAmountAttribute($value) {
  96. return $this->attributes['amount'] = $value * 100;
  97. }
  98. // 支付方式
  99. function getPayWayLabelAttribute() {
  100. switch($this->attributes['pay_way']){
  101. case 'credit':
  102. $pay_way_label = '余额';
  103. break;
  104. case 'youzan':
  105. $pay_way_label = '有赞云';
  106. break;
  107. case 'f2fpay':
  108. $pay_way_label = '支付宝当面付';
  109. break;
  110. case 'codepay':
  111. $pay_way_label = '码支付';
  112. break;
  113. case 'payjs':
  114. $pay_way_label = 'PayJs';
  115. break;
  116. case 'bitpayx':
  117. $pay_way_label = '麻瓜宝';
  118. break;
  119. case 'paypal':
  120. $pay_way_label = 'PayPal';
  121. break;
  122. default:
  123. $pay_way_label = '未知';
  124. }
  125. return $pay_way_label;
  126. }
  127. }