/** * Project Name:TranbPortal * File Name:QuartzPersonIUCodeReportDaoImpl.java * Package Name:com.tranb.reports.dao.impl * Date:2016年6月1日上午11:01:13 * Copyright (c) 2016, lizhi1817@163.com All Rights Reserved */ package com.tranb.reports.dao.impl; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang.StringUtils; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.BatchPreparedStatementSetter; import org.springframework.jdbc.core.ResultSetExtractor; import net.sf.json.JSONObject; import com.tranb.common.JSONPropertyRowMapper; import com.tranb.common.MultiDataSourceDaoSupport; import com.tranb.common.consts.DB; import com.tranb.common.tools.DateUtils; /** * * @author : zhaoshuli * @createTime : 2016-04-26 上午11:13:42 * @version : * @description : * * */ public class QuartzPersonIUCodeReportDaoImpl extends MultiDataSourceDaoSupport{ /** * 统计每日"激活"用户行业分布 人数 * @param time * * @author : zhaoshuli * @param now * @createTime : 2016-6-01 */ public void statisticsPersonIUCodeActivateByDaily(final long beginDate, final long now) { //获取user_account表激活日期为过滤条件的 uid String uaSql="SELECT uid FROM user_account where activeTime BETWEEN ? and ? "; List<Integer> uidList = getJdbcTemplate(DB.JIU_TONG_USER).query(uaSql, new Object[]{beginDate,now},new ResultSetExtractor<List<Integer>>(){ @Override public List<Integer> extractData(ResultSet rs) throws SQLException, DataAccessException { List<Integer> list = new ArrayList<Integer>(); while(rs.next()){ list.add(rs.getInt("uid")); } return list; } }); if(uidList!=null&&uidList.size()>0){ //获取UID,结果为 都好分割的字符串 String uids=StringUtils.join(uidList, ","); String uiSql="SELECT count(1) as count,personIUCode from user_info where uid in ("+uids+" ) and LENGTH(personIUCode)=4 GROUP BY personIUCode"; List<JSONObject> queryList = getJdbcTemplate(DB.JIU_TONG_USER).query(uiSql, new JSONPropertyRowMapper()); //type=1 当日"激活"用户行业分布 byte type = 1; //调通方法,将数据插入Reports_personIUCode_daily 表 batchInsertReports_personIUCode_daily(queryList,DateUtils.timeToInt(beginDate),type); } } /** * 统计每日 "活跃" 用户的行业分布 人数 * @param beginDate * @author : zhaoshuli * @createTime : 2016-06-01 */ public void activePersonIUCode(final int beginDate) { //查询前一日的活跃用户UID String sql ="SELECT uid FROM user_active WHERE logDate = ? "; List<Integer> uidList = getJdbcTemplate(DB.JIU_TONG_REPORT).query(sql, new Object[]{beginDate},new ResultSetExtractor<List<Integer>>(){ @Override public List<Integer> extractData(ResultSet rs) throws SQLException, DataAccessException { List<Integer> list = new ArrayList<Integer>(); while(rs.next()){ list.add(rs.getInt("uid")); } return list; } }); if(uidList!=null&&uidList.size()>0){ //获取UID,结果为 都好分割的字符串 String uids=StringUtils.join(uidList, ","); String uiSql="SELECT count(1) as count,personIUCode from user_info where uid in ("+uids+" ) and LENGTH(personIUCode)=4 GROUP BY personIUCode"; List<JSONObject> queryList = getJdbcTemplate(DB.JIU_TONG_USER).query(uiSql, new JSONPropertyRowMapper()); //type=2 当日活跃用户行业分布 byte type = 2; //调通方法,将数据插入Reports_personIUCode_daily 表 batchInsertReports_personIUCode_daily(queryList,beginDate,type); } } /** * 将数据插入Reports_personIUCode_daily 表 * @param queryList * @param beginDate * @param type * * @author : zhaoshuli * @createTime : 2016-06-01 */ private void batchInsertReports_personIUCode_daily( final List<JSONObject> queryList, final int beginDate, final byte type) { getJdbcTemplate(DB.JIU_TONG_REPORT).batchUpdate( "INSERT INTO reports_personIUCode_daily (personIUCode,count,date,type) values(?,?,?,?)", new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement ps, int i) throws SQLException { JSONObject js = queryList.get(i); ps.setString(1,js.getString("personIUCode")); ps.setInt(2,js.getInt("count")); //激活日期,当前日期前一天 ps.setInt(3,beginDate); //type=2 当日活跃用户行业分布 ps.setInt(4,type); } @Override public int getBatchSize() { return queryList.size(); } }); } /** * 统计全部用户 在每一个行业的分布 * @author : zhaoshuli * @createTime : 2016-06-01 */ public void countAllPersonIUCode() { String sql="SELECT count(1) as count,personIUCode FROM `user_info` WHERE LENGTH(personIUCode)=4 GROUP BY personIUCode"; //查询全部用户行业分布 final List<JSONObject> list = getJdbcTemplate(DB.JIU_TONG_USER).query(sql, new JSONPropertyRowMapper()); getJdbcTemplate(DB.JIU_TONG_REPORT).batchUpdate( "INSERT INTO reports_personIUCode_count (personIUCode,count,date) values(?,?,?)", new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement ps, int i) throws SQLException { JSONObject js = list.get(i); ps.setString(1,js.getString("personIUCode")); ps.setInt(2,js.getInt("count")); ps.setInt(3,DateUtils.timeToInt(System.currentTimeMillis())); } @Override public int getBatchSize() { return list.size(); } }); } /** * 统计4月1号至6月1号的每日"激活"用户行业分布 人数 * @param time * * @author : zhaoshuli * @param now * @createTime : 2016-6-01 */ public void statisticsPersonIUCodeActivateByHistory(final long beginDate, final long now) { //获取user_account表激活日期为过滤条件的 uid String uaSql="SELECT uid FROM user_account where activeTime BETWEEN ? and ? "; List<Integer> uidList = getJdbcTemplate(DB.JIU_TONG_USER).query(uaSql, new Object[]{beginDate,now},new ResultSetExtractor<List<Integer>>(){ @Override public List<Integer> extractData(ResultSet rs) throws SQLException, DataAccessException { List<Integer> list = new ArrayList<Integer>(); while(rs.next()){ list.add(rs.getInt("uid")); } return list; } }); if(uidList!=null&&uidList.size()>0){ //获取UID,结果为 都好分割的字符串 String uids=StringUtils.join(uidList, ","); String uiSql="SELECT count(1) as count,personIUCode from user_info where uid in ("+uids+" ) and LENGTH(personIUCode)=4 GROUP BY personIUCode"; List<JSONObject> queryList = getJdbcTemplate(DB.JIU_TONG_USER).query(uiSql, new JSONPropertyRowMapper()); //type=1 当日"激活"用户行业分布 byte type = 1; //调通方法,将数据插入Reports_personIUCode_daily 表 batchInsertReports_personIUCode_daily(queryList,DateUtils.timeToInt(beginDate),type); } } /** * @param date * * @author : zhaoshuli * @createTime : 2016-04-26 */ public void statisticsPersonIUCodeByActionHistory(int date) { //查询前一日的活跃用户UID String sql ="SELECT uid FROM user_active WHERE logDate = ? "; List<Integer> uidList = getJdbcTemplate(DB.JIU_TONG_REPORT).query(sql, new Object[]{date},new ResultSetExtractor<List<Integer>>(){ @Override public List<Integer> extractData(ResultSet rs) throws SQLException, DataAccessException { List<Integer> list = new ArrayList<Integer>(); while(rs.next()){ list.add(rs.getInt("uid")); } return list; } }); if(uidList!=null&&uidList.size()>0){ //获取UID,结果为 都好分割的字符串 String uids=StringUtils.join(uidList, ","); String uiSql="SELECT count(1) as count,personIUCode from user_info where uid in ("+uids+" ) and LENGTH(personIUCode)=4 GROUP BY personIUCode"; List<JSONObject> queryList = getJdbcTemplate(DB.JIU_TONG_USER).query(uiSql, new JSONPropertyRowMapper()); //type=2 当日活跃用户行业分布 byte type = 2; //调通方法,将数据插入Reports_personIUCode_daily 表 batchInsertReports_personIUCode_daily(queryList,date,type); } } public void countAllPersonIUCodeHistory(final long time) { String sql="SELECT count(1) as count,personIUCode FROM `user_info` WHERE LENGTH(personIUCode)=4 and createTime < ? GROUP BY personIUCode"; //查询全部用户行业分布 final List<JSONObject> list = getJdbcTemplate(DB.JIU_TONG_USER).query(sql, new Object[]{time},new JSONPropertyRowMapper()); getJdbcTemplate(DB.JIU_TONG_REPORT).batchUpdate( "INSERT INTO reports_personIUCode_count (personIUCode,count,date) values(?,?,?)", new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement ps, int i) throws SQLException { JSONObject js = list.get(i); ps.setString(1,js.getString("personIUCode")); ps.setInt(2,js.getInt("count")); ps.setInt(3,DateUtils.timeToInt(time-60*1000L)); } @Override public int getBatchSize() { return list.size(); } }); } }