Orders.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. /**
  5. * @property OrderInfo info
  6. * @property User user
  7. * @property bool is_wait_pay
  8. */
  9. class Orders extends Model
  10. {
  11. protected $type=[
  12. 'tax'=>'json',
  13. ];
  14. #未支付过期时间
  15. const EXP_PAY=30;
  16. #待收货过期时间
  17. const EXP_REC=10*86400;
  18. const PT_QYWY=1;
  19. const PT_WX=2;
  20. const PT_ZFB=3;
  21. const PT_YL=4;
  22. const PT_DF=5;
  23. const PT_OFF=6;
  24. public static $pay_types=[
  25. self::PT_QYWY=>'企业网银',
  26. self::PT_WX=>'微信',
  27. self::PT_ZFB=>'支付宝',
  28. self::PT_YL=>'银联',
  29. self::PT_DF=>'代付',
  30. self::PT_OFF=>'线下支付',
  31. ];
  32. const S_WAIT_PAY=0;
  33. const S_WAIT_REC=10;
  34. const S_OVER=20;
  35. const S_CANCEL=30;
  36. const S_REFUND=40;
  37. public static $status=[
  38. self::S_WAIT_PAY=>'待支付',
  39. self::S_WAIT_REC=>'待收货',
  40. self::S_OVER=>'已完成',
  41. self::S_CANCEL=>'已取消',
  42. //self::S_REFUND=>'退款退货',
  43. ];
  44. /**
  45. * @return string[]
  46. */
  47. public static function getStatus(): array
  48. {
  49. return self::$status;
  50. }
  51. protected $autoWriteTimestamp=true;
  52. public function info(){
  53. return $this->hasMany(OrderInfo::class,'order_id');
  54. }
  55. public function user(){
  56. return $this->belongsTo(User::class);
  57. }
  58. public function address(){
  59. return $this->hasOne(OrderAddress::class,'order_id');
  60. }
  61. public function getGoodsAttr(){
  62. $info=$this->info()->with(['goodsBak'])->find();
  63. $goods=$info['goodsBak'];
  64. return $goods['goods'];
  65. }
  66. public function getIsWaitPayAttr($_,$model){
  67. return $model['status']==self::S_WAIT_PAY;
  68. }
  69. /**
  70. * @return string[]
  71. */
  72. public static function getPayTypes(): array
  73. {
  74. return self::$pay_types;
  75. }
  76. public static function continue($status){
  77. $now=time();
  78. return self::where('status',$status)->where('continue_expire_time','<',$now);
  79. }
  80. #未支付过期
  81. public function makeCancel(){
  82. $this['status']=self::S_CANCEL;
  83. $this['cancel_time']=time();
  84. $this->save();
  85. }
  86. #待收货过期
  87. public function makeRec(){
  88. $this['status']=self::S_OVER;
  89. $this['rec_time']=time();
  90. $this->save();
  91. }
  92. public function makePayInfo($pay_type){
  93. $user=$this->user;
  94. return Payment::pay(
  95. $user,
  96. $pay_type,
  97. $this['amount_pay'],
  98. self::class,
  99. 'whenPaySuccess',
  100. $this['id'],
  101. "订单【{$this['order_no']}】付款",
  102. );
  103. }
  104. public static function makePayed(Payment $payment){
  105. $order=Orders::find($payment['payment_id']);
  106. if(!$order){
  107. return false;
  108. }
  109. if(!$order->isNotPay()){
  110. return false;
  111. }
  112. $order['status']=self::S_WAIT_REC;
  113. $order['pay_time']=$payment['pay_time'];
  114. $order->save();
  115. return true;
  116. }
  117. public function isNotPay(){
  118. return $this['status']===self::S_WAIT_PAY;
  119. }
  120. protected static function init()
  121. {
  122. self::beforeInsert(function (self $orders){
  123. #优惠总金额
  124. $orders['amount_discount']=bcAddAll($orders['amount_coupon']??0,$orders['amount_coupon_kill']);
  125. #过期时间
  126. $orders['continue_expire_time']=time()+60*self::EXP_PAY;
  127. });
  128. self::beforeUpdate(function (self $order){
  129. $data=$order->getChangedData();
  130. if(!empty($data['status'])){
  131. if($data['status']==self::S_WAIT_REC){
  132. $orders['continue_expire_time']=time()+self::EXP_REC;
  133. }
  134. }
  135. });
  136. }
  137. }