123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- <?php
- namespace App\Models;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\Validator;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use \Exception;
- class User extends Model
- {
- use SoftDeletes;
- protected $table='users';
- protected $hidden=['level_change','openid','unionid','sex',
- 'province','city','country','register_type','password'];
- protected $fillable = ['mobile','name',
- 'nickname','realname','cre_num',
- 'level','status','cert_status','agent_id',
- 'recom_id','recom_code','openid','headimgurl',
- 'warea_id','crown_id','store_id','register_type','auth_code','auth_img','auth_startime','auth_endtime','password','login_log_count','service_status'];
- private static $validate = [
- //提示信息
- 'message' => [
- 'mobile.required'=>'手机号不能为空.',
- 'mobile.regex'=>'手机号格式有误.',
- 'mobile.unique'=>'手机号已存在.',
- 'cre_num.unique'=>'身份证号已存在.',
- 'share.required'=>'邀请人手机号不能为空.',
- 'share.regex'=>'邀请人手机号格式有误.',
- 'nickname.required'=>'代理昵称不能为空.',
- 'name.required'=>'名称不能为空.',
- 'address.required'=>'地址不能为空.',
- 'level.required'=>'请选择代理等级.',
- 'level.numeric'=>'代理等级有误.',
- ]
- ];
- public static function validator($data,$rule)
- {
- return Validator::make(
- $data,$rule,
- self::$validate['message']
- );
- }
- public function changeAgentLevel($user,$level){
- // $user = User::find($id);//当前用户信息(等级变更前)
- $recom = User::find($user->recom_id);//邀请人信息
- $data['auth_img']=null;
- if ($user->level > $level) {
- $change = '1';//降级
- $data['level_change']='-1';
- } else {
- $change = '2';//升级
- $data['level_change']='1';
- }
- $data['level'] = $level;
- if ($level == '3') { //升到皇冠上级直接是公司
- $data['agent_id'] = '1';
- $data['crown_id']=NULL;
- } else {
- if ($level == $recom->level) {
- $data['agent_id'] = $recom->agent_id;
- $data['crown_id']=$recom->crown_id;
- $data['warea_id']=$recom->warea_id;
- }
- if ($level < $recom->level) {
- $data['agent_id'] = $recom->id;
- if($recom->level=='3'){
- $data['crown_id']=$recom->id;
- $data['warea_id']=$recom->warea_id;
- }else{
- $data['crown_id']=$recom->crown_id;
- $data['warea_id']=$recom->warea_id;
- }
- }
- if ($level > $recom->level) {
- $agent = User::find($recom->agent_id);//邀请人上级信息
- if($agent->level=='3'){
- $data['agent_id']=$agent->id;
- }else{
- $data['agent_id'] = $agent->agent_id;
- }
- $data['crown_id']=$recom->crown_id;
- $data['warea_id']=$recom->warea_id;
- }
- }
- try {
- User::where('id', $user->id)->update($data);
- if ($change == '1') {//降级
- $newInfo = User::find($user->id);//级别变更后代理人信息
- $warea_id=User::where('id',$newInfo->agent_id)->value('warea_id');
- if($level=='2'){
- $this->getAgentDownCrownId($user->id,$newInfo->agent_id,$warea_id);
- }
- User::where('agent_id', $user->id)->where('level', $newInfo->level)->update(['agent_id'=>$newInfo->agent_id]);
- }elseif($change == '2'){//升级
- if($level=='2'){
- $this->getAgentLevelInfoOne($user->id,$user->id);
- }elseif($level=='3'){
- $this->getAgentLevelInfoTwo($user->id,$user->id);
- // $warea_id=User::where('id',$user->id)->value('warea_id');
- $this->getAgentUpCrownId($user->id,$user->id);
- }
- }
- return true;
- }Catch(Exception $e){
- return false;
- }
- }
- //特级升顶级,被邀请的人上级变更
- public function getAgentLevelInfoOne($uid,$aid){
- $list=User::where('recom_id', $uid)->get();
- if($list){
- for($i=0;$i<count($list);$i++){
- if($list[$i]->level=='1'){
- User::where('id', $list[$i]->id)->update(['agent_id'=>$aid]);
- $this->getAgentLevelInfoOne($list[$i]->id,$aid);
- }
- }
- }
- }
- //顶级升皇冠,被邀请的人上级变更
- public function getAgentLevelInfoTwo($uid,$aid){
- $list=User::where('recom_id', $uid)->get();
- if($list){
- for($i=0;$i<count($list);$i++){
- if($list[$i]->level=='2'){
- User::where('id', $list[$i]->id)->update(['agent_id'=>$aid]);
- $this->getAgentLevelInfoTwo($list[$i]->id,$aid);
- }
- if($list[$i]->level=='1'){
- $this->getAgentLevelInfoTwo($list[$i]->id,$aid);
- }
- }
- }
- }
- //顶级升皇冠时,下级代理的皇冠,战区变更
- public function getAgentUpCrownId($uid,$cid){
- $list=User::where('recom_id', $uid)->get();
- if($list){
- foreach($list as $key=>$val){
- if($val->crown_id!=$uid){
- User::where('id', $val->id)->update(['crown_id'=>$cid]);
- $this->getAgentUpCrownId($val->id,$cid);
- }
- }
- }
- }
- //皇冠降顶级时,下级代理的皇冠,战区变更
- public function getAgentDownCrownId($uid,$cid,$wid){
- $list=User::where('agent_id', $uid)->get();
- if($list){
- foreach($list as $key=>$val){
- User::where('id', $val->id)->update(['crown_id'=>$cid,'warea_id'=>$wid]);
- $childAgent=User::where('agent_id', $val->id)->get();
- if($childAgent){
- foreach($childAgent as $k=>$v){
- User::where('id', $v->id)->update(['crown_id'=>$cid,'warea_id'=>$wid]);
- }
- }
- }
- }
- }
- //顶级邀请人变更
- public function changeAgentLevelInfoTwo($uid,$aid){
- $list=User::where('recom_id', $uid)->get();
- if($list){
- for($i=0;$i<count($list);$i++){
- if($list[$i]->level=='2'){
- User::where('id', $list[$i]->id)->update(['agent_id'=>$aid]);
- $this->changeAgentLevelInfoTwo($list[$i]->id,$aid);
- }
- if($list[$i]->level=='1'){
- $this->changeAgentLevelInfoTwo($list[$i]->id,$aid);
- }
- }
- }
- }
- //te 级邀请人变更
- // public function changeAgentLevelInfoOne($uid,$aid){
- // $list=User::where('recom_id', $uid)->get();
- // if($list){
- // for($i=0;$i<count($list);$i++){
- // if($list[$i]->level=='1' || $list[$i]->level=='2'){
- // User::where('id', $list[$i]->id)->update(['agent_id'=>$aid]);
- // $this->changeAgentLevelInfoOne($list[$i]->id,$aid);
- // }
- // }
- // }
- // }
- //te 级邀请人变更 3
- public function changeAgentLevelInfoThree($uid,$aid,$rid){
- $list=User::where('recom_id', $uid)->get();
- if($list){
- for($i=0;$i<count($list);$i++){
- if($list[$i]->level=='2'){
- User::where('id', $list[$i]->id)->update(['agent_id'=>$aid]);
- $this->changeAgentLevelInfoTwo($list[$i]->id,$aid);
- }
- if($list[$i]->level=='1'){
- User::where('id', $list[$i]->id)->update(['agent_id'=>$rid]);
- $this->changeAgentInfoSuper($list[$i]->id,$aid,$rid);
- }
- }
- }
- }
- public function changeAgentRecomCrown($uid,$crown_id,$warea_id){
- $list=User::where('recom_id', $uid)->get();
- if($list){
- for($i=0;$i<count($list);$i++){
- if($list[$i]->level=='2' || $list[$i]->level=='1'){
- User::where('id', $list[$i]->id)->update(['crown_id'=>$crown_id,'warea_id'=>$warea_id]);
- $this->changeAgentRecomCrown($list[$i]->id,$crown_id,$warea_id);
- }
- }
- }
- }
- public function changeAgentInfoSuper($uid,$aid,$rid){
- $list=User::where('recom_id', $uid)->get();
- foreach($list as $key=>$val){
- if($val->level=='1'){
- User::where('id', $val->id)->update(['agent_id'=>$rid]);
- $this->changeAgentInfoSuper($val->id,$aid,$rid);
- }elseif($val->level=='2'){
- User::where('id', $val->id)->update(['agent_id'=>$aid]);
- $this->changeAgentLevelInfoTwo($val->id,$aid);
- }
- }
- }
- public function changeRecomSystem($uid,$mobile){
- $list=User::where('recom_id', $uid)->get();
- foreach($list as $key=>$val){
- if($val->mobile==$mobile){
- return '222';
- }else{
- return $this->changeRecomSystem($val->id,$mobile);
- }
- }
- }
- public function getAgentSalesResult($user_id){
- $account=DB::table('order')
- ->leftJoin('users', 'order.user_id', '=', 'users.id')
- ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
- ->where('users.id',$user_id)
- ->where('order.status', 2)
- ->sum('totalprice');
- return $account;
- }
- public function order()
- {
- return $this->hasMany('App\Models\Order');
- }
- public function login_log()
- {
- return $this->hasMany(LoginLog::class);
- }
- public function order_test()//订单ordertest
- {
- return $this->hasMany(Ordertest::class,'user_id','id');
- }
- public function every_act_data()
- {
- return $this->hasMany(EveryActData::class,'user_id','id')->orderBy('activity_id');
- }
- public function nopayinfo()
- {
- return $this->hasMany(NoPayInfo::class);
- }
- public function user_pay()
- {
- return $this->hasMany(Userpay::class);
- }
- public function userz(){
- return $this->hasOne('App\Models\UserZ','phone','mobile');
- }
- public function ordergoods()
- {
- return $this->hasMany('App\Models\OrderGoods','ordersn');
- }
- public function warea()
- {
- return $this->hasOne('App\Models\Warea','id','warea_id');
- }
- public function recom()
- {
- return $this->belongsTo('App\Models\User','recom_id','id')->withTrashed();
- }
- public function childRecom()
- {
- return $this->hasMany('App\Models\User','id','recom_id')->withTrashed();
- }
- public function agent()
- {
- return $this->belongsTo('App\Models\User','agent_id','id')->withTrashed();
- }
- public function childAgent()
- {
- return $this->hasMany('App\Models\User','agent_id','id')->withTrashed();
- }
- public function usercert()
- {
- return $this->hasOne('App\Models\UserCert');
- }
- // public function ordergoods()
- // {
- // return $this->hasManyThrough(
- // 'App\Models\OrderGoods',
- // 'App\Models\Order',
- // 'user_id', // 用户表外键
- // 'order_id', // 订单表外键
- // 'id', // 用户表本地键
- // 'id' // 订单表本地键
- // );
- // }
- public function get_order_de(){
- return $this->hasManyThrough(Orderdetail::class,Ordertest::class,'user_id','order_id');
- }
- public function get_order_des(){
- return $this->belongsToMany(Orderdetail::class,'order_test','user_id','order_id');
- }
- /**政委审核信息**/
- public function get_service(){
- return $this->hasOne(ServiceReason::class,'user_id');
- }
- public function get_storage(){
- return $this->hasMany(UserStorage::class,'user_id');
- }
- public function get_put_storage(){
- return $this->hasMany(StoragePutLog::class,'user_id');
- }public function get_out_storage(){
- return $this->hasMany(ExpressStorage::class,'user_id');
- }
- public function get_storage_detail(){
- return $this->hasMany(ExpressStorage::class,'user_id');
- }
- public function get_storage_details(){
- return $this->hasMany(ExpressStorage::class,'user_id');
- }
- public function getQuantityAttribute(){
- return $this->get_storage()->sum('num');
- }
- public function tag_remind_storage(){
- return $this->belongsTo(TagRemind::class,'id','user_id');
- }
- }
|