import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
@Component
public class MiniprogramInnovationInformationPushServiceImpl<main>
implements MiniprogramInnovationInformationPushService {
private static MongoDatabase db;
private static MongoClient client;
private static String mongoUrl = ReadProperties.getValue("mongoUrl", "setting.properties");
private static Integer mongoPort = Integer.valueOf(ReadProperties.getValue("mongoPort", "setting.properties"));
//增加
@Override
public Map<String, Object> saveMiniprogramInnovationInformationPush(String title, String content, String phone, String unionId, String category) {
Map<String, Object> resultMap = new Map<>();
//连接数据库
client = new MongoClient(mongoUrl, mongoPort);
db = client.getDatabase("wechat");
//获取集合
MongoCollection<Document> collection = db.getCollection("miniprogram_innovation_information_push");
//生成_id
long timeChuo = System.currentTimeMillis();
String randomString = StringUtil.getRandomStr(3);
String _id = timeChuo + randomString;
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String dateString = format.format(new Date());
Long date = new Long(dateString);
//条件
Bson filter = Filters.eq("_id", _id);
//判断索引是否存在不存在创建
Document first = collection.find(filter).first();
if (first == null) {
Document document = new Document();
document.put("_id", _id);
document.put("title", title);
document.put("content", content);
document.put("phone", phone);
document.put("unionId", unionId);
document.put("create_date", date);
document.put("category", category);
collection.insertOne(document);
resultMap.put("code", ResultCode.SUCCESS);
resultMap.put("msg", "添加成功");
}
client.close();
return resultMap;
}
//删除
@Override
public Map<String, Object> removeMiniprogramInnovationInformationPush(String id) {
Map<String, Object> resultMap = new HashMap<>();
client = new MongoClient(mongoUrl, mongoPort);
db = client.getDatabase("wechat");
MongoCollection<Document> collection = db.getCollection("miniprogram_innovation_information_push");
Bson filter = Filters.eq("_id", id);
//调用删除方法
collection.deleteOne(filter);
resultMap.put("code", ResultCode.SUCCESS);
resultMap.put("msg", "删除成功");
client.close();
return resultMap;
}
//修改
@Override
public Map<String, Object> updateSolve(String id, String solve, String comment) {
Map<String, Object> resultMap = new HashMap<>();
client = new MongoClient(mongoUrl, mongoPort);
db = client.getDatabase("wechat");
MongoCollection<Document> collection = db.getCollection("miniprogram_innovation_information_push");
Bson filter = Filters.eq("_id", id);
//修改字段
Document document = new Document();
document.put("solve", solve);
document.put("comment", comment);
Document document2 = new Document("$set", document);
collection.updateOne(filter, document2);
resultMap.put("code", ResultCode.SUCCESS);
resultMap.put("msg", "修改成功");
client.close();
return resultMap;
}
//查询
@Override
public Map<String, Object> find(String phone, String startTime, String endTime, int page, int pageSize) {
Map<String, Object> resultMap = new HashMap<>();
List<JSONObject> list = new ArrayList<>();
client = new MongoClient(mongoUrl, mongoPort);
db = client.getDatabase("wechat");
MongoCollection<Document> collection = db.getCollection("miniprogram_innovation_information_push");
BasicDBObject basicDBObject = new BasicDBObject();
//加入查询条件,条件不为空
if(!StringUtils.isEmpty(phone)) {
basicDBObject.put("phone", phone);
}
if(!StringUtils.isEmpty(startTime)) {
basicDBObject.put("create_date", new BasicDBObject("$gte", new Long(startTime)));
}
if(!StringUtils.isEmpty(endTime)) {
basicDBObject.put("create_date", new BasicDBObject("$lte",new Long(endTime)));
}
if(!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {
basicDBObject.put("create_date", new BasicDBObject("$gte", new Long(startTime)).append("$lte",new Long(endTime)));
}
if(!StringUtils.isEmpty(phone) && !StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {
basicDBObject.put("phone", phone);
basicDBObject.put("create_date", new BasicDBObject("$gte", new Long(startTime)).append("$lte",new Long(endTime)));
}
// Sorts是排序用的。有descending 和ascending
MongoCursor<Document> cursor = collection.find(basicDBObject).sort(Sorts.orderBy(Sorts.descending("create_date"))).skip((page - 1) * pageSize).limit(pageSize).iterator();
//数据数量
long countDocuments = collection.countDocuments(basicDBObject);
try {
while (cursor.hasNext()) {
//查询出的结果转换成jsonObject
Document next = cursor.next();
Long create_date = next.getLong("create_date");
JSONObject jsonObject = JSONObject.parseObject( next.toJson().toString());
jsonObject.put("create_date", create_date+"");
list.add(jsonObject);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
client.close();
cursor.close();
}
if(list.size() > 0) {
resultMap.put("msg", ResultCode.SUCCESS);
resultMap.put("data", list);
resultMap.put("count", countDocuments);
} else {
resultMap.put("msg", "暂无数据。");
resultMap.put("code", ResultCode.DATA_NOT_FOUND);
}
return resultMap;
}