/** * 更新用户信息 * * @return 是否更新成功 */ private boolean updateRecord(record exist_record, record new_record) { try { System.out.print("update record"); MongoDAO dao = MongoDAO.GetInstance(); System.out.print("update record1"); Document existing = new Document(); existing.append("admission_number", exist_record.getAdmission_number()); long num = dao.GetCollection("records").deleteMany(existing).getDeletedCount(); System.out.print("remove record number = " + num); try { Map<String, Object> docMap = new_record.getDocMap(); docMap.put("inHospital", true); docMap.put("leaveHospital", false); docMap.put("followup", false); dao.GetCollection("records").insertOne(new Document(docMap)); } catch (Exception e) { e.printStackTrace(); logger.error(e.toString()); return false; } System.out.print("update record2"); } catch (Exception e) { e.printStackTrace(); logger.error(e.toString()); return false; } return true; }
/** * 取得正在住院的用户 * * @return */ public List<record> GetInhospitalRecords() { try { List<record> list = new ArrayList<record>(); MongoDAO dao = MongoDAO.GetInstance(); BasicDBObject cond = new BasicDBObject(); cond.append("inHospital", new BasicDBObject("$eq", true)); cond.append("leaveHospital", new BasicDBObject("$eq", false)); FindIterable<Document> result = dao.GetCollection("records").find(cond); MongoCursor<Document> it = result.iterator(); while (it.hasNext()) { Document doc = it.next(); record fol = new record(); fol.setAdmission_number(doc.getString("admission_number")); fol.setName(doc.getString("name")); fol.setWeixin_openid(doc.getString("weixin_openid")); fol.setInTime(doc.getDate("inTime")); System.out.print("\nname = " + fol.getName()); System.out.print("\nadmission_number = " + fol.getAdmission_number()); System.out.print("\nweixin_openid = " + fol.getWeixin_openid()); System.out.print("\nintime = " + fol.getInTime()); list.add(fol); System.out.print("add to list finished"); } System.out.print("list ready"); return list; } catch (Exception e) { e.printStackTrace(); logger.error(e.toString()); return null; } }
/** * 新用户注册 * * @return */ public boolean addNewRecord(record rec) { try { System.out.print("add new record begin"); record exist_record = this.GetRecord(rec.getAdmission_number()); if (exist_record != null) { System.out.print("has record with same id"); return this.updateRecord(exist_record, rec); } return this.insertRecord(rec); } catch (Exception e) { e.printStackTrace(); logger.error(e.toString()); return false; } }