Dashboard.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace app\admin\controller\agent;
  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. //推广链接 推广二维码
  23. $extend_link = config('site.extend_link');
  24. $extend_qrcode = config('site.domain_cdnurl') . config('site.extend_qrcode');
  25. $user_info = Db::name('user')->where(['id' => $this->auth->user_id])->find();
  26. if (!$user_info) {
  27. $user_info['id'] = '';
  28. $user_info['introcode'] = '';
  29. } else {
  30. $extend_link = $extend_link . '?code=' . $user_info['introcode'];
  31. }
  32. $this->assign('user', $user_info);
  33. $this->assign('extend_link', $extend_link);
  34. $this->assign('extend_qrcode', $extend_qrcode);
  35. return $this->view->fetch();
  36. }
  37. //统计
  38. public function statistics() {
  39. $user_id = input('ids', 0, 'intval');
  40. if (!$user_id) {
  41. $user_id = $this->auth->user_id;
  42. }
  43. $user_info = Db::name('user')->where(['id' => $user_id])->find();
  44. if ($user_info && $user_info['is_agent'] == 1) { //高级代理
  45. $intro_recharge_rebate_rate = $user_info['h_intro_recharge_rebate_rate']; //高级邀请人充值返利比率
  46. $intro_income_rebate_rate = $user_info['h_intro_income_rebate_rate']; //高级邀请人收益返利比率
  47. } else {
  48. $intro_recharge_rebate_rate = config('site.intro_recharge_rebate_rate');
  49. $intro_income_rebate_rate = config('site.intro_income_rebate_rate');
  50. }
  51. if ($user_info) {
  52. $start = strtotime(date('Y-m-d')); //默认今日
  53. $end = $start + 86399;
  54. $mt_user_money_log = Db::name('user_money_log');
  55. //下级id集合
  56. $lower_user_ids = Db::name('user')->where(['intro_uid' => $user_id])->column('id');
  57. $lower_user_ids = $lower_user_ids ? : [];
  58. //收益type
  59. $income_type = [21, 22, 23, 54, 60, 82];
  60. /**
  61. * 今日概览
  62. */
  63. //新增营业额
  64. $lower_new_income = $mt_user_money_log->where(['log_type' => ['in', $income_type], 'user_id' => ['in', $lower_user_ids], 'createtime' => ['egt', $start]])->sum('change_value');
  65. //家族新增收益
  66. $lower_new_my_income = $mt_user_money_log->where(['log_type' => 68, 'user_id' => $user_id, 'createtime' => ['egt', $start]])->sum('change_value');
  67. //主播总人数
  68. $lower_total_count = Db::name('user')->where(['intro_uid' => $user_id, 'gender' => 0])->count('id');
  69. //邀请充值奖励
  70. $lower_total_invite_profit = $mt_user_money_log->where(['log_type' => 65, 'user_id' => $user_id, 'createtime' => ['egt', $start]])->sum('change_value');
  71. /**
  72. * 总览
  73. */
  74. //营业额
  75. //昨日收入
  76. $last_day_map = [
  77. 'log_type' => ['in', $income_type],
  78. 'user_id' => ['in', $lower_user_ids],
  79. 'createtime' => ['between', [$start - 86400, $start - 1]],
  80. ];
  81. $lower_last_day_profit = $mt_user_money_log->where($last_day_map)->sum('change_value');
  82. //获取本周开始时间
  83. $day = 1; // 一周的第一天
  84. $wday = date('w'); // 星期几的数字表示 0(周日)到 6(周六)
  85. $wday_start = strtotime(date('Y-m-d', strtotime('-' . ($wday ? $wday - $day : 6) . ' day'))); // 本周开始日期
  86. $week_map = [
  87. 'log_type' => ['in', $income_type],
  88. 'user_id' => ['in', $lower_user_ids],
  89. 'createtime' => ['between', [$wday_start - 86400 * 7, $wday_start]]
  90. ];
  91. //上周收入
  92. $lower_last_week_profit = $mt_user_money_log->where($week_map)->sum('change_value');
  93. //近30天收入
  94. $last_day_map['createtime'] = ['egt', $start - 86400 * 30];
  95. $lower_thirty_day_profit = $mt_user_money_log->where($last_day_map)->sum('change_value');
  96. //主播总收入
  97. unset($week_map['createtime']);
  98. $lower_total_point_profit = $mt_user_money_log->where($week_map)->sum('change_value');
  99. //收益
  100. //昨日收益
  101. $my_last_day_map = [
  102. 'log_type' => 68,
  103. 'user_id' => $user_id,
  104. 'createtime' => ['between', [$start - 86400, $start - 1]],
  105. ];
  106. $my_yesterday_profit = $mt_user_money_log->where($my_last_day_map)->sum('change_value');
  107. //上周收益
  108. $my_last_day_map['createtime'] = ['between', [$wday_start - 86400 * 7, $wday_start]];
  109. $my_last_week_profit = $mt_user_money_log->where($my_last_day_map)->sum('change_value');
  110. //近30天收益
  111. $my_last_day_map['createtime'] = ['egt', $start - 86400 * 30];
  112. $my_thirty_day_profit = $mt_user_money_log->where($my_last_day_map)->sum('change_value');
  113. //家族总收益
  114. unset($my_last_day_map['createtime']);
  115. $my_total_point_profit = $mt_user_money_log->where($my_last_day_map)->sum('change_value');
  116. //昨日邀请充值收益
  117. $my_intive_map = [
  118. 'log_type' => 65,
  119. 'user_id' => $user_id,
  120. 'createtime' => ['between', [$start - 86400, $start - 1]],
  121. ];
  122. $my_yesterday_invite_profit = $mt_user_money_log->where($my_intive_map)->sum('change_value');
  123. //上周邀请充值收益
  124. $my_intive_map['createtime'] = ['between', [$wday_start - 86400 * 7, $wday_start]];
  125. $my_last_week_invite_profit = $mt_user_money_log->where($my_intive_map)->sum('change_value');
  126. //近30天充值总收益
  127. $my_intive_map['createtime'] = ['egt', $start - 86400 * 30];
  128. $my_thirty_day_invite_profit = $mt_user_money_log->where($my_intive_map)->sum('change_value');
  129. //邀请充值总收益
  130. unset($my_intive_map['createtime']);
  131. $my_total_invite_profit = $mt_user_money_log->where($my_intive_map)->sum('change_value');
  132. } else {
  133. $lower_new_income = 0;
  134. $lower_new_my_income = 0;
  135. $lower_total_count = 0;
  136. $lower_total_invite_profit = 0;
  137. $lower_last_day_profit = 0;
  138. $lower_last_week_profit = 0;
  139. $lower_thirty_day_profit = 0;
  140. $lower_total_point_profit = 0;
  141. $my_yesterday_profit = 0;
  142. $my_last_week_profit = 0;
  143. $my_thirty_day_profit = 0;
  144. $my_total_point_profit = 0;
  145. $my_yesterday_invite_profit = 0;
  146. $my_last_week_invite_profit = 0;
  147. $my_thirty_day_invite_profit = 0;
  148. $my_total_invite_profit = 0;
  149. }
  150. $this->assign('lower_new_income', $lower_new_income);
  151. $this->assign('lower_new_my_income', $lower_new_my_income);
  152. $this->assign('lower_total_count', $lower_total_count);
  153. $this->assign('lower_total_invite_profit', $lower_total_invite_profit);
  154. $this->assign('lower_last_day_profit', $lower_last_day_profit);
  155. $this->assign('lower_last_week_profit', $lower_last_week_profit);
  156. $this->assign('lower_thirty_day_profit', $lower_thirty_day_profit);
  157. $this->assign('lower_total_point_profit', $lower_total_point_profit);
  158. $this->assign('my_yesterday_profit', $my_yesterday_profit);
  159. $this->assign('my_last_week_profit', $my_last_week_profit);
  160. $this->assign('my_thirty_day_profit', $my_thirty_day_profit);
  161. $this->assign('my_total_point_profit', $my_total_point_profit);
  162. $this->assign('my_yesterday_invite_profit', $my_yesterday_invite_profit);
  163. $this->assign('my_last_week_invite_profit', $my_last_week_invite_profit);
  164. $this->assign('my_thirty_day_invite_profit', $my_thirty_day_invite_profit);
  165. $this->assign('my_total_invite_profit', $my_total_invite_profit);
  166. $this->assign('intro_recharge_rebate_rate', $intro_recharge_rebate_rate);
  167. $this->assign('intro_income_rebate_rate', $intro_income_rebate_rate);
  168. return $this->view->fetch();
  169. }
  170. }