package_function.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # 封装mysql执行函数、创建节点函数
  2. from flask_sqlalchemy import SQLAlchemy
  3. from app.core.graph.graph_operations import connect_graph
  4. from py2neo import Node, RelationshipMatch
  5. db = SQLAlchemy()
  6. def execute_sql(cur, sql, params):
  7. result = db.session.execute(sql, params)
  8. return result.fetchall()
  9. def sql_commit(sql):
  10. try:
  11. db.session.execute(sql)
  12. db.session.commit()
  13. except Exception as e:
  14. db.session.rollback()
  15. raise e
  16. def sql_execute_result(sql):
  17. try:
  18. result = db.session.execute(sql)
  19. return result.fetchall()
  20. except Exception as e:
  21. raise e
  22. # 创建或获取节点
  23. def create_or_get_node(label, **properties):
  24. node = connect_graph.nodes.match(label, **properties).first()
  25. if node is None:
  26. node = Node(label, **properties)
  27. connect_graph.create(node)
  28. return node
  29. # 查询是否存在节点
  30. def get_node(label, **properties):
  31. node = connect_graph.nodes.match(label, **properties).first()
  32. # 如果没有找到匹配的节点,node 将会是 None
  33. return node
  34. # 查询是否存在关系
  35. def relationship_exists(start_node, rel_type, end_node, **properties):
  36. matcher = connect_graph.match(nodes=[start_node, end_node], r_type=rel_type)
  37. # 如果需要匹配关系的属性
  38. if properties:
  39. matcher = matcher.where(**properties)
  40. result = matcher.first()
  41. return result is not None
  42. # 关系权重生成
  43. def relation_weights(relation):
  44. relation_list = ['父亲', '母亲', '儿子', '女儿']
  45. if relation in relation_list:
  46. return 3
  47. else:
  48. return 1
  49. def workplace_weights(workplace_list, workplace):
  50. if workplace in workplace_list:
  51. return 3
  52. else:
  53. return 1
  54. def soure_organization_name(workplace):
  55. query = f"match (n:workplace)<-[r:workin]-(subordinate_person:worker)" \
  56. f"WHERE n.organization_no = '{workplace}' " \
  57. f"return subordinate_person.code as code"
  58. # 修复:使用正确的session方式执行查询
  59. driver = connect_graph()
  60. if not driver:
  61. return []
  62. with driver.session() as session:
  63. result = session.run(query)
  64. data = result.data()
  65. return data
  66. # 输入人员编码列表,得到员工与工作单位的关系,并且在此函数内完成员工,亲属,以及人-工作单位关系的创建
  67. def create_person_workplace(code_list, flag, relatives_type):
  68. nodes = []
  69. links = []
  70. condition = tuple(map(int, relatives_type.split(",")))
  71. relation_dict = {
  72. (0, 0, 0, 0): lambda: [],
  73. (0, 0, 0, 1): lambda: [],
  74. (0, 0, 1, 0): lambda: [],
  75. (0, 1, 0, 0): lambda: person_relative(links, code_list, 0),
  76. (0, 1, 0, 1): lambda: person_relative(links, code_list, 0),
  77. (0, 1, 1, 0): lambda: person_relative(links, code_list, 0),
  78. (0, 1, 1, 1): lambda: person_relative(links, code_list, 0),
  79. (1, 0, 0, 0): lambda: person_relative(links, code_list, 1),
  80. (1, 0, 0, 1): lambda: person_relative(links, code_list, 1),
  81. (1, 0, 1, 0): lambda: person_relative(links, code_list, 1),
  82. (1, 0, 1, 1): lambda: person_relative(links, code_list, 1),
  83. (1, 1, 0, 0): lambda: person_relative(links, code_list, (0, 1)),
  84. (1, 1, 0, 1): lambda: person_relative(links, code_list, (0, 1)),
  85. (1, 1, 1, 0): lambda: person_relative(links, code_list, (0, 1)),
  86. (1, 1, 1, 1): lambda: person_relative(links, code_list, (0, 1))
  87. }
  88. query = """
  89. MATCH (n:worker)-[r:relatives]-(m:worker), (n)-[:workin]-(wrk_n:workplace), (m)-[:workin]-(wrk_m:workplace)
  90. WHERE n.code IN $codes
  91. RETURN
  92. n.name as employee,
  93. id(n) as id_n,
  94. wrk_n.name as employee_workplace,
  95. id(wrk_n) as id_wrk_n,
  96. m.name as relatives,
  97. id(m) as id_m,
  98. wrk_m.name as relatives_workplace,
  99. id(wrk_m) as id_wrk_m,
  100. CASE WHEN exists(wrk_m.organization_no) THEN 1 ELSE 0 END as relatives_status
  101. """
  102. result = connect_graph.run(query, codes=code_list).data()
  103. handle_function = relation_dict.get(condition, [])
  104. for row in result:
  105. employee = row['employee']
  106. id_employee = row['id_n']
  107. employee_workplace = row['employee_workplace']
  108. id_employee_workplace = row['id_wrk_n']
  109. relatives = row['relatives']
  110. id_relatives = row['id_m']
  111. relatives_workplace = row['relatives_workplace']
  112. id_relatives_workplace = row['id_wrk_m']
  113. relatives_status = row['relatives_status']
  114. nodes.extend(create_node(employee, id_employee, 'selected'))
  115. nodes.extend(create_node(employee_workplace, id_employee_workplace,
  116. 'work_place_selected' if flag else 'internel_work_place'))
  117. links.extend(create_relation(id_employee, id_employee_workplace, 'work_in'))
  118. temp_node, temp_link = handle_condition(condition, relatives, id_relatives, relatives_workplace,
  119. id_relatives_workplace, relatives_status)
  120. nodes.extend(temp_node)
  121. links.extend(temp_link)
  122. if condition[0] != 0 or condition[1] != 0:
  123. links.extend(handle_function())
  124. return nodes, links
  125. # 处理不同筛选条件的节点/关系
  126. def handle_condition(condition, relatives, id_relatives, relatives_workplace, id_relatives_workplace, relatives_status):
  127. nodes = []
  128. links = []
  129. if condition == (0, 0, 0, 1):
  130. nodes.extend(
  131. create_node(relatives_workplace, id_relatives_workplace, '' if relatives_status else 'externel_work_place'))
  132. elif condition == (0, 0, 1, 0):
  133. nodes.extend(
  134. create_node(relatives_workplace, id_relatives_workplace, 'internel_work_place' if relatives_status else ''))
  135. elif condition == (0, 0, 1, 1):
  136. nodes.extend(
  137. create_node(relatives_workplace, id_relatives_workplace,
  138. 'internel_work_place' if relatives_status else 'externel_work_place'))
  139. elif condition == (0, 1, 0, 0):
  140. nodes.extend(create_node(relatives, id_relatives, 'external_relatives' if relatives_status == 0 else ''))
  141. elif condition == (0, 1, 0, 1):
  142. nodes.extend(create_node(relatives, id_relatives, '' if relatives_status else 'external_relatives'))
  143. nodes.extend(
  144. create_node(relatives_workplace, id_relatives_workplace, '' if relatives_status else 'externel_work_place'))
  145. links.extend(create_relation(id_relatives, id_relatives_workplace if relatives_status == 0 else '', 'work_in'))
  146. elif condition == (0, 1, 1, 0):
  147. nodes.extend(create_node(relatives, id_relatives, '' if relatives_status else 'external_relatives'))
  148. nodes.extend(
  149. create_node(relatives_workplace, id_relatives_workplace, 'internel_work_place' if relatives_status else ''))
  150. elif condition == (0, 1, 1, 1):
  151. nodes.extend(create_node(relatives, id_relatives, '' if relatives_status else 'external_relatives'))
  152. nodes.extend(
  153. create_node(relatives_workplace, id_relatives_workplace,
  154. 'internel_work_place' if relatives_status else 'externel_work_place'))
  155. links.extend(create_relation(id_relatives, id_relatives_workplace if relatives_status == 0 else '', 'work_in'))
  156. elif condition == (1, 0, 0, 0):
  157. nodes.extend(create_node(relatives, id_relatives, 'internal_relatives' if relatives_status else ''))
  158. elif condition == (1, 0, 0, 1):
  159. nodes.extend(create_node(relatives, id_relatives, 'internal_relatives' if relatives_status else ''))
  160. nodes.extend(
  161. create_node(relatives_workplace, id_relatives_workplace, '' if relatives_status else 'externel_work_place'))
  162. elif condition == (1, 0, 1, 0):
  163. nodes.extend(create_node(relatives, id_relatives, 'internal_relatives' if relatives_status else ''))
  164. nodes.extend(
  165. create_node(relatives_workplace, id_relatives_workplace, 'internel_work_place' if relatives_status else ''))
  166. links.extend(create_relation(id_relatives, id_relatives_workplace if relatives_status else '', 'work_in'))
  167. elif condition == (1, 0, 1, 1):
  168. nodes.extend(create_node(relatives, id_relatives, 'internal_relatives' if relatives_status else ''))
  169. nodes.extend(
  170. create_node(relatives_workplace, id_relatives_workplace,
  171. 'internel_work_place' if relatives_status else 'externel_work_place'))
  172. links.extend(create_relation(id_relatives, id_relatives_workplace if relatives_status else '', 'work_in'))
  173. elif condition == (1, 1, 0, 0):
  174. nodes.extend(
  175. create_node(relatives, id_relatives, 'internal_relatives' if relatives_status else 'external_relatives'))
  176. elif condition == (1, 1, 0, 1):
  177. nodes.extend(
  178. create_node(relatives, id_relatives, 'internal_relatives' if relatives_status else 'external_relatives'))
  179. nodes.extend(
  180. create_node(relatives_workplace, id_relatives_workplace,
  181. 'externel_work_place' if relatives_status == 0 else ''))
  182. links.extend(create_relation(id_relatives, id_relatives_workplace if relatives_status == 0 else '', 'work_in'))
  183. elif condition == (1, 1, 1, 0):
  184. nodes.extend(
  185. create_node(relatives, id_relatives, 'internal_relatives' if relatives_status else 'external_relatives'))
  186. nodes.extend(
  187. create_node(relatives_workplace, id_relatives_workplace, 'internel_work_place' if relatives_status else ''))
  188. links.extend(create_relation(id_relatives, id_relatives_workplace if relatives_status else '', 'work_in'))
  189. elif condition == (1, 1, 1, 1):
  190. nodes.extend(
  191. create_node(relatives, id_relatives, 'internal_relatives' if relatives_status else 'external_relatives'))
  192. nodes.extend(
  193. create_node(relatives_workplace, id_relatives_workplace,
  194. 'internel_work_place' if relatives_status else 'externel_work_place'))
  195. links.extend(create_relation(id_relatives, id_relatives_workplace, 'work_in'))
  196. return nodes, links
  197. # 创建节点
  198. def create_node(name, nodeid, node_type):
  199. if name in (None, '无') or node_type == '':
  200. return []
  201. return [{'name': name, 'id': nodeid, 'type': node_type}]
  202. # 创建关系
  203. def create_relation(start, end, relation_type):
  204. if end in (None, '无', ''):
  205. return []
  206. return [{"source": start, "target": end, "type": relation_type}]
  207. # 创建员工和亲属的关系
  208. def person_relative(links, code_list, status):
  209. query = """
  210. MATCH (n:worker)-[r:relatives]-(m:worker)
  211. WHERE n.code IN $codes
  212. {}
  213. RETURN id(STARTNODE(r)) AS startnode, r.content AS content, id(ENDNODE(r)) AS endnode
  214. """.format("WITH CASE WHEN exists(m.code) THEN 1 ELSE 0 END AS status,r "
  215. "WHERE status = $relatives_status" if isinstance(status, int) else "")
  216. result = connect_graph.run(query, codes=code_list, relatives_status=status).data()
  217. for row in result:
  218. startnode = row['startnode']
  219. endnode = row['endnode']
  220. content = row['content']
  221. links.extend(create_relation(startnode, endnode, content))
  222. return links