MobileOrder.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace app\common\model;
  3. use app\admin\model\Admin;
  4. use app\admin\model\MobileOrderAdmin;
  5. use think\db\Query;
  6. use think\Model;
  7. /**
  8. * 配置模型
  9. * @method static static payed()
  10. */
  11. class MobileOrder extends Model
  12. {
  13. const STATUS_WAIT_SEND=10;
  14. public static $status=[
  15. 0=>'待付款',
  16. 10=>'已付款',
  17. 15=>'可发货',
  18. 17=>'有号码未发货',
  19. 20=>'待开卡',
  20. 25=>'已完成',
  21. 30=>'申请退款',
  22. 50=>'已关闭',
  23. 60=>'无号码',
  24. 70=>'换卡',
  25. 80=>'争议单',
  26. 90=>'已退款',
  27. ];
  28. public static $payTypes=[
  29. 1=>'微信',
  30. 2=>'支付宝',
  31. 3=>'京东',
  32. ];
  33. // 自动写入时间戳字段
  34. protected $autoWriteTimestamp = true;
  35. public function mobile(){
  36. return $this->belongsTo(Mobile::class);
  37. }
  38. public function info(){
  39. return $this->hasOne(MobileOrderInfo::class);
  40. }
  41. public function operation(){
  42. return $this->hasMany(MobileOrderOperation::class)->order('mobile_order_operation.id','desc');
  43. }
  44. public function admin(){
  45. return $this->hasMany(MobileOrderAdmin::class);
  46. }
  47. public function proxy(){
  48. $columns=Admin::getTableInfo()['fields'];
  49. unset($columns['password']);
  50. return $this->belongsTo(Admin::class)->field($columns);
  51. }
  52. public function getAddressAttr($a,$b){
  53. return Area::getNameString($b['city']).$a;
  54. }
  55. protected static function init()
  56. {
  57. self::beforeInsert(function (self $mobileOrder){
  58. $mobileOrder['order_no']=order_no();
  59. $mobileOrder['expire_time']=time()+86400;
  60. $mobileOrder['s_id']=$mobileOrder['mobile']['s_id'];
  61. $mobileOrder['brand']=$mobileOrder['mobile']['brand'];
  62. });
  63. self::afterInsert(function (self $mobileOrder){
  64. $mobileOrder->info()->save([
  65. 'mobile'=>$mobileOrder['mobile'],
  66. 'info'=>$mobileOrder['mobile']['info'],
  67. ]);
  68. });
  69. self::beforeUpdate(function (self $mobileOrder){
  70. $data=$mobileOrder->getChangedData();
  71. #已支付
  72. if(isset($data['status']) && $data['status']==self::STATUS_WAIT_SEND){
  73. SysConfig::set('mo_ordered_num',SysConfig::look('mo_ordered_num',0)+1);
  74. Mobile::whenOrderPayed($mobileOrder->mobile);
  75. }
  76. });
  77. }
  78. public function scopeExpired(Query $query){
  79. $query->where('status',0)->where('expire_time','<',time());
  80. }
  81. public function cancel(){
  82. if($this['pay_time'] || $this['pay_time']!=0){
  83. return;
  84. }
  85. $this['status']=50;
  86. $this->save();
  87. }
  88. public function continuePay(){
  89. if($this['expire_time']<time()){
  90. throw_user('订单已过期');
  91. }
  92. if($this['status']!=0){
  93. throw_user('订单状态有误');
  94. }
  95. if($this['type']!=1){
  96. throw_user('该产品无需支付');
  97. }
  98. }
  99. public function scopePayed(Query $query){
  100. $query->where('status','>=',self::STATUS_WAIT_SEND);
  101. }
  102. public function originData(){
  103. return $this->origin;
  104. }
  105. }