package_function.py 11 KB

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