123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- namespace app\common\model;
- use app\admin\model\Admin;
- use app\common\service\MobileComputer;
- use think\Cache;
- use think\Model;
- use traits\model\SoftDelete;
- /**
- * 配置模型
- */
- class Mobile extends Model
- {
- use SoftDelete;
- public static $status=[
- 0=>'正常',
- 1=>'已售',
- 2=>'已下架',
- 3=>'禁止下单',
- ];
- protected $append=[
- 'saled',
- ];
- protected $autoWriteTimestamp='int';
- protected $hidden=[
- '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',
- 'filter_num_8','filter_num_9','filter_middle_3a','filter_middle_4a','filter_middle_5a','filter_middle_6a','filter_middle_7a','filter_middle_8a',
- 'filter_middle_abc','filter_middle_abcd','filter_middle_abcde','filter_middle_abcdef','filter_middle_3ab','filter_middle_4ab','filter_middle_5ab',
- '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',
- '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',
- 'filter_tail_abc','filter_tail_abcd','filter_tail_abcde','filter_tail_abcdef','filter_tail_3ab','filter_tail_4ab','filter_tail_5ab',
- 'filter_exists_2','filter_exists_3','filter_exists_4','filter_exists_5','filter_exists_6','filter_exists_7','filter_exists_8','filter_exists_9',
- ];
- public function info(){
- return $this->hasOne(MobileInfo::class);
- }
- public function orders(){
- return $this->hasMany(MobileOrder::class);
- }
- public function proxy(){
- return $this->belongsTo(Admin::class,'proxy_id');
- }
- public function getSaledAttr($a,$b){
- $s=$b['status']??0;
- return $s===1?1:0;
- }
- public function makeAmount(){
- $mobile=$this;
- if(empty($mobile['amount_base'])){
- $mobile['amount_base']=0;
- }
- if(empty($mobile['amount_charge'])){
- $mobile['amount_charge']=0;
- }
- if(!isset($mobile['is_activity'])){
- $mobile['is_activity']=0;
- }
- if(!$mobile['is_activity']) {
- $mobile['amount'] = $mobile['amount_base'] + $mobile['amount_charge'];
- }else{
- $mobile['amount'] = $mobile['amount_kill'] + $mobile['amount_charge'];
- }
- }
- public function makeRules(){
- $mobile=$this;
- foreach (MobileComputer::setMobile($mobile['no'])->filter() as $key=>$value){
- $mobile[$key]=$value;
- }
- }
- public function amountChangeColumn(){
- return [
- 'amount_base',
- 'amount_charge',
- ];
- }
- public static function init()
- {
- self::beforeWrite(function (self $mobile){
- if(isset($mobile['top_time']) && $mobile['top_time']==1){
- $mobile['top_time']=time();
- }else{
- $mobile['top_time']=null;
- }
- if(isset($mobile['rec_time']) && $mobile['rec_time']==1){
- $mobile['rec_time']=time();
- }else{
- $mobile['rec_time']=null;
- }
- if(isset($mobile->getChangedData()['is_activity']) && $mobile->getChangedData()['is_activity']==1){
- self::where('id','>',0)->update(['activity_time'=>null]);
- $mobile['activity_time']=time();
- }
- });
- self::beforeUpdate(function (self $mobile){
- $data=$mobile->getChangedData();
- if(isset($data['no'])){
- $mobile->makeRules();
- }
- if(array_intersect($data,$mobile->amountChangeColumn())){
- $mobile->makeAmount();
- }
- });
- self::beforeInsert(function (self $mobile){
- $mobile['sort']=mt_rand(0,99999999);
- if(empty($mobile['province_id'])){
- $mobile['province_id']=Area::getIdByName($mobile['province']);
- }
- if(empty($mobile['city_id'])){
- $mobile['city_id']=Area::getIdByName($mobile['city']);
- }
- if(!empty($mobile['province_id'])){
- $mobile['province']=Area::where('id',$mobile['province_id'])->value('name');
- }
- if(!empty($mobile['city_id'])){
- $mobile['city']=Area::where('id',$mobile['city_id'])->value('name');
- }
- $mobile->makeAmount();
- $mobile->makeRules();
- });
- }
- public static function show(){
- $model=new self();
- $model->with(['info']);
- $model->whereIn('status',[0,1,3]);
- return $model;
- }
- /**
- *@param self $mobile
- */
- public static function whenOrderPayed($mobile){
- if($mobile){
- $mobile['status']=1;
- $mobile->save();
- }
- }
- public function shouldBuy(){
- if($this['status']!=0){
- throw_user('该号码已售或不存在');
- }
- }
- public function viewCountCacheName(){
- return __CLASS__."view_count_{$this['id']}_".date('ymdh');
- }
- public function getViewCountAttr(){
- return Cache::get($this->viewCountCacheName(),0);
- }
- public function addViewCount(){
- $num=Cache::get($this->viewCountCacheName(),0);
- Cache::set($this->viewCountCacheName(),$num+1);
- }
- }
|