Commit d9c47959 by shenjinzhu

优化查询,删除无用代码

parent 6c5a1ee3
......@@ -24,6 +24,8 @@ import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
......@@ -177,7 +179,16 @@ public class DataDaoImpl implements DataDao {
end = cal.get(Calendar.YEAR) + "" + (month > 9 ? "" + month : "0" + month);
System.out.println(end);
}
MongoClient mongoClient = new MongoClient(ser.getDbHost(), ser.getDbPort());
MongoClient mongoClient = null;
if (ser.getDbUsername() != null) {
ServerAddress sa = new ServerAddress(ser.getDbHost(), ser.getDbPort());
List<MongoCredential> mongoCredentialList = new ArrayList<MongoCredential>();
mongoCredentialList.add(MongoCredential.createMongoCRCredential(ser.getDbUsername(),
ser.getDbName(), ser.getDbPassword().toCharArray()));
new MongoClient(sa, mongoCredentialList);
mongoClient = new MongoClient(sa, mongoCredentialList);
} else
mongoClient = new MongoClient(ser.getDbHost(), ser.getDbPort());
// 连接到数据库
MongoDatabase mongo = mongoClient.getDatabase(ser.getDbName());
DbDepot.dataCol.put(pt, mongo.getCollection(ser.getCollection() + end));
......@@ -193,7 +204,16 @@ public class DataDaoImpl implements DataDao {
int month = cal.get(Calendar.MONTH) + 1;
end = cal.get(Calendar.YEAR) + "" + (month > 9 ? "" + month : "0" + month);
}
MongoClient mongoClient = new MongoClient(ser.getDbHost(), ser.getDbPort());
MongoClient mongoClient = null;
if (ser.getDbUsername() != null) {
ServerAddress sa = new ServerAddress(ser.getDbHost(), ser.getDbPort());
List<MongoCredential> mongoCredentialList = new ArrayList<MongoCredential>();
mongoCredentialList.add(MongoCredential.createMongoCRCredential(ser.getDbUsername(), ser.getDbName(),
ser.getDbPassword().toCharArray()));
new MongoClient(sa, mongoCredentialList);
mongoClient = new MongoClient(sa, mongoCredentialList);
} else
mongoClient = new MongoClient(ser.getDbHost(), ser.getDbPort());
MongoDatabase mongo = mongoClient.getDatabase(ser.getDbName());
DbDepot.dataCol.put(pt, mongo.getCollection(ser.getCollection() + end));
return mongo.getCollection(ser.getCollection() + end);
......@@ -201,19 +221,19 @@ public class DataDaoImpl implements DataDao {
}
public static void main(String[] args) {
MongoClient mongoClient = new MongoClient("1.119.44.206", 30000);
// 连接到数据库
MongoDatabase mongo = mongoClient.getDatabase("wx_tanglihua");
MongoCollection<Document> col = mongo.getCollection("status_201712");
Date start = new Date();
start.setDate(15);
Date end = new Date();
end.setDate(16);
Bson query = new BasicDBObject("time", new BasicDBObject("$gte", start).append("$lte", end));
ServerAddress sa = new ServerAddress("106.14.249.240", 26807);
List<MongoCredential> mongoCredentialList = new ArrayList<MongoCredential>();
mongoCredentialList
.add(MongoCredential.createMongoCRCredential("zhiweidata", "spyPlat", "1q2w3e4r".toCharArray()));
new MongoClient(sa, mongoCredentialList);
MongoClient mongoClient = new MongoClient(sa, mongoCredentialList);
MongoDatabase mongo = mongoClient.getDatabase("spyPlat");
MongoCollection<Document> col = mongo.getCollection("indexOfA");
Bson query = new BasicDBObject("code", "300104");
System.out.println(col.count(query));
}
@Override
public int findBySourceFromLibrary(Date startTime, Date endTime, String source) {
Calendar c = Calendar.getInstance();
......
......@@ -82,14 +82,15 @@ public class MessageDaoImpl implements MessageDao {
public List<Message> findAll(int pageNo, int pageSize, String pt) {
return mongo.find(
Query.query(Criteria.where("pt").is(pt).andOperator(Criteria.where("handle").is(false)))
.skip((pageNo - 1) * pageSize).with(new Sort(Sort.Direction.ASC, "templateLv")).limit(pageSize),
.skip((pageNo - 1) * pageSize).with(new Sort(Sort.Direction.ASC, "templateLv"))
.with(new Sort(Sort.Direction.DESC, "createDate")).limit(pageSize),
Message.class);
}
@Override
public List<Message> findByTmpName(String templateName, int pageNo, int pageSize, String pt) {
return mongo.find(Query.query(Criteria.where("tempName").regex(templateName)).skip((pageNo - 1) * pageSize)
.limit(pageSize), Message.class);
.limit(pageSize).with(new Sort(Sort.Direction.DESC, "createDate")), Message.class);
}
@Override
......@@ -120,7 +121,8 @@ public class MessageDaoImpl implements MessageDao {
return mongo.find(Query
.query(Criteria.where("tempName").regex(tempName)
.andOperator(Criteria.where("handle").is(handle).andOperator(Criteria.where("pt").is(pt))))
.skip((pageNo - 1) * pageSize).limit(pageSize), Message.class);
.skip((pageNo - 1) * pageSize).limit(pageSize)
.with(new Sort(Sort.Direction.DESC, "handleDate")), Message.class);
}
@Override
......
......@@ -33,43 +33,15 @@ import com.zhiwei.manage.util.Change;
public class TemplateDaoImpl implements TemplateDao {
@Resource(name = "mongoTemplate")
private MongoTemplate mongo;
@Autowired
private DbDepot dbDepot;
@Override
public boolean insert(Template template) {
// DBCollection con = getCollection(template.getPt());
// if (con == null) {
// return false;
// }
// DBObject obj = new BasicDBObject();
// Field[] fields = template.getClass().getDeclaredFields();
// for (int j = 0; j < fields.length; j++) {
// fields[j].setAccessible(true);
// try {
// if (fields[j].getName().equals("id")) {
// continue;
// }
// if (fields[j].get(template) != null)
// // 字段名,字段值
// obj.put(fields[j].getName(), fields[j].get(template));
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// con.insert(obj);
mongo.insert(template);
return true;
}
@Override
public boolean update(Template template) {
// DBCollection con = getCollection(template.getPt());
// if (con == null) {
// return false;
// }
// DBObject update = new BasicDBObject();
// DBObject obj = new BasicDBObject();
Update update = new Update();
Field[] fields = template.getClass().getDeclaredFields();
for (int j = 0; j < fields.length; j++) {
......@@ -80,15 +52,10 @@ public class TemplateDaoImpl implements TemplateDao {
}
if (fields[j].get(template) != null)
update.set(fields[j].getName(), fields[j].get(template));
// 字段名,字段值
} catch (Exception e) {
e.printStackTrace();
}
}
// update.put("$set", obj);
// DBObject query = new BasicDBObject();
// query.put("_id", new ObjectId(template.getTemplateId()));
// WriteResult result = con.update(query, update, false, false);
WriteResult result = mongo.updateFirst(Query.query(Criteria.where("_id").is(template.getTemplateId())), update,
Template.class);
return result.getN() == 1 ? true : false;
......@@ -96,17 +63,7 @@ public class TemplateDaoImpl implements TemplateDao {
@Override
public Template findById(String templateId, String pt) {
// DBCollection con = getCollection(pt);
// if (con == null) {
// return null;
// }
// Map<String, Object> map = (Map<String, Object>) con
// .findOne(new BasicDBObject("_id", new ObjectId(templateId)).append("pt",
// pt));
Template temp = mongo.findOne(Query.query(Criteria.where("_id").is(templateId)), Template.class);
// Template temp = new Template();
// temp = Change.toBean(Template.class, map);
// temp.setTemplateId(String.valueOf(map.get("_id")));
return temp;
}
......@@ -115,32 +72,11 @@ public class TemplateDaoImpl implements TemplateDao {
List<Template> list = mongo.find(Query.query(Criteria.where("tempName").regex(templateName))
.skip((pageNo - 1) * pageSize).limit(pageSize).addCriteria(Criteria.where("pt").is(pt)),
Template.class);
// List<Template> list = new ArrayList<Template>();
// DBCollection con = getCollection(pt);
// DBObject query = new BasicDBObject();
// query.put("tempName", new BasicDBObject("$regex", templateName));
// query.put("pt", pt);
// DBCursor cursor = con.find(query).sort(new BasicDBObject("createDate",
// -1)).skip((pageNo - 1) * pageSize)
// .limit(pageSize);
// while (cursor.hasNext()) {
// Map<String, Object> map = (Map<String, Object>) cursor.next();
// Template temp = new Template();
// temp = Change.toBean(Template.class, map);
// temp.setTemplateId(String.valueOf(map.get("_id")));
// list.add(temp);
// }
return list;
}
@Override
public boolean delete(String templateId, String pt) {
// DBCollection con = getCollection(pt);
// if (con == null) {
// return false;
// }
// WriteResult result = con.remove(new BasicDBObject("_id", new
// ObjectId(templateId)));
WriteResult result = mongo.remove(Query.query(Criteria.where("_id").is(templateId)), Template.class);
return result.getN() == 0 ? true : false;
}
......@@ -149,36 +85,9 @@ public class TemplateDaoImpl implements TemplateDao {
public List<Template> findAll(int pageNo, int pageSize, String pt) {
List<Template> list = mongo.find(
Query.query(Criteria.where("pt").is(pt)).skip((pageNo - 1) * pageSize).limit(pageSize), Template.class);
// List<Template> list = new ArrayList<Template>();
// DBCollection con = getCollection(pt);
// if (con == null) {
// return null;
// }
// DBObject obj = new BasicDBObject();
// obj.put("pt", pt);
// DBCursor currsor = con.find(obj).sort(new BasicDBObject("createDate",
// -1)).skip((pageNo - 1) * 10)
// .limit(pageSize);
// while (currsor.hasNext()) {
// Map<String, Object> map = (Map<String, Object>) currsor.next();
// Template temp = new Template();
// temp = Change.toBean(Template.class, map);
// temp.setTemplateId(String.valueOf(map.get("_id")));
// list.add(temp);
// }
return list;
}
public DBCollection getCollection(String pt) {
DBCollection con = null;
if (DbDepot.dbCons.get(pt + "temp") != null) {
con = (DBCollection) DbDepot.dbCons.get(pt + "temp");
} else {
con = dbDepot.getDBCollection("temp", pt);
}
return con;
}
@Override
public int allCount(String pt) {
return (int) mongo.count(Query.query(Criteria.where("pt").is(pt)), Template.class);
......
package com.zhiwei.manage.handle;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.Service;
import com.mongodb.DBCollection;
import com.zhiwei.manage.bean.ServerBean;
import com.zhiwei.manage.util.MongoConnect;
@Service("firstRun")
public class DbDepot {
@Resource(name = "mongoTemplate")
private MongoTemplate mongo;
// 信息和检测模板切换链接
public static ConcurrentMap<String, Object> dbCons;
public static ConcurrentMap<String, Object> newsCol;
public static ConcurrentMap<String, Object> dataCol;
public static String nowPt = "网媒";
public static int month = 0;
private static final Log log = LogFactory.getLog(DbDepot.class);
......@@ -36,66 +24,6 @@ public class DbDepot {
dataCol = new ConcurrentHashMap<String, Object>();
dbCons = new ConcurrentHashMap<String, Object>();
newsCol = new ConcurrentHashMap<String, Object>();
List<ServerBean> list = mongo.find(Query.query(Criteria.where("pt").is(nowPt)), ServerBean.class);
Calendar cal = Calendar.getInstance();
month = cal.get(Calendar.MONTH + 1);
for (ServerBean sb : list) {
String end = "";
if (sb.isTable()) {
if (sb.getTableKind().equals("month")) {
int month=cal.get(Calendar.MONTH) + 1;
end = cal.get(Calendar.YEAR) + (month>9?""+month:"0"+month);
} else {
}
}
if (sb.getDbType().equals("mongo")) {
if (sb.getDbUsername() != null && !sb.getDbUsername().trim().equals("")) {
DBCollection con = new MongoConnect(sb.getDbHost(), sb.getDbPort(), sb.getDbUsername(),
sb.getDbPassword(), sb.getDbName()).getCollection(sb.getCollection() + end);
dbCons.put(sb.getPt()+sb.getOther(), con);
} else {
DBCollection con = new MongoConnect(sb.getDbHost(), sb.getDbPort(), sb.getDbName())
.getCollection(sb.getCollection() + end);
dbCons.put(sb.getPt()+sb.getOther(), con);
}
}
}
System.out.println("连接初始化成功");
}
public DBCollection getDBCollection(String other,String pt) {
DBCollection con = null;
ServerBean sb = mongo.findOne(
Query.query(Criteria.where("pt").is(pt).orOperator(Criteria.where("other").is(other))),
ServerBean.class);
System.out.println(pt);
System.out.println(sb);
Calendar cal=Calendar.getInstance();
month=cal.get(Calendar.MONTH+1);
if (sb != null) {
if (sb.getDbType().equals("mongo")) {
String end="";
if(sb.isTable()) {
if(sb.getTableKind().equals("month")) {
int month=cal.get(Calendar.MONTH) + 1;
end = cal.get(Calendar.YEAR) + (month>9?""+month:"0"+month);
}else {
}
}
if (sb.getDbUsername() != null && !sb.getDbUsername().equals("")) {
con = new MongoConnect(sb.getDbHost(), sb.getDbPort(), sb.getDbUsername(), sb.getDbPassword(),
sb.getDbName()).getCollection(sb.getCollection()+end);
dbCons.put(pt+sb.getOther(), con);
} else {
con = new MongoConnect(sb.getDbHost(), sb.getDbPort(), sb.getDbName())
.getCollection(sb.getCollection()+end);
dbCons.put(pt+sb.getOther(), con);
}
}
}
return con;
}
}
......@@ -75,7 +75,6 @@ public class DataController {
long end=(long) map.get("end");
String timeType=(String) map.get("timeType");
String method=(String) map.get("method");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar startd = Calendar.getInstance();
Calendar endTime = Calendar.getInstance();
if (method == null) {
......@@ -91,8 +90,6 @@ public class DataController {
}
Calendar ds=Calendar.getInstance();
ds.setTimeInMillis(end);
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startd.getTime()));
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(ds.getTime()));
String source = "";
List<Data> list = new ArrayList<Data>();
startd.add(Calendar.HOUR, -8);
......@@ -127,8 +124,6 @@ public class DataController {
startd.setTimeInMillis(start);
endTime.setTimeInMillis(end);
}
System.out.println(sdf.format(startd.getTime()));
System.out.println(sdf.format(endTime.getTime()));
Channel channel = new Channel();
List<Data> list = new ArrayList<Data>();
if (method.equals("source")) {
......@@ -213,17 +208,4 @@ public class DataController {
JsonResult result = new JsonResult(true, 200, "参数错误");
return JSON.toJSONString(result);
}
@RequestMapping(value = "/getTest", produces = "application/json;charset=utf-8")
@ResponseBody
public String getTestData(@RequestBody Map<String, Object> map){
String value=(String) map.get("value");
String method=(String) map.get("method");
Calendar startTime=Calendar.getInstance();
startTime.add(Calendar.DATE, -6);
Calendar endTime=Calendar.getInstance();
List<Data> list=dataService.findByMethod(startTime.getTime(), endTime.getTime(), method, value);
JsonResult result = new JsonResult(true, 200, "获取成功",list);
return JSON.toJSONString(result);
}
}
......@@ -58,7 +58,6 @@ public class FieldController {
@RequestMapping(value = "/insertFields", produces = "application/json;charset=utf-8")
@ResponseBody
public String insertField(@RequestBody FieldBean fieldbean) {
System.out.println(fieldbean);
fs.insert(fieldbean);
JsonResult result = new JsonResult(true, 200, "添加成功");
return JSON.toJSONString(result);
......
......@@ -30,7 +30,6 @@ public class MessageConteoller {
int pageNo = (int) map.get("pageNo");
int pageSize = (int) map.get("pageSize");
String pt = (String) map.get("pt");
if (tempName == null || tempName.equals("")) {
PageEty page = msgService.findAll(pageNo, pageSize, pt);
JsonResult result = new JsonResult(true, 200, "请求成功", page);
......
......@@ -72,7 +72,6 @@ public class PersonController {
Cookie[] cookies = req.getCookies();
String result = "";
for (Cookie cookie : cookies) {
System.out.println(cookie.getName() + ":" + cookie.getValue());
result += cookie.getName() + ":" + cookie.getValue() + "\n";
}
return JSON.toJSONString(result);
......@@ -110,10 +109,6 @@ public class PersonController {
@ResponseBody
public String deletePerson(@RequestBody Map<String, Object> map){
List<String> deleteList=(List<String>) map.get("personId");
// List<String> deleteList=new ArrayList<>();
// for(String p:personId){
// deleteList.add(p);
// }
boolean b=personService.delete(deleteList);
JsonResult result=new JsonResult(b?true:false, 200, b?"操作成功":"操作失败");
return JSON.toJSONString(result);
......@@ -122,7 +117,6 @@ public class PersonController {
@RequestMapping(value = "/savePerson", produces = "application/json;charset=utf-8")
@ResponseBody
public String savePerson(HttpServletRequest req,@RequestBody Person person){
System.out.println(person);
Cookie[] cookies = req.getCookies();
for (Cookie cookie : cookies) {
System.out.println(cookie.getName());
......
......@@ -56,7 +56,6 @@ public class ServerController {
@RequestMapping(value = "/insertServer", produces = "application/json;charset=utf-8")
@ResponseBody
public String insertServer(@RequestBody ServerBean serverBean) {
System.out.println(serverBean);
ss.insert(serverBean);
JsonResult result = new JsonResult(true, 200, "添加成功");
return JSON.toJSONString(result);
......@@ -88,7 +87,6 @@ public class ServerController {
@RequestMapping(value = "/insertChannel", produces = "application/json;charset=utf-8")
@ResponseBody
public String insertChannel(@RequestBody Channel channel){
System.out.println(channel);
ss.insertChannels(channel);
JsonResult result=new JsonResult(true, 200, "添加成功");
return JSON.toJSONString(result);
......
......@@ -95,7 +95,6 @@ public class TemplateController {
boolean isDelete = false;
String results = "";
for (String l : templateId) {
System.out.println(l);
isDelete = ts.delete(l, pt);
MainThread.delete(l);
results += l + "\n";
......
......@@ -51,7 +51,6 @@ public class Change {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (map.containsKey(propertyName)) {
// 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。
Object value = map.get(propertyName);
if ("".equals(value)) {
value = null;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment