You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
3.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
  1. <?php
  2. namespace App\Model\v3;
  3. use App\Constants\v3\OrderState;
  4. use App\Constants\v3\Payment;
  5. use App\Constants\v3\Shipping;
  6. use App\Model\Model;
  7. use Hyperf\Database\Model\SoftDeletes;
  8. class OrderMain extends Model
  9. {
  10. use SoftDeletes;
  11. protected $table = 'lanzu_order_main';
  12. protected $guarded = [];
  13. // protected $fillable = [
  14. // 'market_id',
  15. // 'order_num',
  16. // 'global_order_id',
  17. // 'user_id',
  18. // 'pay_type',
  19. // 'type',
  20. // 'order_type',
  21. // 'shipping_type',
  22. // 'money',
  23. // 'total_money',
  24. // 'services_money',
  25. // 'coupon_money',
  26. // 'delivery_money',
  27. // 'state',
  28. // 'pay_time',
  29. // 'receive_time',
  30. // 'delivery_time',
  31. // 'complete_time',
  32. // 'cancel_time',
  33. // 'refund_time',
  34. // 'tel',
  35. // 'address',
  36. // 'area',
  37. // 'lat',
  38. // 'lng',
  39. // 'name',
  40. // 'print_num',
  41. // 'plat',
  42. // 'refuse_refund_note',
  43. // 'delivery_time_note',
  44. // 'total_refund_note',
  45. // 'note',
  46. // 'delivery_distance'
  47. // ];
  48. protected $appends = [
  49. 'created_at_text',
  50. 'updated_at_text',
  51. 'state_text',
  52. 'pay_time_text',
  53. 'pay_type_text',
  54. 'shipping_type_text',
  55. 'complete_time_text'
  56. ];
  57. protected $casts = [
  58. 'global_order_id' => 'string'
  59. ];
  60. public function getCreatedAtTextAttribute()
  61. {
  62. return date('Y-m-d H:i:s', $this->attributes['created_at']);
  63. }
  64. public function getUpdatedAtTextAttribute()
  65. {
  66. return date('Y-m-d H:i:s', $this->attributes['updated_at']);
  67. }
  68. public function getPayTimeTextAttribute()
  69. {
  70. return date('Y-m-d H:i:s', $this->attributes['pay_time']);
  71. }
  72. public function getCompleteTimeTextAttribute()
  73. {
  74. return date('Y-m-d H:i:s', $this->attributes['complete_time']);
  75. }
  76. public function getStateTextAttribute()
  77. {
  78. if ($this->attributes['state'] == OrderState::DELIVERY) {
  79. if($this->attributes['shipping_type'] == 3) {
  80. return '待自提';
  81. }elseif ($this->attributes['shipping_type'] == 1 && empty($this->attributes['horseman_id'])){
  82. return '已接单';
  83. }
  84. }
  85. return OrderState::getMessage($this->attributes['state']);
  86. }
  87. public function getPayTypeTextAttribute()
  88. {
  89. return Payment::getMessage($this->attributes['pay_type']);
  90. }
  91. public function getShippingTypeTextAttribute()
  92. {
  93. return Shipping::getMessage($this->attributes['shipping_type']);
  94. }
  95. public function getNoteAttribute($value)
  96. {
  97. if ($this->attributes['refuse_refund_note'] || $this->attributes['total_refund_note']) {
  98. return $this->attributes['refuse_refund_note'] ?: $this->attributes['total_refund_note'];
  99. }
  100. return $value;
  101. }
  102. public function market()
  103. {
  104. return $this->belongsTo(Market::class, 'market_id', 'id');
  105. }
  106. public function orders()
  107. {
  108. return $this->hasMany(Order::class, 'order_main_id', 'global_order_id');
  109. }
  110. public function orderGoods()
  111. {
  112. // firstKey是中间表联当前表的列,secondKey是远程表对应中间表的列,localKey是当前表关联中间表的列,secondLocalKey是中间表关联远程表的列
  113. return $this->hasManyThrough(
  114. OrderGoods::class,
  115. Order::class,
  116. 'order_main_id',
  117. 'order_id',
  118. 'global_order_id',
  119. 'id'
  120. );
  121. }
  122. public function user()
  123. {
  124. return $this->belongsTo(User::class, 'user_id', 'id');
  125. }
  126. public function employees()
  127. {
  128. return $this->belongsTo(Employees::class, 'horseman_id', 'id');
  129. }
  130. }