package com.hh.racejob.oneoff.levelrank;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import com.hh.hhserver.extensions.db.DbManager;
import com.hh.hhserver.extensions.db.ResultObjectBuilder;
import com.hh.job.CustomStarter;
import com.hh.job.JobBase;
public class GetLevelRank extends JobBase
{
private final static int DB_PART = 1000 * 10000;
private final static int DEFAULT_TABLE_PART = 100 * 10000;
private int partUnit = DEFAULT_TABLE_PART * 10;
private static String SELECT_USER_INFO ="SELECT UserId, Nickname,Level"
+ " FROM UserInfo{0} order by Level desc limit 100";
private static String INSERT="INSERT INTO RankingData VALUES(?,?,?,NOW())";
private static String DELETE="DELETE FROM RankingData WHERE NAME='level'";
private final static int TOPCOUNT=100;
private TreeSet<Content> set = new TreeSet<Content>();
public static void main(String[]args)
{
CustomStarter.main(new String[]{"GetLevelRank", "com.hh.racejob.oneoff.levelrank.GetLevelRank", null, null});
}
@Override
public void doJob(String[] arg0) {
//删除以前的废弃数据
DbManager.getWorkDb().executeCommand(DELETE);
//增加新的数据
for (int dbIndex = 0; dbIndex < DbManager.getPartDbCount(); dbIndex++)
{
int tableCount = DB_PART / partUnit;
for (int i = 0; i < tableCount; i++) {
int tableIndex = dbIndex * tableCount + i;
String newSQL = SELECT_USER_INFO.replace("{0}", String.valueOf(tableIndex));
newSQL = newSQL.replace("{1}", String.valueOf(tableIndex * partUnit));
newSQL = newSQL.replace("{2}", String.valueOf(tableIndex * partUnit + partUnit));
List<Content> contentList = DbManager.getPartitionDb(dbIndex).executeQuery_ObjectListEx(newSQL, Content.builder);
for(Content content : contentList){
if(content!=null)
{
content.setTime(String.valueOf(System.currentTimeMillis()));
set.add(content);
}
}
}
}
//得到要插入的100条RankData数据
ArrayList<Object[]>rankDatas=new ArrayList<>();
Iterator<Content> it = set.iterator();
int count=0;
while(it.hasNext()&&count<TOPCOUNT){
count++;
Content content=(Content) it.next();
RankData rankData=new RankData();
rankData.setContent(content.toString());
rankData.setDataKey(content.getUserId()+"");
rankData.setName("level");//固定
rankData.setAddDate(String.valueOf(System.currentTimeMillis()));
rankDatas.add(new Object[]{rankData.getName(),rankData.getDataKey(),rankData.getContent()});
}
//批量插入数据库
DbManager.getWorkDb().executeBatchCommandEx(INSERT, rankDatas);
}
@SuppressWarnings("rawtypes")
static class Content implements Comparable
{
public int userId;
public String nickName;
public int level;
public String time;
public Content()
{
}
public static ResultObjectBuilder<Content>builder=new ResultObjectBuilder<Content>() {
@Override
public Content build(ResultSet rs) throws SQLException {
// TODO Auto-generated method stub
Content content=new Content();
content.userId=rs.getInt("UserId");
content.nickName=rs.getString("Nickname");
content.level=rs.getInt("Level");
return content;
}
};
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
@Override
public int compareTo(Object o) {
Content content=(Content)o;
if(level > content.getLevel()){
return -1;
} else if(level == content.getLevel()){
if(userId < content.getUserId()){
return -1;
} else{
return 1;
}
} else {
return 1;
}
}
public String toString()
{
return userId+":"+nickName+":"+level+":"+time;
}
}
class RankData
{
public String Name;
public String DataKey;
public String Content;
public String AddDate;
public RankData()
{
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getDataKey() {
return DataKey;
}
public void setDataKey(String dataKey) {
DataKey = dataKey;
}
public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
public String getAddDate() {
return AddDate;
}
public void setAddDate(String addDate) {
AddDate = addDate;
}
}
}
类Content中,当level一样大的时候,uid小的排序在前面,否则相反;
Compareto中,不要返回0,如果直接返回result可能会出现0的状况,一般返回值的含义如下:
1.返回 1 那么当前的值会排在 被比较者 后面。
2.返回 0 那么当前的值【不会被加入到 TreeSet 中】,因为当前的值【被认为是跟现有的某一个值相等】。
3.返回 -1 会被添加到 被比较者 的前边。
总结:主要需求:从十张表中,选出level的前 100名;
主要思路是:从十张表中,每张表拿出前100名用户,放在treeset里面,然后交给treeset的compare进行比较,就可以得到了