def upsert_relation_of_visited(user_id, teacher_id, teacher_name): """ by chen 在图数据库中添加拜访关系 添加拜访信息到 mongoDB 的user 表中 :param user_id: :param teacher_id: :param teacher_name: :return: None """ # 操作neo4j try: # back ==> {success: True / False, message:xxxx} back = upsert_relation_of_agent2teacher(user_id, teacher_id) if not back['success']: print("更新拜访记录到图数据库失败,原因:%s" % back['message']) except Exception as e: print("更新拜访记录到图数据库失败,原因:%s" % e) # TODO 后期剔除该部分 # 操作mongo try: collection_user = MongoOperator(**MongoDB_CONFIG).get_collection("user") # back => None or dict{"_id":ObjectId("xxx"), "related_teacher":[{id:xx, name, visited_count:12, acitve_count: 123}]} visited_record = collection_user.find_one({"id": user_id, "related_teacher.id": teacher_id}, {"related_teacher.$": 1}) if visited_record: info = visited_record['related_teacher'][0] count = int(info['visited_count'] + 1) collection_user.update_one({"_id": visited_record["_id"], "related_teacher.id": teacher_id}, {"$set": {"related_teacher.$.visited_count": count}}) else: insert_data = { "id": teacher_id, "name": teacher_name, "visited_count": int(1), "acitve_count": int(0) } collection_user.update_one({"id": user_id}, {"$set": {"related_teacher": [insert_data]}}) except Exception as e: print("更新拜访记录到mongo失败, 原因:%s" % e)
def set_completed_or_canceled(schedule_id, status): """ 设置该用户下的该计划是否完成或取消 :param schedule_id: :param status: ==0 表示已完成, ==1 表示未完成 ==-1表示该计划已经取消 :return: 返回true表示修改成功,否则失败 """ schedule_col = MongoOperator(**MongoDB_CONFIG).get_collection("schedule") status = -1 if status == -1 else 0 try: # 更新schedule_list result = schedule_col.update_one({"_id": ObjectId(schedule_id)}, {"$set": { "status": status }}) # print(result.modified_count, result.matched_count) # print(result) return result.modified_count except Exception as e: print("schedule_id 不符合标准", e) return 0
def insert_or_edit_schedule(s_data, schedule_id): """ 根据 schedule_id 决定 插入/更新 日程数据 :param s_data: 具体数据 :param schedule_id: string 类型的objectId :return: 0 / 1 or objectId """ schedule_col = MongoOperator(**MongoDB_CONFIG).get_collection("schedule") try: # 合法objectId ==> 修改 obj_id = ObjectId(schedule_id) result = schedule_col.update_one({'_id': obj_id}, {"$set": s_data}) return result.modified_count except TypeError as t: # 非法objectId ==> 创建 print("type error", type(t), t) result = schedule_col.insert_one(s_data) return result.inserted_id except Exception as e: print("添加/修改错误 ", e, schedule_id) return 0