SpringJDBC 活躍 用戶的行業分佈 人數 || 統計每日"激活"用 || 每一個行業的分佈

/** 
 * 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();
                    }
                });
        
    }
    
    
    
    
    
    
    

}
相關文章
相關標籤/搜索