123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- <?php
- namespace app\common\model;
- use app\admin\model\Admin;
- use app\admin\model\Network;
- use app\common\service\MobileComputer;
- use app\common\service\MobilePriceLogService;
- use app\service\EsMobileService;
- use think\Cache;
- use think\Db;
- use think\db\Query;
- use think\helper\Str;
- use think\Model;
- use traits\model\SoftDelete;
- use app\admin\model\MobileFlowAreaTemplate;
- /**
- * 配置模型
- */
- class Mobile extends Model
- {
- const S_DOWN=2;
- public static $status=[
- 0=>'正常',
- 1=>'已售',
- self::S_DOWN=>'已下架',
- 3=>'禁止下单',
- ];
- public static $flowStatus=[
- '0'=>'售卖中',
- //'1'=>'仓库中',
- '2'=>'已下架',
- ];
- protected $append=[
- 'saled',
- 'is_private',
- ];
- protected $readonly=['no'];
- protected $autoWriteTimestamp='int';
- protected $hidden=[];
- 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 holdLog(){
- return $this->hasMany(MobileHoldLog::class);
- }
- public function sub(){
- return $this->hasMany(MobileSub::class);
- }
- public function priceLog(){
- return $this->hasMany(MobilePriceLog::class);
- }
- const BEAUTI=1;
- const FLOW=2;
- const API=3;
- public static $noTypes=[
- self::BEAUTI=>'靓号',
- self::FLOW=>'流量卡',
- self::API=>'api号码',
- ];
- public static function isTypeNo($type){
- return in_array($type,[self::BEAUTI,self::API]);
- }
- public static function isType($type){
- return in_array($type,array_keys(self::$noTypes));
- }
- public function getSaledAttr($a,$b){
- $s=$b['status']??0;
- return $s===1?1:0;
- }
- #是否是民营
- public function getIsPrivateAttr($_,$data){
- $network=$data['network']?:'';
- return mb_strpos($network,'中国')===false;
- }
- public function getLogoAttr($logo,$data=[]){
- if($logo){
- $arr=explode(',',$logo);
- return array_shift($arr);
- }
- return null;
- }
- 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'];
- $mobile['activity_time']=time();
- }
- }
- public function makeArea(){
- $mobile=$this;
- if(empty($mobile['city_id'])){
- $mobile['city_id']=Area::getIdByName($mobile['city']??'',2);
- }
- if(empty($mobile['province_id'])){
- if(!empty($mobile['province'])) {
- $mobile['province_id'] = Area::getIdByName($mobile['province'],1);
- }
- if(!empty($mobile['city_id'])){
- $mobile['province_id'] = Area::where('id',$mobile['city_id'])->value('pid');
- }
- }
- if(!empty($mobile['province_id'])){
- $mobile['province']=Area::where('id',$mobile['province_id'])->cache(true)->value('shortname');
- }
- if(!empty($mobile['city_id'])){
- $mobile['city']=Area::where('id',$mobile['city_id'])->cache(true)->value('shortname');
- }
- }
- public function makePriceLog(){
- (new MobilePriceLogService)
- ->setMobile($this)
- ->setBeforePrice($this->origin['amount'])
- ->setAfterPrice($this['amount'])
- ->log();
- }
- public function makeRules(){
- $mobile=$this;
- if($this['type']==self::BEAUTI) {
- foreach (MobileComputer::setMobile($mobile['no'])->filter() as $key => $value) {
- $mobile[$key] = $value;
- }
- }
- }
- public function amountChangeColumn(){
- return [
- 'amount_base',
- 'amount_charge',
- 'amount_kill',
- 'is_activity',
- ];
- }
- public static function init()
- {
- self::beforeWrite(function (self $mobile){
- if(isset($mobile['amount_di']) && isset($mobile['amount_base']) && $mobile['amount_di']>$mobile['amount_base']){
- throw_user('底价不能大于售价');
- }
- });
- self::beforeUpdate(function (self $mobile){
- $data=$mobile->getChangedData();
- if(isset($data['is_activity'])){
- if($data['is_activity']==1) {
- $mobile['activity_time'] = time();
- }else{
- $mobile->makeNotActivity(false);
- }
- }
- if(isset($data['no'])){
- $mobile->makeRules();
- }
- if(array_intersect(array_keys($data),$mobile->amountChangeColumn())){
- $mobile->makeAmount();
- }
- $mobile->makeArea();
- $mobile->makePriceLog();
- $mobile->makeNetwork(true);
- if(isset($data['sort']) && $data['sort']!=$mobile->origin['sort']){
- $mobile->makeSort();
- }
- $mobile->makeSortLine();
- if($mobile['is_activity'] && $mobile['amount_di']>$mobile['amount_kill']){
- if(request()->module()=='api'){
- throw_user('该号码已是最低优惠');
- }else{
- throw_user('底价不能大于秒杀价');
- }
- }
- });
- self::beforeInsert(function (self $mobile){
- $mobile['sort']=self::max('sort')+1;
- $mobile->makeAmount();
- $mobile->makeRules();
- $mobile->makeArea();
- $mobile->makeNetwork();
- $mobile['sort_line']=$mobile['sort'];
- });
- self::afterInsert(function (self $mobile){
- if($mobile['type']==self::BEAUTI) {
- MobileId::insert(['id' => $mobile['id']]);
- }
- #api号码增加api
- MobileApi::create([
- 'mobile_id'=>$mobile['id'],
- ]);
- });
- self::afterDelete(function (self $mobile){
- $mobile->makeDelete();
- });
- self::afterUpdate(function (self $mobile){
- if(in_array($mobile['type'],[self::BEAUTI,self::API])) {
- EsMobileService::addMobile($mobile);
- }
- });
- }
- public function makeNewSort($type){
- }
- public function makeSortLine(){
- if($this['top_time']){
- $this['sort_line'] = -100*(10-$this['top_time']);
- }
- elseif($this['rec_time']){
- $this['sort_line'] = -10*(10-$this['rec_time']);
- }
- if($this['is_activity']){
- $this['sort_line']=-$this['activity_time'];
- }
- if(!$this['rec_time'] && !$this['top_time'] && !$this['is_activity']){
- $this['sort_line']=$this['sort'];
- }
- }
- public function makeNetwork($upd=false){
- $no=$this['no']??'';
- if(!$upd){
- $needMake=true;
- }else{
- $changed=$this->getChangedData();
- $needMake=isset($changed['no']);
- }
- if($needMake) {
- $this['network'] = Network::getNetWorkString($no);
- }
- }
- public function makeDelete(){
- $this->info()->delete();
- $this->sub()->delete();
- $this->priceLog()->delete();
- $this->holdLog()->delete();
- EsMobileService::delMobile($this);
- MobileId::destroy($this['id']);
- MobileUserHistory::where('mobile_id',$this['id'])->delete();
- }
- protected function initialize()
- {
- parent::initialize(); // TODO: Change the autogenerated stub
- static $columns=null;
- if(is_null($columns)){
- $columnsArr=self::getTableInfo('','fields');
- $columns=collection($columnsArr)->filter(function ($column){
- return Str::startsWith($column,'filter_');
- });
- }
- $this->hidden=$columns?:[];
- }
- public static function show($param=[]){
- $model=new self();
- $model->with(['info']);
- if(isset($param['type'])) {
- if($param['type']==1) {
- $model->whereIn('status', self::beautiFrontShowStatus());
- }else{
- $model->where('status',0);
- }
- }
- return $model->getQuery();
- }
- public static function beautiFrontShowStatus(){
- return [0,3];
- }
- public static function hiddenColumn(){
- $columnsArr=self::getTableInfo('','fields');
- $columns=collection($columnsArr)->filter(function ($column){
- return Str::startsWith($column,'filter_');
- });
- //$columns[]='sort';
- //$columns[]='sort_line';
- $columns[]='sub_admin_id';
- $columns[]='sub_rec_time';
- $columns[]='sub_sort';
- //$columns[]='sub_sort_line';
- $columns[]='sub_top_time';
- $columns[]='update_time';
- $columns[]='create_time';
- $columns[]='mobile_id';
- $columns[]='hold_chan';
- $columns[]='hold_user';
- $columns[]='admin_id';
- $columns[]='mobile_sub_id';
- $columns[]='remark';
- $columns[]='remark_me';
- //$columns[]='rec_time';
- $columns[]='proxy_id';
- $columns[]='batch_no';
- $columns[]='mobile_sub_id';
- //$columns[]='top_time';
- $columns[]='amount_di';
- return $columns->toArray();
- }
- public function makeHidden(){
- foreach ($this->toArray() as $key=>$_){
- if(Str::startsWith($key,'filter_')){
- $this->hidden([$key]);
- }
- }
- }
- /**
- *@param self $mobile
- */
- public static function whenOrderPayed($mobile){
- if($mobile){
- if(self::isTypeNo($mobile['type'])) {
- $mobile['status'] = 1;
- $mobile->makeNotActivity();
- $mobile->save();
- }else{
- $mobile['stock_num']=$mobile['stock_num']-1;
- if($mobile['stock_num']) {
- $mobile->save();
- }
- }
- }
- }
- public function shouldBuy(){
- if($this['type']==self::BEAUTI){
- if($this['status']!=0){
- throw_user('该号码已售或不存在');
- }
- }elseif ($this['type']==2){
- if($this['status']!=0){
- throw_user('该流量卡暂时无法购买');
- }
- if($this['stock_num']==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 getActivityTimeEndAttr($a){
- if(!$a){
- $a=3;
- }
- return time()+$a*60;
- }
- public function setIsActivityAttr($a){
- return intval($a);
- }
- public function addViewCount(){
- $num=Cache::get($this->viewCountCacheName(),0);
- Cache::set($this->viewCountCacheName(),++$num);
- }
- public function needCheckSmsCode(){
- return in_array($this['type'],[self::BEAUTI, self::FLOW]);
- }
- #禁止下单
- public static function cantOrderStatus(){
- return 3;
- }
- #导入状态
- public static function importStatus($s){
- $s=preg_replace('/[\s| ]+/','',$s);
- switch ($s){
- case '正常':
- return 0;
- case '禁止下单':
- return 3;
- case '下架':
- return 2;
- case '已售':
- return 1;
- }
- return 2;
- }
- #流量卡状态
- public static function flowStatus(){
- return self::$flowStatus;
- }
- #流量卡下架
- public function flowMakeDown(){
- $this['status']=2;
- $this->save();
- }
- public function flowMakeUp(){
- $this['status']=0;
- $this->save();
- }
- public function flowMakeDownProxy(){
- $this['proxy_status']=2;
- $this->save();
- }
- public function flowMakeUpProxy(){
- $this['proxy_status']=0;
- $this->save();
- }
- #设置为非活动
- public function makeNotActivity($save=true){
- $this['is_activity'] = 0;
- $this['activity_time'] = 0;
- $this['hold_chan']=0;
- $this['hold_user']=0;
- if($save) {
- $this->save();
- }
- }
- #设置活动
- public function makeActivity($amountKill,$chanId){
- $this['is_activity'] = 1;
- $this['activity_time'] = time();
- $this['hold_chan']=$chanId;
- $this['hold_user']=$chanId;
- $this['amount_kill']=$amountKill;
- $this->save();
- }
- #已售
- public function scopeFilterSaled(Query $query){
- $query->where('status',1);
- }
- #靓号
- public static function beauti($type=self::BEAUTI){
- return self::where('type',$type);
- }
- public function makeSort()
- {
- $sort = $this['sort'];
- $has = self::beauti($this['type'])->where('id', '<>', $this['id'])->where('sort', $sort)->find();
- if ($has) {
- self::beauti($this['type'])->where('sort', '>=', $sort)->setInc('sort');
- self::beauti($this['type'])->where('is_activity', 0)->where('top_time', 0)->where('rec_time', 0)->update([
- 'sort_line' => Db::raw('sort')
- ]);
- }
- }
- public function getAmountBaseAttr($amount){
- return str_replace('.00','',$amount);
- }
- public function getAmountAttr($amount){
- return str_replace('.00','',$amount);
- }
- public function getCopyWordAttr($_,$model){
- if($model['type']!=self::BEAUTI){
- return '';
- }
- $config=config('site.copy_words')?:[];
- $middle=array_filter(array_keys($config)?:['优惠']);
- shuffle($middle);
- $tail=array_filter(array_values($config)?:[]);
- shuffle($tail);
- $middleOne=$middle[0]??'';
- $tailOne=$tail[0]??'';
- return sprintf('%s %s %s %s %s %s',$model['province'],$model['city'],substr($model['no'],3,4),$middleOne,substr($model['no'],7,4),$tailOne);
- }
- public function getAmountKillAttr($amount){
- return str_replace('.00','',$amount);
- }
- public function getAmountDiAttr($amount){
- return str_replace('.00','',$amount);
- }
- public function getAmountOriginalAttr($amount){
- return str_replace('.00','',$amount);
- }
- public function getAmountChargeAttr($amount){
- return str_replace('.00','',$amount);
- }
- public function getAmountExistsAttr($amount){
- return str_replace('.00','',$amount);
- }
- public static function deleteOtherTableInfo(){
- MobileInfo::whereNotExists('select * from mobile where mobile_info.mobile_id=mobile.id')->delete();
- MobilePriceLog::whereNotExists('select * from mobile where mobile_price_log.mobile_id=mobile.id')->delete();
- MobileSub::whereNotExists('select * from mobile where mobile_sub.mobile_id=mobile.id')->delete();
- MobileUserHistory::whereNotExists('select * from mobile where mobile_user_history.mobile_id=mobile.id')->delete();
- MobileApi::deleteNotExists();
- }
- public function again_sell(){
- if($this['status']==0){
- throw_user('当前已是上架状态');
- }
- $this['status']=0;
- $this->save();
- }
- public static function beautiStatus(){
- return self::$status;
- }
- /**
- * 只发货
- */
- public function sendTemplate(){
- return $this->belongsTo(MobileFlowAreaTemplate::class, 'send_template', 'id');
- }
- /**
- * 不发货
- */
- public function notSendTemplate(){
- return $this->belongsTo(MobileFlowAreaTemplate::class, 'not_send_template', 'id');
- }
- /**
- * 只发货
- * dataDistrict 选择的城市
- */
- public static function sendTemplateLimit(Mobile $mobile, $dataDistrict = 0){
- if(!$dataDistrict) return false;
- $district = $mobile->sendTemplate->area()->where('district', $dataDistrict)->count();
- if(!$district){
- $tmp = Area::getTreeId($dataDistrict);
- $city = $mobile->sendTemplate->area()->where('city', $tmp[1])->where('district', 0)->count();
- if(!$city){
- $province = $mobile->sendTemplate->area()->where('province', $tmp[0])->where('city', 0)->where('district', 0)->count();
- if(!$province){
- return false;
- }
- }
- }
-
- return true;
- }
- /**
- * 不发货
- * dataDistrict 选择的城市
- */
- public static function notSendTemplateLimit(Mobile $mobile, $dataDistrict = 0){
- if(!$dataDistrict) return true;
- $district = $mobile->notSendTemplate->area()->where('district', $dataDistrict)->count();
- if(!$district){
- $tmp = Area::getTreeId($dataDistrict);
- $city = $mobile->notSendTemplate->area()->where('city', $tmp[1])->where('district', 0)->count();
- if(!$city){
- $province = $mobile->notSendTemplate->area()->where('province', $tmp[0])->where('city', 0)->where('district', 0)->count();
- if(!$province){
- return false;
- }
- }
- }
-
- return true;
- }
- public function fill($data){
- foreach ($data as $name=>$val){
- $this[$name]=$val;
- }
- }
- }
|