Statistics.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace app\admin\controller;
  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\Cache;
  9. use think\Db;
  10. /**
  11. * 控制台
  12. *
  13. * @icon fa fa-dashboard
  14. * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
  15. */
  16. class Statistics extends Backend
  17. {
  18. /**
  19. * 统计
  20. */
  21. public function index()
  22. {
  23. ini_set("memory_limit","256M");
  24. $start = strtotime(date('Y-m-d')); //默认今日
  25. $end = $start + 86399;
  26. $mt_user_gold_log = Db::name('user_gold_log'); //金币日志表
  27. //总余额(金币)
  28. $total_gold = Db::name('user_wallet')->sum('gold');
  29. //总充值金额(人民币)各支付充值+客服代充(每笔资金备注充值来源,支付宝 微信 客服代充等)
  30. $total_recharge_money = Db::name('user_wallet')->sum('pay_money');
  31. //今天总充值金额
  32. $today_recharge_gold = $mt_user_gold_log->where(['log_type' => ['in', [1, 10]], 'change_value' => ['gt', 0], 'createtime' => ['egt', $start]])->sum('change_value');
  33. //昨天总充值金额
  34. $yesterday_recharge_gold = $mt_user_gold_log->where(['log_type' => ['in', [1, 10]], 'change_value' => ['gt', 0], 'createtime' => ['between', [$start - 86400, $start - 1]]])->sum('change_value');
  35. //今天总消耗金币
  36. $today_use_gold = $mt_user_gold_log->where(['change_value' => ['lt', 0], 'createtime' => ['egt', $start]])->sum('change_value');
  37. $today_use_gold = abs($today_use_gold);
  38. //昨日总消耗金币
  39. $yesterday_use_gold = $mt_user_gold_log->where(['change_value' => ['lt', 0], 'createtime' => ['between', [$start - 86400, $start - 1]]])->sum('change_value');
  40. $yesterday_use_gold = abs($yesterday_use_gold);
  41. //累计总消耗金币
  42. $total_use_gold = $mt_user_gold_log->where(['change_value' => ['lt', 0]])->sum('change_value');
  43. $total_use_gold = abs($total_use_gold);
  44. //今天总提现/今天总提现支出
  45. $today_withdraw = Db::name('take_cash')->where(['status' => 1, 'createtime' => ['egt', $start]])->sum('number');
  46. $today_withdraw_ava = Db::name('take_cash')->where(['status' => 1, 'createtime' => ['egt', $start]])->sum('money');
  47. //昨日总提现/昨日总提现支出
  48. $yesterday_withdraw = Db::name('take_cash')->where(['status' => 1, 'createtime' => ['between', [$start - 86400, $start - 1]]])->sum('number');
  49. $yesterday_withdraw_ava = Db::name('take_cash')->where(['status' => 1, 'createtime' => ['between', [$start - 86400, $start - 1]]])->sum('money');
  50. //累计总提现/累计总提现支出
  51. $total_withdraw = Db::name('take_cash')->where(['status' => 1])->sum('number');
  52. $total_withdraw_ava = Db::name('take_cash')->where(['status' => 1])->sum('money');
  53. //今天总兑换金币
  54. $today_exchange_gold = $mt_user_gold_log->where(['log_type' => 19, 'createtime' => ['egt', $start]])->sum('change_value');
  55. //昨日总兑换金币
  56. $yesterday_exchange_gold = $mt_user_gold_log->where(['log_type' => 19, 'createtime' => ['between', [$start - 86400, $start - 1]]])->sum('change_value');
  57. //累计总兑换金币
  58. $total_exchange_gold = $mt_user_gold_log->where(['log_type' => 19])->sum('change_value');
  59. //用户下载注册统计
  60. //男用户统计
  61. //总数
  62. $man_num = Db::name('user')->where(['gender' => 1])->count('id');
  63. //今日注册
  64. $today_man_num = Db::name('user')->where(['gender' => 1, 'createtime' => ['egt', $start]])->count('id');
  65. //昨日注册
  66. $yesterday_man_num = Db::name('user')->where(['gender' => 1, 'createtime' => ['between', [$start - 86400, $start - 1]]])->count('id');
  67. //注册来源
  68. //明确注册来源的数量
  69. $from_man_num = Db::name('user')->where(['gender' => 1, 'register_from' => ['neq', '']])->count('id');
  70. //ios数量
  71. $ios_from_man_num = Db::name('user')->where(['gender' => 1, 'register_from' => 'iOS'])->count('id');
  72. //安卓数量
  73. $android_from_man_num = $from_man_num - $ios_from_man_num;
  74. //女用户统计
  75. //总数
  76. $woman_num = Db::name('user')->where(['gender' => 0])->count('id');
  77. //今日注册
  78. $today_woman_num = Db::name('user')->where(['gender' => 0, 'createtime' => ['egt', $start]])->count('id');
  79. //昨日注册
  80. $yesterday_woman_num = Db::name('user')->where(['gender' => 0, 'createtime' => ['between', [$start - 86400, $start - 1]]])->count('id');
  81. //注册来源
  82. //明确注册来源的数量
  83. $from_woman_num = Db::name('user')->where(['gender' => 0, 'register_from' => ['neq', '']])->count('id');
  84. //ios数量
  85. $ios_from_woman_num = Db::name('user')->where(['gender' => 0, 'register_from' => 'iOS'])->count('id');
  86. //安卓数量
  87. $android_from_woman_num = $from_woman_num - $ios_from_woman_num;
  88. //邀请奖励 邀请充值奖励和邀请收益分开统计
  89. $today = day_now();
  90. $yesterday = day_yesterday();
  91. //邀请收益总奖励 //今日邀请收益 //昨日邀请收益 68
  92. $userMoneyLog = Db::name('user_money_log');
  93. $inviteWhere['log_type'] = 68;
  94. $inviteTotal = $userMoneyLog->where($inviteWhere)->sum('change_value');
  95. $inviteNow = $userMoneyLog->where($inviteWhere)->where(['createtime' => ['between', [$today[0],$today[1]]]])->sum('change_value');
  96. $inviteYesterday = $userMoneyLog->where($inviteWhere)->where(['createtime' => ['between', [$yesterday[0],$yesterday[1]]]])->sum('change_value');
  97. //邀请充值总奖励 今日邀请充值收益 昨日邀请充值收益 65
  98. $inviteRechargeWhere['log_type'] = 65;
  99. $inviteRechargeTotal = $userMoneyLog->where($inviteRechargeWhere)->sum('change_value');
  100. $inviteRechargeNow = $userMoneyLog->where($inviteRechargeWhere)->where(['createtime' => ['between', [$today[0],$today[1]]]])->sum('change_value');
  101. $inviteRechargeYesterday = $userMoneyLog->where($inviteRechargeWhere)->where(['createtime' => ['between', [$yesterday[0],$yesterday[1]]]])->sum('change_value');
  102. //送金币
  103. //男 任务总送金币 //今日送金币 //昨日送金币 20
  104. //充会员送金币 //今日充值送金币 //昨日充值送金币 14
  105. //总送金币 14,17,20,41,101
  106. $goldTaskWhere['g.log_type'] = ['in',[14,17,20,41,101]];
  107. $goldTaskField = 'g.id,u.gender,g.change_value,g.log_type,g.createtime';
  108. //$goldModelObj = $mt_user_gold_log->alias('g')->field($goldTaskField)->join('user u','u.id = g.user_id','LEFT')->where($goldTaskWhere)->select();
  109. $goldModelObj = Cache::remember('admin_statis_gold_log',function () use ($mt_user_gold_log,$goldTaskField,$goldTaskWhere){
  110. return $mt_user_gold_log->alias('g')->field($goldTaskField)->join('user u','u.id = g.user_id','LEFT')->where($goldTaskWhere)->select();
  111. },20);
  112. $manGoldTaskTotal = $manGoldTaskNow = $manGoldTaskYesterday = $manGoldTotal = $goldTotal = 0;
  113. $manGoldRechargeTotal = $manGoldRechargeNow = $manGoldRechargeYesterday = 0;
  114. $manGoldSignTotal = $manGoldSignNow = $manGoldSignYesterday = 0;
  115. $womanGoldTaskTotal = $womanGoldTaskNow = $womanGoldTaskYesterday = $womanGoldTotal = 0;
  116. $womanGoldRechargeTotal = $womanGoldRechargeNow = $womanGoldRechargeYesterday = 0;
  117. $womanGoldSignTotal = $womanGoldSignNow = $womanGoldSignYesterday = 0;
  118. if (!empty($goldModelObj)) {
  119. foreach ($goldModelObj as $goldKey =>$goldValue) {
  120. if ($goldValue['gender'] == 1) {//男用户
  121. if (in_array($goldValue['log_type'],[20])) {//任务送金币
  122. if ($goldValue['createtime'] >= $today[0] && $goldValue['createtime'] < $today[1]) {//今天
  123. $manGoldTaskNow = bcadd($manGoldTaskNow,$goldValue['change_value']);
  124. } elseif ($goldValue['createtime'] >= $yesterday[0] && $goldValue['createtime'] < $yesterday[1]) {//昨天
  125. $manGoldTaskYesterday = bcadd($manGoldTaskYesterday,$goldValue['change_value']);
  126. }
  127. $manGoldTaskTotal = bcadd($manGoldTaskTotal,$goldValue['change_value']);
  128. } elseif (in_array($goldValue['log_type'],[14,17,101])) {//充值送金币
  129. if ($goldValue['createtime'] >= $today[0] && $goldValue['createtime'] < $today[1]) {//今天
  130. $manGoldRechargeNow = bcadd($manGoldRechargeNow,$goldValue['change_value']);
  131. } elseif ($goldValue['createtime'] >= $yesterday[0] && $goldValue['createtime'] < $yesterday[1]) {//昨天
  132. $manGoldRechargeYesterday = bcadd($manGoldRechargeYesterday,$goldValue['change_value']);
  133. }
  134. $manGoldRechargeTotal = bcadd($manGoldRechargeTotal,$goldValue['change_value']);
  135. } elseif (in_array($goldValue['log_type'],[41])) {//签到送金币
  136. if ($goldValue['createtime'] >= $today[0] && $goldValue['createtime'] < $today[1]) {//今天
  137. $manGoldSignNow = bcadd($manGoldSignNow,$goldValue['change_value']);
  138. } elseif ($goldValue['createtime'] >= $yesterday[0] && $goldValue['createtime'] < $yesterday[1]) {//昨天
  139. $manGoldSignYesterday = bcadd($manGoldSignYesterday,$goldValue['change_value']);
  140. }
  141. $manGoldSignTotal = bcadd($manGoldSignTotal,$goldValue['change_value']);
  142. }
  143. $manGoldTotal = bcadd($manGoldTotal,$goldValue['change_value']);
  144. } elseif ($goldValue['gender'] == 0) {//女用户
  145. if (in_array($goldValue['log_type'],[20])) {//任务送金币
  146. if ($goldValue['createtime'] >= $today[0] && $goldValue['createtime'] < $today[1]) {//今天
  147. $womanGoldTaskNow = bcadd($womanGoldTaskNow, $goldValue['change_value']);
  148. } elseif ($goldValue['createtime'] >= $yesterday[0] && $goldValue['createtime'] < $yesterday[1]) {//昨天
  149. $womanGoldTaskYesterday = bcadd($womanGoldTaskYesterday, $goldValue['change_value']);
  150. }
  151. $womanGoldTaskTotal = bcadd($womanGoldTaskTotal, $goldValue['change_value']);
  152. } elseif (in_array($goldValue['log_type'],[14,17,101])) {//充值送金币
  153. if ($goldValue['createtime'] >= $today[0] && $goldValue['createtime'] < $today[1]) {//今天
  154. $womanGoldRechargeNow = bcadd($womanGoldRechargeNow,$goldValue['change_value']);
  155. } elseif ($goldValue['createtime'] >= $yesterday[0] && $goldValue['createtime'] < $yesterday[1]) {//昨天
  156. $womanGoldRechargeYesterday = bcadd($womanGoldRechargeYesterday,$goldValue['change_value']);
  157. }
  158. $womanGoldRechargeTotal = bcadd($womanGoldRechargeTotal,$goldValue['change_value']);
  159. } elseif (in_array($goldValue['log_type'],[41])) {//签到送金币
  160. if ($goldValue['createtime'] >= $today[0] && $goldValue['createtime'] < $today[1]) {//今天
  161. $womanGoldSignNow = bcadd($womanGoldSignNow,$goldValue['change_value']);
  162. } elseif ($goldValue['createtime'] >= $yesterday[0] && $goldValue['createtime'] < $yesterday[1]) {//昨天
  163. $womanGoldSignYesterday = bcadd($womanGoldSignYesterday,$goldValue['change_value']);
  164. }
  165. $womanGoldSignTotal = bcadd($womanGoldSignTotal,$goldValue['change_value']);
  166. }
  167. $womanGoldTotal = bcadd($womanGoldTotal,$goldValue['change_value']);
  168. }
  169. }
  170. }
  171. $goldTotal = bcadd($manGoldTotal,$womanGoldTotal);//总送金币
  172. //充值
  173. //微信 总充值 //今日充值 //昨日充值
  174. //$userGoldLog = Db::name('user_gold_log');
  175. $wechatTotal = $wechatNow = $wechatYesterday = 0.00;
  176. $payOrder = Db::name('pay_order');
  177. $payOrderWhere['status'] = 1;
  178. $payOrderWhere['type'] = 'recharge_gold';
  179. $payOrderWhere['payment_class'] = 'wechat';
  180. $wechatTotal = $payOrder->where($payOrderWhere)->sum('money');
  181. $wechatNow = $payOrder->where($payOrderWhere)->where(['createtime' => ['between', [$today[0],$today[1]]]])->sum('money');
  182. $wechatYesterday = $payOrder->where($payOrderWhere)->where(['createtime' => ['between', [$yesterday[0],$yesterday[1]]]])->sum('money');
  183. //支付宝 总充值 //今日充值 //昨日充值
  184. /*$goldLogWhere['log_type'] = 10;
  185. $goldLogWhere['remark'] = ['like','%支付宝%'];
  186. $alipayTotal = $userGoldLog->where($goldLogWhere)->sum('change_value');
  187. $alipayNow = $userGoldLog->where($goldLogWhere)->where(['createtime' => ['between', [$today[0],$today[1]]]])->sum('change_value');
  188. $alipayYesterday = $userGoldLog->where($goldLogWhere)->where(['createtime' => ['between', [$yesterday[0],$yesterday[1]]]])->sum('change_value');*/
  189. $payOrder = Db::name('pay_order');
  190. $payOrderWhere['status'] = 1;
  191. $payOrderWhere['type'] = 'recharge_gold';
  192. $payOrderWhere['payment_class'] = 'alipay';
  193. $alipayTotal = $payOrder->where($payOrderWhere)->sum('money');
  194. $alipayNow = $payOrder->where($payOrderWhere)->where(['createtime' => ['between', [$today[0],$today[1]]]])->sum('money');
  195. $alipayYesterday = $payOrder->where($payOrderWhere)->where(['createtime' => ['between', [$yesterday[0],$yesterday[1]]]])->sum('money');
  196. $statistics = [
  197. 'invite_total' => $inviteTotal,//邀请奖励余额
  198. 'invite_now' => $inviteNow,
  199. 'invite_yesterday' => $inviteYesterday,
  200. 'invite_recharge_total' => $inviteRechargeTotal,//邀请充值奖励余额
  201. 'invite_recharge_now' => $inviteRechargeNow,
  202. 'invite_recharge_yesterday' => $inviteRechargeYesterday,
  203. 'man_gold_task_total' => $manGoldTaskTotal,//男任务送金币
  204. 'man_gold_task_now' => $manGoldTaskNow,
  205. 'man_gold_task_yesterday' => $manGoldTaskYesterday,
  206. 'man_gold_recharge_total' => $manGoldRechargeTotal,//男充值送金币
  207. 'man_gold_recharge_now' => $manGoldRechargeNow,
  208. 'man_gold_recharge_yesterday' => $manGoldRechargeYesterday,
  209. 'man_gold_sign_total' => $manGoldSignTotal,//男签到送金币
  210. 'man_gold_sign_now' => $manGoldSignNow,
  211. 'man_gold_sign_yesterday' => $manGoldSignYesterday,
  212. 'man_gold_total' => $manGoldTotal,
  213. 'woman_gold_task_total' => $womanGoldTaskTotal,//女任务送金币
  214. 'woman_gold_task_now' => $womanGoldTaskNow,
  215. 'woman_gold_task_yesterday' => $womanGoldTaskYesterday,
  216. 'woman_gold_recharge_total' => $womanGoldRechargeTotal,//女充值送金币
  217. 'woman_gold_recharge_now' => $womanGoldRechargeNow,
  218. 'woman_gold_recharge_yesterday' => $womanGoldRechargeYesterday,
  219. 'woman_gold_sign_total' => $womanGoldSignTotal,//女签到送金币
  220. 'woman_gold_sign_now' => $womanGoldSignNow,
  221. 'woman_gold_sign_yesterday' => $womanGoldSignYesterday,
  222. 'woman_gold_total' => $womanGoldTotal,
  223. 'gold_total' => $goldTotal,//总送金币
  224. 'wechat_total' => $wechatTotal,//微信充值
  225. 'wechat_now' => $wechatNow,
  226. 'wechat_yesterday' => $wechatYesterday,
  227. 'alipay_total' => $alipayTotal,//支付宝充值
  228. 'alipay_now' => $alipayNow,
  229. 'alipay_yesterday' => $alipayYesterday,
  230. ];
  231. $this->assign('total_gold', $total_gold);
  232. $this->assign('total_recharge_money', $total_recharge_money);
  233. $this->assign('today_recharge_gold', $today_recharge_gold);
  234. $this->assign('yesterday_recharge_gold', $yesterday_recharge_gold);
  235. $this->assign('today_use_gold', $today_use_gold);
  236. $this->assign('yesterday_use_gold', $yesterday_use_gold);
  237. $this->assign('total_use_gold', $total_use_gold);
  238. $this->assign('today_withdraw', $today_withdraw);
  239. $this->assign('today_withdraw_ava', $today_withdraw_ava);
  240. $this->assign('yesterday_withdraw', $yesterday_withdraw);
  241. $this->assign('yesterday_withdraw_ava', $yesterday_withdraw_ava);
  242. $this->assign('total_withdraw', $total_withdraw);
  243. $this->assign('total_withdraw_ava', $total_withdraw_ava);
  244. $this->assign('today_exchange_gold', $today_exchange_gold);
  245. $this->assign('yesterday_exchange_gold', $yesterday_exchange_gold);
  246. $this->assign('total_exchange_gold', $total_exchange_gold);
  247. $this->assign('man_num', $man_num);
  248. $this->assign('today_man_num', $today_man_num);
  249. $this->assign('yesterday_man_num', $yesterday_man_num);
  250. $this->assign('android_from_man_num', $android_from_man_num);
  251. $this->assign('ios_from_man_num', $ios_from_man_num);
  252. $this->assign('woman_num', $woman_num);
  253. $this->assign('today_woman_num', $today_woman_num);
  254. $this->assign('yesterday_woman_num', $yesterday_woman_num);
  255. $this->assign('android_from_woman_num', $android_from_woman_num);
  256. $this->assign('ios_from_woman_num', $ios_from_woman_num);
  257. $this->assign($statistics);
  258. return $this->view->fetch();
  259. }
  260. }