123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?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 UserCopy extends Model
- {
- use SoftDeletes;
- protected $table='users_copy1';
- protected $hidden=['name','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'];
- 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 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);//邀请人信息
- if (empty($recom)) {
- return $this->error('400115', '代理邀请人信息不存在,升降级失败');
- }
- 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 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 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->hasOne('App\Models\UserCopy','id','recom_id')->select('id','nickname','mobile');
- }
- public function agent()
- {
- return $this->belongsTo('App\Models\UserCopy','agent_id','id')->select('id','nickname','mobile');
- }
- public function childAgent()
- {
- return $this->hasMany('App\Models\UserCopy','agent_id','id')->select('id','nickname','mobile','level');
- }
- 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' // 订单表本地键
- // );
- // }
- }
|