UserOrder.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. <?php
  2. namespace app\common\model;
  3. use app\service\pay_user\PayUser;
  4. use think\Db;
  5. use think\db\Query;
  6. use think\Model;
  7. use think\model\Collection;
  8. use traits\model\SoftDelete;
  9. /**
  10. *@property array send_detail 运送详情
  11. *@property float cal_refund 退款手续费
  12. *@property float refund_money 退款金额
  13. *@property Collection currentSenderOrder
  14. *@property float settle_amount
  15. *@property array senders
  16. *@property array settle_info
  17. *@property float profit
  18. *@property boolean is_same_user
  19. *@property User user
  20. *@method Query|self expired()
  21. *@method Query|self show()
  22. *@method Query|self service()()
  23. *@method Query|self waitGet()
  24. *@method Query|self running($from='')
  25. *@method Query|self success()
  26. *@method Query|self settled()
  27. *@method Query|self canceled()
  28. *@method Query|self completed()
  29. *@method Query|self caled()
  30. *@method Query|self profit()
  31. *@method Query|self over()
  32. *@method Query|self turnover($date='today')
  33. */
  34. class UserOrder extends Model
  35. {
  36. //use SoftDelete;
  37. protected $hidden=[
  38. 'status_bak',
  39. 'deleted_at',
  40. ];
  41. protected $autoWriteTimestamp=true;
  42. protected $createTime = 'created_at';
  43. protected $updateTime = 'updated_at';
  44. protected $deleteTime='deleted_at';
  45. protected $type=[
  46. 'has_cage'=>'boolean',
  47. 'from_addr'=>'json',
  48. 'to_addr'=>'json',
  49. 'refund_images'=>'array',
  50. ];
  51. const FREIGHT_FAST='fast';#快车
  52. const FREIGHT_AIR='air';#空运
  53. const FREIGHT_SPECIAL='special';#专车
  54. public static $freights=[
  55. self::FREIGHT_FAST=>'快车',
  56. self::FREIGHT_AIR=>'空运',
  57. self::FREIGHT_SPECIAL=>'专车',
  58. ];
  59. const STATUS_WAIT_PAY=0;
  60. const STATUS_WAIT_GET=1;
  61. const STATUS_GETTING=10;
  62. const STATUS_SENDING=20;
  63. const STATUS_GIVING=30;
  64. const STATUS_GIVED=40;
  65. const STATUS_COMPLETE=50;
  66. const STATUS_WAIT_SET=55;
  67. const STATUS_SET=60;
  68. const STATUS_SET_REJECT=65;
  69. const STATUS_REFUND=70;
  70. const STATUS_CANCEL=80;
  71. public static $statusList=[
  72. self::STATUS_WAIT_PAY=>'待支付',
  73. self::STATUS_WAIT_GET=>'待接单',
  74. self::STATUS_GETTING=>'已接单,取宠中',
  75. self::STATUS_SENDING=>'已取宠,运输中',
  76. self::STATUS_GIVING=>'送宠中',
  77. self::STATUS_GIVED=>'送宠完成,待用户确认',
  78. self::STATUS_COMPLETE=>'已完成,待提交结算信息',
  79. self::STATUS_WAIT_SET=>'已提交结算信息,待后台结算',
  80. self::STATUS_SET=>'已结算',
  81. self::STATUS_SET_REJECT=>'已拒绝结算',
  82. self::STATUS_REFUND=>'退款处理中',
  83. self::STATUS_CANCEL=>'已取消',
  84. ];
  85. const REFUND_STATUS_DEFAULT=0;
  86. const REFUND_STATUS_PASS=1;
  87. const REFUND_STATUS_REJECT=2;
  88. public static $refundStatus=[
  89. self::REFUND_STATUS_DEFAULT=>'审核中',
  90. self::REFUND_STATUS_PASS=>'审核通过',
  91. self::REFUND_STATUS_REJECT=>'驳回',
  92. ];
  93. protected $append=[
  94. 'protect_valid',
  95. ];
  96. public function user(){
  97. return $this->belongsTo(User::class);
  98. }
  99. public function log(){
  100. return $this->hasMany(UserOrderLog::class,'order_id')/*->order('id','desc')*/;
  101. }
  102. public static function payed($params){
  103. $id=$params['id'];
  104. $order=self::find($id);
  105. $order['pay_time']=time();
  106. $order['status']=self::STATUS_WAIT_GET;
  107. $order->save();
  108. }
  109. public function setImagesAttr($v){
  110. if(!$v){
  111. $v=[];
  112. }
  113. return json_encode($v);
  114. }
  115. public function getImagesAttr($v){
  116. return array_filter(json_decode($v));
  117. }
  118. /**
  119. * 取消
  120. *user.type 1未支付2驳回3退款
  121. *user.reason
  122. */
  123. const CT_NOT_PAY=1;
  124. const CT_REJECT=2;
  125. const CT_REFUND=3;
  126. public function cancel($type=self::CT_NOT_PAY,$force=false){
  127. if($this['status']!==self::STATUS_WAIT_PAY && !$force){
  128. throw_user('非待付款订单无法取消');
  129. }
  130. $this['status']=self::STATUS_CANCEL;
  131. $this['cancel_at']=time();
  132. $this['cancel_type']=$type;
  133. $this->save();
  134. if($this['coupon_id']){
  135. UserCoupon::setUse($this['coupon_id'],0);
  136. }
  137. }
  138. public function pay(){
  139. if($this['status']!=self::STATUS_WAIT_PAY){
  140. throw_user('状态有误');
  141. }
  142. if($this['expired_at']<time()){
  143. $this->cancel();
  144. throw_user('该订单已超时');
  145. }
  146. if($this['pay_type']==1){
  147. $params='';
  148. $this->user->money(bcsub(0,$this['real_amount'],2),$this['user_id'],MoneyLog::TYPE_ORDER_PAY,"订单[{$this['no']}]付款");
  149. self::payed(['id'=>$this['id']]);
  150. }else {
  151. $params = Payment::pay($this->user, $this['real_amount'], self::class, 'payed', ['id' => $this['id']]);
  152. }
  153. return $params;
  154. }
  155. public function complete(){
  156. $this->refunding();
  157. if($this['status']!=self::STATUS_GIVED){
  158. throw_user('非已送达状态无法确认完成');
  159. }
  160. $this['status']=self::STATUS_COMPLETE;
  161. $this['completed_at']=time();
  162. if(!$this->save()){
  163. throw_user('保存状态失败');
  164. }
  165. }
  166. public function refund($reason,$images){
  167. if(!in_array($this['status'],[self::STATUS_WAIT_GET,self::STATUS_GETTING,self::STATUS_SENDING])){
  168. throw_user('当前无法申请退款');
  169. }
  170. $this['refund_reason']=$reason;
  171. $this['refund_apply_at']=time();
  172. $this['refund_amount']=$this->refund_money;
  173. $this['refund_images']=$images;
  174. $this['refund_status']=self::REFUND_STATUS_DEFAULT;
  175. $this['status_bak']=$this['status'];
  176. $this['status']=self::STATUS_REFUND;
  177. if(!$this->save()){
  178. throw_user('保存失败');
  179. }
  180. }
  181. public function refundCancel(){
  182. if(!in_array($this['refund_status'],[self::REFUND_STATUS_DEFAULT])){
  183. throw_user('当前无法取消申请退款');
  184. }
  185. $this['refund_status']=-1;
  186. $this['status']=$this['status_bak'];
  187. if(!$this->save()){
  188. throw_user('保存失败');
  189. }
  190. }
  191. public function refunding(bool $throw=true){
  192. $is=in_array($this['refund_status'],[self::REFUND_STATUS_DEFAULT,self::REFUND_STATUS_PASS]);
  193. if($throw && $is){
  194. throw_user('当前正在申请退款或已退款,您无法操作');
  195. }
  196. return $is;
  197. }
  198. /**
  199. *处理退款申请
  200. * @status 1同意2拒绝
  201. */
  202. public function dealRefund($data){
  203. if($data['status']==1){
  204. $this['refund_status']=self::REFUND_STATUS_PASS;
  205. $this['refund_at']=time();
  206. PayUser::setType($this['pay_type']);
  207. PayUser::getClass()->payment($this['refund_amount'],$this['user'],MoneyLog::TYPE_REFUND,"订单退款",['order_id'=>$this['id']]);
  208. $this->cancel(self::CT_REFUND,true);
  209. }elseif($data['status']==2){
  210. $this['refund_status']=self::REFUND_STATUS_REJECT;
  211. $this['status']=$this['status_bak'];
  212. }
  213. $this->save();
  214. }
  215. /**
  216. *处理结算申请
  217. * @status 1同意2拒绝
  218. */
  219. public function dealSettle($data){
  220. if($this['status']!=self::STATUS_WAIT_SET){
  221. throw_user(sprintf('非待结算订单无法结算[%s]',self::$statusList[$this['status']]));
  222. }
  223. if($data['status']==1){
  224. $this['status']=self::STATUS_SET;
  225. foreach ($this->currentSenderOrder as $senderOrder){
  226. if(is_null($senderOrder['fee_total'])){
  227. throw_user('有配送员尚未申请结算,请稍后再试');
  228. }
  229. PayUser::getClass()->payment($senderOrder['fee_total'],$senderOrder['user'],MoneyLog::TYPE_SETTLE,"订单[{$this['no']}]结算",['order_id'=>$this['id']]);
  230. }
  231. #加一个盈利记录
  232. User::money($this->profit,0,MoneyLog::TYPE_PROFIT,"订单[{$this['no']}]盈利",['order_id'=>$this['id']],false);
  233. }elseif($data['status']==2){
  234. $this['status']=self::STATUS_SET_REJECT;
  235. }
  236. $this['settled_at']=time();
  237. if(!$this->save()){
  238. throw_user('保存失败');
  239. }
  240. }
  241. public function getCalRefundAttr($v,$data){
  242. if($this['freight']==self::FREIGHT_AIR){
  243. $fee=bcdiv(config('site.order_refund_air'),100,4);
  244. }else{
  245. $fee=bcdiv(config('site.order_refund_land'),100,4);
  246. }
  247. $amount=bcmul($this['real_amount'],$fee);
  248. return $amount;
  249. }
  250. public function getRefundMoneyAttr($v,$data){
  251. return bcsub($this['real_amount'],$this->cal_refund);
  252. }
  253. public function getSendDetailAttr(){
  254. $log=$this->log()->select();
  255. $res['addr']=$this['to_addr']['location'];
  256. $res['log']=$log;
  257. return $res;
  258. }
  259. public function remove(){
  260. if(!in_array($this['status'],[self::STATUS_CANCEL,self::STATUS_SET,self::STATUS_SET_REJECT])){
  261. throw_user('非已取消或完成订单无法删除');
  262. }
  263. if($this['user_deleted_at']){
  264. throw_user('该订单已删除');
  265. }
  266. $this['user_deleted_at']=time();
  267. $this->save();
  268. }
  269. public function submit($data){
  270. $this['status']=self::STATUS_WAIT_PAY;
  271. $res=$this->allowField(true)->save($data);
  272. if(!$res){
  273. throw_user('保存失败');
  274. }
  275. if($this['coupon_id']){
  276. UserCoupon::setUse($this['coupon_id']);
  277. }
  278. }
  279. /** 待接单 */
  280. public static function wait(User $user){
  281. $q=self::alias('user_order');
  282. $city=$user->area()->column('name');
  283. if(empty($city)){
  284. $q->where('user_order.id',0);
  285. return $q;
  286. }
  287. $q
  288. ->join('sender_order b','user_order.id=b.user_order_id and b.now=1','LEFT')
  289. ->whereIn('user_order.from_city|user_order.to_city',$city)
  290. ->whereBetween('user_order.status',[self::STATUS_WAIT_GET,self::STATUS_GIVING])
  291. ->whereNotExists("select user_id from sender_order where user_order_id=user_order.id and user_id={$user['id']}")
  292. ->group('user_order.id')
  293. ->having('COUNT(b.id)<2')
  294. ->order('user_order.id','desc');
  295. return $q;
  296. }
  297. public function getProtectValidAttr($v,$d){
  298. if(!isset($this['status'])){
  299. return false;
  300. }
  301. return $this['status']>=self::STATUS_WAIT_GET && $this['status']<=self::STATUS_GIVED;
  302. }
  303. public function senderOrder(){
  304. return $this->hasMany(SenderOrder::class);
  305. }
  306. /** 当前的配送员订单 */
  307. public function currentSenderOrder($user_id=null){
  308. $q=$this->senderOrder()->where('now',1);
  309. if($user_id){
  310. $q->where('user_id',$user_id);
  311. }
  312. return $q;
  313. }
  314. /** 是否能接单 */
  315. public function checkGet(User $user){
  316. if(count($this->currentSenderOrder)>1){
  317. throw_user('订单已被接单,您暂时无法接单(1)');
  318. }
  319. $type=$user->getSendType($this);
  320. /*if($user->isSendGet($type) && $this['status'] != self::STATUS_WAIT_GET) {
  321. throw_user("订单已被接单,您暂时无法接单(2)");
  322. }*/
  323. return $type;
  324. }
  325. /** 接单 */
  326. public function accept(User $user){
  327. $type=$this->checkGet($user);
  328. if($this['status']==self::STATUS_WAIT_GET) {
  329. $this['status'] = self::STATUS_GETTING;
  330. }
  331. #取送都是自己,取送订单创建
  332. if($user->isSendAll($type)){
  333. $time=time();
  334. $get=$this->senderOrder()->save([
  335. 'user_id'=>$user['id'],
  336. 'status' =>$this['status'],
  337. 'type' =>1,
  338. 'now' =>1,
  339. 'get_at' =>$time,
  340. ]);
  341. if(!$get){
  342. throw_user('保存取宠订单失败');
  343. }
  344. $sender=$this->senderOrder()->save([
  345. 'user_id'=>$user['id'],
  346. 'status' =>$this['status'],
  347. 'type' =>2,
  348. 'now' =>1,
  349. 'get_at' =>$time,
  350. ]);
  351. if(!$sender){
  352. throw_user('保存送宠订单失败');
  353. }
  354. }else{
  355. $sender_order=$this->senderOrder()->save([
  356. 'user_id'=>$user['id'],
  357. 'status' =>$this['status'],
  358. 'type' =>$type,
  359. 'now' =>1,
  360. 'get_at' =>time(),
  361. ]);
  362. if(!$sender_order){
  363. throw_user('保存配送订单失败');
  364. }
  365. }
  366. if(!$this['get_at']){
  367. $this['get_at']=time();
  368. }
  369. $this->save();
  370. }
  371. /** 更新状态 */
  372. public function updateStatus($status,User $user){
  373. if(!$status){
  374. return;
  375. }
  376. $this->refunding();
  377. $senderS=[
  378. self::STATUS_GETTING=>self::STATUS_SENDING,
  379. self::STATUS_SENDING=>self::STATUS_GIVING,
  380. ];
  381. $recS=[
  382. self::STATUS_GIVING=>self::STATUS_GIVED,
  383. ];
  384. $arr=$senderS+$recS;
  385. if($this['status']!=$status){
  386. throw_user('状态'.self::$statusList[$this['status']].'有误');
  387. }
  388. if(!$this->is_same_user) {
  389. $type = $user->getSendType($this);
  390. if ($user->isSendGet($type) && !isset($senderS[$status])) {
  391. throw_user('您是取宠人,无法更新状态' . self::$statusList[$status]);
  392. } elseif ($user->isSendSend($type) && !isset($recS[$status])) {
  393. throw_user('您是送宠人,无法更新状态' . self::$statusList[$status]);
  394. }
  395. }
  396. $this['status']=$arr[$status];
  397. if(!$this->save()){
  398. throw_user('更新失败');
  399. }
  400. return $this['status'];
  401. }
  402. /** 进行中的订单 */
  403. public function scopeRunning(Query $query,$from=''){
  404. $status=[];
  405. if($from=='user'){
  406. $status=[
  407. self::STATUS_WAIT_GET,
  408. ];
  409. }
  410. $query
  411. ->show()
  412. ->whereIn('status',array_merge($status,[
  413. self::STATUS_GETTING,
  414. self::STATUS_SENDING,
  415. self::STATUS_GIVING,
  416. self::STATUS_GIVED,
  417. self::STATUS_REFUND,
  418. ]));
  419. }
  420. /** 计算收益 */
  421. public static function calcProfit($date=null){
  422. $profit=Db::query($sql=UserOrder::withTrashed()->profit($date)->buildSql());
  423. return bcadd(array_sum(array_column($profit,'profit')),0);
  424. }
  425. /** 结算金额 */
  426. public function getSettleAmountAttr($v,$data){
  427. $sender_order=$this->currentSenderOrder;
  428. $amount=0;
  429. foreach ($sender_order as $order){
  430. $amount=bcadd($amount,$order['fee_total']);
  431. }
  432. return $amount;
  433. }
  434. /** 配送员 */
  435. public function getSendersAttr($v,$data){
  436. $sender_order=$this->currentSenderOrder;
  437. foreach ($sender_order as $item){
  438. $item->append(['user']);
  439. }
  440. return array_column($sender_order->toArray(),null);
  441. }
  442. /** 结算总的金额 */
  443. public function getSettleInfoAttr($v,$data){
  444. $arr=$this->senders;
  445. $total=[
  446. 'fee_total'=>0
  447. ];
  448. foreach ($arr as $item){
  449. for ($i=1;$i<=6;$i++) {
  450. $total["fee_$i"] = bcadd($total["fee_$i"]??0,$item["fee_$i"]);
  451. }
  452. $total['fee_total']=bcadd($total['fee_total']??0,$item['fee_total']);
  453. }
  454. return $total;
  455. }
  456. /** 盈利总的金额 */
  457. public function getProfitAttr($v,$data){
  458. return bcsub($this['real_amount'],$this->settle_info['fee_total']);
  459. }
  460. /** 是否相同接单人 */
  461. public function getIsSameUserAttr(){
  462. dump($this->currentSenderOrder->toArray());
  463. dump($this->currentSenderOrder->count());
  464. dump(count(array_unique($this->currentSenderOrder->column('user_id'))));
  465. dd($this->currentSenderOrder->count()==2 && count(array_unique($this->currentSenderOrder->column('user_id')))==1);
  466. return $this->currentSenderOrder->count()==2 && count(array_unique($this->currentSenderOrder->column('user_id')))==1;
  467. }
  468. protected function getCreatedAtAttr($a){
  469. return date('Y-m-d H:i:s',$a)?:'';
  470. }
  471. protected function getPayTimeAttr($a){
  472. return date('Y-m-d H:i:s',$a)?:'';
  473. }
  474. /** init */
  475. protected static function init()
  476. {
  477. self::beforeInsert(function (self $order){
  478. $order['no']=order_no();
  479. $order['expired_at']=strtotime("+10minutes");
  480. });
  481. self::afterInsert(function (self $order){
  482. /* if(!$order->user->address()->exists($order['from_addr'])->find()) {
  483. $addr=$order['from_addr'];
  484. $addr['user_id']=$order->user_id;
  485. (new UserAddress())->allowField(true)->save($addr);
  486. }*/
  487. });
  488. self::afterUpdate(function (self $order){
  489. #更新配送订单状态
  490. $data=$order->getChangedData();
  491. if(isset($data['status'])){
  492. $order->senderOrder()->update(['status'=>$data['status']]);
  493. }
  494. });
  495. }
  496. /** scope */
  497. /**
  498. * 待结算
  499. * @param Query $query
  500. */
  501. public function scopeSettle(Query $query){
  502. $query->where('status',self::STATUS_WAIT_SET);
  503. }
  504. /**
  505. * 待处理退款
  506. * @param Query $query
  507. */
  508. public function scopeWaitRefund(Query $query){
  509. $query->where('status',self::STATUS_REFUND);
  510. }
  511. /**
  512. * 售后
  513. * @param static $query
  514. */
  515. public function scopeService(Query $query){
  516. $query->show()->where('refund_status','>',-1);
  517. }
  518. /**
  519. * 总营业额
  520. * @param Query $query
  521. * @param null|array $date
  522. */
  523. public function scopeTurnover(Query $query,$date='today'){
  524. $query
  525. ->whereBetween(($table=$this->getTable()).'.status', [self::STATUS_WAIT_GET, self::STATUS_SET]);
  526. if($date=='today') {
  527. $query->whereTime($table.'.created_at', $date);
  528. }elseif(is_array($date)){
  529. list($type,$time)=$date;
  530. if($type=='day'){
  531. $query->whereBetween($table.'.created_at',[strtotime($time),strtotime("+1day",strtotime($time))]);
  532. }elseif ($type=='month'){
  533. if (substr_count($time,'-')==1){
  534. $time="$time-01 00:00:00";
  535. }
  536. $query->whereBetween($table.'.created_at',[strtotime($time),strtotime("+1month",strtotime($time))]);
  537. }elseif ($type=='year'){
  538. $query->whereBetween($table.'.created_at',[strtotime("$time-01-01"),strtotime(($time+1).'-01-01')]);
  539. }
  540. }
  541. }
  542. /**
  543. * 盈利统计
  544. * @param null $date
  545. * @param bool $detail
  546. * @return string
  547. * @throws \think\db\exception\DataNotFoundException
  548. * @throws \think\db\exception\ModelNotFoundException
  549. * @throws \think\exception\DbException
  550. */
  551. public function scopeProfit(Query $query,$date=null){
  552. $profit=$query
  553. ->join('sender_order b','user_order.id=b.user_order_id and b.now=1')
  554. ->where('user_order.status',self::STATUS_SET)
  555. ->show()
  556. ->group('user_order.id')
  557. ->fieldRaw("(user_order.real_amount-SUM(b.fee_total)) AS profit,SUM(b.fee_total) as fee_total");
  558. if(is_null($date)){
  559. $profit->whereTime('user_order.created_at','today');
  560. }else{
  561. list($type,$time)=$date;
  562. if($type=='day'){
  563. $query->whereTime('user_order.created_at',$time);
  564. }elseif ($type=='month'){
  565. if (substr_count($time,'-')==1){
  566. $time="$time-01 00:00:00";
  567. }
  568. $query->whereBetween('user_order.created_at',[strtotime($time),strtotime("+1month",strtotime($time))]);
  569. }elseif ($type=='year'){
  570. $query->whereBetween('user_order.created_at',[strtotime("$time-01-01"),strtotime(($time+1).'-01-01')]);
  571. }
  572. }
  573. }
  574. /** 已付款 */
  575. public function scopeWaitGet(Query $query){
  576. $query
  577. ->show()
  578. ->where('status',self::STATUS_WAIT_GET)->show();
  579. }
  580. /** 已完成 */
  581. public function scopeSuccess(Query $query){
  582. $query
  583. ->show()
  584. ->whereBetween('status',[
  585. self::STATUS_COMPLETE,
  586. self::STATUS_WAIT_SET,
  587. ]);
  588. }
  589. /** 已结算
  590. * @param Query|static $query
  591. */
  592. public function scopeSettled(Query $query){
  593. $query
  594. ->whereIn('status',[self::STATUS_SET,self::STATUS_SET_REJECT]);
  595. }
  596. /**
  597. * 已取消
  598. * @param Query|self $query
  599. */
  600. public function scopeCanceled(Query $query){
  601. $query->where('status',self::STATUS_CANCEL);
  602. }
  603. /** 已超时 */
  604. public function scopeExpired(Query $query,$limit=10){
  605. $query->where('expired_at','<',time())
  606. ->where('status',self::STATUS_WAIT_PAY)
  607. ->order('id','desc')
  608. ->limit($limit);
  609. }
  610. /**
  611. * 已完成
  612. * @param Query|self $query
  613. */
  614. public function scopeOver(Query $query){
  615. $query
  616. ->show()
  617. ->whereBetween('status',[self::STATUS_COMPLETE,self::STATUS_SET_REJECT]);
  618. }
  619. /** 展示的 */
  620. public function scopeShow(Query $query){
  621. $query->whereNull("{$this->getTable()}.user_deleted_at");
  622. }
  623. /** 后台使用已完成 */
  624. public function scopeCompleted(Query $query){
  625. $query->where('status',UserOrder::STATUS_COMPLETE)
  626. ->show()
  627. ->order('id','desc');
  628. }
  629. /** 后台使用已结算 */
  630. public function scopeCaled(Query $query){
  631. $query->where('status',UserOrder::STATUS_SET)
  632. ->show()
  633. ->order('id','desc');
  634. }
  635. /** trashed */
  636. public static function withTrashed(){
  637. return new self;
  638. }
  639. }