Dashboard.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\service\platform;
  10. use app\admin\service\Service;
  11. use app\admin\model\Order as OrderModel;
  12. use app\admin\model\Users as UsersModel;
  13. use app\admin\model\GroupGoods as GoodsModel;
  14. use app\common\models\users\UsersComment;
  15. use app\common\models\users\UsersRechange;
  16. use app\common\models\order\OrderRefundment;
  17. use app\common\models\users\UsersSms;
  18. use app\common\models\users\UsersWithdrawLog;
  19. use mall\utils\Tool;
  20. use think\App;
  21. use think\facade\Db;
  22. use think\facade\Request;
  23. class Dashboard extends Service {
  24. /**
  25. * 获取统计信息
  26. * @return array
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\DbException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. */
  31. public static function getData(){
  32. // 会员订单总数和订单总购物额
  33. $order = OrderModel::field("COUNT(*) AS order_num,order_amount")->where("status",5)->find();
  34. $usersGoodsCount = OrderModel::withJoin("users")->field("COUNT(DISTINCT order.user_id) as count")->where("order.status",5)->find();
  35. // 订单排行
  36. $orderHot = OrderModel::withJoin("users")->field("SUM(order.order_amount) as total,COUNT(order.id) as count,users.username")->where("order.status",5)->group("order.user_id")->order("total","DESC")->limit(10)->select()->toArray();
  37. foreach($orderHot as $k=>$v){
  38. $orderHot[$k]['p'] = $k+1;
  39. }
  40. $data = OrderModel::field('SUM(order_amount) as amount,count(*) as total,create_time,FROM_UNIXTIME(create_time,"%Y%m%d") as days')
  41. ->where("status","in","2,5")
  42. ->group('days having create_time >= ' . strtotime(date('Y-m-d 00:00:00', strtotime("-30 day"))) . ' and create_time <= ' . strtotime(date('Y-m-d 23:59:59', time())))->select()->toArray();
  43. $count = $days = $amount = [];
  44. foreach ($data as $key => $val) {
  45. $days[] = $val['days'];
  46. $amount[] = $val['amount'];
  47. $count[] = $val['total'];
  48. }
  49. return [
  50. "order_total"=>OrderModel::count(),
  51. "goods_total"=>GoodsModel::count(),
  52. "users_total"=>UsersModel::count(),
  53. "users_day_count"=>UsersModel::whereDay("create_time")->count(),
  54. "order_pay_day_count"=>OrderModel::where("pay_status",1)->whereDay("create_time")->count(),
  55. "order_total_price"=>OrderModel::whereDay("create_time")->sum("order_amount"),
  56. "order_pay_total_price"=>OrderModel::where("pay_status",1)->whereDay("create_time")->sum("order_amount"),
  57. "recharge_total"=>UsersRechange::where("status",1)->whereDay("create_time")->sum("order_amount"),
  58. "order_no_pay_count"=>OrderModel::where(["pay_status"=>0,"status"=>1])->count(),
  59. "order_delivery_count"=>OrderModel::where(["pay_status"=>0,"status"=>2,"delivery_status"=>0])->count(),
  60. "order_refundment_count"=>OrderRefundment::where("pay_status",0)->count(),
  61. "users_bill_order_count"=>UsersWithdrawLog::where("status",0)->count(),
  62. "users_comment_total"=>UsersComment::count(),
  63. "days"=>implode(',',$days),
  64. "order_amount"=>implode(',',$amount),
  65. "order_count"=>implode(',',$count),
  66. "e"=>empty($order["order_amount"]) ? "0.00" : number_format($order["order_amount"],2),
  67. "f"=>empty($order["order_amount"]) ? "0.00" : number_format(($order["order_amount"] > 0 && $usersGoodsCount["count"] > 0 ? $order["order_amount"]/$usersGoodsCount["count"] : 0),2),
  68. "g"=>$orderHot
  69. ];
  70. }
  71. /**
  72. * 获取系统信息
  73. * @return array
  74. */
  75. public static function getSystemInfo(){
  76. $version = Db::query("SELECT VERSION() as version");
  77. return [ "version"=>$version[0]['version'], "think_ver"=>App::VERSION, "ip"=>gethostbyname(Request::host()) ];
  78. }
  79. /**
  80. * 获取缓存目录
  81. * @return array
  82. */
  83. public static function getCacheList(){
  84. $path = Tool::getRootPath() . 'runtime/';
  85. return ["code"=>0,"msg"=>"","data"=>[
  86. [
  87. "type"=>"db-sms","info"=>"清理已过期短信验证码",
  88. 'size'=>Db::name("users_sms")->where("create_time","<=", strtotime("-1 day"))->count() . " 条"
  89. ],
  90. [
  91. "type"=>"file-cache","info"=>"数据缓存",
  92. 'size'=>Tool::convert(
  93. Tool::getDirSize($path . "admin/cache") +
  94. Tool::getDirSize($path . "home/cache") +
  95. Tool::getDirSize($path . "cache")
  96. )
  97. ],
  98. [
  99. "type"=>"file-log","info"=>"日志数据",
  100. 'size'=>Tool::convert(
  101. Tool::getDirSize($path . "admin/log") +
  102. Tool::getDirSize($path . "home/log") +
  103. Tool::getDirSize($path . "api/log")
  104. )
  105. ],
  106. [
  107. "type"=>"file-temp","info"=>"模板缓存",
  108. 'size'=>Tool::convert(
  109. Tool::getDirSize($path . "admin/temp") +
  110. Tool::getDirSize($path . "home/temp")
  111. )
  112. ]
  113. ]];
  114. }
  115. /**
  116. * 清理缓存
  117. * @param $type
  118. * @return bool
  119. * @throws \think\db\exception\DbException
  120. */
  121. public static function clearCache($type){
  122. $path = Tool::getRootPath() . 'runtime/';
  123. if(!in_array($type, ["file-cache","file-log","file-temp","db-token","db-sms"])){
  124. throw new \Exception("非法操作!",0);
  125. }
  126. switch($type){
  127. case "db-sms":
  128. UsersSms::where("create_time","<=", strtotime("-1 day"))->delete();
  129. break;
  130. case "file-cache":
  131. Tool::deleteFile($path . "cache");
  132. Tool::deleteFile($path . "admin/cache");
  133. Tool::deleteFile($path . "home/cache");
  134. break;
  135. case "file-log":
  136. Tool::deleteFile($path . "admin/log");
  137. Tool::deleteFile($path . "home/log");
  138. Tool::deleteFile($path . "api/log");
  139. break;
  140. case "file-temp":
  141. Tool::deleteFile($path . "admin/temp");
  142. Tool::deleteFile($path . "home/temp");
  143. break;
  144. }
  145. return true;
  146. }
  147. }