UserStatistics.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. class UserStatistics extends Auth {
  6. public function user() {
  7. $roles = Db::name('roles')
  8. ->where('type', 1)
  9. ->where('parent_id', 0)
  10. ->select();
  11. $all = 0;
  12. foreach ($roles as $k => $v) {
  13. $res = Db::name('user')
  14. ->alias('a')
  15. ->join('user_roles b', 'a.id=b.user_id')
  16. ->join('user_org c', 'a.id=c.user_id')
  17. ->join('roles d', 'b.roles_id=d.id')
  18. ->where('a.del', 0)
  19. ->where('a.enable', 1)
  20. ->where('d.parent_id', $v['id'])
  21. ->where('c.org_id', $this->orgId)
  22. ->count();
  23. $roles[$k]['value'] = $res ? $res : 0;
  24. $all += $res;
  25. }
  26. $this->assign('list', $roles);
  27. $this->assign('allCount', $all);
  28. return $this->fetch();
  29. }
  30. public function org_order() {
  31. $cur = date('Y-m-d');
  32. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
  33. $end = input('end', date('Y-m-d'));
  34. $title = input('title','');
  35. $start1 = $start . ' 00:00:00';
  36. $end1 = $end . ' 23:59:59';
  37. $list = $this->orgOrderData($start1, $end1,$title);
  38. $this->assign('list', $list['list']);
  39. $this->assign('totalList', $list['dt']);
  40. $this->assign('start', $start);
  41. $this->assign('end', $end);
  42. $this->assign('title', $title);
  43. return $this->fetch();
  44. }
  45. public function orgOrderData($start1, $end1,$title) {
  46. $map[] = ['del','=',0];
  47. if($title !=''){
  48. $map[] = ['name','like','%'.$title.'%'];
  49. }
  50. $list = Db::name('org')
  51. ->where('type',1)
  52. ->where('enable',1)
  53. ->where($map)
  54. ->where('parent_id',1)
  55. ->field('id,name')
  56. ->select();
  57. $map1[] = ['create_time', '>=', $start1];
  58. $map1[] = ['create_time', '<=', $end1];
  59. $alist = [];
  60. foreach ($list as $k1 => $v1) {
  61. $orgs = model('org')->getAllNextId($v1['id']);
  62. $orgList = Db::name('org')
  63. ->where('del',0)
  64. ->where('enable',1)
  65. ->where('type',2)
  66. ->whereIn('id',$orgs)
  67. ->field('id,name')
  68. ->select();
  69. foreach ($orgList as $kk=>$vv){
  70. $count = Db::name('orders')
  71. ->where('del', 0)
  72. ->where('org_id', $vv['id'])
  73. ->where($map1)
  74. ->count();
  75. $bx = Db::name('orders')
  76. ->where('del', 0)
  77. ->where('org_id', $vv['id'])
  78. ->where('work_type_mode', 1)
  79. ->where($map1)
  80. ->count();
  81. $bj = Db::name('orders')
  82. ->where('del', 0)
  83. ->where('org_id', $vv['id'])
  84. ->where('work_type_mode', 2)
  85. ->where($map1)
  86. ->count();
  87. $ys = Db::name('orders')
  88. ->where('del', 0)
  89. ->where('org_id', $vv['id'])
  90. ->where('work_type_mode', 3)
  91. ->where($map1)
  92. ->count();
  93. $yh = Db::name('orders')
  94. ->where('del', 0)
  95. ->where('org_id', $vv['id'])
  96. ->where('work_type_mode', 4)
  97. ->where($map1)
  98. ->count();
  99. $alist[] = [
  100. 'area_name'=> $kk==0 ?$v1['name']:'',
  101. 'org_num'=>count($orgList),
  102. 'name'=>$vv['name'],
  103. 'count'=>$count,
  104. 'bx'=>$bx,
  105. 'bj'=>$bj,
  106. 'ys'=>$ys,
  107. 'yh'=>$yh,
  108. ];
  109. }
  110. }
  111. $totalList =[
  112. array_sum(array_column($alist,'count')),
  113. array_sum(array_column($alist,'bx')),
  114. array_sum(array_column($alist,'bj')),
  115. array_sum(array_column($alist,'ys')),
  116. array_sum(array_column($alist,'yh')),
  117. ];
  118. return ['list'=>$alist,'dt'=>$totalList];
  119. }
  120. public function orgOrderExport() {
  121. $cur = date('Y-m-d');
  122. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
  123. $end = input('end', date('Y-m-d'));
  124. $start1 = $start . ' 00:00:00';
  125. $end1 = $end . ' 23:59:59';
  126. $title = input('title','');
  127. $list = $this->orgOrderData($start1, $end1,$title);
  128. $ret = $list['list'];
  129. $totalList = $list['dt'];
  130. set_time_limit(0);
  131. ini_set("memory_limit", "1024M");
  132. $title = '各项目订单统计';
  133. header("Content-type: application/vnd.ms-excel");
  134. header("Content-Type: application/force-download");
  135. header("Content-Disposition: attachment; filename=".$title.".xls");
  136. header('Expires:0');
  137. header('Pragma:public');
  138. $res = '';
  139. $res.='<table style="border-top: 1px solid #ddd;border-left: 1px solid #ddd;border-spacing: 0;">';
  140. $res.='<tr style="background: #ffffff;">';
  141. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">区域名称</th>';
  142. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">项目名称</th>';
  143. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">总数</th>';
  144. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">报修</th>';
  145. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">保洁</th>';
  146. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">运送</th>';
  147. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">应急</th>';
  148. $res.='</tr>';
  149. foreach ($ret as $k=>$v){
  150. $res.='<tr style="background: #ffffff;">';
  151. if($v['area_name']){
  152. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="'.$v['org_num'].'">'.$v['area_name'].'</td>';
  153. }
  154. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['name'].'</td>';
  155. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['count'].'</td>';
  156. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['bx'].'</td>';
  157. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['bj'].'</td>';
  158. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['ys'].'</td>';
  159. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['yh'].'</td>';
  160. $res.='</tr>';
  161. }
  162. $res.='<tr style="background: #ffffff;">';
  163. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" colspan="2">合计:</td>';
  164. foreach ($totalList as $k=>$v){
  165. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" >'.$v.'</td>';
  166. }
  167. $res.='</tr>';
  168. echo $res;
  169. }
  170. public function org_user() {
  171. $title = input('title','');
  172. $list = $this->orgUserData($title);
  173. $this->assign('list', $list['list']);
  174. $this->assign('totalList', $list['totalList']);
  175. $this->assign('title',$title);
  176. return $this->fetch();
  177. }
  178. public function orgUserData($title) {
  179. $map[] = ['del','=',0];
  180. if($title !=''){
  181. $map[] = ['name','like','%'.$title.'%'];
  182. }
  183. $lists = Db::name('org')
  184. ->where('type',1)
  185. ->where('enable',1)
  186. ->where($map)
  187. ->where('parent_id',1)
  188. ->field('id,name')
  189. ->select();
  190. $alist = [];
  191. foreach ($lists as $k1 => $v1) {
  192. $orgs = model('org')->getAllNextId($v1['id']);
  193. $orgList = Db::name('org')
  194. ->where('del',0)
  195. ->where('enable',1)
  196. ->where('type',2)
  197. ->whereIn('id',$orgs)
  198. ->field('id,name')
  199. ->select();
  200. foreach ($orgList as $kk=>$vv){
  201. $res = Db::name('user')
  202. ->alias('a')
  203. ->field('a.id,d.parent_id')
  204. ->join('user_roles b', 'a.id=b.user_id')
  205. ->join('user_org c', 'a.id=c.user_id')
  206. ->join('roles d', 'b.roles_id=d.id')
  207. ->where('a.del', 0)
  208. ->where('a.enable', 1)
  209. ->where('d.parent_id', 'in',[3,4,5,6,7,8,9,10])
  210. ->where('c.org_id', $vv['id'])
  211. ->select();
  212. $a1 = $a2 = $a3= $a4= $a5= $a6= $a7 = $a8 =0;
  213. foreach ($res as $kkk=>$vvv){
  214. if($vvv['parent_id'] == 3){
  215. $a1 +=1;
  216. }
  217. if($vvv['parent_id'] == 4){
  218. $a2 +=1;
  219. }
  220. if($vvv['parent_id'] == 5){
  221. $a3 +=1;
  222. }
  223. if($vvv['parent_id'] == 6){
  224. $a4 +=1;
  225. }
  226. if($vvv['parent_id'] == 7){
  227. $a5 +=1;
  228. }
  229. if($vvv['parent_id'] == 8){
  230. $a6 +=1;
  231. }
  232. if($vvv['parent_id'] == 9){
  233. $a7 +=1;
  234. }
  235. if($vvv['parent_id'] == 10){
  236. $a8 +=1;
  237. }
  238. }
  239. $alist[] = [
  240. 'area_name'=> $kk==0 ?$v1['name']:'',
  241. 'org_num'=>count($orgList),
  242. 'name'=>$vv['name'],
  243. 'count'=>count($res),
  244. 'a1'=>$a1,
  245. 'a2'=>$a2,
  246. 'a3'=>$a3,
  247. 'a4'=>$a4,
  248. 'a5'=>$a5,
  249. 'a6'=>$a6,
  250. 'a7'=>$a7,
  251. 'a8'=>$a8,
  252. ];
  253. }
  254. }
  255. $totalList =[
  256. array_sum(array_column($alist,'count')),
  257. array_sum(array_column($alist,'a1')),
  258. array_sum(array_column($alist,'a2')),
  259. array_sum(array_column($alist,'a3')),
  260. array_sum(array_column($alist,'a4')),
  261. array_sum(array_column($alist,'a5')),
  262. array_sum(array_column($alist,'a6')),
  263. array_sum(array_column($alist,'a7')),
  264. array_sum(array_column($alist,'a8')),
  265. ];
  266. return ['list'=>$alist,'totalList'=>$totalList];
  267. }
  268. public function orgUserExport() {
  269. $title = input('title','');
  270. $list = $this->orgUserData($title);
  271. $ret = $list['list'];
  272. $totalList = $list['totalList'];
  273. set_time_limit(0);
  274. ini_set("memory_limit", "1024M");
  275. $title = '各项目人员统计';
  276. header("Content-type: application/vnd.ms-excel");
  277. header("Content-Type: application/force-download");
  278. header("Content-Disposition: attachment; filename=".$title.".xls");
  279. header('Expires:0');
  280. header('Pragma:public');
  281. $res = '';
  282. $res.='<table style="border-top: 1px solid #ddd;border-left: 1px solid #ddd;border-spacing: 0;">';
  283. $res.='<tr style="background: #ffffff;">';
  284. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">区域名称</th>';
  285. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">项目名称</th>';
  286. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">总数</th>';
  287. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">客户</th>';
  288. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">综合</th>';
  289. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">保安</th>';
  290. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">运送</th>';
  291. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">维修</th>';
  292. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">保洁</th>';
  293. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">调度</th>';
  294. $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">管理层</th>';
  295. $res.='</tr>';
  296. foreach ($ret as $k=>$v){
  297. $res.='<tr style="background: #ffffff;">';
  298. if($v['area_name']){
  299. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="'.$v['org_num'].'">'.$v['area_name'].'</td>';
  300. }
  301. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['name'].'</td>';
  302. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['count'].'</td>';
  303. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['a1'].'</td>';
  304. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['a2'].'</td>';
  305. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['a3'].'</td>';
  306. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['a4'].'</td>';
  307. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['a5'].'</td>';
  308. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['a6'].'</td>';
  309. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['a7'].'</td>';
  310. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['a8'].'</td>';
  311. $res.='</tr>';
  312. }
  313. $res.='<tr style="background: #ffffff;">';
  314. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" colspan="2">合计:</td>';
  315. foreach ($totalList as $k=>$v){
  316. $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" >'.$v.'</td>';
  317. }
  318. $res.='</tr>';
  319. echo $res;
  320. }
  321. }