Portal.php 2.6 KB

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