StatisticalController.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Activity;
  4. use App\Models\GoodsSpec;
  5. use App\Models\Ordertest;
  6. use App\Models\WdStore;
  7. use Carbon\Carbon;
  8. use Illuminate\Http\Request;
  9. use App\Models\GoodsSku;
  10. use App\Models\User;
  11. use App\Models\OrderGoods;
  12. use App\Models\System;
  13. use App\Models\Store;
  14. use App\Models\Rewards;
  15. use Illuminate\Database\Eloquent\Builder;
  16. use Illuminate\Support\Facades\DB;
  17. use App\Exports\StatisticalExport;
  18. use App\Http\Requests;
  19. use Excel;
  20. class StatisticalController extends Controller
  21. {
  22. public function export(Request $request){
  23. return Excel::download(new StatisticalExport, 'users.csv');
  24. }
  25. //销售额统计(扇形图)
  26. public function SalesStatisticsFan(){
  27. $starttime='2000-01-01 00:00:00';
  28. $endtime='2050-01-01 00:00:00';
  29. $list=$this->getStatisticalList($starttime,$endtime);
  30. return $this->success($list);
  31. }
  32. //销售额统计(柱形图)
  33. public function SalesStatisticsCyl(Request $request){
  34. $time=date("Y-01-01 00:00:00",strtotime($request->get('year').'-01'));
  35. for($i=0;$i<12;$i++){
  36. $start=$time;
  37. $time= date("Y-m-d H:i:s",strtotime("$time +1 month"));
  38. $end=$time;
  39. $list[$i+1]=$this->getStatisticalList($start,$end);
  40. }
  41. $lists=[];
  42. foreach($list as $key=>$val){
  43. foreach($val as $k=>$v){
  44. $lists[$v['goods_id']]['name']=$v['goods']['name'];
  45. $lists[$v['goods_id']]['account'][$key]=$v['account'];
  46. }
  47. }
  48. return $this->success($lists);
  49. }
  50. //销售额统计(折线图)
  51. public function SalesStatisticsLine(Request $request){
  52. $t=date('t', strtotime($request->get('month')));
  53. $time=date("Y-m-01 00:00:00",strtotime($request->get('month')));
  54. for($i=0;$i<$t;$i++){
  55. $start=$time;
  56. $time= date("Y-m-d H:i:s",strtotime("$time +1 days"));
  57. $end=$time;
  58. $list[$i+1]=$this->getStatisticalList($start,$end);
  59. }
  60. $lists=[];
  61. foreach($list as $key=>$val){
  62. foreach($val as $k=>$v){
  63. $lists[$v['goods_id']]['name']=$v['goods']['name'];
  64. $lists[$v['goods_id']]['account'][$key]=$v['account'];
  65. }
  66. }
  67. return $this->success_list($lists,$msg="SUCCESS",$t);
  68. }
  69. public function getStatisticalList($starttime,$endtime){
  70. $time=[$starttime,$endtime];
  71. $list=OrderGoods::
  72. select('goods_id','order_id',DB::raw('SUM(totalprice) as account'))//,DB::raw('SUM(totalprice) as account'),'order_id','sku_id','totalprice'
  73. ->with(['goods:id,name'])
  74. ->whereHas('order', function (Builder $query) use ($time) {
  75. $query->whereBetween('created_at',$time)//'2020-01-01 00:00:00'
  76. ->whereIn('status',[2,3])
  77. ->where('agent_id',1);
  78. })
  79. ->groupBy('goods_id')
  80. ->get();
  81. return $list;
  82. }
  83. //销售量统计(扇形图)
  84. public function SalesStatisticsNumFan(){
  85. $starttime='2000-01-01 00:00:00';
  86. $endtime='2050-01-01 00:00:00';
  87. $list=$this->getStatisticalNumList($starttime,$endtime);
  88. return $this->success($list);
  89. }
  90. //销售量统计(柱形图)
  91. public function SalesStatisticsNumCyl(Request $request){
  92. $time=date("Y-01-01 00:00:00",strtotime($request->get('year').'-01'));
  93. for($i=0;$i<12;$i++){
  94. $start=$time;
  95. $time= date("Y-m-d H:i:s",strtotime("$time +1 month"));
  96. $end=$time;
  97. $list[$i+1]=$this->getStatisticalNumList($start,$end);
  98. }
  99. $lists=[];
  100. foreach($list as $key=>$val){
  101. foreach($val as $k=>$v){
  102. $lists[$v['goods_id']]['name']=$v['goods']['name'];
  103. $lists[$v['goods_id']]['total'][$key]=$v['total'];
  104. }
  105. }
  106. return $this->success($lists);
  107. }
  108. //销售量统计(折线图)
  109. public function SalesStatisticsNumLine(Request $request){
  110. $t=date('t', strtotime($request->get('month')));
  111. $time=date("Y-m-01 00:00:00",strtotime($request->get('month')));
  112. for($i=0;$i<$t;$i++){
  113. $start=$time;
  114. $time= date("Y-m-d H:i:s",strtotime("$time +1 days"));
  115. $end=$time;
  116. $list[$i+1]=$this->getStatisticalNumList($start,$end);
  117. }
  118. $lists=[];
  119. foreach($list as $key=>$val){
  120. foreach($val as $k=>$v){
  121. $lists[$v['goods_id']]['name']=$v['goods']['name'];
  122. $lists[$v['goods_id']]['total'][$key]=$v['total'];
  123. }
  124. }
  125. return $this->success_list($lists,$msg="SUCCESS",$t);
  126. }
  127. public function getStatisticalNumList($starttime,$endtime){
  128. $time=[$starttime,$endtime];
  129. $list=OrderGoods::
  130. select('goods_id','order_id',DB::raw('SUM(num) as total'))//,DB::raw('SUM(totalprice) as account'),'order_id','sku_id','totalprice'
  131. ->with(['goods:id,name'])
  132. ->whereHas('order', function (Builder $query) use ($time) {
  133. $query
  134. ->whereBetween('created_at',$time)//'2020-01-01 00:00:00'
  135. ->whereIn('status',[2,3])
  136. ->where('agent_id',1)
  137. ;
  138. })
  139. ->groupBy('goods_id')
  140. ->get();
  141. return $list;
  142. }
  143. //新增代理统计
  144. public function agentAddNum(Request $request)
  145. {
  146. $input=$request->all();
  147. switch($input['type']){
  148. case '0':
  149. $starttime=date("Y-m-d 00:00:00",strtotime($input['time'][0]));
  150. $endtime= date("Y-m-d 00:00:00",strtotime($input['time'][1] ."+1 days"));
  151. break;
  152. case '1':
  153. $starttime=date("Y-m-01 00:00:00",strtotime($input['time']));
  154. $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months"));
  155. break;
  156. case '2':
  157. $starttime=date("Y-01-01 00:00:00",strtotime($input['time']));
  158. $endtime= date("Y-01-01 00:00:00",strtotime($input['time'] ."+1 year"));
  159. break;
  160. }
  161. $time = [$starttime, $endtime];
  162. //新增代理统计
  163. $count['all'] = User::whereBetween('created_at', $time)->count();
  164. //新增皇冠代理统计
  165. $count['crow'] = User::whereBetween('level_at', $time)->where('level', 3)->count();
  166. //新增顶级代理统计
  167. $count['top'] = User::whereBetween('level_at', $time)->where('level', 2)->count();
  168. //新增特级代理统计
  169. $count['super'] = User::whereBetween('created_at', $time)->orwhereBetween('level_at',$time)->where('level', 1)->count();
  170. return $this->success($count);
  171. }
  172. public function agentAddNumLine(Request $request){
  173. $input=$request->all();
  174. switch($input['type']){
  175. case '0':
  176. $starttime=date("Y-m-d 00:00:00",strtotime($input['time'][0]));
  177. $endtime= date("Y-m-d 00:00:00",strtotime($input['time'][1] ."+1 days"));
  178. break;
  179. case '1':
  180. $starttime=date("Y-m-01 00:00:00",strtotime($input['time']));
  181. $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months"));
  182. break;
  183. case '2':
  184. $starttime=date("Y-01-01 00:00:00",strtotime($input['time']));
  185. $endtime= date("Y-01-01 00:00:00",strtotime($input['time'] ."+1 year"));
  186. break;
  187. }
  188. $time = [$starttime, $endtime];
  189. //新增代理统计
  190. $count['all'] = User::whereBetween('created_at', $time)
  191. ->selectRaw('DATE(created_at) as date,COUNT(*) as value')
  192. ->groupBy('date')
  193. ->get();
  194. //新增皇冠代理统计
  195. $count['crow'] = User::whereBetween('created_at', $time)
  196. ->where('level', 3)
  197. ->selectRaw('DATE(created_at) as date,COUNT(*) as value')
  198. ->groupBy('date')
  199. ->get();
  200. //新增顶级代理统计
  201. $count['top'] = User::whereBetween('created_at', $time)
  202. ->where('level', 2)
  203. ->selectRaw('DATE(created_at) as date,COUNT(*) as value')
  204. ->groupBy('date')
  205. ->get();
  206. //新增特级代理统计
  207. $count['super'] = User::whereBetween('created_at', $time)
  208. ->where('level', 1)
  209. ->selectRaw('DATE(created_at) as date,COUNT(*) as value')
  210. ->groupBy('date')
  211. ->get();
  212. return $this->success($count);
  213. }
  214. //代理业绩排行榜
  215. public function agentSalesResults(Request $request){
  216. $input=$request->all();
  217. $page_size=$input['page_size'];
  218. $page_index=$input['page_index'];
  219. $num = ($page_index - 1) * $page_size;
  220. $search_name=$input['search_name'];
  221. switch($input['type']){
  222. case '0':
  223. $starttime=date("Y-m-d 00:00:00",strtotime($input['time'][0]));
  224. $endtime= date("Y-m-d 00:00:00",strtotime($input['time'][1] ."+1 days"));
  225. break;
  226. case '1':
  227. $starttime=date("Y-m-01 00:00:00",strtotime($input['time']));
  228. $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months"));
  229. break;
  230. case '2':
  231. $starttime=date("Y-01-01 00:00:00",strtotime($input['time']));
  232. $endtime= date("Y-01-01 00:00:00",strtotime($input['time'] ."+1 year"));
  233. break;
  234. }
  235. $time = [$starttime, $endtime];
  236. $count=DB::table('users')
  237. ->select('users.nickname','users.level','warea.name as warea','uu.nickname as agent',DB::raw('SUM(order_goods.totalprice) as account'))
  238. ->leftJoin('order','order.user_id','=','users.id')
  239. ->leftJoin('order_goods','order.id','=','order_goods.order_id')
  240. ->leftJoin('warea','warea.id','=','users.warea_id')
  241. ->leftJoin('users as uu','uu.id','=','users.agent_id')
  242. ->whereIn('order.status',[2,3])
  243. ->whereBetween('order.created_at',$time)
  244. ->where('users.nickname', 'like', '%' . $search_name . '%')
  245. ->groupBy('users.id')
  246. ->get()->count();
  247. // $list=User::
  248. // select('goods_id','order_id',DB::raw('SUM(num) as total'))//,DB::raw('SUM(totalprice) as account'),'order_id','sku_id','totalprice'
  249. // ->with(['agent:id,nickname','warea:id,name'])
  250. // ->whereHas('order', function (Builder $query) use ($time) {
  251. // $query
  252. // ->whereBetween('created_at',$time)//'2020-01-01 00:00:00'
  253. // ->whereIn('status',[2,3])
  254. // ->whereHas('user', function (Builder $query1) {
  255. // $query1->where('level',3);
  256. // })
  257. // ;
  258. // })
  259. // ->groupBy('goods_id')
  260. // ->get();
  261. if($count==0){
  262. $this->error('400001','没有数据');
  263. }
  264. $list=DB::table('users')
  265. ->select('users.nickname','users.level','warea.name as warea','uu.nickname as agent',DB::raw('SUM(order_goods.totalprice) as account'))
  266. ->leftJoin('order','order.user_id','=','users.id')
  267. ->leftJoin('order_goods','order.id','=','order_goods.order_id')
  268. ->leftJoin('warea','warea.id','=','users.warea_id')
  269. ->leftJoin('users as uu','uu.id','=','users.agent_id')
  270. ->whereIn('order.status',[2,3])
  271. ->whereBetween('order.created_at',$time)
  272. ->where('users.nickname', 'like', '%' . $search_name . '%')
  273. ->groupBy('users.id')
  274. ->orderBy('account','desc')
  275. ->skip($num)->take($page_size)
  276. ->get();
  277. if($list){
  278. return $this->success_list($list,'SUCCESS',$count);
  279. }else{
  280. return $this->error();
  281. }
  282. }
  283. //战区销售额排行
  284. public function wareaSalesResults(Request $request){
  285. $input=$request->all();
  286. $page_size=$input['page_size'];
  287. $page_index=$input['page_index'];
  288. $num = ($page_index - 1) * $page_size;
  289. switch($input['type']){
  290. case '0':
  291. $starttime=date("Y-m-d 00:00:00",strtotime($input['time'][0]));
  292. $endtime= date("Y-m-d 00:00:00",strtotime($input['time'][1] ."+1 days"));
  293. break;
  294. case '1':
  295. $starttime=date("Y-m-01 00:00:00",strtotime($input['time']));
  296. $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months"));
  297. break;
  298. case '2':
  299. $starttime=date("Y-01-01 00:00:00",strtotime($input['time']));
  300. $endtime= date("Y-01-01 00:00:00",strtotime($input['time'] ."+1 year"));
  301. break;
  302. }
  303. $time = [$starttime, $endtime];
  304. $count =DB::table('warea')
  305. ->select('warea.name as warea', DB::raw('SUM(order_goods.totalprice) as account'))
  306. ->leftJoin('users', function ($join) {
  307. $join->on('users.warea_id', '=', 'warea.id')
  308. ->where('users.level', '=', 3);
  309. })
  310. ->leftJoin('order', 'order.user_id', '=', 'users.id')
  311. ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
  312. ->whereIn('order.status',[2,3])
  313. ->whereBetween('order.created_at',$time)
  314. ->groupBy('warea.id')
  315. ->get()->count();
  316. if($count==0){
  317. $this->error('400001','没有数据');
  318. }
  319. $list = DB::table('warea')
  320. ->select('warea.name as warea', DB::raw('SUM(order_goods.totalprice) as account'))
  321. ->leftJoin('users', function ($join) {
  322. $join->on('users.warea_id', '=', 'warea.id')
  323. ->where('users.level', '=', 3);//只计算战区中皇冠的进货总金额
  324. })
  325. ->leftJoin('order', 'order.user_id', '=', 'users.id')
  326. ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
  327. ->whereIn('order.status',[2,3])
  328. ->whereBetween('order.created_at',$time)
  329. ->groupBy('warea.id')
  330. ->orderBy('account','desc')
  331. ->skip($num)->take($page_size)
  332. ->get();
  333. if ($list) {
  334. return $this->success_list($list,'SUCCESS',$count);
  335. } else {
  336. return $this->error();
  337. }
  338. }
  339. //皇冠代理团队业绩排行
  340. public function crownTeamSalesResults(Request $request){
  341. $input=$request->all();
  342. $page_size=$input['page_size'];
  343. $page_index=$input['page_index'];
  344. $num = ($page_index - 1) * $page_size;
  345. switch($input['type']){
  346. case '0':
  347. $starttime=date("Y-m-d 00:00:00",strtotime($input['time'][0]));
  348. $endtime= date("Y-m-d 00:00:00",strtotime($input['time'][1] ."+1 days"));
  349. break;
  350. case '1':
  351. $starttime=date("Y-m-01 00:00:00",strtotime($input['time']));
  352. $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months"));
  353. break;
  354. case '2':
  355. $starttime=date("Y-01-01 00:00:00",strtotime($input['time']));
  356. $endtime= date("Y-01-01 00:00:00",strtotime($input['time'] ."+1 year"));
  357. break;
  358. }
  359. $time = [$starttime, $endtime];
  360. $count = DB::table('users')
  361. ->select('users.name as crowteam', DB::raw('SUM(order_goods.totalprice) as account'))
  362. ->leftJoin('users as uu', 'users.id', '=', 'uu.agent_id')
  363. ->leftJoin('order', 'order.user_id', '=', 'uu.id')
  364. ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
  365. ->whereIn('order.status',[2,3])
  366. ->whereBetween('order.created_at',$time)
  367. ->where('users.level', '=', 3)
  368. ->groupBy('users.id')
  369. ->get()->count();
  370. if($count==0){
  371. $this->error('400001','没有数据');
  372. }
  373. $list = DB::table('users')
  374. ->select('users.nickname as crowteam', DB::raw('SUM(order_goods.totalprice) as account'))
  375. ->leftJoin('users as uu', 'users.id', '=', 'uu.agent_id')
  376. ->leftJoin('order', 'order.user_id', '=', 'uu.id') //皇冠下级进货总量
  377. ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
  378. ->whereIn('order.status',[2,3])
  379. ->whereBetween('order.created_at',$time)
  380. ->where('users.level', '=', 3)
  381. ->groupBy('users.id')
  382. ->orderBy('account','desc')
  383. ->skip($num)->take($page_size)
  384. ->get();
  385. if ($list) {
  386. return $this->success_list($list,'SUCCESS',$count);
  387. } else {
  388. return $this->error();
  389. }
  390. }
  391. //皇冠团队新增代理人数
  392. public function crownAddAgentNum(Request $request){
  393. $input=$request->all();
  394. $page_size=$input['page_size'];
  395. $page_index=$input['page_index'];
  396. $num = ($page_index - 1) * $page_size;
  397. switch($input['type']){
  398. case '0':
  399. $starttime=date("Y-m-d 00:00:00",strtotime($input['time'][0]));
  400. $endtime= date("Y-m-d 00:00:00",strtotime($input['time'][1] ."+1 days"));
  401. break;
  402. case '1':
  403. $starttime=date("Y-m-01 00:00:00",strtotime($input['time']));
  404. $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months"));
  405. break;
  406. case '2':
  407. $starttime=date("Y-01-01 00:00:00",strtotime($input['time']));
  408. $endtime= date("Y-01-01 00:00:00",strtotime($input['time'] ."+1 year"));
  409. break;
  410. }
  411. $time = [$starttime, $endtime];
  412. $count = DB::table('users')
  413. ->select('users.nickname as teamName','users.id',DB::raw('COUNT(uu.id) as count'))
  414. ->where('users.level', '=', 3)
  415. ->leftJoin('users as uu', 'users.id', '=', 'uu.crown_id')
  416. ->whereBetween('uu.created_at',$time)
  417. ->groupBy('users.id')
  418. ->get()->count();
  419. if($count==0){
  420. $this->error('400001','没有数据');
  421. }
  422. $list = DB::table('users')
  423. ->select('users.nickname as teamName','users.id',DB::raw('COUNT(uu.id) as count'))
  424. ->where('users.level', '=', 3)
  425. ->leftJoin('users as uu', 'users.id', '=', 'uu.crown_id')
  426. ->whereBetween('uu.created_at',$time)
  427. ->groupBy('users.id')
  428. ->orderBy('count','desc')
  429. ->skip($num)->take($page_size)
  430. ->get();
  431. if ($list) {
  432. return $this->success_list($list,'SUCCESS',$count);
  433. } else {
  434. return $this->error();
  435. }
  436. }
  437. //店铺业绩排行榜
  438. public function storeSalesResults(Request $request){
  439. $input=$request->all();
  440. $page_size=$input['page_size'];
  441. $page_index=$input['page_index'];
  442. $num = ($page_index - 1) * $page_size;
  443. switch($input['type']){
  444. case '0':
  445. $starttime=date("Y-m-d 00:00:00",strtotime($input['time'][0]));
  446. $endtime= date("Y-m-d 00:00:00",strtotime($input['time'][1] ."+1 days"));
  447. break;
  448. case '1':
  449. $starttime=date("Y-m-01 00:00:00",strtotime($input['time']));
  450. $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months"));
  451. break;
  452. case '2':
  453. $starttime=date("Y-01-01 00:00:00",strtotime($input['time']));
  454. $endtime= date("Y-01-01 00:00:00",strtotime($input['time'] ."+1 year"));
  455. break;
  456. }
  457. $time = [$starttime, $endtime];
  458. $count = DB::table('store')
  459. ->select('store.name as storename', DB::raw('SUM(order_goods.totalprice) as account'))
  460. ->leftJoin('users', function ($join) {
  461. $join->on('users.store_id', '=', 'store.id');
  462. })
  463. ->leftJoin('order', 'order.user_id', '=', 'users.id')
  464. ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
  465. ->whereIn('order.status',[2,3])
  466. ->whereBetween('order.created_at',$time)
  467. ->groupBy('store.id')
  468. ->get()->count();
  469. if($count==0){
  470. $this->error('400001','没有数据');
  471. }
  472. $list = DB::table('store')
  473. ->select('store.name as storename', DB::raw('SUM(order_goods.totalprice) as account'))
  474. ->leftJoin('users', function ($join) {
  475. $join->on('users.store_id', '=', 'store.id');
  476. })
  477. ->leftJoin('order', 'order.user_id', '=', 'users.id')
  478. ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
  479. ->whereIn('order.status',[2,3])
  480. ->whereBetween('order.created_at',$time)
  481. ->groupBy('store.id')
  482. ->orderBy('account','desc')
  483. ->skip($num)->take($page_size)
  484. ->get();
  485. if ($list) {
  486. return $this->success_list($list,'SUCCESS',$count);
  487. } else {
  488. return $this->error();
  489. }
  490. }
  491. //流水账单
  492. public function salesWaterBills(Request $request){
  493. $input=$request->all();
  494. $page_size=$input['page_size'];
  495. $page_index=$input['page_index'];
  496. $num = ($page_index - 1) * $page_size;
  497. switch($input['type']){
  498. case '0':
  499. $starttime=date("Y-m-d 00:00:00",strtotime($input['time'][0]));
  500. $endtime= date("Y-m-d 00:00:00",strtotime($input['time'][1] ."+1 days"));
  501. break;
  502. case '1':
  503. $starttime=date("Y-m-01 00:00:00",strtotime($input['time']));
  504. $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months"));
  505. break;
  506. case '2':
  507. $starttime=date("Y-01-01 00:00:00",strtotime($input['time']));
  508. $endtime= date("Y-01-01 00:00:00",strtotime($input['time'] ."+1 year"));
  509. break;
  510. }
  511. $time = [$starttime, $endtime];
  512. $count=DB::table('order')
  513. ->select('order.id','order.ordersn', DB::raw('SUM(order_goods.totalprice) as account'),DB::raw('SUM(order_goods.num) as total'),'order.created_at')
  514. ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
  515. ->whereIn('order.status',[2,3])
  516. ->whereBetween('order.created_at',$time)
  517. ->groupBy('order.id')
  518. ->orderBy('account','desc')
  519. ->get()->count();
  520. if($count==0){
  521. $this->error('400001','没有数据');
  522. }
  523. $list=DB::table('order')
  524. ->select('order.id','order.ordersn', DB::raw('SUM(order_goods.totalprice) as account'),DB::raw('SUM(order_goods.num) as total'),'order.created_at')
  525. ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
  526. ->whereIn('order.status',[2,3])
  527. ->whereBetween('order.created_at',$time)
  528. ->groupBy('order.id')
  529. ->orderBy('account','desc')
  530. ->skip($num)->take($page_size)
  531. ->get();
  532. foreach($list as $key =>$val){
  533. $goodsinfo=DB::table('order_goods')->select('goods.name','order_goods.sku_id','order_goods.price','order_goods.num','order_goods.totalprice')
  534. ->where('order_id',$val->id)
  535. ->leftJoin('goods','goods.id','order_goods.goods_id')
  536. ->get();
  537. foreach($goodsinfo as $k =>$v){
  538. $goodsinfo[$k]->sku=$this->getSku($v->sku_id);
  539. }
  540. $list[$key]->goods=$goodsinfo;
  541. }
  542. if ($list) {
  543. return $this->success_list($list,'SUCCESS',$count);
  544. } else {
  545. return $this->error();
  546. }
  547. }
  548. public function getSku($id){
  549. $goodsSku=GoodsSku::withTrashed()->find($id);
  550. $path=$goodsSku->sku_path;
  551. $arr=explode('/',$path);
  552. $goodsSku=[];
  553. foreach($arr as $k=>$v){
  554. $upSpec=GoodsSpec::withTrashed()->where('id',$v)->first();
  555. $upSpec['uptitle']=GoodsSpec::withTrashed()->where('id',$upSpec->pid)->value('title');
  556. $goodsSku[$upSpec['uptitle']]=$upSpec->title;
  557. }
  558. return $goodsSku;
  559. }
  560. //进货奖励
  561. public function rewardSpending(Request $request){
  562. $input=$request->all();
  563. $page_size=$input['page_size'];
  564. $page_index=$input['page_index'];
  565. $num = ($page_index - 1) * $page_size;
  566. switch($input['type']){
  567. case '0':
  568. $starttime=date("Y-m-d 00:00:00",strtotime($input['time'][0]));
  569. $endtime= date("Y-m-d 00:00:00",strtotime($input['time'][1] ."+1 days"));
  570. break;
  571. case '1':
  572. $starttime=date("Y-m-01 00:00:00",strtotime($input['time']));
  573. $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months"));
  574. break;
  575. case '2':
  576. $starttime=date("Y-01-01 00:00:00",strtotime($input['time']));
  577. $endtime= date("Y-01-01 00:00:00",strtotime($input['time'] ."+1 year"));
  578. break;
  579. }
  580. $time = [$starttime, $endtime];
  581. $count=DB::table('rewards')
  582. ->select('rewards.id','uu.nickname as agent_name','users.nickname as recom_name','rewards.bonus','rewards.created_at')
  583. // DB::raw('SUM(rewards.bonus) as account')
  584. ->leftJoin('users','users.id','=','rewards.recom_id')
  585. ->leftJoin('users as uu','uu.id','=','rewards.agent_id')
  586. // ->where('rewards.agent_id',0)
  587. ->where('rewards.type', 1)
  588. ->whereBetween('rewards.created_at',$time)
  589. // ->groupBy('rewards.recom_id')
  590. ->get()->count();
  591. if($count==0){
  592. $this->error('400001','没有数据');
  593. }
  594. $list=DB::table('rewards')
  595. ->select('rewards.id','uu.nickname as agent_name','users.nickname as recom_name','rewards.bonus','rewards.created_at')
  596. ->leftJoin('users','users.id','=','rewards.recom_id')
  597. ->leftJoin('users as uu','uu.id','=','rewards.agent_id')
  598. // ->where('rewards.agent_id',0)
  599. ->where('rewards.type',1)
  600. ->whereBetween('rewards.created_at',$time)
  601. // ->groupBy('rewards.recom_id')
  602. ->orderBy('rewards.id','desc')
  603. ->skip($num)->take($page_size)
  604. ->get();
  605. if ($list) {
  606. return $this->success_list($list,'SUCCESS',$count);
  607. } else {
  608. return $this->error();
  609. }
  610. }
  611. //业绩奖励
  612. public function performanceReward(Request $request)
  613. {
  614. $input = $request->all();
  615. $page_size = $input['page_size'];
  616. $page_index = $input['page_index'];
  617. $num = ($page_index - 1) * $page_size;
  618. switch ($input['type']) {
  619. case '0':
  620. $starttime = date("Y-m-d 00:00:00", strtotime($input['time'][0]));
  621. $endtime = date("Y-m-d 00:00:00", strtotime($input['time'][1] . "+1 days"));
  622. break;
  623. case '1':
  624. $starttime = date("Y-m-01 00:00:00", strtotime($input['time']));
  625. $endtime = date("Y-m-01 00:00:00", strtotime($input['time'] . "+1 months"));
  626. break;
  627. case '2':
  628. $starttime = date("Y-01-01 00:00:00", strtotime($input['time']));
  629. $endtime = date("Y-01-01 00:00:00", strtotime($input['time'] . "+1 year"));
  630. break;
  631. }
  632. $time = [$starttime, $endtime];
  633. $count = DB::table('rewards')
  634. ->select('rewards.id','users.nickname', 'rewards.bonus','rewards.month','rewards.created_at')
  635. ->leftJoin('users', 'users.id', '=', 'rewards.recom_id')
  636. // ->where('rewards.agent_id', 0)
  637. ->where('rewards.type', 2)
  638. ->whereBetween('rewards.created_at', $time)
  639. // ->groupBy('rewards.recom_id')
  640. ->get()->count();
  641. if ($count == 0) {
  642. $this->error('400001', '没有数据');
  643. }
  644. $list = DB::table('rewards')
  645. ->select('rewards.id','users.nickname', 'rewards.bonus','rewards.month','rewards.created_at')
  646. ->leftJoin('users', 'users.id', '=', 'rewards.recom_id')
  647. // ->where('rewards.agent_id', 0)
  648. ->where('rewards.type', 2)
  649. ->whereBetween('rewards.created_at', $time)
  650. // ->groupBy('rewards.recom_id')
  651. ->orderBy('rewards.id', 'desc')
  652. ->skip($num)->take($page_size)
  653. ->get();
  654. if ($list) {
  655. return $this->success_list($list, 'SUCCESS', $count);
  656. } else {
  657. return $this->error();
  658. }
  659. }
  660. //店铺奖励
  661. public function storeReward(Request $request)
  662. {
  663. $input = $request->all();
  664. $page_size = $input['page_size'];
  665. $page_index = $input['page_index'];
  666. $num = ($page_index - 1) * $page_size;
  667. switch ($input['type']) {
  668. case '0':
  669. $starttime = date("Y-m-d 00:00:00", strtotime($input['time'][0]));
  670. $endtime = date("Y-m-d 00:00:00", strtotime($input['time'][1] . "+1 days"));
  671. break;
  672. case '1':
  673. $starttime = date("Y-m-01 00:00:00", strtotime($input['time']));
  674. $endtime = date("Y-m-01 00:00:00", strtotime($input['time'] . "+1 months"));
  675. break;
  676. case '2':
  677. $starttime = date("Y-01-01 00:00:00", strtotime($input['time']));
  678. $endtime = date("Y-01-01 00:00:00", strtotime($input['time'] . "+1 year"));
  679. break;
  680. }
  681. $time = [$starttime, $endtime];
  682. $count = DB::table('rewards')
  683. ->select('store.id', 'store.name','goods.name','rewards.*')
  684. ->leftJoin('goods', 'goods.id', '=', 'rewards.goods_id')
  685. ->leftJoin('store', 'store.id', '=', 'rewards.store_id')
  686. // ->where('rewards.agent_id', 0)
  687. ->where('rewards.type', 3)
  688. ->whereBetween('rewards.created_at', $time)
  689. // ->groupBy('store.id')
  690. ->get()->count();
  691. if ($count == 0) {
  692. $this->error('400001', '没有数据');
  693. }
  694. $list = DB::table('rewards')
  695. ->select('store.id', 'store.name','goods.name as goodsname','rewards.*')
  696. ->leftJoin('goods', 'goods.id', '=', 'rewards.goods_id')
  697. ->leftJoin('store', 'store.id', '=', 'rewards.store_id')
  698. // ->where('rewards.agent_id', 0)
  699. ->where('rewards.type', 3)
  700. ->whereBetween('rewards.created_at', $time)
  701. // ->groupBy('store.id')
  702. ->orderBy('rewards.id', 'desc')
  703. ->skip($num)->take($page_size)
  704. ->get();
  705. if ($list) {
  706. return $this->success_list($list, 'SUCCESS', $count);
  707. } else {
  708. return $this->error();
  709. }
  710. }
  711. //店铺奖励详情
  712. public function getStoreRewardDetail(Request $request){
  713. $input = $request->all();
  714. switch ($input['type']) {
  715. case '0':
  716. $starttime = date("Y-m-d 00:00:00", strtotime($input['time'][0]));
  717. $endtime = date("Y-m-d 00:00:00", strtotime($input['time'][1] . "+1 days"));
  718. break;
  719. case '1':
  720. $starttime = date("Y-m-01 00:00:00", strtotime($input['time']));
  721. $endtime = date("Y-m-01 00:00:00", strtotime($input['time'] . "+1 months"));
  722. break;
  723. case '2':
  724. $starttime = date("Y-01-01 00:00:00", strtotime($input['time']));
  725. $endtime = date("Y-01-01 00:00:00", strtotime($input['time'] . "+1 year"));
  726. break;
  727. }
  728. $time = [$starttime, $endtime];
  729. $list = DB::table('users')
  730. ->select('users.id', 'users.nickname', DB::raw('SUM(rewards.bonus) as account'))
  731. ->leftJoin('users', 'users.store_id', '=', 'store.id')
  732. ->leftJoin('rewards', 'users.id', '=', 'rewards.recom_id')
  733. ->where('users.store_id',$input['id'])
  734. ->where('rewards.agent_id', 0)
  735. ->where('rewards.type', 2)
  736. ->whereBetween('rewards.created_at', $time)
  737. ->groupBy('users.id')
  738. ->get();
  739. if ($list) {
  740. return $this->success($list);
  741. } else {
  742. return $this->error();
  743. }
  744. }
  745. //获取代理业绩奖励结算信息
  746. public function getAgentSalesResult(Request $request){
  747. $input=$request->all();
  748. $page_size=$input['page_size'];
  749. $page_index=$input['page_index'];
  750. $num = ($page_index - 1) * $page_size;
  751. $search_name=$input['search_name'];
  752. $starttime=date("Y-m-01 00:00:00",strtotime($input['time']));
  753. $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months"));
  754. $time = [$starttime, $endtime];
  755. $sales_area_reward_str=System::where('keys','sales_area_reward')->value('values');
  756. $sales_area_reward_arr=explode('/',$sales_area_reward_str);
  757. foreach($sales_area_reward_arr as $key =>$val){
  758. $sales_area_reward[$key]=explode('*',$val);
  759. }
  760. $result = array();
  761. foreach($sales_area_reward as $k=>$v){
  762. $result[$k] = $v[0];
  763. }
  764. $minValue=min($result);
  765. $count=DB::table('users')
  766. ->select('users.id','users.nickname','users.level','warea.name as warea','uu.nickname as agent',DB::raw('SUM(order_goods.totalprice) as account'))
  767. ->leftJoin('order','order.user_id','=','users.id')
  768. ->leftJoin('order_goods','order.id','=','order_goods.order_id')
  769. ->leftJoin('warea','warea.id','=','users.warea_id')
  770. ->leftJoin('users as uu','uu.id','=','users.agent_id')
  771. ->whereIn('order.status',[2,3])
  772. ->whereBetween('order.created_at',$time)
  773. ->where('users.nickname', 'like', '%' . $search_name . '%')
  774. ->groupBy('users.id')
  775. ->having('account','>=',$minValue)
  776. ->get()->count();
  777. if($count==0){
  778. $this->error('400001','没有数据');
  779. }
  780. $list=DB::table('users')
  781. ->select('users.id','users.nickname','users.level','warea.name as warea','uu.nickname as agent',DB::raw('SUM(order_goods.totalprice) as account'))
  782. ->leftJoin('order','order.user_id','=','users.id')
  783. ->leftJoin('order_goods','order.id','=','order_goods.order_id')
  784. ->leftJoin('warea','warea.id','=','users.warea_id')
  785. ->leftJoin('users as uu','uu.id','=','users.agent_id')
  786. ->whereIn('order.status',[2,3])
  787. ->whereBetween('order.created_at',$time)
  788. ->where('users.nickname', 'like', '%' . $search_name . '%')
  789. ->groupBy('users.id')
  790. ->having('account','>=',$minValue)
  791. ->orderBy('account','desc')
  792. ->skip($num)->take($page_size)
  793. ->get();
  794. foreach ($list as $key=>$val){
  795. $rewards=Rewards::where('recom_id',$val->id)->where('type',2)->where('month',$input['time'])->first();
  796. if($rewards){
  797. $list[$key]->finished=1;
  798. }else{
  799. $list[$key]->finished=0;
  800. }
  801. foreach($sales_area_reward as $k=>$v){
  802. if($val->account>=$v[0] && $val->account<$v[1]){
  803. $list[$key]->rewards=round($val->account*$v[2]/100,2);
  804. }
  805. }
  806. $list[$key]->minValue=$minValue;
  807. }
  808. if($list){
  809. return $this->success_list($list,'SUCCESS',$count);
  810. }else{
  811. return $this->error();
  812. }
  813. }
  814. //获取门店业绩奖励货物结算信息
  815. public function getStoreSalesResultGoods(Request $request){
  816. $input=$request->all();
  817. $page_size=$input['page_size'];
  818. $page_index=$input['page_index'];
  819. $num = ($page_index - 1) * $page_size;
  820. $type=$input['type'];
  821. $starttime=date("Y-m-01 00:00:00",strtotime($input['time']));
  822. $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months"));
  823. $time = [$starttime, $endtime];
  824. if($type==1){
  825. $rate=System::where('keys','store_reward_goods_job')->value('values');
  826. }elseif($type==2){
  827. $rate=System::where('keys','store_reward_goods_enjoy')->value('values');
  828. }
  829. $count = DB::table('store')
  830. ->select('store.name as storename', DB::raw('SUM(order_goods.totalprice) as account'))
  831. ->leftJoin('users', function ($join) {
  832. $join->on('users.store_id', '=', 'store.id');
  833. })
  834. ->leftJoin('order', 'order.user_id', '=', 'users.id')
  835. ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
  836. ->leftJoin('users as uu','uu.id','=','store.man_id')
  837. ->where('store.type',$type)
  838. ->whereIn('order.status',[2,3])
  839. ->whereBetween('order.created_at',$time)
  840. ->groupBy('store.id')
  841. ->get()->count();
  842. if($count==0){
  843. $this->error('400001','没有数据');
  844. }
  845. $list = DB::table('store')
  846. ->select('store.id','store.name as storename','store.man_id','uu.nickname', DB::raw('SUM(order_goods.totalprice) as account'))
  847. ->leftJoin('users', function ($join) {
  848. $join->on('users.store_id', '=', 'store.id');
  849. })
  850. ->leftJoin('order', 'order.user_id', '=', 'users.id')
  851. ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
  852. ->leftJoin('users as uu','uu.id','=','store.man_id')
  853. ->where('store.type',$type)
  854. ->whereIn('order.status',[2,3])
  855. ->whereBetween('order.created_at',$time)
  856. ->groupBy('store.id')
  857. ->orderBy('account','desc')
  858. ->skip($num)->take($page_size)
  859. ->get();
  860. foreach($list as $key =>$val){
  861. $rewards=Rewards::where('recom_id',$val->man_id)->where('type',3)->where('month',$input['time'])->first();
  862. if($rewards){
  863. $list[$key]->finished=1;
  864. }else{
  865. $list[$key]->finished=0;
  866. }
  867. $list[$key]->style=2;
  868. $list[$key]->rewards=round($val->account*$rate/100,2);
  869. }
  870. if ($list) {
  871. return $this->success_list($list,'SUCCESS',$count);
  872. } else {
  873. return $this->error();
  874. }
  875. }
  876. //获取门店业绩奖励钱结算信息
  877. public function getStoreSalesResultMoney(Request $request){
  878. $input=$request->all();
  879. $page_size=$input['page_size'];
  880. $page_index=$input['page_index'];
  881. $num = ($page_index - 1) * $page_size;
  882. $type=$input['type'];
  883. $starttime=date("Y-m-01 00:00:00",strtotime($input['time']));
  884. $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months"));
  885. $time = [$starttime, $endtime];
  886. if($type==2){
  887. $rate=System::where('keys','store_reward_money_enjoy')->value('values');
  888. }elseif($type==3){
  889. $rate=System::where('keys','store_reward_money_experience')->value('values');
  890. }
  891. $rates=explode('/',$rate);
  892. $count=Store::where('type',$type)->with(['user:id,nickname'])->count();
  893. if($count==0){
  894. $this->error('400001','没有数据');
  895. }
  896. $list=Store::where('type',$type)
  897. ->with(['user:id,nickname'])
  898. ->orderBy('id','desc')->skip($num)->take($page_size)->get();
  899. foreach($list as $key=>$val){
  900. $rewards=Rewards::where('recom_id',$val->man_id)->where('type',3)->where('month',$input['time'])->first();
  901. if($rewards){
  902. $list[$key]->finished=1;
  903. }else{
  904. $list[$key]->finished=0;
  905. }
  906. $list[$key]->style=1;
  907. $list[$key]->acount=0;
  908. $info=DB::table('store')
  909. ->select('goods.type', DB::raw('SUM(order_goods.num) as total'))
  910. ->leftJoin('users', function ($join) {
  911. $join->on('users.store_id', '=', 'store.id');
  912. })
  913. ->leftJoin('order', 'order.user_id', '=', 'users.id')
  914. ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
  915. ->leftJoin('goods', 'goods.id', '=', 'order_goods.goods_id')
  916. ->where('store.id',$val['id'])
  917. ->where('store.type',$type)
  918. ->whereIn('order.status',[2,3])
  919. ->whereBetween('order.created_at',$time)
  920. ->groupBy('store.id','goods.type')
  921. ->get();
  922. foreach($info as $k =>$v){
  923. if($v->type==1){
  924. $info[$k]->price=$rates[0];
  925. $info[$k]->totalprice=$v->total*$rates[0];
  926. $list[$key]->acount+=$v->total*$rates[0];
  927. }
  928. if($v->type==2){
  929. $info[$k]->price=$rates[1];
  930. $info[$k]->totalprice=$v->total*$rates[1];
  931. $list[$key]->acount+=$v->total*$rates[1];
  932. }
  933. if($v->type==3){
  934. $info[$k]->price=$rates[2];
  935. $info[$k]->totalprice=$v->total*$rates[2];
  936. $list[$key]->acount+=$v->total*$rates[2];
  937. }
  938. }
  939. $list[$key]->info=$info;
  940. }
  941. if ($list) {
  942. return $this->success_list($list,'SUCCESS',$count);
  943. } else {
  944. return $this->error();
  945. }
  946. }
  947. //订单统计
  948. public function getOrderGoodsNumList(Request $request){
  949. $input=$request->all();
  950. switch ($input['type']) {
  951. case '0':
  952. $starttime = date("Y-m-d 00:00:00", strtotime($input['time'][0]));
  953. $endtime = date("Y-m-d 00:00:00", strtotime($input['time'][1] . "+1 days"));
  954. break;
  955. case '1':
  956. $starttime = date("Y-m-01 00:00:00", strtotime($input['time']));
  957. $endtime = date("Y-m-01 00:00:00", strtotime($input['time'] . "+1 months"));
  958. break;
  959. case '2':
  960. $starttime = date("Y-01-01 00:00:00", strtotime($input['time']));
  961. $endtime = date("Y-01-01 00:00:00", strtotime($input['time'] . "+1 year"));
  962. break;
  963. }
  964. $time = [$starttime, $endtime];
  965. $list=DB::table('goods')
  966. ->select('goods.id','goods.name', DB::raw('COUNT(distinct order.id) as account'))
  967. ->leftJoin('order_goods','order_goods.goods_id','=','goods.id')
  968. ->leftJoin('order','order.id','=','order_goods.order_id')
  969. ->whereIn('order.status',[2,3])
  970. ->whereBetween('order.created_at', $time)
  971. ->groupBy('goods.id')
  972. ->get();
  973. if ($list) {
  974. return $this->success($list);
  975. } else {
  976. return $this->error();
  977. }
  978. }
  979. /*获取所有的代理公司**/
  980. public function GetCrown(){
  981. $user_ids=Ordertest::where('is_deleted',0)->groupBy('user_id')->pluck('user_id')->toArray();
  982. $crown_ids=User::where('level',3)->whereNull('deleted_at')->pluck('id')->toArray();
  983. $crown_ids=array_unique(array_merge($crown_ids,$user_ids));
  984. $user=User::whereIn('id',$crown_ids)->select('id','remark_name','nickname','mobile','realname')->get();
  985. return $this->success($user);
  986. }
  987. /**每个代理公司每次活动统计*/
  988. public function GetCrownMoney(Request $request){
  989. $id=$request->input('id');
  990. $activity_list=Activity::where('disabled',0)->whereNull('deleted_at')->orderBy('id')->get();
  991. $data=[];
  992. foreach ($activity_list as $k=>$v){
  993. $money=Ordertest::where('user_id',$id)->where('is_deleted',0)->whereBetween('created_at',[$v->start_time,$v->end_time])->sum('money');
  994. $data[$k]['money']=$money;
  995. $data[$k]['title']=$v->title;
  996. $data[$k]['start_time']=$v->start_time;
  997. $data[$k]['end_time']=$v->end_time;
  998. }
  999. return $this->success($data);
  1000. }
  1001. /**今年活动金额*/
  1002. public function NewYearMoney(){
  1003. $time_start=Carbon::now()->startOfYear();
  1004. $time_end=Carbon::now()->endOfYear();
  1005. $moneys=Ordertest::where('is_deleted',0)->whereBetween('created_at',[$time_start,$time_end])->sum('money');
  1006. return $this->success($moneys);
  1007. }
  1008. public function ActivityMoney(){
  1009. $activity_list=Activity::where('disabled',0)->whereNull('deleted_at')->orderBy('id')->get();
  1010. $data=[];
  1011. foreach ($activity_list as $k=>$v){
  1012. $money=Ordertest::where('is_deleted',0)->whereBetween('created_at',[$v->start_time,$v->end_time])->sum('money');
  1013. $data[$k]['money']=$money;
  1014. $data[$k]['title']=$v->title;
  1015. $data[$k]['start_time']=$v->start_time;
  1016. $data[$k]['end_time']=$v->end_time;
  1017. }
  1018. return $this->success($data);
  1019. }
  1020. public function GetCrownInfo(Request $request){
  1021. $data=User::whereNull('deleted_at')->where('level',3)->get();
  1022. $re=[];
  1023. foreach ($data as $k=>$v){
  1024. $re[$k]['备注昵称']=$v->remark_name;
  1025. $re[$k]['真实姓名']=$v->realname;
  1026. $re[$k]['手机号']=$v->mobile;
  1027. $re[$k]['身份证号']=WdStore::where('user_id',$v->id)->value('idCard');
  1028. }
  1029. return $this->success_list($re);
  1030. }
  1031. public function GetTeamCount(){
  1032. $data=User::whereNull('deleted_at')->where('level',3)->get();
  1033. $re=[];
  1034. foreach ($data as $k=>$v){
  1035. $ids=User::where('agent_id',$v->id)->whereIn('cert_status',[3,6])->pluck('id')->toArray();
  1036. $idss=User::whereIn('agent_id',$ids)->whereIn('cert_status',[3,6])->pluck('id')->toArray();
  1037. $id_all=array_merge($ids,$idss);
  1038. $count=count($id_all);
  1039. $count1=User::whereIn('id',$id_all)->where('level',1)->count();
  1040. $count2=User::whereIn('id',$id_all)->where('level',2)->count();
  1041. $re[$k]['count']=$count;
  1042. $re[$k]['count_jl']=$count2;
  1043. $re[$k]['count_zg']=$count1;
  1044. $re[$k]['nickname']=$v->remark_name=null?$v->nickname:$v->remark_name;
  1045. }
  1046. $last_ages = array_column($re,'count');
  1047. array_multisort($last_ages ,SORT_DESC,$re);
  1048. return $this->success_list($re);
  1049. }
  1050. public function GetNewTeamCount(){
  1051. $data=User::whereNull('deleted_at')->where('level',3)->get();
  1052. $re=[];
  1053. $new_ids=User::whereIn('cert_status',[3,6])->whereBetween('created_at',[Carbon::now()->startOfDay(),Carbon::now()->endOfDay()])->pluck('id')->toArray();
  1054. $i=0;
  1055. foreach ($data as $k=>$v){
  1056. $ids=User::where('agent_id',$v->id)->whereIn('cert_status',[3,6])->pluck('id')->toArray();
  1057. $idss=User::whereIn('agent_id',$ids)->whereIn('cert_status',[3,6])->pluck('id')->toArray();
  1058. $id_all=array_merge($ids,$idss);
  1059. $arr=array_intersect($new_ids,$id_all);
  1060. if (count($arr)){
  1061. $re[]=[
  1062. 'count'=>count($arr),
  1063. 'nickname'=>$v->remark_name=null?$v->nickname:$v->remark_name
  1064. ];
  1065. }
  1066. }
  1067. $last_ages = array_column($re,'count');
  1068. array_multisort($last_ages ,SORT_DESC,$re);
  1069. return $this->success_list($re);
  1070. }
  1071. public function GetAreaCount(Request $request){
  1072. $province=$request->input('province','');
  1073. $city=$request->input('city','');
  1074. $area=$request->input('area','');
  1075. $user=User::whereNull('deleted_at')->whereIn('cert_status',[3,6]);
  1076. if ($province){
  1077. $user->where('province',$province);
  1078. }
  1079. if ($city){
  1080. $user->where('city',$city);
  1081. }
  1082. if ($area){
  1083. $user->where('country',$area);
  1084. }
  1085. $user=$user->where('id','>',1)->select(DB::raw('count(*) as total'),'level')->groupBy('level')->get();
  1086. return $this->success($user);
  1087. }
  1088. }