Dashboard.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\Admin;
  4. use app\admin\model\User;
  5. use app\common\controller\Backend;
  6. use app\common\model\Attachment;
  7. use fast\Date;
  8. use think\Db;
  9. /**
  10. * 控制台
  11. *
  12. * @icon fa fa-dashboard
  13. * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
  14. */
  15. class Dashboard extends Backend
  16. {
  17. /**
  18. * 查看
  19. */
  20. public function index()
  21. {
  22. try {
  23. \think\Db::execute("SET @@sql_mode='';");
  24. } catch (\Exception $e) {
  25. }
  26. $column = [];
  27. $starttime = Date::unixtime('day', -6);
  28. $endtime = Date::unixtime('day', 0, 'end');
  29. $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
  30. ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
  31. ->group('join_date')
  32. ->select();
  33. for ($time = $starttime; $time <= $endtime;) {
  34. $column[] = date("Y-m-d", $time);
  35. $time += 86400;
  36. }
  37. $userlist = array_fill_keys($column, 0);
  38. foreach ($joinlist as $k => $v) {
  39. $userlist[$v['join_date']] = $v['nums'];
  40. }
  41. $dbTableList = Db::query("SHOW TABLE STATUS");
  42. $addonList = get_addon_list();
  43. $totalworkingaddon = 0;
  44. $totaladdon = count($addonList);
  45. foreach ($addonList as $index => $item) {
  46. if ($item['state']) {
  47. $totalworkingaddon += 1;
  48. }
  49. }
  50. $this->view->assign([
  51. 'totaluser' => User::count(),
  52. 'totaladdon' => $totaladdon,
  53. 'totaladmin' => Admin::count(),
  54. 'totalcategory' => \app\common\model\Category::count(),
  55. 'todayusersignup' => User::whereTime('jointime', 'today')->count(),
  56. 'todayuserlogin' => User::whereTime('logintime', 'today')->count(),
  57. 'sevendau' => User::whereTime('jointime|logintime|prevtime', '-7 days')->count(),
  58. 'thirtydau' => User::whereTime('jointime|logintime|prevtime', '-30 days')->count(),
  59. 'threednu' => User::whereTime('jointime', '-3 days')->count(),
  60. 'sevendnu' => User::whereTime('jointime', '-7 days')->count(),
  61. 'dbtablenums' => count($dbTableList),
  62. 'dbsize' => array_sum(array_map(function ($item) {
  63. return $item['Data_length'] + $item['Index_length'];
  64. }, $dbTableList)),
  65. 'totalworkingaddon' => $totalworkingaddon,
  66. 'attachmentnums' => Attachment::count(),
  67. 'attachmentsize' => Attachment::sum('filesize'),
  68. 'picturenums' => Attachment::where('mimetype', 'like', 'image/%')->count(),
  69. 'picturesize' => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
  70. ]);
  71. $this->assignconfig('column', array_keys($userlist));
  72. $this->assignconfig('userdata', array_values($userlist));
  73. return $this->view->fetch();
  74. }
  75. /**
  76. * 订单统计笔数
  77. */
  78. public function order(){
  79. $starttime = strtotime(date('Y-m-d',strtotime("-30 day"))); //默认30天前
  80. $endtime = strtotime(date('Y-m-d')) - 1;
  81. //接收
  82. $choicedatetime = input('choicedatetime','');
  83. if(!empty($choicedatetime)){
  84. $choicedatetime = explode(' - ',$choicedatetime);
  85. $starttime = strtotime($choicedatetime[0]);
  86. $endtime = strtotime($choicedatetime[1]) + 86399;
  87. }
  88. $this->view->assign('defaultdatetime',date('Y-m-d',$starttime).' - '.date('Y-m-d',$endtime));
  89. //dump($starttime);
  90. //dump($endtime);
  91. //过去30天
  92. $ec_date = [];
  93. $ec_ordernum = [];
  94. // $ec_orderfee = [];
  95. $whereop = $this->whereop('company_id');
  96. $whereop['status'] = 3;
  97. for($i=$starttime;$i<$endtime;$i+=86400){
  98. $starttime_i = $i;
  99. $endtime_i = $i + 86399;
  100. //dump($starttime);
  101. //日历
  102. $ec_date[] = date('Y-m-d',$starttime_i);
  103. //下单人数
  104. $whereop['finish_time'] = ['between',[$starttime_i,$endtime_i]];
  105. $ec_ordernum[] = Db::name('order')->where($whereop)->count('id');
  106. //下单金额
  107. // $ec_orderfee[] = Db::name('order')->where(['createtime'=>['between',[$starttime_i,$endtime_i]]])->sum('pay_fee');
  108. }
  109. $this->assignconfig('ec_date', $ec_date);
  110. $this->assignconfig('ec_ordernum', $ec_ordernum);
  111. // $this->assignconfig('ec_orderfee', $ec_orderfee);
  112. return $this->view->fetch();
  113. }
  114. /**
  115. * 订单统计各类型
  116. */
  117. public function orderservicetype(){
  118. $starttime = strtotime(date('Y-m-d',strtotime("-30 day"))); //默认30天前
  119. $endtime = strtotime(date('Y-m-d')) - 1;
  120. //接收
  121. $choicedatetime = input('choicedatetime','');
  122. if(!empty($choicedatetime)){
  123. $choicedatetime = explode(' - ',$choicedatetime);
  124. $starttime = strtotime($choicedatetime[0]);
  125. $endtime = strtotime($choicedatetime[1]) + 86399;
  126. }
  127. $this->view->assign('defaultdatetime',date('Y-m-d',$starttime).' - '.date('Y-m-d',$endtime));
  128. //饼图
  129. $whereop = $this->whereop('company_id');
  130. $whereop['status'] = 3;
  131. $whereop['finish_time'] = ['between',[$starttime,$endtime]];
  132. $servicetype = Db::name('servicetype')->field('id,title as name')->select();
  133. $all_count = Db::name('order')->where($whereop)->count('id');
  134. foreach($servicetype as $key => &$val){
  135. $whereop['servicetype_id'] = $val['id'];
  136. $val['value'] = Db::name('order')->where($whereop)->count('id');
  137. $val['name'] = $val['name'] . bcdiv($val['value'],$all_count,4)*100 . '%';
  138. unset($val['id']);
  139. }
  140. $this->assignconfig('servicetype', $servicetype);
  141. return $this->view->fetch();
  142. }
  143. /**
  144. * 客户统计
  145. */
  146. public function datacentertwo(){
  147. $whereop = $this->whereop('company_id');
  148. $usernumber = Db::name('user_wallet')->where($whereop)->count();//充卡客户数量
  149. $summoney = Db::name('user_wallet')->where($whereop)->sum('money');//充卡余额
  150. //客户类别
  151. $comefrom_config = ['线下新客','老带新','平台引流','自然进店'];
  152. $comefrom_array = [];
  153. foreach($comefrom_config as $key => $val){
  154. $number = Db::name('user_wallet')->where($whereop)->where('comefrom',$val)->count();
  155. $comefrom_array[] = ['name'=>$val . bcdiv($number,$usernumber,4)*100 . '%','value'=>$number];
  156. }
  157. //
  158. $this->assign('usernumber', $usernumber);
  159. $this->assign('summoney', $summoney);
  160. $this->assignconfig('comefrom_array', $comefrom_array);
  161. return $this->view->fetch();
  162. }
  163. /**
  164. * 七日销售金额数据统计
  165. */
  166. public function datacenterthree(){
  167. $servicetype = Db::name('servicetype')->field('id,title as name')->select();
  168. $servicename = array_column($servicetype,'name');
  169. $this->assignconfig('servicename',$servicename);
  170. //七日
  171. $starttime = strtotime(date('Y-m-d')) - 518400;
  172. $endtime = strtotime(date('Y-m-d')) + 86399;
  173. $ec_date = [];
  174. $map = [
  175. 'company_id' => $this->auth->company_id,
  176. 'status' => 3,
  177. ];
  178. /*if($this->auth->type == 2){
  179. $map['staff_id'] = $this->auth->id;
  180. }*/
  181. for($i=$starttime;$i<$endtime;$i+=86400){
  182. $starttime_i = $i;
  183. $endtime_i = $i + 86399;
  184. //日历
  185. $ec_date[] = date('d',$starttime_i).'日';
  186. }
  187. $this->assignconfig('ec_date',$ec_date);
  188. foreach($servicetype as $key => &$val){
  189. $val['type'] = 'bar';
  190. if($key == 0){
  191. $val['barGap'] = 0;
  192. }
  193. $val['emphasis'] = ['focus'=>'series'];
  194. $val['data'] = [];
  195. $map['servicetype_id'] = $val['id'];
  196. for($i=$starttime;$i<$endtime;$i+=86400){
  197. $starttime_i = $i;
  198. $endtime_i = $i + 86399;
  199. //销售金额
  200. $map['finish_time'] = ['between',[$starttime_i,$endtime_i]];
  201. $val['data'][] = Db::name('order')->where($map)->sum('total_fee');
  202. }
  203. unset($val['id']);
  204. }
  205. $this->assignconfig('servicetype',$servicetype);
  206. return $this->view->fetch();
  207. }
  208. /**
  209. * 半年销售金额数据统计
  210. */
  211. public function datacenterthree2(){
  212. $servicetype = Db::name('servicetype')->field('id,title as name')->select();
  213. $servicename = array_column($servicetype,'name');
  214. $this->assignconfig('servicename',$servicename);
  215. $thismonth = strtotime(date('Y-m-01'));
  216. $ec_date = [];
  217. $map = [
  218. 'company_id' => $this->auth->company_id,
  219. 'status' => 3,
  220. ];
  221. /*if($this->auth->type == 2){
  222. $map['staff_id'] = $this->auth->id;
  223. }*/
  224. for($i=5;$i>=0;$i--){
  225. $starttime_i = strtotime("-".$i." month",$thismonth);
  226. $endtime_i = strtotime("-".($i-1) ." month",$thismonth) - 1;
  227. //日历
  228. $ec_date[] = date('m',$starttime_i).'月';
  229. //dump(date('Y-m-d H:i:s',$starttime_i));
  230. //dump(date('Y-m-d H:i:s',$endtime_i));
  231. }
  232. $this->assignconfig('ec_date',$ec_date);
  233. foreach($servicetype as $key => &$val){
  234. $val['type'] = 'bar';
  235. if($key == 0){
  236. $val['barGap'] = 0;
  237. }
  238. $val['emphasis'] = ['focus'=>'series'];
  239. $val['data'] = [];
  240. $map['servicetype_id'] = $val['id'];
  241. for($i=5;$i>=0;$i-=1){
  242. $starttime_i = strtotime("-".$i." month",$thismonth);
  243. $endtime_i = strtotime("-".($i-1) ." month",$thismonth) - 1;
  244. //销售金额
  245. $map['finish_time'] = ['between',[$starttime_i,$endtime_i]];
  246. $val['data'][] = Db::name('order')->where($map)->sum('total_fee');
  247. }
  248. unset($val['id']);
  249. }
  250. $this->assignconfig('servicetype',$servicetype);
  251. return $this->view->fetch();
  252. }
  253. /**
  254. * 业绩统计笔数
  255. */
  256. public function datacenterfour(){
  257. $starttime = strtotime(date('Y-m-d',strtotime("-30 day"))); //默认30天前
  258. $endtime = strtotime(date('Y-m-d')) - 1;
  259. //接收
  260. $choicedatetime = input('choicedatetime','');
  261. if(!empty($choicedatetime)){
  262. $choicedatetime = explode(' - ',$choicedatetime);
  263. $starttime = strtotime($choicedatetime[0]);
  264. $endtime = strtotime($choicedatetime[1]) + 86399;
  265. }
  266. $this->view->assign('defaultdatetime',date('Y-m-d',$starttime).' - '.date('Y-m-d',$endtime));
  267. $staff = Db::name('company_staff')->field('id,truename')->where('type',2)->where('company_id',$this->auth->company_id)->select();
  268. //柱状图
  269. $ec_date = [];
  270. $ec_ordernum = [];
  271. $map = [
  272. 'company_id' => $this->auth->company_id,
  273. 'status' => 3,
  274. 'finish_time' => ['between',[$starttime,$endtime]],
  275. ];
  276. foreach($staff as $key => $val){
  277. //日历
  278. $ec_date[] = $val['truename'];
  279. //下单人数
  280. $map['staff_id'] = $val['id'];
  281. $ec_ordernum[] = Db::name('order')->where($map)->count('id');
  282. }
  283. $this->assignconfig('ec_date', $ec_date);
  284. $this->assignconfig('ec_ordernum', $ec_ordernum);
  285. return $this->view->fetch();
  286. }
  287. /**
  288. * 业绩统计金额
  289. */
  290. public function datacenterfive(){
  291. $starttime = strtotime(date('Y-m-d',strtotime("-30 day"))); //默认30天前
  292. $endtime = strtotime(date('Y-m-d')) - 1;
  293. //接收
  294. $choicedatetime = input('choicedatetime','');
  295. if(!empty($choicedatetime)){
  296. $choicedatetime = explode(' - ',$choicedatetime);
  297. $starttime = strtotime($choicedatetime[0]);
  298. $endtime = strtotime($choicedatetime[1]) + 86399;
  299. }
  300. $this->view->assign('defaultdatetime',date('Y-m-d',$starttime).' - '.date('Y-m-d',$endtime));
  301. $staff = Db::name('company_staff')->field('id,truename')->where('type',2)->where('company_id',$this->auth->company_id)->select();
  302. //柱状图
  303. $ec_date = [];
  304. $ec_ordernum = [];
  305. $map = [
  306. 'company_id' => $this->auth->company_id,
  307. 'status' => 3,
  308. 'finish_time' => ['between',[$starttime,$endtime]],
  309. ];
  310. foreach($staff as $key => $val){
  311. //日历
  312. $ec_date[] = $val['truename'];
  313. //下单人数
  314. $map['staff_id'] = $val['id'];
  315. $ec_ordernum[] = Db::name('order')->where($map)->sum('total_fee');
  316. }
  317. $this->assignconfig('ec_date', $ec_date);
  318. $this->assignconfig('ec_ordernum', $ec_ordernum);
  319. return $this->view->fetch();
  320. }
  321. }