UserStatisticsAction.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. class UserStatisticsAction extends CommonAction{
  3. //私信收礼统计
  4. public function private_statistics()
  5. {
  6. //统计所有送礼物表
  7. $prop_tables = array();
  8. $prop_tables[] = DB_PREFIX.'video_prop';
  9. $time = time()-28800;
  10. $year = intval(to_date($time,'Y')); //年
  11. $month = intval(to_date($time,'m')); //月
  12. $table = DB_PREFIX.'video_prop_'.to_date($time,'Ym');
  13. $res = $GLOBALS['db']->getRow("SHOW TABLES LIKE'$table'");
  14. /*admin_ajax_return($table);*/
  15. while ($res)
  16. {
  17. $prop_tables[]=$table; //获取该数据库下所有的送礼物表
  18. $month--;
  19. if ($month>0)
  20. {
  21. $month>=10?$prev_m = strval($year).$month:$prev_m = strval($year).'0'.$month;
  22. $table = DB_PREFIX . 'video_prop_' . $prev_m;
  23. $res = $GLOBALS['db']->getRow("SHOW TABLES LIKE'$table'");
  24. }
  25. else if ($month<=0)
  26. {
  27. $month = '12';
  28. $year --;
  29. $prev_y = $year.$month;
  30. $table = DB_PREFIX . 'video_prop_' . $prev_y;
  31. $res = $GLOBALS['db']->getRow("SHOW TABLES LIKE'$table'");
  32. }
  33. }
  34. /*admin_ajax_return($prop_tables);*/
  35. /* SELECT id,nick_name,sum(nums) as nums,sum(diamonds) as diamonds FROM (SELECT ... fanwe_video_prop_201707 ...
  36. union SELECT ... fanwe_video_prop_201706 ... ) as res group by id;;*/
  37. $map = $this->com_search(); //获取时间搜索状态
  38. $parameter = '';
  39. $sql_w = '';
  40. //查看是否有进行时间搜索
  41. if($map['start_time'] != '' && $map['end_time'] != ''){
  42. $parameter.=" t1.create_time between '". $map['start_time'] . "' and '". $map['end_time'] ."'&";
  43. $sql_w .=" t1.create_time between '". $map['start_time']. "' and '". $map['end_time'] ."' and ";
  44. }
  45. if(strim($_REQUEST['id'])!=''){
  46. $sql_w .= " to_user_id like '%".strim($_REQUEST['id'])."%' and ";
  47. }
  48. if(strim($_REQUEST['nick_name'])!=''){
  49. $sql_w .= " nick_name like '%".strim($_REQUEST['nick_name'])."%' and ";
  50. }
  51. $model = D ();
  52. $sql_str = 'SELECT id,nick_name,sum(nums) as nums,sum(diamonds) as diamonds FROM (';
  53. for($i=0;$i<count($prop_tables);$i++) //连接所有礼物表进行查询
  54. {
  55. $sql_str .= "SELECT to_user_id as id,nick_name,count(distinct from_user_id) as nums,sum(total_ticket) as diamonds "."FROM ".$prop_tables[$i]." t1,".DB_PREFIX."user t2 where t1.to_user_id=t2.id and t1.is_private = 1 and t1.is_red_envelope = 0";
  56. $sql_str .= " and ".$sql_w." 1=1 group by to_user_id ";
  57. if($i<count($prop_tables)-1)
  58. {
  59. $sql_str .= " union all ";
  60. }
  61. }
  62. $sql_str .= " ) as res group by id " ;
  63. /*admin_ajax_return($sql_str);*/
  64. $voList = $this->_Sql_list($model,$sql_str,"&".$parameter,'id',true);
  65. foreach ($voList as $key => $value) {
  66. $voList[$key]['nick_name'] = emoji_decode($value['nick_name']);
  67. }
  68. $this->assign ( 'list', $voList );
  69. $this->display ();
  70. return;
  71. }
  72. //私信收礼明细
  73. public function private_detail()
  74. {
  75. //统计所有送礼物表
  76. $prop_tables = array();
  77. $prop_tables[] = DB_PREFIX.'video_prop';
  78. $time = time()-28800;
  79. $year = intval(to_date($time,'Y')); //年
  80. $month = intval(to_date($time,'m')); //月
  81. $table = DB_PREFIX.'video_prop_'.to_date($time,'Ym');
  82. $res = $GLOBALS['db']->getRow("SHOW TABLES LIKE'$table'");
  83. /*admin_ajax_return($table);*/
  84. while ($res)
  85. {
  86. $prop_tables[]=$table; //获取该数据库下所有的送礼物表
  87. $month--;
  88. if ($month>0)
  89. {
  90. $month>=10?$prev_m = strval($year).$month:$prev_m = strval($year).'0'.$month;
  91. $table = DB_PREFIX . 'video_prop_' . $prev_m;
  92. $res = $GLOBALS['db']->getRow("SHOW TABLES LIKE'$table'");
  93. }
  94. else if ($month<=0)
  95. {
  96. $month = '12';
  97. $year --;
  98. $prev_y = $year.$month;
  99. $table = DB_PREFIX . 'video_prop_' . $prev_y;
  100. $res = $GLOBALS['db']->getRow("SHOW TABLES LIKE'$table'");
  101. }
  102. }
  103. /*SELECT from_user_id as id,nick_name,total_diamonds as diamonds,t1.create_time as time from fanwe_video_prop t1,fanwe_user t2 where t1.from_user_id=t2.id and t1.is_private = 1 and to_user_id = 100993;*/
  104. $map = $this->com_search(); //获取时间搜索状态
  105. $id = strim($_REQUEST['id']); //主播ID
  106. $parameter = '';
  107. $sql_w = '';
  108. //查看是否有进行时间搜索
  109. if($map['start_time'] != '' && $map['end_time'] != ''){
  110. $parameter.=" t1.create_time between '". $map['start_time'] . "' and '". $map['end_time'] ."'&";
  111. $sql_w .=" t1.create_time between '". $map['start_time']. "' and '". $map['end_time'] ."' and ";
  112. }
  113. //查看是否有进行送礼者ID或昵称搜索
  114. if(strim($_REQUEST['sid'])!=''){
  115. $sql_w .= " t2.id like '%".strim($_REQUEST['sid'])."%' and ";
  116. }
  117. if(strim($_REQUEST['nick_name'])!=''){
  118. $sql_w .= " nick_name like '%".strim($_REQUEST['nick_name'])."%' and ";
  119. }
  120. $model = D ();
  121. $sql_str = '';
  122. for ($i=0;$i<count($prop_tables);$i++) //连接所有礼物表进行查询
  123. {
  124. $sql_str .= "SELECT from_user_id as sid,nick_name,total_ticket as diamonds,t1.create_time as time "."FROM ".$prop_tables[$i]." t1,".DB_PREFIX."user t2 where t1.from_user_id = t2.id and t1.is_private = 1 and t1.is_red_envelope = 0 and to_user_id = ".$id;
  125. $sql_str .= " and ".$sql_w." 1=1 ";
  126. if($i<count($prop_tables)-1)
  127. {
  128. $sql_str .= " union all ";
  129. }
  130. }
  131. /*admin_ajax_return($sql_str);*/
  132. $voList = $this->_Sql_list($model, $sql_str, "&".$parameter,'time');
  133. foreach ($voList as $key => $value) {
  134. $voList[$key]['nick_name'] = emoji_decode($value['nick_name']);
  135. $voList[$key]['time'] = to_date($value['time']);
  136. }
  137. $this->assign ( 'list', $voList );
  138. //获取总获益
  139. $sql_diamonds_count = "SELECT sum(diamonds) as count_diamonds FROM ( ".$sql_str." ) as res";
  140. $count_diamonds = $GLOBALS['db']->getOne($sql_diamonds_count);
  141. //获取总人数
  142. $sql_nums_count = "SELECT res.sid FROM ( ".$sql_str." ) as res group by res.sid";
  143. $count = $GLOBALS['db']->getAll($sql_nums_count);
  144. $count = count($count);
  145. $this->assign("count",$count);
  146. $this->assign("count_diamonds",$count_diamonds);
  147. $this->display ();
  148. return;
  149. }
  150. //导出电子表
  151. public function export_csv($page = 1)
  152. {
  153. $pagesize = 10;
  154. set_time_limit(0);
  155. $limit = (($page - 1)*intval($pagesize)).",".(intval($pagesize));
  156. //统计所有送礼物表
  157. $prop_tables = array();
  158. $prop_tables[] = DB_PREFIX.'video_prop';
  159. $time = time()-28800;
  160. $year = intval(to_date($time,'Y')); //年
  161. $month = intval(to_date($time,'m')); //月
  162. $table = DB_PREFIX.'video_prop_'.to_date($time,'Ym');
  163. $res = $GLOBALS['db']->getRow("SHOW TABLES LIKE'$table'");
  164. /*admin_ajax_return($table);*/
  165. while ($res)
  166. {
  167. $prop_tables[]=$table; //获取该数据库下所有的送礼物表
  168. $month--;
  169. if ($month>0)
  170. {
  171. $month>=10?$prev_m = strval($year).$month:$prev_m = strval($year).'0'.$month;
  172. $table = DB_PREFIX . 'video_prop_' . $prev_m;
  173. $res = $GLOBALS['db']->getRow("SHOW TABLES LIKE'$table'");
  174. }
  175. else if ($month<=0)
  176. {
  177. $month = '12';
  178. $year --;
  179. $prev_y = $year.$month;
  180. $table = DB_PREFIX . 'video_prop_' . $prev_y;
  181. $res = $GLOBALS['db']->getRow("SHOW TABLES LIKE'$table'");
  182. }
  183. }
  184. $map = $this->com_search(); //获取时间搜索状态
  185. $sql_w = '';
  186. //查看是否有进行时间搜索
  187. if($map['start_time'] != '' && $map['end_time'] != ''){
  188. $parameter.=" t1.create_time between '". $map['start_time'] . "' and '". $map['end_time'] ."'&";
  189. $sql_w .=" t1.create_time between '". $map['start_time']. "' and '". $map['end_time'] ."' and ";
  190. }
  191. if(strim($_REQUEST['id'])!=''){
  192. $sql_w .= " to_user_id like '%".strim($_REQUEST['id'])."%' and ";
  193. }
  194. if(strim($_REQUEST['nick_name'])!=''){
  195. $sql_w .= " nick_name like '%".strim($_REQUEST['nick_name'])."%' and ";
  196. }
  197. $sql_str = 'SELECT id,nick_name,sum(nums) as nums,sum(diamonds) as diamonds FROM (';
  198. for($i=0;$i<count($prop_tables);$i++) //连接所有礼物表进行查询
  199. {
  200. $sql_str .= "SELECT to_user_id as id,nick_name,count(distinct from_user_id) as nums,sum(total_ticket) as diamonds "."FROM ".$prop_tables[$i]." t1,".DB_PREFIX."user t2 where t1.to_user_id=t2.id and t1.is_private = 1 and t1.is_red_envelope = 0";
  201. $sql_str .= " and ".$sql_w." 1=1 group by to_user_id ";
  202. if($i<count($prop_tables)-1)
  203. {
  204. $sql_str .= " union all ";
  205. }
  206. }
  207. $sql_str .= " ) as res group by id " ;
  208. $time ='1970-01-01 16:00:00';
  209. $sql =$sql_str." limit ";
  210. $sql .= $limit;
  211. /*admin_ajax_return($sql);*/
  212. $list=$GLOBALS['db']->getAll($sql);
  213. /*admin_ajax_return($list);*/
  214. if($list)
  215. {
  216. register_shutdown_function(array(&$this, 'export_csv'), $page+1);
  217. $m_config = load_auto_cache('m_config');
  218. $ticket_name = $m_config['ticket_name']!=''?$m_config['ticket_name']:'印票';
  219. $refund_value = array( 'id'=>'""','nick_name'=>'""','diamonds'=>'""','nums'=>'""');
  220. if($page == 1)
  221. {
  222. $content = iconv("utf-8","gbk","主播ID,主播昵称,收礼总获益(钻石),私信送礼人数");
  223. $content = $content . "\n";
  224. }
  225. foreach($list as $k=>$v)
  226. {
  227. $refund_value['id'] = '"' . iconv('utf-8','gbk',$list[$k]['id']) . '"';
  228. $refund_value['nick_name'] = '"' . iconv('utf-8','gbk',$list[$k]['nick_name']) . '"';
  229. $refund_value['nums'] = '"' . iconv('utf-8','gbk',$list[$k]['nums']) . '"';
  230. $refund_value['diamonds'] = '"' . iconv('utf-8','gbk',$list[$k]['diamonds']) . '"';
  231. $content .= implode(",", $refund_value) . "\n";
  232. }
  233. header("Content-Disposition: attachment; filename=private_statistics.csv");
  234. echo $content ;
  235. }
  236. else
  237. {
  238. if($page==1)
  239. $this->error(L("NO_RESULT"));
  240. }
  241. }
  242. }