package_function.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. result = connect_graph.run(query).data()
  59. return result
  60. # 输入人员编码列表,得到员工与工作单位的关系,并且在此函数内完成员工,亲属,以及人-工作单位关系的创建
  61. def create_person_workplace(code_list, flag, relatives_type):
  62. nodes = []
  63. links = []
  64. condition = tuple(map(int, relatives_type.split(",")))
  65. relation_dict = {
  66. (0, 0, 0, 0): lambda: [],
  67. (0, 0, 0, 1): lambda: [],
  68. (0, 0, 1, 0): lambda: [],
  69. (0, 1, 0, 0): lambda: person_relative(links, code_list, 0),
  70. (0, 1, 0, 1): lambda: person_relative(links, code_list, 0),
  71. (0, 1, 1, 0): lambda: person_relative(links, code_list, 0),
  72. (0, 1, 1, 1): lambda: person_relative(links, code_list, 0),
  73. (1, 0, 0, 0): lambda: person_relative(links, code_list, 1),
  74. (1, 0, 0, 1): lambda: person_relative(links, code_list, 1),
  75. (1, 0, 1, 0): lambda: person_relative(links, code_list, 1),
  76. (1, 0, 1, 1): lambda: person_relative(links, code_list, 1),
  77. (1, 1, 0, 0): lambda: person_relative(links, code_list, (0, 1)),
  78. (1, 1, 0, 1): lambda: person_relative(links, code_list, (0, 1)),
  79. (1, 1, 1, 0): lambda: person_relative(links, code_list, (0, 1)),
  80. (1, 1, 1, 1): lambda: person_relative(links, code_list, (0, 1))
  81. }
  82. query = """
  83. MATCH (n:worker)-[r:relatives]-(m:worker), (n)-[:workin]-(wrk_n:workplace), (m)-[:workin]-(wrk_m:workplace)
  84. WHERE n.code IN $codes
  85. RETURN
  86. n.name as employee,
  87. id(n) as id_n,
  88. wrk_n.name as employee_workplace,
  89. id(wrk_n) as id_wrk_n,
  90. m.name as relatives,
  91. id(m) as id_m,
  92. wrk_m.name as relatives_workplace,
  93. id(wrk_m) as id_wrk_m,
  94. CASE WHEN exists(wrk_m.organization_no) THEN 1 ELSE 0 END as relatives_status
  95. """
  96. result = connect_graph.run(query, codes=code_list).data()
  97. handle_function = relation_dict.get(condition, [])
  98. for row in result:
  99. employee = row['employee']
  100. id_employee = row['id_n']
  101. employee_workplace = row['employee_workplace']
  102. id_employee_workplace = row['id_wrk_n']
  103. relatives = row['relatives']
  104. id_relatives = row['id_m']
  105. relatives_workplace = row['relatives_workplace']
  106. id_relatives_workplace = row['id_wrk_m']
  107. relatives_status = row['relatives_status']
  108. nodes.extend(create_node(employee, id_employee, 'selected'))
  109. nodes.extend(create_node(employee_workplace, id_employee_workplace,
  110. 'work_place_selected' if flag else 'internel_work_place'))
  111. links.extend(create_relation(id_employee, id_employee_workplace, 'work_in'))
  112. temp_node, temp_link = handle_condition(condition, relatives, id_relatives, relatives_workplace,
  113. id_relatives_workplace, relatives_status)
  114. nodes.extend(temp_node)
  115. links.extend(temp_link)
  116. if condition[0] != 0 or condition[1] != 0:
  117. links.extend(handle_function())
  118. return nodes, links
  119. # 处理不同筛选条件的节点/关系
  120. def handle_condition(condition, relatives, id_relatives, relatives_workplace, id_relatives_workplace, relatives_status):
  121. nodes = []
  122. links = []
  123. if condition == (0, 0, 0, 1):
  124. nodes.extend(
  125. create_node(relatives_workplace, id_relatives_workplace, '' if relatives_status else 'externel_work_place'))
  126. elif condition == (0, 0, 1, 0):
  127. nodes.extend(
  128. create_node(relatives_workplace, id_relatives_workplace, 'internel_work_place' if relatives_status else ''))
  129. elif condition == (0, 0, 1, 1):
  130. nodes.extend(
  131. create_node(relatives_workplace, id_relatives_workplace,
  132. 'internel_work_place' if relatives_status else 'externel_work_place'))
  133. elif condition == (0, 1, 0, 0):
  134. nodes.extend(create_node(relatives, id_relatives, 'external_relatives' if relatives_status == 0 else ''))
  135. elif condition == (0, 1, 0, 1):
  136. nodes.extend(create_node(relatives, id_relatives, '' if relatives_status else 'external_relatives'))
  137. nodes.extend(
  138. create_node(relatives_workplace, id_relatives_workplace, '' if relatives_status else 'externel_work_place'))
  139. links.extend(create_relation(id_relatives, id_relatives_workplace if relatives_status == 0 else '', 'work_in'))
  140. elif condition == (0, 1, 1, 0):
  141. nodes.extend(create_node(relatives, id_relatives, '' if relatives_status else 'external_relatives'))
  142. nodes.extend(
  143. create_node(relatives_workplace, id_relatives_workplace, 'internel_work_place' if relatives_status else ''))
  144. elif condition == (0, 1, 1, 1):
  145. nodes.extend(create_node(relatives, id_relatives, '' if relatives_status else 'external_relatives'))
  146. nodes.extend(
  147. create_node(relatives_workplace, id_relatives_workplace,
  148. 'internel_work_place' if relatives_status else 'externel_work_place'))
  149. links.extend(create_relation(id_relatives, id_relatives_workplace if relatives_status == 0 else '', 'work_in'))
  150. elif condition == (1, 0, 0, 0):
  151. nodes.extend(create_node(relatives, id_relatives, 'internal_relatives' if relatives_status else ''))
  152. elif condition == (1, 0, 0, 1):
  153. nodes.extend(create_node(relatives, id_relatives, 'internal_relatives' if relatives_status else ''))
  154. nodes.extend(
  155. create_node(relatives_workplace, id_relatives_workplace, '' if relatives_status else 'externel_work_place'))
  156. elif condition == (1, 0, 1, 0):
  157. nodes.extend(create_node(relatives, id_relatives, 'internal_relatives' if relatives_status else ''))
  158. nodes.extend(
  159. create_node(relatives_workplace, id_relatives_workplace, 'internel_work_place' if relatives_status else ''))
  160. links.extend(create_relation(id_relatives, id_relatives_workplace if relatives_status else '', 'work_in'))
  161. elif condition == (1, 0, 1, 1):
  162. nodes.extend(create_node(relatives, id_relatives, 'internal_relatives' if relatives_status else ''))
  163. nodes.extend(
  164. create_node(relatives_workplace, id_relatives_workplace,
  165. 'internel_work_place' if relatives_status else 'externel_work_place'))
  166. links.extend(create_relation(id_relatives, id_relatives_workplace if relatives_status else '', 'work_in'))
  167. elif condition == (1, 1, 0, 0):
  168. nodes.extend(
  169. create_node(relatives, id_relatives, 'internal_relatives' if relatives_status else 'external_relatives'))
  170. elif condition == (1, 1, 0, 1):
  171. nodes.extend(
  172. create_node(relatives, id_relatives, 'internal_relatives' if relatives_status else 'external_relatives'))
  173. nodes.extend(
  174. create_node(relatives_workplace, id_relatives_workplace,
  175. 'externel_work_place' if relatives_status == 0 else ''))
  176. links.extend(create_relation(id_relatives, id_relatives_workplace if relatives_status == 0 else '', 'work_in'))
  177. elif condition == (1, 1, 1, 0):
  178. nodes.extend(
  179. create_node(relatives, id_relatives, 'internal_relatives' if relatives_status else 'external_relatives'))
  180. nodes.extend(
  181. create_node(relatives_workplace, id_relatives_workplace, 'internel_work_place' if relatives_status else ''))
  182. links.extend(create_relation(id_relatives, id_relatives_workplace if relatives_status else '', 'work_in'))
  183. elif condition == (1, 1, 1, 1):
  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,
  188. 'internel_work_place' if relatives_status else 'externel_work_place'))
  189. links.extend(create_relation(id_relatives, id_relatives_workplace, 'work_in'))
  190. return nodes, links
  191. # 创建节点
  192. def create_node(name, nodeid, node_type):
  193. if name in (None, '无') or node_type == '':
  194. return []
  195. return [{'name': name, 'id': nodeid, 'type': node_type}]
  196. # 创建关系
  197. def create_relation(start, end, relation_type):
  198. if end in (None, '无', ''):
  199. return []
  200. return [{"source": start, "target": end, "type": relation_type}]
  201. # 创建员工和亲属的关系
  202. def person_relative(links, code_list, status):
  203. query = """
  204. MATCH (n:worker)-[r:relatives]-(m:worker)
  205. WHERE n.code IN $codes
  206. {}
  207. RETURN id(STARTNODE(r)) AS startnode, r.content AS content, id(ENDNODE(r)) AS endnode
  208. """.format("WITH CASE WHEN exists(m.code) THEN 1 ELSE 0 END AS status,r "
  209. "WHERE status = $relatives_status" if isinstance(status, int) else "")
  210. result = connect_graph.run(query, codes=code_list, relatives_status=status).data()
  211. for row in result:
  212. startnode = row['startnode']
  213. endnode = row['endnode']
  214. content = row['content']
  215. links.extend(create_relation(startnode, endnode, content))
  216. return links