Order.php 4.1 KB

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