Portal.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace app\data\controller\total;
  3. use think\admin\Controller;
  4. /**
  5. * 商城数据报表
  6. * Class Portal
  7. * @package app\data\controller\total
  8. */
  9. class Portal extends Controller
  10. {
  11. /**
  12. * 商城数据报表
  13. * @auth true
  14. * @menu true
  15. */
  16. public function index()
  17. {
  18. $this->usersTotal = $this->app->db->name('DataUser')->cache(true, 60)->count();
  19. $this->goodsTotal = $this->app->db->name('ShopGoods')->cache(true, 60)->where(['deleted' => 0])->count();
  20. $this->orderTotal = $this->app->db->name('ShopOrder')->cache(true, 60)->whereRaw('status >= 4')->count();
  21. $this->amountTotal = $this->app->db->name('ShopOrder')->cache(true, 60)->whereRaw('status >= 4')->sum('amount_total');
  22. // 近七天用户及交易趋势
  23. $this->days = $this->app->cache->get('portals', []);
  24. if (empty($this->days)) {
  25. for ($i = 15; $i >= 0; $i--) {
  26. $date = date('Y-m-d', strtotime("-{$i}days"));
  27. $this->days[] = [
  28. '当天日期' => date('m-d', strtotime("-{$i}days")),
  29. '增加用户' => $this->app->db->name('DataUser')->cache(true, 60)->whereLike('create_at', "{$date}%")->count(),
  30. '订单数量' => $this->app->db->name('ShopOrder')->cache(true, 60)->whereLike('create_at', "{$date}%")->whereRaw('status>=4')->count(),
  31. '订单金额' => $this->app->db->name('ShopOrder')->cache(true, 60)->whereLike('create_at', "{$date}%")->whereRaw('status>=4')->sum('amount_total'),
  32. '返利金额' => $this->app->db->name('DataUserRebate')->cache(true, 60)->whereLike('create_at', "{$date}%")->sum('amount'),
  33. '充值余额' => $this->app->db->name('DataUserBalance')->cache(true, 60)->whereLike('create_at', "{$date}%")->whereRaw('amount>0 and deleted=0')->sum('amount'),
  34. '消费余额' => $this->app->db->name('DataUserBalance')->cache(true, 60)->whereLike('create_at', "{$date}%")->whereRaw('amount<0 and deleted=0')->sum('amount'),
  35. ];
  36. }
  37. $this->app->cache->set('portals', $this->days, 60);
  38. }
  39. // 会员级别分布统计
  40. $levels = $this->app->db->name('BaseUserUpgrade')->where(['status' => 1])->order('number asc')->column('number code,name,0 count', 'number');
  41. foreach ($this->app->db->name('DataUser')->field('count(1) count,vip_code level')->group('vip_code')->cursor() as $vo) {
  42. $levels[$vo['level']]['count'] = isset($levels[$vo['level']]) ? $vo['count'] : 0;
  43. }
  44. $this->levels = array_values($levels);
  45. $this->fetch();
  46. }
  47. }