import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.iqibai.gamedata.model.ResultUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
@Slf4j
public class DumpExcelHandler {
@Autowired
private MongoTemplate mongoTemplate;
public void processExcel(){
String filePrefix = "/data/work/user/redis-user";
for (int i = 1; i <= 4; i++) {
String file = filePrefix + i +".txt";
List<String> userIds = FileUtil.readLines(file,"utf-8");
List<ResultUser> resultUsers = new ArrayList<>();
for(String userId:userIds){
Integer userIdInt = Convert.toInt(userId,0);
if(userIdInt == 0){
continue;
}
Query findQuery = new Query(Criteria.where("userId").is(userIdInt));
ResultUser findUser = mongoTemplate.findOne(findQuery,ResultUser.class);
if(findUser == null){
continue;
}
resultUsers.add(findUser);
}
ExcelWriter excelWriter = new ExcelWriter(filePrefix+i+".xls");
excelWriter.write(resultUsers);
excelWriter.flush();
log.info("end process {}",file);
}
}
}
网友评论