Mobile.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\common\model;
  3. use app\admin\model\Admin;
  4. use app\common\service\MobileComputer;
  5. use think\Cache;
  6. use think\Model;
  7. use traits\model\SoftDelete;
  8. /**
  9. * 配置模型
  10. */
  11. class Mobile extends Model
  12. {
  13. use SoftDelete;
  14. public static $status=[
  15. 0=>'正常',
  16. 1=>'已售',
  17. 2=>'已下架',
  18. 3=>'禁止下单',
  19. ];
  20. protected $append=[
  21. 'saled',
  22. ];
  23. protected $autoWriteTimestamp='int';
  24. protected $hidden=[
  25. 'filter_exists_0','filter_num_0','filter_num_1','filter_num_2','filter_num_3','filter_num_4','filter_num_5','filter_num_6','filter_num_7',
  26. 'filter_num_8','filter_num_9','filter_middle_3a','filter_middle_4a','filter_middle_5a','filter_middle_6a','filter_middle_7a','filter_middle_8a',
  27. 'filter_middle_abc','filter_middle_abcd','filter_middle_abcde','filter_middle_abcdef','filter_middle_3ab','filter_middle_4ab','filter_middle_5ab',
  28. 'filter_no_pos_2','filter_no_pos_3','filter_no_pos_4','filter_no_pos_5','filter_no_pos_6','filter_no_pos_7','filter_no_pos_8','filter_no_pos_9',
  29. 'filter_no_pos_10','filter_no_pos_11','filter_tail_3a','filter_tail_4a','filter_tail_5a','filter_tail_6a','filter_tail_7a','filter_tail_8a',
  30. 'filter_tail_abc','filter_tail_abcd','filter_tail_abcde','filter_tail_abcdef','filter_tail_3ab','filter_tail_4ab','filter_tail_5ab',
  31. 'filter_exists_2','filter_exists_3','filter_exists_4','filter_exists_5','filter_exists_6','filter_exists_7','filter_exists_8','filter_exists_9',
  32. ];
  33. public function info(){
  34. return $this->hasOne(MobileInfo::class);
  35. }
  36. public function orders(){
  37. return $this->hasMany(MobileOrder::class);
  38. }
  39. public function proxy(){
  40. return $this->belongsTo(Admin::class,'proxy_id');
  41. }
  42. public function getSaledAttr($a,$b){
  43. $s=$b['status']??0;
  44. return $s===1?1:0;
  45. }
  46. public function makeAmount(){
  47. $mobile=$this;
  48. if(empty($mobile['amount_base'])){
  49. $mobile['amount_base']=0;
  50. }
  51. if(empty($mobile['amount_charge'])){
  52. $mobile['amount_charge']=0;
  53. }
  54. if(!isset($mobile['is_activity'])){
  55. $mobile['is_activity']=0;
  56. }
  57. if(!$mobile['is_activity']) {
  58. $mobile['amount'] = $mobile['amount_base'] + $mobile['amount_charge'];
  59. }else{
  60. $mobile['amount'] = $mobile['amount_kill'] + $mobile['amount_charge'];
  61. }
  62. }
  63. public function makeRules(){
  64. $mobile=$this;
  65. foreach (MobileComputer::setMobile($mobile['no'])->filter() as $key=>$value){
  66. $mobile[$key]=$value;
  67. }
  68. }
  69. public function amountChangeColumn(){
  70. return [
  71. 'amount_base',
  72. 'amount_charge',
  73. ];
  74. }
  75. public static function init()
  76. {
  77. self::beforeWrite(function (self $mobile){
  78. if(isset($mobile['top_time']) && $mobile['top_time']==1){
  79. $mobile['top_time']=time();
  80. }else{
  81. $mobile['top_time']=null;
  82. }
  83. if(isset($mobile['rec_time']) && $mobile['rec_time']==1){
  84. $mobile['rec_time']=time();
  85. }else{
  86. $mobile['rec_time']=null;
  87. }
  88. if(isset($mobile->getChangedData()['is_activity']) && $mobile->getChangedData()['is_activity']==1){
  89. self::where('id','>',0)->update(['activity_time'=>null]);
  90. $mobile['activity_time']=time();
  91. }
  92. });
  93. self::beforeUpdate(function (self $mobile){
  94. $data=$mobile->getChangedData();
  95. if(isset($data['no'])){
  96. $mobile->makeRules();
  97. }
  98. if(array_intersect($data,$mobile->amountChangeColumn())){
  99. $mobile->makeAmount();
  100. }
  101. });
  102. self::beforeInsert(function (self $mobile){
  103. $mobile['sort']=mt_rand(0,99999999);
  104. if(empty($mobile['province_id'])){
  105. $mobile['province_id']=Area::getIdByName($mobile['province']);
  106. }
  107. if(empty($mobile['city_id'])){
  108. $mobile['city_id']=Area::getIdByName($mobile['city']);
  109. }
  110. if(!empty($mobile['province_id'])){
  111. $mobile['province']=Area::where('id',$mobile['province_id'])->value('name');
  112. }
  113. if(!empty($mobile['city_id'])){
  114. $mobile['city']=Area::where('id',$mobile['city_id'])->value('name');
  115. }
  116. $mobile->makeAmount();
  117. $mobile->makeRules();
  118. });
  119. }
  120. public static function show(){
  121. $model=new self();
  122. $model->with(['info']);
  123. $model->whereIn('status',[0,1,3]);
  124. return $model;
  125. }
  126. /**
  127. *@param self $mobile
  128. */
  129. public static function whenOrderPayed($mobile){
  130. if($mobile){
  131. $mobile['status']=1;
  132. $mobile->save();
  133. }
  134. }
  135. public function shouldBuy(){
  136. if($this['status']!=0){
  137. throw_user('该号码已售或不存在');
  138. }
  139. }
  140. public function viewCountCacheName(){
  141. return __CLASS__."view_count_{$this['id']}_".date('ymdh');
  142. }
  143. public function getViewCountAttr(){
  144. return Cache::get($this->viewCountCacheName(),0);
  145. }
  146. public function addViewCount(){
  147. $num=Cache::get($this->viewCountCacheName(),0);
  148. Cache::set($this->viewCountCacheName(),$num+1);
  149. }
  150. }