Dashboard.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\admin\controller\unishop;
  3. use app\admin\model\unishop\Goods;
  4. use app\admin\model\unishop\Income;
  5. use app\common\controller\Backend;
  6. use fast\Date;
  7. /**
  8. * 控制台
  9. *
  10. * @icon fa fa-dashboard
  11. * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
  12. */
  13. class Dashboard extends Backend
  14. {
  15. public function _initialize()
  16. {
  17. parent::_initialize();
  18. }
  19. /**
  20. * 查看
  21. */
  22. public function index()
  23. {
  24. try {
  25. \think\Db::execute("SET @@sql_mode='';");
  26. } catch (\Exception $e) {
  27. }
  28. $column = [];
  29. $starttime = Date::unixtime('day', -30);
  30. $endtime = Date::unixtime('day', 0, 'end');
  31. $joinlist = Db("unishop_order")
  32. ->where('status = 1 and have_paid != 0')
  33. ->where('createtime', 'between time', [$starttime, $endtime])
  34. ->field('createtime, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(createtime), "%m-%d") AS create_time')
  35. ->group('createtime')
  36. ->select();
  37. for ($time = $starttime; $time <= $endtime;) {
  38. $column[] = date("m-d", $time);
  39. $time += 86400;
  40. }
  41. $orderlist = array_fill_keys($column, 0);
  42. foreach ($joinlist as $k => $v) {
  43. $orderlist[$v['create_time']] = $v['nums'];
  44. }
  45. $this->view->assign([
  46. 'totalCategory' => \app\admin\model\unishop\Category::count(),
  47. 'goodsNums' => \app\admin\model\unishop\Product::count(),
  48. 'orderNums' => (new \app\admin\model\unishop\Order)->where('status = 1 and have_paid != 0')->count(),
  49. 'totalCoupon' => \app\admin\model\unishop\Coupon::count(),
  50. 'totalAds' => \app\admin\model\unishop\Ads::count(),
  51. 'orderAmount' => (new Income)->sum('order_amount'),
  52. ]);
  53. return $this->view->fetch();
  54. }
  55. /**
  56. * 收入明细
  57. */
  58. public function income()
  59. {
  60. //设置过滤方法
  61. $this->request->filter(['strip_tags']);
  62. try {
  63. \think\Db::execute("SET @@sql_mode='';");
  64. } catch (\Exception $e) {
  65. }
  66. $model = new Income();
  67. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  68. $total = $model
  69. ->where($where)
  70. ->field('DATE_FORMAT(FROM_UNIXTIME(date_time), "%Y-%m-%d") AS join_date')
  71. ->group('join_date')
  72. ->count();
  73. $list = $model
  74. ->where($where)
  75. ->field('date_time,SUM(order_amount) as order_amount,COUNT(*) as order_nums,DATE_FORMAT(FROM_UNIXTIME(date_time), "%Y-%m-%d") AS join_date')
  76. ->group('join_date')
  77. ->order($sort, $order)
  78. ->limit($offset, $limit)
  79. ->select();
  80. $result = array(
  81. "total" => $total,
  82. "rows" => $list,
  83. 'join_date' => array_column($list, 'join_date'),
  84. 'order_amount' => array_column($list, 'order_amount'),
  85. 'order_nums' => array_column($list, 'order_nums'),
  86. );
  87. return json($result);
  88. }
  89. /**
  90. * 商品销量
  91. */
  92. public function goods()
  93. {
  94. //设置过滤方法
  95. $this->request->filter(['strip_tags']);
  96. try {
  97. \think\Db::execute("SET @@sql_mode='';");
  98. } catch (\Exception $e) {
  99. }
  100. $model = new Goods();
  101. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  102. $total = $model
  103. ->where($where)
  104. ->field('DATE_FORMAT(FROM_UNIXTIME(date_time), "%Y-%m-%d") AS join_date')
  105. ->group('join_date')
  106. ->count();
  107. $list = $model
  108. ->where($where)
  109. ->field('date_time,SUM(number) as sell_total,product_id,product_name,DATE_FORMAT(FROM_UNIXTIME(date_time), "%Y-%m-%d") AS join_date')
  110. ->group('join_date,product_id')
  111. ->order($sort, $order)
  112. ->order('sell_total', 'desc')
  113. ->limit($offset, $limit)
  114. ->select();
  115. $result = array(
  116. "total" => $total,
  117. "rows" => $list
  118. );
  119. return json($result);
  120. }
  121. }