PropStatisticsAction.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Fanwe 方维p2p借贷系统
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: 云淡风轻(1956838968@qq.com)
  8. // +----------------------------------------------------------------------
  9. class PropStatisticsAction extends CommonAction{
  10. //道具消耗统计
  11. public function consume_statistics()
  12. {
  13. //统计所有送礼物表
  14. $prop_tables = array(); //存放送礼物表的数组
  15. $prop_tables[] = DB_PREFIX.'video_prop';
  16. $time = time()-28800;
  17. $year = intval(to_date($time,'Y')); //年
  18. $month = intval(to_date($time,'m')); //月
  19. $table = DB_PREFIX.'video_prop_'.to_date($time,'Ym');
  20. $res = $GLOBALS['db']->getRow("SHOW TABLES LIKE'$table'");
  21. /*admin_ajax_return($table);*/
  22. while ($res)
  23. {
  24. $prop_tables[]=$table; //获取该数据库下所有的送礼物表
  25. $month--;
  26. if ($month>0)
  27. {
  28. $month>=10?$prev_m = strval($year).$month:$prev_m = strval($year).'0'.$month;
  29. $table = DB_PREFIX . 'video_prop_' . $prev_m;
  30. $res = $GLOBALS['db']->getRow("SHOW TABLES LIKE'$table'");
  31. }
  32. else if ($month<=0)
  33. {
  34. $month = '12';
  35. $year --;
  36. $prev_y = $year.$month;
  37. $table = DB_PREFIX . 'video_prop_' . $prev_y;
  38. $res = $GLOBALS['db']->getRow("SHOW TABLES LIKE'$table'");
  39. }
  40. }
  41. //列表过滤器,生成时间搜索查询Map对象
  42. $map = $this->com_search();
  43. $parameter = '';
  44. $sql_w = '';
  45. //查看是否有进行时间搜索
  46. if($map['start_time'] != '' && $map['end_time'] != ''){
  47. $parameter.=" create_time between '". $map['start_time'] . "' and '". $map['end_time'] ."'&";
  48. $sql_w .=" create_time between '". $map['start_time']. "' and '". $map['end_time'] ."' and ";
  49. }
  50. //查看是否有进行道具名搜索
  51. if(strim($_REQUEST['name'])!=''){
  52. $sql_w .= " prop_name like '%".strim($_REQUEST['name'])."%' and ";
  53. }
  54. $model = D ();
  55. //获取道具统计表的sql
  56. $sql_str = "SELECT res.id,res.name,res.price,sum(res.sale) as sale,sum(res.total) as total FROM (";
  57. //获取总量与总金额
  58. $sql_count = "SELECT sum(res.count) as count,sum(res.count_price) as count_price FROM (";
  59. for($i=0;$i<count($prop_tables);$i++) //连接所有礼物表进行查询
  60. {
  61. $sql_str .= "SELECT p.id as id,p.name as name,p.diamonds as price,count(v.prop_id) as sale,sum(v.total_diamonds) as total " .
  62. " FROM ".DB_PREFIX."prop p , ".$prop_tables[$i]." v WHERE p.id = v.prop_id ";
  63. $sql_str .= " and ".$sql_w." 1=1 group by p.id ";
  64. $sql_count .= "SELECT count(prop_id) as count, sum(total_diamonds) as count_price " .
  65. " FROM ".$prop_tables[$i]." where 1=1 ";
  66. $sql_count .= " and ".$sql_w." 1=1 and prop_id > 0 ";
  67. if($i<count($prop_tables)-1)
  68. {
  69. $sql_str .= " union all ";
  70. $sql_count .= " union all ";
  71. }
  72. }
  73. $sql_str .= " ) as res group by res.id " ;
  74. $sql_count .= " ) as res ";
  75. $voList = $this->_Sql_list($model, $sql_str, "&".$parameter,'id',true);
  76. $countList = $GLOBALS['db']->getRow($sql_count);
  77. $this->assign ( 'list', $voList );
  78. $this->assign("count",$countList['count']);
  79. $this->assign("count_price",$countList['count_price']);
  80. $this->display ();
  81. return;
  82. }
  83. //道具消耗明细
  84. public function detail()
  85. {
  86. //统计所有送礼物表
  87. $prop_tables = array(); //存放送礼物表的数组
  88. $prop_tables[] = DB_PREFIX.'video_prop';
  89. $time = time()-28800;
  90. $year = intval(to_date($time,'Y')); //年
  91. $month = intval(to_date($time,'m')); //月
  92. $table = DB_PREFIX.'video_prop_'.to_date($time,'Ym');
  93. $res = $GLOBALS['db']->getRow("SHOW TABLES LIKE'$table'");
  94. /*admin_ajax_return($table);*/
  95. while ($res)
  96. {
  97. $prop_tables[]=$table; //获取该数据库下所有的送礼物表
  98. $month--;
  99. if ($month>0)
  100. {
  101. $month>=10?$prev_m = strval($year).$month:$prev_m = strval($year).'0'.$month;
  102. $table = DB_PREFIX . 'video_prop_' . $prev_m;
  103. $res = $GLOBALS['db']->getRow("SHOW TABLES LIKE'$table'");
  104. }
  105. else if ($month<=0)
  106. {
  107. $month = '12';
  108. $year --;
  109. $prev_y = $year.$month;
  110. $table = DB_PREFIX . 'video_prop_' . $prev_y;
  111. $res = $GLOBALS['db']->getRow("SHOW TABLES LIKE'$table'");
  112. }
  113. }
  114. //列表过滤器,生成时间搜索查询Map对象
  115. $map = $this->com_search();
  116. $prop_id = strim($_REQUEST['id']);
  117. $parameter = '';
  118. $sql_w = '';
  119. //查看是否有进行时间搜索
  120. if($map['start_time'] != '' && $map['end_time'] != ''){
  121. $parameter.=" create_time between '". $map['start_time'] . "' and '". $map['end_time'] ."'&";
  122. $sql_w .=" create_time between '". $map['start_time']. "' and '". $map['end_time'] ."' and ";
  123. }
  124. //查看是否有进行使用者或接收者ID搜索
  125. if(strim($_REQUEST['from_user_id'])!=''){
  126. $sql_w .= " from_user_id like '%".strim($_REQUEST['from_user_id'])."%' and ";
  127. }
  128. if(strim($_REQUEST['to_user_id'])!=''){
  129. $sql_w .= " to_user_id like '%".strim($_REQUEST['to_user_id'])."%' and ";
  130. }
  131. $model = D ();
  132. //获取道具明细表的sql
  133. $sql_str = "";
  134. //获取总量与总金额的sql
  135. $sql_count = "SELECT sum(res.count) as count,sum(res.count_price) as count_price FROM (";
  136. for($i=0;$i<count($prop_tables);$i++) //连接所有礼物表进行查询
  137. {
  138. $sql_str .= "SELECT prop_id as id ,prop_name as name,from_user_id,to_user_id,total_diamonds as diamonds,create_time as time " .
  139. " FROM ".$prop_tables[$i]." where prop_id = ".$prop_id;
  140. $sql_str .= " and ".$sql_w." 1=1 ";
  141. $sql_count .= "SELECT count(prop_id) as count, sum(total_diamonds) as count_price " .
  142. " FROM ".$prop_tables[$i]." where prop_id = ".$prop_id;
  143. $sql_count .= " and ".$sql_w." 1=1 ";
  144. if($i<count($prop_tables)-1)
  145. {
  146. $sql_str .= " union all ";
  147. $sql_count .= " union all ";
  148. }
  149. }
  150. $sql_count .= ") as res ";
  151. /*admin_ajax_return($sql_str);*/
  152. $voList = $this->_Sql_list($model, $sql_str, "&".$parameter,'time');
  153. for($i=0;$i<count($voList);$i++)
  154. {
  155. $voList[$i]['time'] = to_date($voList[$i]['time']);
  156. }
  157. $countList = $GLOBALS['db']->getRow($sql_count);
  158. $this->assign ( 'list', $voList );
  159. $this->assign("count",$countList['count']);
  160. $this->assign("count_price",$countList['count_price']);
  161. $this->display ();
  162. return;
  163. }
  164. //导出电子表
  165. public function export_csv($page = 1)
  166. {
  167. $pagesize = 10;
  168. set_time_limit(0);
  169. $limit = (($page - 1)*intval($pagesize)).",".(intval($pagesize));
  170. //统计所有送礼物表
  171. $prop_tables = array(); //存放送礼物表的数组
  172. $prop_tables[] = DB_PREFIX.'video_prop';
  173. $time = time()-28800;
  174. $year = intval(to_date($time,'Y')); //年
  175. $month = intval(to_date($time,'m')); //月
  176. $table = DB_PREFIX.'video_prop_'.to_date($time,'Ym');
  177. $res = $GLOBALS['db']->getRow("SHOW TABLES LIKE'$table'");
  178. /*admin_ajax_return($table);*/
  179. while ($res)
  180. {
  181. $prop_tables[]=$table; //获取该数据库下所有的送礼物表
  182. $month--;
  183. if ($month>0)
  184. {
  185. $month>=10?$prev_m = strval($year).$month:$prev_m = strval($year).'0'.$month;
  186. $table = DB_PREFIX . 'video_prop_' . $prev_m;
  187. $res = $GLOBALS['db']->getRow("SHOW TABLES LIKE'$table'");
  188. }
  189. else if ($month<=0)
  190. {
  191. $month = '12';
  192. $year --;
  193. $prev_y = $year.$month;
  194. $table = DB_PREFIX . 'video_prop_' . $prev_y;
  195. $res = $GLOBALS['db']->getRow("SHOW TABLES LIKE'$table'");
  196. }
  197. }
  198. //列表过滤器,生成时间搜索查询Map对象
  199. $map = $this->com_search();
  200. $sql_w = '';
  201. //查看是否有进行时间搜索
  202. if($map['start_time'] != '' && $map['end_time'] != ''){
  203. $parameter.=" create_time between '". $map['start_time'] . "' and '". $map['end_time'] ."'&";
  204. $sql_w .=" create_time between '". $map['start_time']. "' and '". $map['end_time'] ."' and ";
  205. }
  206. //查看是否有进行道具名搜索
  207. if(strim($_REQUEST['name'])!=''){
  208. $sql_w .= " prop_name like '%".strim($_REQUEST['name'])."%' and ";
  209. }
  210. //获取道具统计表的sql
  211. $sql_str = "SELECT res.id,res.name,res.price,sum(res.sale) as sale,sum(res.total) as total FROM (";
  212. //获取总量与总金额
  213. $sql_count = "SELECT sum(res.count) as count,sum(res.count_price) as count_price FROM (";
  214. for($i=0;$i<count($prop_tables);$i++) //连接所有礼物表进行查询
  215. {
  216. $sql_str .= "SELECT p.id as id,p.name as name,p.diamonds as price,count(v.prop_id) as sale,sum(v.total_diamonds) as total " .
  217. " FROM ".DB_PREFIX."prop p , ".$prop_tables[$i]." v WHERE p.id = v.prop_id ";
  218. $sql_str .= " and ".$sql_w." 1=1 group by p.id ";
  219. $sql_count .= "SELECT count(prop_id) as count, sum(total_diamonds) as count_price " .
  220. " FROM ".$prop_tables[$i]." where 1=1 ";
  221. $sql_count .= " and ".$sql_w." 1=1 ";
  222. if($i<count($prop_tables)-1)
  223. {
  224. $sql_str .= " union all ";
  225. $sql_count .= " union all ";
  226. }
  227. }
  228. $sql_str .= " ) as res group by res.id " ;
  229. $sql_count .= " ) as res ";
  230. $time ='1970-01-01 16:00:00';
  231. $sql =$sql_str." limit ";
  232. $sql .= $limit;
  233. /*admin_ajax_return($sql);*/
  234. $list=$GLOBALS['db']->getAll($sql);
  235. /*admin_ajax_return($list);*/
  236. if($list)
  237. {
  238. register_shutdown_function(array(&$this, 'export_csv'), $page+1);
  239. $m_config = load_auto_cache('m_config');
  240. $ticket_name = $m_config['ticket_name']!=''?$m_config['ticket_name']:'印票';
  241. $refund_value = array( 'id'=>'""','name'=>'""','price'=>'""', 'sale'=>'""', 'total'=>'""');
  242. if($page == 1)
  243. {
  244. $content = iconv("utf-8","gbk","ID,道具名,道具单价(钻石),道具消耗量,总消耗额(钻石)");
  245. $content = $content . "\n";
  246. }
  247. foreach($list as $k=>$v)
  248. {
  249. $refund_value['id'] = '"' . iconv('utf-8','gbk',$list[$k]['id']) . '"';
  250. $refund_value['name'] = '"' . iconv('utf-8','gbk',$list[$k]['name']) . '"';
  251. $refund_value['price'] = '"' . iconv('utf-8','gbk',$list[$k]['price']) . '"';
  252. $refund_value['sale'] = '"' . iconv('utf-8','gbk',$list[$k]['sale']) . '"';
  253. $refund_value['total'] = '"' . iconv('utf-8','gbk',$list[$k]['total']) . '"';
  254. $content .= implode(",", $refund_value) . "\n";
  255. }
  256. header("Content-Disposition: attachment; filename=prop_statistics.csv");
  257. echo $content ;
  258. }
  259. else
  260. {
  261. if($page==1)
  262. $this->error(L("NO_RESULT"));
  263. }
  264. }
  265. }