Order.php 4.1 KB

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