TeamController.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. <?php
  2. namespace App\Http\Controllers;
  3. /*
  4. * 人员管理
  5. * */
  6. use App\Models\AgentOperationLog;
  7. use App\Models\ApplyUser;
  8. use App\Models\IncomExpenses;
  9. use App\Models\Store;
  10. use App\Models\User;
  11. use App\Models\UserCert;
  12. use Carbon\Carbon;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\Auth;
  15. use Illuminate\Support\Facades\Cache;
  16. use Illuminate\Support\Facades\Log;
  17. class TeamController extends Controller{
  18. /*
  19. * 今日新增人数,本月新增
  20. * */
  21. public function new_per(){
  22. $user=Auth::user();
  23. Log::error('{---------}'.$user->nickname);
  24. $today=Carbon::today()->toDateTimeString();
  25. $month=Carbon::now()->firstOfMonth()->toDateTimeString();
  26. $user_all=User::whereIn('cert_status',[3,6])->where('created_at','>',$today)->get();
  27. $num=0;
  28. foreach ($user_all as $k=>$v){
  29. if ($v->agent_id==null){
  30. }else{
  31. if ($v->agent_id==$user->id){
  32. $num=$num+1;
  33. }else{
  34. $arr=User::where('id',$v->agent_id)->first();
  35. if ($arr->agent_id==$user->id){
  36. $num=$num+1;
  37. }
  38. }
  39. }
  40. }
  41. $user_all=User::whereIn('cert_status',[3,6])->where('created_at','>',$month)->get();
  42. $num_all=0;
  43. foreach ($user_all as $k=>$v){
  44. if ($v->agent_id==null){
  45. }else{
  46. if ($v->agent_id==$user->id){
  47. $num_all=$num_all+1;
  48. }else{
  49. $arr=User::where('id',$v->agent_id)->first();
  50. if ($arr->agent_id==$user->id){
  51. $num_all=$num_all+1;
  52. }
  53. }
  54. }
  55. }
  56. $data=['num'=>$num,'all_num'=>$num_all];
  57. // Log::error('{-0-----新增代理-------}'.json_encode($data));
  58. return $this->success($data);
  59. }
  60. /*
  61. * 直属代理
  62. * */
  63. public function level_count(Request $request){
  64. $user=Auth::user();
  65. // $page=($request->page)-1;
  66. $data=[];
  67. if ($request->has('page')){
  68. $page=($request->page)-1;
  69. }else{
  70. $page=0;
  71. }
  72. if ($request->type==0){
  73. $user_all=User::where('agent_id',$user->id)->where('status',0)->whereIn('cert_status',[3,6])->where('deleted_at','=',null)->orderBy('id')->get();
  74. $arr_per=[];
  75. foreach ($user_all as $k=>$v){
  76. $arr_per[$k]['id']=$v->id;
  77. $arr_per[$k]['avatar']=$v->headimgurl;
  78. $arr_per[$k]['nickname']=$v->nickname;
  79. $arr_per[$k]['level_name']=judge_level($v->level);
  80. }
  81. $num=count($arr_per);
  82. $num1=$num-$page*15;
  83. if ($num1>=15){
  84. $j=15;
  85. }else{
  86. $j=$num1;
  87. }
  88. for ($i=0;$i<$j;$i++){
  89. $data[$i]['id']=$arr_per[$page*15+$i]['id'];
  90. $data[$i]['avatar']=$arr_per[$page*15+$i]['avatar'];
  91. $data[$i]['nickname']=$arr_per[$page*15+$i]['nickname'];
  92. $data[$i]['level_name']=$arr_per[$page*15+$i]['level_name'];
  93. }
  94. // return $this->success($data);
  95. }else{
  96. $user_all=User::where('agent_id',$user->id)->where('status',0)->where('deleted_at','=',null)->orderBy('id')->whereIn('cert_status',[3,6])->get();
  97. // $user_all_1=User::where('agent_id',$user->id)->where('recom_id',$user->id)->where('status',0)->whereIn('cert_status',[3,6])->where('deleted_at','=',null)->orderBy('id')->get();
  98. $user_data=[];
  99. $arr_per=[];
  100. $all=[];
  101. $all_1=[];
  102. // foreach ($user_all as $k=>$v){
  103. // $arr=[];
  104. // $arr_per[$k]['id']=$v->id;
  105. // $arr_per[$k]['avatar']=$v->headimgurl;
  106. // $arr_per[$k]['nickname']=$v->nickname;
  107. // $arr_per[$k]['level_name']=judge_level($v->level);
  108. // $user_per=User::where('agent_id',$v->id)->where('status',0)->where('deleted_at','=',null)->orderBy('id')->whereIn('cert_status',[3,6])->get();
  109. // if ($user){
  110. // foreach ($user_per as $key=>$value){
  111. // $arr[$key]['id']=$value->id;
  112. // $arr[$key]['avatar']=$value->headimgurl;
  113. // $arr[$key]['nickname']=$value->nickname;
  114. // $arr[$key]['level_name']=judge_level($value->level);
  115. // }
  116. // $all=array_merge($all,$arr);
  117. // }
  118. // }
  119. // $user_data=array_merge($all,$arr_per);
  120. foreach ($user_all as $k=>$v){
  121. $arr_per=[];
  122. $user_per=User::where('agent_id',$v->id)->where('status',0)->where('deleted_at','=',null)->orderBy('id')->whereIn('cert_status',[3,6])->get();
  123. if ($user){
  124. foreach ($user_per as $key=>$value){
  125. $arr_per[$key]['id']=$value->id;
  126. $arr_per[$key]['avatar']=$value->headimgurl;
  127. $arr_per[$key]['nickname']=$value->nickname;
  128. $arr_per[$key]['level_name']=judge_level($value->level);
  129. }
  130. $all_1=array_merge($all_1,$arr_per);
  131. }
  132. }
  133. $user_data=array_merge($user_data,$all_1);
  134. $num=count($user_data);
  135. $num1=$num-$page*15;
  136. if ($num1>=15){
  137. $j=15;
  138. }else{
  139. $j=$num1;
  140. }
  141. for ($i=0;$i<$j;$i++){
  142. $data[$i]['id']=$user_data[$page*15+$i]['id'];
  143. $data[$i]['avatar']=$user_data[$page*15+$i]['avatar'];
  144. $data[$i]['nickname']=$user_data[$page*15+$i]['nickname'];
  145. $data[$i]['level_name']=$user_data[$page*15+$i]['level_name'];
  146. }
  147. // return $this->success($data);
  148. }
  149. $arr=$this->num_test();
  150. // $re= $arr->original;
  151. $res=[
  152. 'today_new'=>count($arr[0]),
  153. 'month_new'=>count($arr[1]),
  154. 'size'=>15,
  155. 'total'=>$num,
  156. 'list'=>$data
  157. ];
  158. // Log::error('{-------代理数量-----}'.json_encode($res));
  159. return $this->success($res);
  160. // return $this->success_list($data,'',$total['total']);
  161. }
  162. /*名片信息*/
  163. public function card_data(Request $request){
  164. $user=User::find($request->id);
  165. if ($user->recom_id==null){
  166. $users["recom_nickname"]= '';
  167. }else{
  168. $users["recom_nickname"]= $user->recom_user->nickname;
  169. }
  170. $users["id"]=$user->id;
  171. $users["nickname"]=$user->nickname;
  172. $users["real_name"]= $user->realname;
  173. $users["avatar"]= $user->headimgurl;
  174. $users["level"]= judge_level($user->level);
  175. $users["mobile"]= $user->mobile;
  176. $users["up_nickname"]= $user->agent_user->nickname;
  177. $users["team_num"]= User::where('agent_id',$user->id)->count();
  178. if ($user->cert_status==3||$user->cert_status==6){
  179. if ($user->auth_startime &&$user->auth_code){
  180. $users['status']=0;
  181. }else{
  182. $users['status']=1;
  183. }
  184. }else{
  185. $users['status']=1;
  186. }
  187. return $this->success($users);
  188. }
  189. /*---团队业绩---*/
  190. public function money(Request $request){
  191. $user=Auth::user();
  192. if ($request->type==0){
  193. $user_all=User::where('agent_id',$user->id)->orderByDesc('created_at')->where('deleted_at','=',null)->paginate(15);
  194. }elseif ($request->type==1){
  195. $user_all=User::where('agent_id',$user->id)->orderByDesc('created_at')->where('deleted_at','=',null)->where('level',2)->paginate(15);
  196. }else{
  197. $user_all=User::where('agent_id',$user->id)->orderByDesc('created_at')->where('deleted_at','=',null)->where('level',1)->paginate(15);
  198. }
  199. if ($request->time==0){
  200. $time=Carbon::now()->toDateTimeString();
  201. }elseif ($request->time==1){
  202. $time=Carbon::now()->startOfDay()->toDateTimeString();
  203. }else{
  204. $time=Carbon::now()->startOfMonth()->toDateTimeString();
  205. }
  206. $total=$user_all->toArray();
  207. $data=[];
  208. $money=0;
  209. foreach ($user_all as $k=>$v){
  210. $data[$k]['money']=IncomExpenses::where('user_id',$v->id)
  211. ->where('created_at','>=',$time)->sum('totalprice');
  212. $money=$money+$data[$k]['money'];
  213. $data[$k]['avatar']=$v->headimgurl;
  214. $data[$k]['level']=judge_level($v->level);
  215. $data[$k]['nickname']=$v->nickname;
  216. }
  217. return $this->success_list($data,'',$total['total'],$money,'money');
  218. }
  219. /*我的邀请*/
  220. public function invite(Request $request){
  221. $id=Auth::user()->id;
  222. if ($request->type==0){
  223. $type='=';
  224. $user_all=User::where('recom_id',$id)->whereIn('cert_status',[3,6])->where('deleted_at','=',null)->where('status',0)->paginate(15);
  225. }elseif ($request->type==1){
  226. $type='<>';
  227. $user_all=User::where('recom_id',$id)->whereIn('cert_status',[1,2,4,5])->where('deleted_at','=',null)->where('status',0)->paginate(15);
  228. }
  229. $total=$user_all->toArray();
  230. $data=[];
  231. foreach ($user_all as $k=>$v){
  232. $data[$k]['id']=$v->id;
  233. $data[$k]['avatar']=$v->headimgurl;
  234. $data[$k]['level']=judge_level($v->level);
  235. $data[$k]['nickname']=$v->nickname;
  236. $data[$k]['created_at']=Carbon::parse($v->created_at)->toDateTimeString();
  237. }
  238. $count=User::where('agent_id',$id)->where('status',0)->whereIn('cert_status',[3,6])->count();
  239. // $arr=$this->new_per();
  240. // $re= $arr->original;
  241. $res=[
  242. // 'today_new'=>$re['data']['num'],
  243. // 'month_new'=>$re['data']['all_num'],
  244. 'size'=>15,
  245. 'total'=>$user_all->total(),
  246. 'list'=>$data
  247. ];
  248. return $this->success($res);
  249. }
  250. public function add_list(Request $request){
  251. if ($request->has('page')){
  252. $page=($request->page)-1;
  253. }else{
  254. $page=0;
  255. }
  256. $re=$this->num_test();
  257. if ($request->type==0){
  258. $num=count($re[0]);
  259. $arr_per=$re[0];
  260. $num1=$num-$page*15;
  261. if ($num1>=15){
  262. $j=15;
  263. }else{
  264. $j=$num1;
  265. }
  266. $data=[];
  267. for ($i=0;$i<$j;$i++){
  268. $data[$i]['id']=$arr_per[$page*15+$i]['id'];
  269. $data[$i]['avatar']=$arr_per[$page*15+$i]['avatar'];
  270. $data[$i]['nickname']=$arr_per[$page*15+$i]['nickname'];
  271. $data[$i]['level_name']=$arr_per[$page*15+$i]['level_name'];
  272. }
  273. return $this->success_list($data);
  274. }else{
  275. $num=count($re[1]);
  276. $arr_per=$re[1];
  277. $num1=$num-$page*15;
  278. if ($num1>=15){
  279. $j=15;
  280. }else{
  281. $j=$num1;
  282. }
  283. $data=[];
  284. for ($i=0;$i<$j;$i++){
  285. $data[$i]['id']=$arr_per[$page*15+$i]['id'];
  286. $data[$i]['avatar']=$arr_per[$page*15+$i]['avatar'];
  287. $data[$i]['nickname']=$arr_per[$page*15+$i]['nickname'];
  288. $data[$i]['level_name']=$arr_per[$page*15+$i]['level_name'];
  289. }
  290. return $this->success_list($data);
  291. }
  292. }
  293. public function num_test(){
  294. $id=Auth::user()->id;
  295. // $id=DB::table('users')->where('mobile',13009681680)->value('id');
  296. $user=User::with(['recom_user:id,nickname,mobile','agent_user:id,nickname,mobile'
  297. // ,'childAgent'=>function($query){$query->select('id','agent_id','nickname','level','mobile');}
  298. ])->find($id);
  299. //,'childAgent'=>function($query){$query->select('id','agent_id','nickname','level','mobile');}
  300. if($user){
  301. if($user->level=='3'){
  302. $childAgent=User::where('agent_id',$user->id)->select('id', 'nickname','level','mobile','created_at','headimgurl as avatar')->orderBy('id')->get()->toArray();
  303. $childAgentIds=array_column($childAgent,'id');
  304. if($childAgentIds){
  305. $childChildAgent=User::whereIn('agent_id',$childAgentIds)->select('id', 'nickname','level','mobile','created_at','headimgurl as avatar')->orderBy('id')->get()->toArray();
  306. $agent=array_merge($childAgent,$childChildAgent);
  307. }else{
  308. $agent=$childAgent;
  309. }
  310. $re=[];
  311. $res=[];
  312. $i=0;
  313. $j=0;
  314. foreach ($agent as $k=>$v){
  315. if (strtotime($v['created_at'])>mktime(0,0,0,date('m'),1,date('Y'))){
  316. // $re[$i]=$v;
  317. $re[$i]['nickname']=$v['nickname'];
  318. $re[$i]['id']=$v['id'];
  319. $re[$i]['avatar']=$v['avatar'];
  320. $re[$i]['level_name']=judge_level($v['level']);
  321. // $re[$i]['up']=User::where('id',$v['recom_id'])->value('nickname');
  322. $i++;
  323. }
  324. if (strtotime($v['created_at'])>Carbon::today()->timestamp){
  325. // $res[$j]=$v;
  326. $res[$j]['nickname']=$v['nickname'];
  327. $res[$j]['id']=$v['id'];
  328. $res[$j]['avatar']=$v['avatar'];
  329. $res[$j]['level_name']=judge_level($v['level']);
  330. // $re[$j]['up']=User::where('id',$v['recom_id'])->value('nickname');
  331. $j++;
  332. }
  333. }
  334. }elseif($user->level=='2'){
  335. $user->child_agent=User::where('agent_id',$user->id)->select('id', 'nickname','level','mobile','created_at','headimgurl as avatar')->orderBy('id')->get()->toArray();
  336. $agent=$user->child_agent;
  337. $re=[];
  338. $res=[];
  339. $i=0;
  340. $j=0;
  341. foreach ($agent as $k=>$v){
  342. if (strtotime($v['created_at'])>mktime(0,0,0,date('m'),1,date('Y'))){
  343. // $re[$i]=$v;
  344. $re[$i]['nickname']=$v['nickname'];
  345. $re[$i]['id']=$v['id'];
  346. $re[$i]['avatar']=$v['avatar'];
  347. $re[$i]['level_name']=judge_level($v['level']);
  348. // $re[$i]['up']=User::where('id',$v['recom_id'])->value('nickname');
  349. $i++;
  350. }
  351. if (strtotime($v['created_at'])>Carbon::today()->timestamp){
  352. // $res[$j]=$v;
  353. $res[$j]['nickname']=$v['nickname'];
  354. $res[$j]['id']=$v['id'];
  355. $res[$j]['avatar']=$v['avatar'];
  356. $res[$j]['level_name']=judge_level($v['level']);
  357. // $re[$j]['up']=User::where('id',$v['recom_id'])->value('nickname');
  358. $j++;
  359. }
  360. }
  361. }else{
  362. $re=[];
  363. $res=[];
  364. }
  365. // Cache::put('team_list'.$id,[$re,$res]);
  366. // return [count($re),count($res)];
  367. // return $this->success_list($re);
  368. return [$res,$re];
  369. }else{
  370. return [];
  371. }
  372. }
  373. /**邀请码**/
  374. public function invite_code(){
  375. $user=Auth::user();
  376. $invite_num=UserCert::where('agent_id',$user->id)->where('agent_audit',0)->count();
  377. $team_num=User::where('agent_id',$user->id)->count();
  378. return $this->success([
  379. 'invite_code'=>$user->recom_code,
  380. 'incite_num'=>(int)$invite_num,
  381. 'team_num'=>$team_num
  382. ]);
  383. }
  384. public function GetUserTeamListNum(Request $request){
  385. $user=Auth::user();
  386. $id=$user->id;
  387. $totay_num=$this->GetNewUserTotal($id,Carbon::today());
  388. $month_num=$this->GetNewUserTotal($id,Carbon::now()->startOfMonth());
  389. $counts=$this->getStore($id);
  390. $applyCount=ApplyUser::where('user_id',$id)->count();
  391. $levelUp=0;
  392. if ($user->level==3){
  393. $id_arr=User::where('agent_id',$user->id)->where('level',2)->pluck('id');
  394. $levelUp=\DB::table('agent_operation_log')->whereIn('user_id',$id_arr)->where('type',2)
  395. ->where('created_at','>',Carbon::now()->startOfYear()->toDateTimeString())->count();
  396. }
  397. $data=[
  398. 'totay_num'=>$totay_num,
  399. 'month_num'=>$month_num,
  400. 'store_no_num'=>$counts,
  401. 'year_num'=>$this->GetNewUserTotal($id,Carbon::now()->startOfYear()),
  402. 'level_up'=>$levelUp,
  403. 'applyCount'=>$applyCount,
  404. ];
  405. return $this->success($data);
  406. }
  407. public function GetUserTeamList(Request $request){
  408. $user=Auth::user();
  409. $arr=['crown'=>0,'manager'=>0,'lowest'=>0];
  410. // if ($user->level==1){
  411. // $data=[
  412. // 'totay_num'=>0,
  413. // 'month_num'=>0,
  414. // 'group'=>$arr
  415. // ];
  416. // return $this->success($data);
  417. // }
  418. $id=$user->id;
  419. // if ($id==39516){
  420. // $user=User::where('mobile',18753967396)->first();
  421. // $id=$user->id;
  422. // }
  423. $s=false;
  424. if ($user->level==1){
  425. $s=true;
  426. }
  427. $type=$request->type;
  428. // $id=7;
  429. if ($type==0){
  430. $data=User::where('status',0)
  431. ->where('service_status',0)
  432. ->whereNull('deleted_at')
  433. ->where('recom_id',$id)
  434. ->when($s,function ($q){
  435. return $q->where('level','<>',3);
  436. })
  437. ->select('users.level',\DB::raw('count(*) as total'))->groupBy('level')->get();
  438. }else{
  439. $arrs=User::where('agent_id',$id)->where('status',0)
  440. ->where('service_status',0)
  441. ->whereNull('deleted_at')
  442. ->pluck('id')->toArray();
  443. array_push($arrs,$id);
  444. $data=User::where('status',0)
  445. ->where('service_status',0)
  446. ->whereNull('deleted_at')
  447. ->whereIn('agent_id',$arrs)
  448. ->select('users.level',\DB::raw('count(*) as total'))->groupBy('level')->get();
  449. }
  450. foreach ($data as $k=>$v){
  451. if ($v->level==1){
  452. $arr['lowest']=$v->total;
  453. }
  454. if ($v->level==2){
  455. $arr['manager']=$v->total;
  456. }
  457. if ($v->level==3){
  458. $arr['crown']=$v->total;
  459. }
  460. }
  461. // $totay_num=$this->GetNewUserTotal($id,Carbon::today());
  462. // $month_num=$this->GetNewUserTotal($id,Carbon::now()->startOfMonth());
  463. // $counts=$this->getStore($id);
  464. // $applyCount=ApplyUser::where('user_id',$id)->count();
  465. // $levelUp=0;
  466. // if ($user->level==3){
  467. // $id_arr=User::where('agent_id',$user->id)->where('level',2)->pluck('id');
  468. // $levelUp=\DB::table('agent_operation_log')->whereIn('user_id',$id_arr)->where('type',2)
  469. // ->where('created_at','>',Carbon::now()->startOfYear()->toDateTimeString())->count();
  470. // }
  471. $data=[
  472. // 'totay_num'=>$totay_num,
  473. // 'month_num'=>$month_num,
  474. // 'store_no_num'=>$counts,
  475. // 'year_num'=>$this->GetNewUserTotal($id,Carbon::now()->startOfYear()),
  476. 'group'=>$arr,
  477. // 'level_up'=>$levelUp,
  478. // 'applyCount'=>$applyCount,
  479. ];
  480. return $this->success($data);
  481. }
  482. public function GetLevelUp(){
  483. $user=Auth::user();
  484. $id_arr=User::where('agent_id',$user->id)->where('level',2)->pluck('id');
  485. $query=\DB::table('agent_operation_log')->whereIn('user_id',$id_arr)->where('type',2);
  486. $query1=clone $query;
  487. $query2=clone $query;
  488. $yesterday=$query->where('created_at','>',Carbon::yesterday()->toDateTimeString())->count();
  489. $month=$query1->where('created_at','>',Carbon::now()->startOfMonth()->toDateTimeString())->count();
  490. $year=$query2->where('created_at','>',Carbon::now()->startOfYear()->toDateTimeString())->count();
  491. return $this->success(['yesterday'=>$yesterday,'month'=>$month,'year'=>$year]);
  492. }
  493. public function GetLevelUpList(Request $request){
  494. $page_index=$request->input('page_index');
  495. $page_size=$request->input('page_size');
  496. $num=($page_index-1)*$page_size;
  497. $search_name=$request->input('search_name')??'';
  498. $type=$request->input('type');
  499. $start_time=$request->input('time_start')??'';
  500. $end_time=$request->input('time_end')??'';
  501. $user=Auth::user();
  502. $id_arr=User::where('agent_id',$user->id)->where('level',2)->pluck('id');
  503. if ($search_name){
  504. $id_arr=User::whereIn('id',$id_arr)->where('mobile','like',"%$search_name%")
  505. ->orwhere('nickname','like',"%$search_name%")->pluck('id');
  506. }
  507. $query=AgentOperationLog::whereIn('user_id',$id_arr)->where('type',2);
  508. if ($type==0){
  509. $query->where('created_at','>',Carbon::yesterday()->toDateTimeString());
  510. }elseif($type==1){
  511. $query->where('created_at','>',Carbon::now()->startOfMonth()->toDateTimeString());
  512. }elseif($type==2){
  513. if ($start_time){
  514. $query->where('created_at','>',Carbon::now()->format('Y').'-'.$start_time.' 00:00:00')
  515. ->where('created_at','<',Carbon::now()->format('Y').'-'.$end_time.' 23:59:59');
  516. }else{
  517. $query->where('created_at','>',Carbon::now()->startOfYear()->toDateTimeString());
  518. }
  519. }
  520. $count=$query->count();
  521. $data=$query->with(['user'=>function($q){
  522. $q->select('id','nickname','level','mobile','headimgurl','realname','recom_id')->with(['recom_user:id,nickname']);
  523. }])->select('user_id')
  524. ->take($page_size)->skip($num)->get();
  525. return $this->success_list($data,'',$count);
  526. }
  527. public function GetUserTeamLists(Request $request){
  528. // $user=Auth::user();
  529. $arr=['crown'=>0,'manager'=>0,'lowest'=>0];
  530. // if ($user->level==1){
  531. // $data=[
  532. // 'totay_num'=>0,
  533. // 'month_num'=>0,
  534. // 'group'=>$arr
  535. // ];
  536. // return $this->success($data);
  537. // }
  538. // $id=$user->id;
  539. $user=User::where('mobile','15538160193')->first();
  540. $id=$user->id;
  541. $s=false;
  542. if ($user->level==1){
  543. $s=true;
  544. }
  545. $type=$request->type;
  546. // $id=7;
  547. if ($type==0){
  548. $data=User::where('status',0)
  549. ->where('service_status',0)
  550. ->whereNull('deleted_at')
  551. ->where('recom_id',$id)
  552. ->when($s,function ($q){
  553. return $q->where('level','<>',3);
  554. })
  555. ->select('users.level',\DB::raw('count(*) as total'))->groupBy('level')->get();
  556. }else{
  557. $arrs=User::where('agent_id',$id)->where('status',0)
  558. ->where('service_status',0)
  559. ->whereNull('deleted_at')
  560. ->pluck('id')->toArray();
  561. array_push($arrs,$id);
  562. $data=User::where('status',0)
  563. ->where('service_status',0)
  564. ->whereNull('deleted_at')
  565. ->whereIn('agent_id',$arrs)
  566. ->select('users.level',\DB::raw('count(*) as total'))->groupBy('level')->get();
  567. }
  568. foreach ($data as $k=>$v){
  569. if ($v->level==1){
  570. $arr['lowest']=$v->total;
  571. }
  572. if ($v->level==2){
  573. $arr['manager']=$v->total;
  574. }
  575. if ($v->level==3){
  576. $arr['crown']=$v->total;
  577. }
  578. }
  579. $totay_num=$this->GetNewUserTotal($id,Carbon::today());
  580. $month_num=$this->GetNewUserTotal($id,Carbon::now()->startOfMonth());
  581. $counts=$this->getStore($id);
  582. $applyCount=ApplyUser::where('user_id',$id)->count();
  583. $data=[
  584. 'totay_num'=>$totay_num,
  585. 'month_num'=>$month_num,
  586. 'store_no_num'=>$counts,
  587. 'year_num'=>$this->GetNewUserTotal($id,Carbon::now()->startOfYear()),
  588. 'group'=>$arr,
  589. 'applyCount'=>$applyCount,
  590. ];
  591. return $this->success($data);
  592. }
  593. public function getStore($id){
  594. $arrs=User::where('agent_id',$id)->where('status',0)
  595. ->where('service_status',0)
  596. ->whereNull('deleted_at')
  597. ->pluck('id')->toArray();
  598. array_push($arrs,$id);
  599. $data=User::where('status',0)
  600. ->where('service_status',0)
  601. ->whereIn('agent_id',$arrs)
  602. ->whereNull('deleted_at')->pluck('id')->toArray();
  603. $arrs=array_merge($arrs,$data);
  604. $arrs=array_diff($arrs,[$id]);
  605. array_unique($arrs);
  606. $w_ids=Store::where('is_apply',3)->pluck('user_id')->toArray();
  607. array_unique($w_ids);
  608. $count=count(array_unique(array_diff($arrs,$w_ids)));
  609. return $count;
  610. }
  611. public function GetUserTeamLevelDetail(Request $request){
  612. $user=Auth::user();
  613. $id=$user->id;
  614. // $id=7;
  615. // if ($id==39516){
  616. // $user=User::where('mobile',18753967396)->first();
  617. // $id=$user->id;
  618. // }
  619. $input=$request->all();
  620. $page_index=$input['page_index'];
  621. $page_size=$input['page_size'];
  622. $level=$input['level'];
  623. $type=$input['type'];
  624. $search_name=$input['search_name'];
  625. // $search_type=$request->input('search_type',0);
  626. $s=0;
  627. if ($level<$user->level){
  628. if ($level!=1){
  629. $s=1;
  630. }
  631. }
  632. if ($type==0){
  633. $data=User::where('status',0)
  634. ->where('service_status',0)
  635. ->whereNull('deleted_at')
  636. ->where('level',$level)
  637. ->where('recom_id',$id)
  638. ->with(['cert_user:id,user_id,recom_updated_at,agent_updated_at,company_updated_at'])
  639. ->with(['agent_user:id,nickname'])->with(['get_store:id,user_id,is_apply'])->with(['recom_user:id,nickname'])
  640. ->select('level','id','headimgurl','mobile','nickname','recom_id','agent_id','realname','created_at','service_end_time',\DB::raw("if($s,1,0) as status"),'service_status','in_group');
  641. }else{
  642. $arrs=User::where('agent_id',$id)->where('status',0)
  643. ->where('service_status',0)
  644. ->whereNull('deleted_at')
  645. ->pluck('id')->toArray();
  646. array_push($arrs,$id);
  647. $data=User::where('status',0)
  648. ->where('service_status',0)
  649. ->whereNull('deleted_at')
  650. ->where('level',$level)
  651. ->whereIn('agent_id',$arrs)
  652. ->with(['cert_user:id,user_id,recom_updated_at,agent_updated_at,company_updated_at'])
  653. ->with(['agent_user:id,nickname'])->with(['get_store:id,user_id,is_apply'])->with(['recom_user:id,nickname'])
  654. ->select('level','headimgurl','mobile','nickname','recom_id','agent_id','realname','id','created_at','service_end_time',\DB::raw("if($s,1,0) as status"),'service_status','in_group');
  655. }
  656. if ($search_name){
  657. $wheres=function ($q)use($search_name){
  658. $q->where('nickname','like',"%$search_name%")->orwhere('mobile','like',"%$search_name%")->orwhere('realname','like',"%$search_name%");
  659. };
  660. $data=$data->where($wheres);
  661. }
  662. $total=$data->count();
  663. $data=$data->take($page_size)->skip(($page_index-1)*$page_size)->get();
  664. foreach($data as $key=>$val){
  665. if(empty($val->service_end_time)){
  666. if(!empty($val->cert_user)){
  667. $cert_user=$val->cert_user;
  668. $data[$key]->pass_time=$cert_user->company_updated_at?:$cert_user->agent_updated_at?:$cert_user->recom_updated_at?:date("Y-m-d H:i:s",strtotime($val->created_at));
  669. }else{
  670. $data[$key]->pass_time=date("Y-m-d H:i:s",strtotime($val->created_at));
  671. }
  672. }else{
  673. $data[$key]->pass_time=$val->service_end_time;
  674. }
  675. }
  676. if ($level==2){
  677. foreach ($data as $k=>$v){
  678. $data[$k]->up_time=\DB::table('agent_operation_log')->orderByDesc('id')->where('user_id',$v->id)->where('type',2)->value('created_at');
  679. }
  680. }
  681. return $this->success_list($data,'',$total);
  682. }
  683. /**未开通微店**/
  684. public function StoreNoOpen(Request $request){
  685. $id=Auth::user()->id;
  686. $arrs=User::where('agent_id',$id)->where('status',0)
  687. ->where('service_status',0)
  688. ->whereNull('deleted_at')
  689. ->pluck('id')->toArray();
  690. array_push($arrs,$id);
  691. $data=User::where('status',0)
  692. ->where('service_status',0)
  693. ->whereIn('agent_id',$arrs)
  694. ->whereNull('deleted_at')->pluck('id')->toArray();
  695. $arrs=array_merge($arrs,$data);
  696. $arrs=array_diff($arrs,[$id]);
  697. array_unique($arrs);
  698. $w_ids=Store::where('is_apply',3)->pluck('user_id')->toArray();
  699. array_unique($w_ids);
  700. $ids=array_unique(array_diff($arrs,$w_ids));
  701. $search_name=$request->input('search_name','');
  702. $page_index=$request->input('page_index',0);
  703. $page_size=$request->input('page_size',20);
  704. $data=User::whereIn('id',$ids)->with(['agent_user:id,nickname'])->with(['get_store:id,user_id,is_apply'])->with(['recom_user:id,nickname'])
  705. ->select('level','headimgurl','mobile','nickname','recom_id','agent_id','realname','id',\DB::raw("if(level=1,0,1) as status"),'service_status','in_group');
  706. if ($search_name){
  707. $wheres=function ($q)use($search_name){
  708. $q->where('nickname','like',"%$search_name%")->orwhere('mobile','like',"%$search_name%")->orwhere('realname','like',"%$search_name%");
  709. };
  710. $data=$data->where($wheres);
  711. }
  712. $total=$data->count();
  713. $data=$data->take($page_size)->skip(($page_index-1)*$page_size)->get();
  714. return $this->success_list($data,'',$total);
  715. }
  716. public function SearchUserTeamLevelDetail(Request $request){
  717. $user=Auth::user();
  718. $id=$user->id;
  719. $input=$request->all();
  720. $page_index=$input['page_index'];
  721. $page_size=$input['page_size'];
  722. $search_name=$input['search_name'];
  723. $arrs=User::where('agent_id',$id)->where('status',0)
  724. ->where('service_status',0)
  725. ->whereNull('deleted_at')
  726. ->pluck('id')->toArray();
  727. array_push($arrs,$id);
  728. $data=User::where('status',0)
  729. ->where('service_status',0)
  730. ->whereNull('deleted_at')
  731. ->whereIn('agent_id',$arrs)
  732. ->with(['agent_user:id,nickname'])->with(['get_store:id,user_id,is_apply'])->with(['recom_user:id,nickname'])->select('level','headimgurl','mobile','nickname','recom_id','agent_id','realname','id',\DB::raw("if(level=1,0,1) as status"));
  733. if ($search_name){
  734. $wheres=function ($q)use($search_name){
  735. $q->where('nickname','like',"%$search_name%")->orwhere('mobile','like',"%$search_name%")->orwhere('realname','like',"%$search_name%");
  736. };
  737. $data=$data->where($wheres);
  738. }
  739. $total=$data->count();
  740. $data=$data->take($page_size)->skip(($page_index-1)*$page_size)->get();
  741. return $this->success_list($data,'',$total);
  742. }
  743. public function GetTeamList(Request $request){
  744. $page_size=$request->page_size;
  745. $page_index=$request->page_index;
  746. $data=User::where('status',0)
  747. ->where('service_status',0)
  748. ->whereNull('deleted_at')
  749. ->where('level',1)
  750. ->where('agent_id',$request->id)
  751. ->with(['agent_user:id,nickname'])->with(['recom_user:id,nickname'])->select('level','headimgurl','mobile','nickname','recom_id','agent_id','realname');
  752. $total=$data->count();
  753. $data=$data->take($page_size)->skip(($page_index-1)*$page_size)->get();
  754. return $this->success_list($data,'',$total);
  755. }
  756. public function GetNewUserTotal($id,$time){
  757. $arr=User::where('agent_id',$id)->where('status',0)
  758. ->where('service_status',0)
  759. ->whereNull('deleted_at')
  760. ->pluck('id')->toArray();
  761. array_push($arr,$id);
  762. $total=User::whereIn('agent_id',$arr)->where('status',0)
  763. ->where('service_status',0)
  764. ->whereNull('deleted_at')
  765. ->where('created_at','>=',$time)
  766. ->count();
  767. return $total;
  768. }
  769. public function GetNewUserDetail(Request $request){
  770. // $id=39516;
  771. $id=Auth::user()->id;
  772. $input=$request->all();
  773. $page_index=$input['page_index'];
  774. $page_size=$input['page_size'];
  775. $type=$input['type'];
  776. $search_name=$input['search_name'];
  777. if ($type==0){
  778. $time=Carbon::today();
  779. }elseif($type==1){
  780. $time=Carbon::now()->startOfMonth();
  781. }else{
  782. $time=Carbon::now()->startOfYear();
  783. }
  784. $arr=User::where('agent_id',$id)->where('status',0)
  785. ->where('service_status',0)
  786. ->whereNull('deleted_at')
  787. ->pluck('id')->toArray();
  788. array_push($arr,$id);
  789. $query=User::whereIn('agent_id',$arr)->where('status',0)
  790. ->where('service_status',0)
  791. ->whereNull('deleted_at')
  792. ->where('created_at','>=',$time);
  793. if ($search_name){
  794. $wheres=function ($q)use($search_name){
  795. $q->where('mobile','like',"%$search_name%")
  796. ->orwhere('nickname','like',"%$search_name%")
  797. ->orwhere('realname','like',"%$search_name%");
  798. };
  799. $query->where($wheres);
  800. }
  801. $total=$query->count();
  802. $data=$query->with(['get_store:id,user_id,is_apply'])
  803. ->with(['agent_user:id,nickname'])->with(['recom_user:id,nickname'])
  804. ->select('level','headimgurl','mobile','nickname','recom_id','agent_id','realname','id','service_status','in_group')
  805. ->take($page_size)
  806. ->skip(($page_index-1)*$page_size)
  807. ->get();
  808. return $this->success_list($data,'',$total);
  809. }
  810. /****/
  811. public function GetNewTeam(Request $request){
  812. $page_index=$request->input('page_index');
  813. $page_size=$request->input('page_size');
  814. $num=($page_index-1)*$page_size;
  815. // $re=User::where('agent_id',39516)->whereNull('deleted_at');
  816. $re=User::where('agent_id',Auth::user()->id)->whereIn('cert_status',[3,6])->where('in_group',1)->whereNull('deleted_at');
  817. $count=$re->count();
  818. $data=$re->select('headimgurl','nickname','mobile','created_at')->orderByDesc('created_at')->take($page_size)->skip($num)->get();
  819. foreach ($data as $k=>$v){
  820. $v->mobile=hide_phone($v->mobile);
  821. }
  822. return $this->success_list($data,'',$count);
  823. }
  824. /****/
  825. public function GetSearchNewTeam(Request $request){
  826. $page_index=$request->input('page_index');
  827. $page_size=$request->input('page_size');
  828. $search_name=$request->input('search_name');
  829. $num=($page_index-1)*$page_size;
  830. // $re=User::where('agent_id',39516)->whereNull('deleted_at');
  831. $re=User::where('agent_id',Auth::user()->id)->whereIn('cert_status',[3,6])->where('in_group',1)->whereNull('deleted_at');
  832. if ($search_name){
  833. if (is_numeric($search_name)){
  834. if (mb_strlen($search_name)<4){
  835. return $this->errormsg(500214,'','请输入至少四位手机号');
  836. }
  837. }
  838. $where=function ($q)use ($search_name){
  839. $q->where('mobile','like',"%$search_name%")->orwhere('nickname','like',"%$search_name%");
  840. };
  841. $re->where($where);
  842. }
  843. $count=$re->count();
  844. $data=$re->select('headimgurl','nickname','mobile','created_at','recom_id')->with(['recom_user:id,nickname,mobile'])->take($page_size)->skip($num)->get();
  845. $arr=[];
  846. foreach ($data as $k=>$v){
  847. if ($v->recom_user){
  848. $arr[]=[
  849. 'nickname'=>$v->nickname,
  850. 'headimgurl'=>$v->headimgurl,
  851. 'created_at'=>$v->created_at->toDateTimeString(),
  852. 'mobile'=>hide_phone($v->mobile),
  853. 'user_nickname'=>$v->recom_user->nickname,
  854. 'user_mobile'=>hide_phone($v->recom_user->mobile)
  855. ];
  856. }else{
  857. $arr[]=[
  858. 'nickname'=>$v->nickname,
  859. 'headimgurl'=>$v->headimgurl,
  860. 'created_at'=>$v->created_at->toDateTimeString(),
  861. 'mobile'=>hide_phone($v->mobile),
  862. 'user_nickname'=>'',
  863. 'user_mobile'=>''
  864. ];
  865. }
  866. }
  867. return $this->success_list($arr,'',$count);
  868. }
  869. }