Order.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\admin\model\shopro\trade;
  3. use app\admin\model\shopro\Common;
  4. use traits\model\SoftDelete;
  5. use app\admin\model\shopro\user\User;
  6. use app\admin\model\shopro\Pay as PayModel;
  7. class Order extends Common
  8. {
  9. use SoftDelete;
  10. protected $name = 'shopro_trade_order';
  11. protected $deleteTime = 'deletetime';
  12. protected $type = [
  13. 'ext' => 'json',
  14. 'paid_time' => 'timestamp',
  15. ];
  16. // 追加属性
  17. protected $append = [
  18. 'type_text',
  19. 'status_text',
  20. 'platform_text',
  21. ];
  22. // 订单状态
  23. const STATUS_CLOSED = 'closed';
  24. const STATUS_CANCEL = 'cancel';
  25. const STATUS_UNPAID = 'unpaid';
  26. const STATUS_PAID = 'paid';
  27. const STATUS_COMPLETED = 'completed';
  28. public function statusList()
  29. {
  30. return [
  31. 'closed' => '交易关闭',
  32. 'cancel' => '已取消',
  33. 'unpaid' => '未支付',
  34. 'paid' => '已支付',
  35. 'completed' => '已完成'
  36. ];
  37. }
  38. /**
  39. * 订单列表状态搜索
  40. */
  41. public function searchStatusList()
  42. {
  43. return [
  44. 'unpaid' => '待付款',
  45. 'paid' => '已支付', // 包括刚支付的,以及已完成的所有付过款的订单
  46. 'completed' => '已完成',
  47. 'cancel' => '已取消',
  48. 'closed' => '交易关闭',
  49. ];
  50. }
  51. public function typeList()
  52. {
  53. return [
  54. 'recharge' => '充值订单',
  55. ];
  56. }
  57. public function platformList()
  58. {
  59. return [
  60. 'H5' => 'H5',
  61. 'WechatOfficialAccount' => '微信公众号',
  62. 'WechatMiniProgram' => '微信小程序',
  63. 'App' => 'App',
  64. ];
  65. }
  66. public function getPlatformTextAttr($value, $data)
  67. {
  68. $value = $value ?: ($data['platform'] ?? null);
  69. $list = $this->platformList();
  70. return isset($list[$value]) ? $list[$value] : '';
  71. }
  72. /**
  73. * 已支付订单,支付类型
  74. *
  75. * @param string $value
  76. * @param array $data
  77. * @return void
  78. */
  79. public function getPayTypeAttr($value, $data)
  80. {
  81. $status = $data['status'] ?? '';
  82. $payTypes = [];
  83. if (in_array($status, [self::STATUS_PAID, self::STATUS_COMPLETED])) {
  84. $payTypes = PayModel::typeTradeOrder()->where('order_id', $data['id'])->where('status', '<>', PayModel::PAY_STATUS_UNPAID)->group('pay_type')->column('pay_type');
  85. }
  86. return $payTypes[0] ?? '';
  87. }
  88. /**
  89. * 已支付订单,支付类型文字
  90. *
  91. * @param string $value
  92. * @param array $data
  93. * @return void
  94. */
  95. public function getPayTypeTextAttr($value, $data)
  96. {
  97. $pay_types = $this->pay_type;
  98. $list = (new PayModel)->payTypeList();
  99. return isset($list[$pay_types]) ? $list[$pay_types] : '';
  100. }
  101. // 已关闭
  102. public function scopeClosed($query)
  103. {
  104. return $query->where('status', Order::STATUS_CLOSED);
  105. }
  106. // 已取消
  107. public function scopeCancel($query)
  108. {
  109. return $query->where('status', Order::STATUS_CANCEL);
  110. }
  111. // 未支付
  112. public function scopeUnpaid($query)
  113. {
  114. return $query->where('status', Order::STATUS_UNPAID);
  115. }
  116. // 已支付
  117. public function scopePaid($query)
  118. {
  119. return $query->whereIn('status', [Order::STATUS_PAID, Order::STATUS_COMPLETED]);
  120. }
  121. // 已完成
  122. public function scopeCompleted($query)
  123. {
  124. return $query->where('status', Order::STATUS_COMPLETED);
  125. }
  126. public function scopeRecharge($query)
  127. {
  128. return $query->where('type', 'recharge');
  129. }
  130. public function user()
  131. {
  132. return $this->belongsTo(User::class, 'user_id', 'id');
  133. }
  134. public function pays()
  135. {
  136. return $this->hasMany(PayModel::class, 'order_id', 'id')->typeTradeOrder();
  137. }
  138. }