Mobile.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 holdLog(){
  43. return $this->hasMany(MobileHoldLog::class);
  44. }
  45. const BEAUTI=1;
  46. const FLOW=2;
  47. public function getSaledAttr($a,$b){
  48. $s=$b['status']??0;
  49. return $s===1?1:0;
  50. }
  51. public function makeAmount(){
  52. $mobile=$this;
  53. if(empty($mobile['amount_base'])){
  54. $mobile['amount_base']=0;
  55. }
  56. if(empty($mobile['amount_charge'])){
  57. $mobile['amount_charge']=0;
  58. }
  59. if(!isset($mobile['is_activity'])){
  60. $mobile['is_activity']=0;
  61. }
  62. if(!$mobile['is_activity']) {
  63. $mobile['amount'] = $mobile['amount_base'] + $mobile['amount_charge'];
  64. }else{
  65. $mobile['amount'] = $mobile['amount_kill'] + $mobile['amount_charge'];
  66. }
  67. }
  68. public function makeRules(){
  69. $mobile=$this;
  70. foreach (MobileComputer::setMobile($mobile['no'])->filter() as $key=>$value){
  71. $mobile[$key]=$value;
  72. }
  73. }
  74. public function amountChangeColumn(){
  75. return [
  76. 'amount_base',
  77. 'amount_charge',
  78. ];
  79. }
  80. public static function init()
  81. {
  82. self::beforeWrite(function (self $mobile){
  83. if(isset($mobile['top_time']) && $mobile['top_time']==1){
  84. $mobile['top_time']=time();
  85. }else{
  86. $mobile['top_time']=null;
  87. }
  88. if(isset($mobile['rec_time']) && $mobile['rec_time']==1){
  89. $mobile['rec_time']=time();
  90. }else{
  91. $mobile['rec_time']=null;
  92. }
  93. if(isset($mobile->getChangedData()['is_activity']) && $mobile->getChangedData()['is_activity']==1){
  94. self::where('id','>',0)->update(['activity_time'=>null]);
  95. $mobile['activity_time']=time();
  96. }
  97. });
  98. self::beforeUpdate(function (self $mobile){
  99. $data=$mobile->getChangedData();
  100. if(isset($data['no'])){
  101. $mobile->makeRules();
  102. }
  103. if(array_intersect($data,$mobile->amountChangeColumn())){
  104. $mobile->makeAmount();
  105. }
  106. });
  107. self::beforeInsert(function (self $mobile){
  108. $mobile['sort']=mt_rand(0,99999999);
  109. if(empty($mobile['province_id'])){
  110. $mobile['province_id']=Area::getIdByName($mobile['province']);
  111. }
  112. if(empty($mobile['city_id'])){
  113. $mobile['city_id']=Area::getIdByName($mobile['city']);
  114. }
  115. if(!empty($mobile['province_id'])){
  116. $mobile['province']=Area::where('id',$mobile['province_id'])->value('shortname');
  117. }
  118. if(!empty($mobile['city_id'])){
  119. $mobile['city']=Area::where('id',$mobile['city_id'])->value('shortname');
  120. }
  121. $mobile->makeAmount();
  122. $mobile->makeRules();
  123. });
  124. }
  125. public static function show(){
  126. $model=new self();
  127. $model->with(['info']);
  128. $model->whereIn('status',[0,1,3]);
  129. return $model;
  130. }
  131. /**
  132. *@param self $mobile
  133. */
  134. public static function whenOrderPayed($mobile){
  135. if($mobile){
  136. $mobile['status']=1;
  137. $mobile->save();
  138. }
  139. }
  140. public function shouldBuy(){
  141. if($this['status']!=0){
  142. throw_user('该号码已售或不存在');
  143. }
  144. }
  145. public function viewCountCacheName(){
  146. return __CLASS__."view_count_{$this['id']}_".date('ymdh');
  147. }
  148. public function getViewCountAttr(){
  149. return Cache::get($this->viewCountCacheName(),0);
  150. }
  151. public function addViewCount(){
  152. $num=Cache::get($this->viewCountCacheName(),0);
  153. Cache::set($this->viewCountCacheName(),$num+1);
  154. }
  155. public function needCheckSmsCode(){
  156. return in_array($this['type'],[self::BEAUTI]);
  157. }
  158. }