package_function.py 11 KB

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