Order.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/4/6
  6. * Time: 下午4:35
  7. */
  8. namespace app\frontend\models;
  9. use Carbon\Carbon;
  10. use Illuminate\Database\Eloquent\Builder;
  11. /**
  12. * Class Order
  13. * @package app\frontend\models
  14. * @property Member belongsToMember
  15. * @property Carbon finish_time
  16. * @property int refund_id
  17. */
  18. class Order extends \app\common\models\Order
  19. {
  20. protected $appends = ['dispatch_type_name', 'status_name', 'pay_type_name', 'button_models'];
  21. protected $hidden = [
  22. 'uniacid',
  23. 'is_deleted',
  24. 'is_member_deleted',
  25. // 'create_time',
  26. // 'pay_time',
  27. // 'send_time',
  28. // 'finish_time',
  29. 'uid',
  30. 'cancel_time',
  31. 'created_at',
  32. 'updated_at',
  33. 'deleted_at',
  34. 'hasOneDispatchType',
  35. ];
  36. public function scopeDetail($query){
  37. return $query->with(['hasManyOrderGoods'=>function($query){
  38. return $query->detail();
  39. }])->select(['id','uid','order_sn','price','goods_price','create_time','finish_time','pay_time','send_time','cancel_time','dispatch_type_id','pay_type_id','status','refund_id','dispatch_price','deduction_price']);
  40. }
  41. public function scopeKeywordSearch($query, $keyword)
  42. {
  43. if (empty($keyword)) {
  44. return $query;
  45. }
  46. $query->leftJoin('yz_order_address','yz_order_address.order_id', '=', 'yz_order.id');
  47. $query->join('yz_order_goods','yz_order_goods.order_id', '=', 'yz_order.id');
  48. $query->where(function ($where) use ($keyword) {
  49. return $where->where('yz_order_address.mobile', 'like', '%' . $keyword . '%')
  50. ->orWhere('yz_order_address.realname', 'like', '%' . $keyword . '%')
  51. ->orWhere('yz_order_goods.title', 'like', '%' . $keyword . '%')
  52. ->orWhere('yz_order.order_sn','like',"%{$keyword}%");
  53. });
  54. //$query->addSelect('yz_order.*');
  55. return $query;
  56. }
  57. /**
  58. * 订单列表
  59. * @return $this
  60. */
  61. public function scopeOrders($query)
  62. {
  63. return $query->with([
  64. 'hasManyOrderGoods'=> function($query){
  65. return $query->with(['hasOneComment' => function($comment_query){
  66. $comment_query->select('id','order_id','goods_id','level','content');
  67. }])->select(['id','order_id','goods_id', 'refund_id', 'is_refund','goods_price','total','price', 'payment_amount','thumb','title','goods_option_id','goods_option_title','comment_status','comment_id','vip_price']);
  68. },
  69. 'hasOnePayType',
  70. 'process',
  71. 'address' => self::addressBuilder(),
  72. ])->orderBy('yz_order.id','desc');
  73. }
  74. private static function addressBuilder()
  75. {
  76. return function ($query) {
  77. return $query->select('address', 'mobile', 'realname', 'order_id');
  78. };
  79. }
  80. public function getDispatchTypeNameAttribute()
  81. {
  82. $name = $this->hasOneDispatchType ? $this->hasOneDispatchType->name : '';
  83. if ($name == '快递') {
  84. $name = \Setting::get('shop.lang.zh_cn.order.express')?:'快递';
  85. }
  86. return $name;
  87. }
  88. public function belongsToMember()
  89. {
  90. return $this->belongsTo(app('OrderManager')->make('Member'), 'uid', 'uid');
  91. }
  92. public function belongsToOrderGoods()
  93. {
  94. return $this->belongsTo(app('OrderManager')->make('OrderGoods'), 'id', 'order_id');
  95. }
  96. public function orderGoodsBuilder($status)
  97. {
  98. $operator = [];
  99. if ($status == 0) {
  100. $operator['operator'] = '=';
  101. $operator['status'] = 0;
  102. } else {
  103. $operator['operator'] = '>';
  104. $operator['status'] = 0;
  105. }
  106. return function ($query) use ($operator) {
  107. return $query->with('hasOneComment')->where('comment_status', $operator['operator'], $operator['status']);
  108. };
  109. }
  110. public static function getMyCommentList($status)
  111. {
  112. $operator = [];
  113. if ($status == 0) {
  114. $operator['operator'] = '=';
  115. $operator['status'] = 0;
  116. } else {
  117. $operator['operator'] = '>';
  118. $operator['status'] = 0;
  119. }
  120. return self::whereHas('hasManyOrderGoods', function($query) use ($operator){
  121. return $query->where('comment_status', $operator['operator'], $operator['status']);
  122. })
  123. ->with([
  124. 'hasManyOrderGoods' => self::orderGoodsBuilder($status)
  125. ])->where('status', 3)->orderBy('id', 'desc')->get();
  126. }
  127. public static function getMyCommentListPaginate($status,$page,$pageSize)
  128. {
  129. $operator = [];
  130. if ($status == 0) {
  131. $operator['operator'] = '=';
  132. $operator['status'] = 0;
  133. } else {
  134. $operator['operator'] = '>';
  135. $operator['status'] = 0;
  136. }
  137. return self::whereHas('hasManyOrderGoods', function($query) use ($operator){
  138. return $query->where('comment_status', $operator['operator'], $operator['status']);
  139. })
  140. ->with([
  141. 'hasManyOrderGoods' => self::orderGoodsBuilder($status)
  142. ])->where('status', 3)->orderBy('id', 'desc')->paginate($pageSize,['*'],'page',$page);
  143. }
  144. /**
  145. * 关系链 指定商品
  146. *
  147. * @param $uid
  148. * @return \Illuminate\Database\Eloquent\Collection|static[]
  149. */
  150. public static function getOrderListByUid($uid)
  151. {
  152. return self::select(['*'])
  153. ->where('status','>=',1)
  154. ->where('status','<=',3)
  155. ->with(['hasManyOrderGoods'=>function($query){
  156. return $query->select(['*']);
  157. }])
  158. ->get();
  159. }
  160. public static function boot()
  161. {
  162. parent::boot();
  163. self::addGlobalScope(function(Builder $query){
  164. return $query->uid();
  165. });
  166. }
  167. /**
  168. * @return array
  169. * @throws \app\common\exceptions\AppException
  170. */
  171. public function getOperationsSetting()
  172. {
  173. if (Member::current()->uid == $this->uid) {
  174. $operationsSettings = \app\common\modules\shop\OrderFrontendButtonConfig::current()->get('order_frontend_button');
  175. $operations = array_values(collect($operationsSettings)->where('plugin_id',$this->plugin_id)->sortByDesc('weight')->all());
  176. if (empty($operations)) {//没找到用plugin_id为0的
  177. $operations = array_values(collect($operationsSettings)->where('plugin_id',0)->sortByDesc('weight')->all());
  178. }
  179. return $operations;
  180. // //return app('OrderManager')->setting('member_order_operations')[$this->statusCode] ?: [];
  181. //
  182. // $arr = app('OrderManager')->setting('member_order_operations')[$this->statusCode] ?: [];
  183. //
  184. // if ($this->statusCode == 'waitSend' && !in_array('app\frontend\modules\order\operations\member\ExpeditingDelivery',$arr) && in_array($this->plugin_id,[0,92])) {
  185. // array_push($arr,'app\frontend\modules\order\operations\member\ExpeditingDelivery');
  186. // }
  187. // return $arr;
  188. }
  189. return [];
  190. }
  191. /**
  192. * 订单状态汉字 (因为前端不想显示锁定字样,所以在前端模型重写这个)
  193. * @return string
  194. */
  195. public function getStatusNameAttribute()
  196. {
  197. if (!$this->isClose() && $this->currentProcess()) {
  198. return $this->currentProcess()->status_name;
  199. }
  200. $statusName = $this->getStatusService()->getStatusName();
  201. // if ($this->isPending()) {
  202. // $statusName .= ' : 锁定';
  203. // }
  204. return $statusName;
  205. }
  206. }