|
@@ -1,458 +0,0 @@
|
|
|
-<?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;
|
|
|
-
|
|
|
-/**
|
|
|
- * 配置模型
|
|
|
- */
|
|
|
-class Mobile extends Model
|
|
|
-{
|
|
|
- public static $status=[
|
|
|
- 0=>'正常',
|
|
|
- 1=>'已售',
|
|
|
- 2=>'已下架',
|
|
|
- 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;
|
|
|
-
|
|
|
-
|
|
|
- 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'])->value('shortname');
|
|
|
- }
|
|
|
- if(!empty($mobile['city_id'])){
|
|
|
- $mobile['city']=Area::where('id',$mobile['city_id'])->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']){
|
|
|
- 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']]);
|
|
|
- }
|
|
|
- });
|
|
|
- self::afterDelete(function (self $mobile){
|
|
|
- $mobile->makeDelete();
|
|
|
- });
|
|
|
- self::afterUpdate(function (self $mobile){
|
|
|
- if($mobile['type']==self::BEAUTI) {
|
|
|
- EsMobileService::addMobile($mobile);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- public function makeSortLine(){
|
|
|
- $change=false;
|
|
|
- if($this['rec_time']){
|
|
|
- $change=true;
|
|
|
- $this['sort_line'] = -10;
|
|
|
- }
|
|
|
- if($this['top_time']){
|
|
|
- $change=true;
|
|
|
- $this['sort_line'] = -20;
|
|
|
- }
|
|
|
- if($this['is_activity']){
|
|
|
- $change=true;
|
|
|
- $this['sort_line']=-$this['activity_time'];
|
|
|
- }
|
|
|
-
|
|
|
- if(!$change){
|
|
|
- $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']);
|
|
|
- }
|
|
|
-
|
|
|
- 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', [0, 3]);
|
|
|
- }else{
|
|
|
- $model->where('status',0);
|
|
|
- }
|
|
|
- }
|
|
|
- return $model->getQuery();
|
|
|
- }
|
|
|
-
|
|
|
- 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();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- *@param self $mobile
|
|
|
- */
|
|
|
- public static function whenOrderPayed($mobile){
|
|
|
- if($mobile){
|
|
|
- if($mobile['type']==self::BEAUTI) {
|
|
|
- $mobile['status'] = 1;
|
|
|
- $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]);
|
|
|
- }
|
|
|
- #禁止下单
|
|
|
- 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 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 function makeSort()
|
|
|
- {
|
|
|
- $sort=$this['sort'];
|
|
|
- $has=self::where('id','<>',$this['id'])->where('sort',$sort)->find();
|
|
|
- if($has) {
|
|
|
- self::where('sort', '>=', $sort)->setInc('sort');
|
|
|
- self::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 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();
|
|
|
- }
|
|
|
-
|
|
|
- public function again_sell(){
|
|
|
- if($this['status']==0){
|
|
|
- throw_user('当前已是上架状态');
|
|
|
- }
|
|
|
- $this['status']=0;
|
|
|
- $this->save();
|
|
|
- }
|
|
|
-
|
|
|
- public static function beautiStatus(){
|
|
|
- return self::$status;
|
|
|
- }
|
|
|
-}
|