routes.py 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  1. import io
  2. import logging
  3. from flask import current_app, jsonify, request, send_file
  4. from minio import Minio
  5. from minio.error import S3Error
  6. from sqlalchemy import or_
  7. from app import db
  8. from app.api.meta_data import bp
  9. from app.core.meta_data import (
  10. check_redundancy_for_add,
  11. check_redundancy_for_update,
  12. convert_tag_ids_to_tags,
  13. get_file_content,
  14. get_formatted_time,
  15. handle_id_unstructured,
  16. handle_txt_graph,
  17. meta_impact_graph,
  18. meta_kinship_graph,
  19. meta_list,
  20. normalize_tag_inputs,
  21. parse_text,
  22. solve_unstructured_data,
  23. text_resource_solve,
  24. )
  25. from app.core.system.auth import require_auth
  26. from app.models.metadata_review import (
  27. MetadataReviewRecord,
  28. MetadataVersionHistory,
  29. update_review_record_resolution,
  30. )
  31. from app.models.result import failed, success
  32. from app.services.neo4j_driver import neo4j_driver
  33. logger = logging.getLogger("app")
  34. def get_minio_client():
  35. """获取 MinIO 客户端实例"""
  36. return Minio(
  37. current_app.config["MINIO_HOST"],
  38. access_key=current_app.config["MINIO_USER"],
  39. secret_key=current_app.config["MINIO_PASSWORD"],
  40. secure=current_app.config["MINIO_SECURE"],
  41. )
  42. def get_minio_config():
  43. """获取 MinIO 配置"""
  44. return {
  45. "MINIO_BUCKET": current_app.config["MINIO_BUCKET"],
  46. "PREFIX": current_app.config["PREFIX"],
  47. "ALLOWED_EXTENSIONS": current_app.config["ALLOWED_EXTENSIONS"],
  48. }
  49. def allowed_file(filename):
  50. """检查文件扩展名是否允许"""
  51. if "." not in filename:
  52. return False
  53. ext = filename.rsplit(".", 1)[1].lower()
  54. return ext in get_minio_config()["ALLOWED_EXTENSIONS"]
  55. # 元数据列表
  56. @bp.route("/node/list", methods=["POST"])
  57. def meta_node_list():
  58. try:
  59. payload = request.get_json() or {}
  60. if not isinstance(payload, dict):
  61. return jsonify(failed("请求数据格式错误,应为 JSON 对象"))
  62. def to_int(value, default):
  63. try:
  64. return int(value)
  65. except (TypeError, ValueError):
  66. return default
  67. # 分页参数
  68. page = to_int(payload.get("current", 1), 1)
  69. page_size = to_int(payload.get("size", 10), 10)
  70. # 过滤参数
  71. name_en_filter = payload.get("name_en") or None
  72. name_zh_filter = payload.get("name_zh") or None
  73. category_filter = payload.get("category") or None
  74. time_filter = payload.get("time") or None
  75. logger.info(
  76. f"[node/list] 过滤参数: name_zh={name_zh_filter}, "
  77. f"name_en={name_en_filter}, category={category_filter}"
  78. )
  79. tag_filter = payload.get("tag")
  80. if tag_filter is not None and not isinstance(tag_filter, list):
  81. tag_filter = None
  82. # 调用核心业务逻辑
  83. result, total_count = meta_list(
  84. page,
  85. page_size,
  86. "",
  87. name_en_filter,
  88. name_zh_filter,
  89. category_filter,
  90. time_filter,
  91. tag_filter,
  92. )
  93. # 返回结果
  94. return jsonify(
  95. success(
  96. {
  97. "records": result,
  98. "total": total_count,
  99. "size": page_size,
  100. "current": page,
  101. }
  102. )
  103. )
  104. except Exception as e:
  105. logger.error(f"获取元数据列表失败: {str(e)}")
  106. return jsonify(failed(str(e)))
  107. # 元数据图谱
  108. @bp.route("/node/graph", methods=["POST"])
  109. def meta_node_graph():
  110. try:
  111. if not request.json:
  112. return jsonify(failed("请求数据不能为空"))
  113. # 从请求中获取节点ID
  114. node_id = request.json.get("nodeId")
  115. if node_id is None:
  116. return jsonify(failed("nodeId 不能为空"))
  117. try:
  118. node_id_int = int(node_id)
  119. except (TypeError, ValueError):
  120. return jsonify(failed("nodeId 必须为整数"))
  121. # 调用核心业务逻辑
  122. graph = meta_kinship_graph(node_id_int)
  123. is_dict = isinstance(graph, dict)
  124. nodes = graph.get("nodes", []) if is_dict else []
  125. relationships = graph.get("relationships", []) if is_dict else []
  126. # 当前节点属性
  127. node_info = next(
  128. (n for n in nodes if n.get("id") == node_id_int),
  129. {},
  130. )
  131. # 关联节点(包含属性,便于前端展示名称等)
  132. related_nodes = [n for n in nodes if n.get("id") != node_id_int]
  133. payload = {
  134. "node": node_info,
  135. "related_nodes": related_nodes,
  136. "relationships": relationships,
  137. }
  138. return jsonify(success(payload))
  139. except Exception as e:
  140. logger.error(f"获取元数据图谱失败: {str(e)}")
  141. return jsonify(failed(str(e)))
  142. # 删除元数据
  143. @bp.route("/node/delete", methods=["POST"])
  144. def meta_node_delete():
  145. try:
  146. if not request.json:
  147. return jsonify(failed("请求数据不能为空"))
  148. # 从请求中获取节点ID
  149. node_id = request.json.get("id")
  150. # 删除节点逻辑
  151. with neo4j_driver.get_session() as session:
  152. cypher = "MATCH (n) WHERE id(n) = $node_id DETACH DELETE n"
  153. session.run(cypher, node_id=int(node_id))
  154. # 返回结果
  155. return jsonify(success({}))
  156. except Exception as e:
  157. logger.error(f"删除元数据失败: {str(e)}")
  158. return jsonify(failed(str(e)))
  159. # 编辑元数据
  160. @bp.route("/node/edit", methods=["POST"])
  161. def meta_node_edit():
  162. try:
  163. if not request.json:
  164. return jsonify(failed("请求数据不能为空"))
  165. # 从请求中获取节点ID
  166. node_id = request.json.get("id")
  167. if not node_id:
  168. return jsonify(failed("节点ID不能为空"))
  169. # 获取节点
  170. with neo4j_driver.get_session() as session:
  171. # 查询节点信息
  172. cypher = """
  173. MATCH (n:DataMeta)
  174. WHERE id(n) = $node_id
  175. RETURN n
  176. """
  177. result = session.run(cypher, node_id=int(node_id))
  178. node = result.single()
  179. if not node or not node["n"]:
  180. return jsonify(failed("节点不存在"))
  181. # 获取节点数据
  182. node_data = dict(node["n"])
  183. node_data["id"] = node["n"].id
  184. # 获取标签信息
  185. tag_cypher = """
  186. MATCH (n:DataMeta)-[:LABEL]->(t:DataLabel)
  187. WHERE id(n) = $node_id
  188. RETURN t
  189. """
  190. tag_result = session.run(tag_cypher, node_id=int(node_id))
  191. tags: list[dict] = []
  192. for record in tag_result:
  193. tag_node = record.get("t")
  194. if tag_node:
  195. tags.append(
  196. {
  197. "id": tag_node.id,
  198. "name_zh": tag_node.get("name_zh", ""),
  199. "name_en": tag_node.get("name_en", ""),
  200. }
  201. )
  202. # 获取主数据信息
  203. master_data_cypher = """
  204. MATCH (n:DataMeta)-[:master_data]->(m:master_data)
  205. WHERE id(n) = $node_id
  206. RETURN m
  207. """
  208. master_data_result = session.run(master_data_cypher, node_id=int(node_id))
  209. master_data = master_data_result.single()
  210. # 构建返回数据
  211. response_data = [
  212. {
  213. "master_data": (
  214. master_data["m"].id
  215. if master_data and master_data["m"]
  216. else None
  217. ),
  218. "name_zh": node_data.get("name_zh", ""),
  219. "name_en": node_data.get("name_en", ""),
  220. "create_time": node_data.get("create_time", ""),
  221. "update_time": node_data.get("update_time", ""),
  222. "status": bool(node_data.get("status", True)),
  223. "data_type": node_data.get("data_type", ""),
  224. "tag": tags,
  225. "affiliation": node_data.get("affiliation"),
  226. "category": node_data.get("category"),
  227. "alias": node_data.get("alias"),
  228. "describe": node_data.get("describe"),
  229. }
  230. ]
  231. logger.info(f"成功获取元数据节点: ID={node_data['id']}")
  232. return jsonify(success(response_data))
  233. except Exception as e:
  234. logger.error(f"获取元数据节点失败: {str(e)}")
  235. return jsonify(failed(str(e)))
  236. # 增加元数据
  237. @bp.route("/check", methods=["GET"])
  238. def meta_check():
  239. """
  240. 检查元数据中文名是否已存在
  241. 请求参数:
  242. - name_zh: 元数据中文名(URL参数)
  243. 返回:
  244. - exists: true/false 表示是否存在
  245. """
  246. try:
  247. name_zh = request.args.get("name_zh")
  248. if not name_zh:
  249. return jsonify(failed("缺少name_zh参数"))
  250. # 查询数据库检查是否存在
  251. with neo4j_driver.get_session() as session:
  252. cypher = """
  253. MATCH (n:DataMeta {name_zh: $name_zh})
  254. RETURN count(n) > 0 as exists
  255. """
  256. result = session.run(cypher, name_zh=name_zh)
  257. record = result.single()
  258. if record:
  259. exists = record["exists"]
  260. logger.info(f"检查元数据 '{name_zh}': {'存在' if exists else '不存在'}")
  261. return jsonify(
  262. success({"exists": exists, "name_zh": name_zh}, "查询成功")
  263. )
  264. else:
  265. return jsonify(
  266. success({"exists": False, "name_zh": name_zh}, "查询成功")
  267. )
  268. except Exception as e:
  269. logger.error(f"检查元数据失败: {str(e)}")
  270. return jsonify(failed(f"检查失败: {str(e)}"))
  271. @bp.route("/node/add", methods=["POST"])
  272. def meta_node_add():
  273. """
  274. 新增元数据节点
  275. 在创建前会进行冗余检测:
  276. - 如果存在完全匹配的元数据,返回已存在的节点信息
  277. - 如果存在疑似重复的元数据,创建审核记录并返回提示
  278. - 如果无重复,正常创建新节点
  279. """
  280. try:
  281. if not request.json:
  282. return jsonify(failed("请求数据不能为空"))
  283. # 从请求中获取节点信息
  284. node_name_zh = request.json.get("name_zh")
  285. node_type = request.json.get("data_type")
  286. node_category = request.json.get("category")
  287. node_alias = request.json.get("alias")
  288. node_affiliation = request.json.get("affiliation")
  289. node_tag = request.json.get("tag")
  290. node_desc = request.json.get("describe")
  291. node_status = bool(request.json.get("status", True))
  292. node_name_en = request.json.get("name_en")
  293. # 是否强制创建(跳过冗余检测)
  294. force_create = bool(request.json.get("force_create", False))
  295. if not node_name_zh:
  296. return jsonify(failed("节点名称不能为空"))
  297. if not node_type:
  298. return jsonify(failed("节点类型不能为空"))
  299. # 统一处理标签ID
  300. tag_ids = normalize_tag_inputs(node_tag)
  301. # ========== 冗余检测 ==========
  302. has_suspicious_duplicates = False
  303. suspicious_candidates = []
  304. if not force_create:
  305. redundancy_result = check_redundancy_for_add(
  306. name_zh=node_name_zh,
  307. name_en=node_name_en or "",
  308. data_type=node_type,
  309. tag_ids=tag_ids,
  310. )
  311. # 存在完全匹配的元数据,直接返回,不做任何操作
  312. if redundancy_result["has_exact_match"]:
  313. exact_id = redundancy_result["exact_match_id"]
  314. logger.info(
  315. f"元数据已存在(完全匹配): name_zh={node_name_zh}, "
  316. f"existing_id={exact_id}"
  317. )
  318. return jsonify(
  319. failed(
  320. f"元数据已存在(完全匹配),无需重复创建。"
  321. f"已存在的元数据ID: {exact_id}"
  322. )
  323. )
  324. # 存在疑似重复的元数据,标记状态,稍后创建节点后再写入审核记录
  325. if redundancy_result["has_candidates"]:
  326. has_suspicious_duplicates = True
  327. suspicious_candidates = redundancy_result["candidates"]
  328. logger.info(
  329. f"发现疑似重复元数据: name_zh={node_name_zh}, "
  330. f"候选数量={len(suspicious_candidates)}"
  331. )
  332. # ========== 创建节点 ==========
  333. with neo4j_driver.get_session() as session:
  334. cypher = """
  335. MERGE (n:DataMeta {name_zh: $name_zh})
  336. ON CREATE SET n.name_en = $name_en,
  337. n.data_type = $data_type,
  338. n.category = $category,
  339. n.alias = $alias,
  340. n.affiliation = $affiliation,
  341. n.describe = $describe,
  342. n.create_time = $create_time,
  343. n.updateTime = $update_time,
  344. n.status = $status,
  345. n.name_en = $name_en
  346. ON MATCH SET n.data_type = $data_type,
  347. n.category = $category,
  348. n.alias = $alias,
  349. n.affiliation = $affiliation,
  350. n.describe = $describe,
  351. n.updateTime = $update_time,
  352. n.status = $status,
  353. n.name_en = $name_en
  354. RETURN n
  355. """
  356. create_time = update_time = get_formatted_time()
  357. result = session.run(
  358. cypher,
  359. name_zh=node_name_zh,
  360. data_type=node_type,
  361. category=node_category,
  362. alias=node_alias,
  363. affiliation=node_affiliation,
  364. describe=node_desc,
  365. create_time=create_time,
  366. update_time=update_time,
  367. status=node_status,
  368. name_en=node_name_en,
  369. )
  370. node = result.single()
  371. if node and node["n"]:
  372. node_data = dict(node["n"])
  373. node_data["id"] = node["n"].id
  374. # 如果提供了标签列表,创建标签关系
  375. tag_nodes = []
  376. if tag_ids:
  377. for tag_id in tag_ids:
  378. # 获取标签节点信息
  379. tag_fetch = session.run(
  380. "MATCH (t:DataLabel) WHERE id(t) = $tag_id RETURN t",
  381. tag_id=tag_id,
  382. ).single()
  383. if not tag_fetch or not tag_fetch.get("t"):
  384. logger.warning(f"未找到标签节点: {tag_id}")
  385. continue
  386. tag_node = tag_fetch["t"]
  387. tag_nodes.append(
  388. {
  389. "id": tag_node.id,
  390. "name_zh": tag_node.get("name_zh", ""),
  391. "name_en": tag_node.get("name_en", ""),
  392. }
  393. )
  394. tag_cypher = """
  395. MATCH (n:DataMeta), (t:DataLabel)
  396. WHERE id(n) = $node_id AND id(t) = $tag_id
  397. MERGE (n)-[r:LABEL]->(t)
  398. RETURN r
  399. """
  400. session.run(tag_cypher, node_id=node["n"].id, tag_id=tag_id)
  401. node_data["tag"] = tag_nodes
  402. logger.info(
  403. f"成功创建或更新元数据节点: "
  404. f"ID={node_data['id']}, name={node_name_zh}"
  405. )
  406. # ========== 处理疑似重复情况 ==========
  407. # 如果存在疑似重复,创建审核记录
  408. if has_suspicious_duplicates and suspicious_candidates:
  409. from app.core.meta_data.redundancy_check import (
  410. write_redundancy_review_record_with_new_id,
  411. )
  412. # 构建新元数据快照(包含新创建的节点ID)
  413. new_meta_snapshot = {
  414. "id": node_data["id"],
  415. "name_zh": node_name_zh,
  416. "name_en": node_name_en or "",
  417. "data_type": node_type,
  418. "tag_ids": tag_ids,
  419. }
  420. # 写入审核记录
  421. write_redundancy_review_record_with_new_id(
  422. new_meta=new_meta_snapshot,
  423. candidates=suspicious_candidates,
  424. source="api",
  425. )
  426. # 返回成功创建,但提示疑似重复
  427. candidate_names = [
  428. c.get("name_zh", "") for c in suspicious_candidates[:3]
  429. ]
  430. return jsonify(
  431. success(
  432. node_data,
  433. message=(
  434. f"元数据创建成功,但发现疑似重复元数据。"
  435. f"疑似重复: {', '.join(candidate_names)}。"
  436. f"已创建审核记录,请前往元数据审核页面进行处理。"
  437. ),
  438. )
  439. )
  440. return jsonify(success(node_data))
  441. else:
  442. logger.error(f"创建元数据节点失败: {node_name_zh}")
  443. return jsonify(failed("创建元数据节点失败"))
  444. except Exception as e:
  445. logger.error(f"添加元数据失败: {str(e)}")
  446. return jsonify(failed(str(e)))
  447. # 搜索元数据
  448. @bp.route("/search", methods=["GET"])
  449. def search_metadata_route():
  450. try:
  451. keyword = request.args.get("keyword", "")
  452. if not keyword:
  453. return jsonify(success([]))
  454. cypher = """
  455. MATCH (n:DataMeta)
  456. WHERE n.name_zh CONTAINS $keyword
  457. RETURN n LIMIT 100
  458. """
  459. with neo4j_driver.get_session() as session:
  460. result = session.run(cypher, keyword=keyword)
  461. metadata_list = [dict(record["n"]) for record in result]
  462. return jsonify(success(metadata_list))
  463. except Exception as e:
  464. logger.error(f"搜索元数据失败: {str(e)}")
  465. return jsonify(failed(str(e)))
  466. # 全文检索查询
  467. @bp.route("/full/text/query", methods=["POST"])
  468. def full_text_query():
  469. try:
  470. if not request.json:
  471. return jsonify(failed("请求数据不能为空"))
  472. # 获取查询条件
  473. search_term = request.json.get("query", "")
  474. if not search_term:
  475. return jsonify(failed("查询条件不能为空"))
  476. # 执行Neo4j全文索引查询
  477. with neo4j_driver.get_session() as session:
  478. cypher = """
  479. CALL db.index.fulltext.queryNodes("DataMetaFulltext", $term)
  480. YIELD node, score
  481. RETURN node, score
  482. ORDER BY score DESC
  483. LIMIT 20
  484. """
  485. result = session.run(cypher, term=search_term)
  486. # 处理查询结果
  487. search_results = []
  488. for record in result:
  489. node_data = dict(record["node"])
  490. node_data["id"] = record["node"].id
  491. node_data["score"] = record["score"]
  492. search_results.append(node_data)
  493. return jsonify(success(search_results))
  494. except Exception as e:
  495. logger.error(f"全文检索查询失败: {str(e)}")
  496. return jsonify(failed(str(e)))
  497. # 非结构化文本查询
  498. @bp.route("/unstructure/text/query", methods=["POST"])
  499. def unstructure_text_query():
  500. try:
  501. if not request.json:
  502. return jsonify(failed("请求数据不能为空"))
  503. # 获取查询参数
  504. node_id = request.json.get("id")
  505. if not node_id:
  506. return jsonify(failed("节点ID不能为空"))
  507. # 获取节点信息
  508. node_data = handle_id_unstructured(node_id)
  509. if not node_data:
  510. return jsonify(failed("节点不存在"))
  511. # 获取对象路径
  512. object_name = node_data.get("url")
  513. if not object_name:
  514. return jsonify(failed("文档路径不存在"))
  515. # 获取 MinIO 配置
  516. minio_client = get_minio_client()
  517. config = get_minio_config()
  518. bucket_name = config["MINIO_BUCKET"]
  519. # 从MinIO获取文件内容
  520. file_content = get_file_content(minio_client, bucket_name, object_name)
  521. # 解析文本内容
  522. parsed_data = parse_text(file_content)
  523. # 返回结果
  524. result = {
  525. "node": node_data,
  526. "parsed": parsed_data,
  527. "content": (
  528. file_content[:1000] + "..."
  529. if len(file_content) > 1000
  530. else file_content
  531. ),
  532. }
  533. return jsonify(success(result))
  534. except Exception as e:
  535. logger.error(f"非结构化文本查询失败: {str(e)}")
  536. return jsonify(failed(str(e)))
  537. # 文件上传
  538. @bp.route("/resource/upload", methods=["POST"])
  539. def upload_file():
  540. try:
  541. # 检查请求中是否有文件
  542. if "file" not in request.files:
  543. return jsonify(failed("没有找到上传的文件"))
  544. file = request.files["file"]
  545. # 检查文件名
  546. if not file.filename:
  547. return jsonify(failed("未选择文件"))
  548. # 保存文件名到本地变量(确保类型安全)
  549. filename = file.filename
  550. # 检查文件类型
  551. if not allowed_file(filename):
  552. return jsonify(failed("不支持的文件类型"))
  553. # 获取 MinIO 配置
  554. minio_client = get_minio_client()
  555. config = get_minio_config()
  556. # 上传到MinIO
  557. file_content = file.read()
  558. file_size = len(file_content)
  559. file_type = filename.rsplit(".", 1)[1].lower()
  560. # 提取文件名(不包含扩展名)
  561. filename_without_ext = filename.rsplit(".", 1)[0]
  562. # 生成紧凑的时间戳 (yyyyMMddHHmmss)
  563. import time
  564. timestamp = time.strftime("%Y%m%d%H%M%S", time.localtime())
  565. # 生成唯一文件名
  566. object_name = (
  567. f"{config['PREFIX']}/{filename_without_ext}_{timestamp}.{file_type}"
  568. )
  569. # 上传文件
  570. minio_client.put_object(
  571. config["MINIO_BUCKET"],
  572. object_name,
  573. io.BytesIO(file_content),
  574. file_size,
  575. content_type=f"application/{file_type}",
  576. )
  577. # 返回结果
  578. return jsonify(
  579. success(
  580. {
  581. "filename": file.filename,
  582. "size": file_size,
  583. "type": file_type,
  584. "url": object_name,
  585. }
  586. )
  587. )
  588. except Exception as e:
  589. logger.error(f"文件上传失败: {str(e)}")
  590. return jsonify(failed(str(e)))
  591. # 文件下载显示
  592. @bp.route("/resource/display", methods=["POST"])
  593. def upload_file_display():
  594. response = None
  595. try:
  596. if not request.json:
  597. return jsonify(failed("请求数据不能为空"))
  598. object_name = request.json.get("url")
  599. if not object_name:
  600. return jsonify(failed("文件路径不能为空"))
  601. # 获取 MinIO 配置
  602. minio_client = get_minio_client()
  603. config = get_minio_config()
  604. # 获取文件内容
  605. response = minio_client.get_object(config["MINIO_BUCKET"], object_name)
  606. file_data = response.read()
  607. # 获取文件名
  608. file_name = object_name.split("/")[-1]
  609. # 确定文件类型
  610. file_extension = file_name.split(".")[-1].lower()
  611. # 为不同文件类型设置合适的MIME类型
  612. mime_types = {
  613. "pdf": "application/pdf",
  614. "doc": "application/msword",
  615. "docx": (
  616. "application/vnd.openxmlformats-"
  617. "officedocument.wordprocessingml.document"
  618. ),
  619. "xls": "application/vnd.ms-excel",
  620. "xlsx": (
  621. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  622. ),
  623. "txt": "text/plain",
  624. "csv": "text/csv",
  625. }
  626. content_type = mime_types.get(file_extension, "application/octet-stream")
  627. # 返回结果
  628. return jsonify(
  629. success(
  630. {
  631. "filename": file_name,
  632. "type": file_extension,
  633. "contentType": content_type,
  634. "size": len(file_data),
  635. "url": f"/api/meta/resource/download?url={object_name}",
  636. }
  637. )
  638. )
  639. except S3Error as e:
  640. logger.error(f"MinIO操作失败: {str(e)}")
  641. return jsonify(failed(f"文件访问失败: {str(e)}"))
  642. except Exception as e:
  643. logger.error(f"文件显示信息获取失败: {str(e)}")
  644. return jsonify(failed(str(e)))
  645. finally:
  646. if response:
  647. response.close()
  648. response.release_conn()
  649. # 文件下载接口
  650. @bp.route("/resource/download", methods=["GET"])
  651. def download_file():
  652. response = None
  653. try:
  654. object_name = request.args.get("url")
  655. if not object_name:
  656. return jsonify(failed("文件路径不能为空"))
  657. # URL解码,处理特殊字符
  658. import urllib.parse
  659. object_name = urllib.parse.unquote(object_name)
  660. # 记录下载请求信息,便于调试
  661. logger.info(f"下载文件请求: {object_name}")
  662. # 获取 MinIO 配置
  663. minio_client = get_minio_client()
  664. config = get_minio_config()
  665. # 获取文件
  666. try:
  667. response = minio_client.get_object(config["MINIO_BUCKET"], object_name)
  668. file_data = response.read()
  669. except S3Error as e:
  670. logger.error(f"MinIO获取文件失败: {str(e)}")
  671. return jsonify(failed(f"文件获取失败: {str(e)}"))
  672. # 获取文件名,并处理特殊字符
  673. file_name = object_name.split("/")[-1]
  674. # 直接从内存返回文件,不创建临时文件
  675. file_stream = io.BytesIO(file_data)
  676. # 返回文件
  677. return send_file(
  678. file_stream,
  679. as_attachment=True,
  680. download_name=file_name,
  681. mimetype="application/octet-stream",
  682. )
  683. except Exception as e:
  684. logger.error(f"文件下载失败: {str(e)}")
  685. return jsonify(failed(str(e)))
  686. finally:
  687. if response:
  688. response.close()
  689. response.release_conn()
  690. # 文本资源翻译
  691. @bp.route("/resource/translate", methods=["POST"])
  692. def text_resource_translate():
  693. try:
  694. if not request.json:
  695. return jsonify(failed("请求数据不能为空"))
  696. # 获取参数
  697. name_zh = request.json.get("name_zh", "")
  698. keyword = request.json.get("keyword", "")
  699. if not name_zh:
  700. return jsonify(failed("名称不能为空"))
  701. # 调用资源处理逻辑
  702. result = text_resource_solve(None, name_zh, keyword)
  703. return jsonify(success(result))
  704. except Exception as e:
  705. logger.error(f"文本资源翻译失败: {str(e)}")
  706. return jsonify(failed(str(e)))
  707. # 创建文本资源节点
  708. @bp.route("/resource/node", methods=["POST"])
  709. def text_resource_node():
  710. try:
  711. if not request.json:
  712. return jsonify(failed("请求数据不能为空"))
  713. # 获取参数
  714. name_zh = request.json.get("name_zh", "")
  715. name_en = request.json.get("name_en", "")
  716. keywords = request.json.get("keywords", [])
  717. keywords_en = request.json.get("keywords_en", [])
  718. object_name = request.json.get("url", "")
  719. if not name_zh or not name_en or not object_name:
  720. return jsonify(failed("参数不完整"))
  721. # 创建节点
  722. with neo4j_driver.get_session() as session:
  723. # 创建资源节点
  724. cypher = """
  725. CREATE (n:DataMeta {
  726. name_zh: $name_zh,
  727. name_en: $name_en,
  728. keywords: $keywords,
  729. keywords_en: $keywords_en,
  730. url: $object_name,
  731. create_time: $create_time,
  732. updateTime: $update_time
  733. })
  734. RETURN n
  735. """
  736. create_time = update_time = get_formatted_time()
  737. result = session.run(
  738. cypher,
  739. name_zh=name_zh,
  740. name_en=name_en,
  741. keywords=keywords,
  742. keywords_en=keywords_en,
  743. object_name=object_name,
  744. create_time=create_time,
  745. update_time=update_time,
  746. )
  747. record = result.single()
  748. if not record:
  749. return jsonify(failed("创建节点失败"))
  750. node = record["n"]
  751. # 为每个关键词创建标签节点并关联
  752. for i, keyword in enumerate(keywords):
  753. if keyword:
  754. # 创建标签节点
  755. tag_cypher = """
  756. MERGE (t:Tag {name_zh: $name_zh})
  757. ON CREATE SET t.name_en = $name_en,
  758. t.create_time = $create_time
  759. RETURN t
  760. """
  761. tag_result = session.run(
  762. tag_cypher,
  763. name_zh=keyword,
  764. name_en=keywords_en[i] if i < len(keywords_en) else "",
  765. create_time=create_time,
  766. )
  767. tag_record = tag_result.single()
  768. if not tag_record:
  769. continue
  770. tag_node = tag_record["t"]
  771. # 创建关系
  772. rel_cypher = """
  773. MATCH (n), (t)
  774. WHERE id(n) = $node_id AND id(t) = $tag_id
  775. CREATE (n)-[r:HAS_TAG]->(t)
  776. RETURN r
  777. """
  778. session.run(rel_cypher, node_id=node.id, tag_id=tag_node.id)
  779. # 返回创建的节点
  780. return jsonify(success(dict(node)))
  781. except Exception as e:
  782. logger.error(f"创建文本资源节点失败: {str(e)}")
  783. return jsonify(failed(str(e)))
  784. # 处理非结构化数据
  785. @bp.route("/unstructured/process", methods=["POST"])
  786. def processing_unstructured_data():
  787. try:
  788. if not request.json:
  789. return jsonify(failed("请求数据不能为空"))
  790. # 获取参数
  791. node_id = request.json.get("id")
  792. if not node_id:
  793. return jsonify(failed("节点ID不能为空"))
  794. # 获取 MinIO 配置
  795. minio_client = get_minio_client()
  796. config = get_minio_config()
  797. prefix = config["PREFIX"]
  798. # 调用处理逻辑
  799. result = solve_unstructured_data(node_id, minio_client, prefix)
  800. if result:
  801. return jsonify(success({"message": "处理成功"}))
  802. else:
  803. return jsonify(failed("处理失败"))
  804. except Exception as e:
  805. logger.error(f"处理非结构化数据失败: {str(e)}")
  806. return jsonify(failed(str(e)))
  807. # 创建文本图谱
  808. @bp.route("/text/graph", methods=["POST"])
  809. def create_text_graph():
  810. try:
  811. if not request.json:
  812. return jsonify(failed("请求数据不能为空"))
  813. # 获取参数
  814. node_id = request.json.get("id")
  815. entity_zh = request.json.get("entity_zh")
  816. entity_en = request.json.get("entity_en")
  817. if not all([node_id, entity_zh, entity_en]):
  818. return jsonify(failed("参数不完整"))
  819. # 创建图谱
  820. result = handle_txt_graph(node_id, entity_zh, entity_en)
  821. if result:
  822. return jsonify(success({"message": "图谱创建成功"}))
  823. else:
  824. return jsonify(failed("图谱创建失败"))
  825. except Exception as e:
  826. logger.error(f"创建文本图谱失败: {str(e)}")
  827. return jsonify(failed(str(e)))
  828. @bp.route("/config", methods=["GET"])
  829. @require_auth
  830. def get_meta_config():
  831. """获取元数据配置信息"""
  832. config = get_minio_config()
  833. return jsonify(
  834. {
  835. "bucket_name": config["MINIO_BUCKET"],
  836. "prefix": config["PREFIX"],
  837. "allowed_extensions": list(config["ALLOWED_EXTENSIONS"]),
  838. }
  839. )
  840. # 更新元数据
  841. @bp.route("/node/update", methods=["POST"])
  842. def meta_node_update():
  843. """
  844. 更新元数据节点
  845. 在更新前会进行冗余检测(如果修改了 name_zh/name_en):
  846. - 如果更新后的名称与其他节点完全匹配,返回错误
  847. - 如果存在疑似重复的元数据,创建审核记录并返回提示
  848. - 如果无重复,正常更新节点
  849. """
  850. try:
  851. if not request.json:
  852. return jsonify(failed("请求数据不能为空"))
  853. # 从请求中获取节点ID和更新数据
  854. node_id = request.json.get("id")
  855. if not node_id:
  856. return jsonify(failed("节点ID不能为空"))
  857. # 验证并转换节点ID为整数
  858. try:
  859. node_id = int(node_id)
  860. except (ValueError, TypeError):
  861. return jsonify(failed(f"节点ID必须为整数,当前值: {node_id}"))
  862. # 是否强制更新(跳过冗余检测)
  863. force_update = bool(request.json.get("force_update", False))
  864. # 更新节点
  865. with neo4j_driver.get_session() as session:
  866. # 检查节点是否存在并获取当前值
  867. check_cypher = """
  868. MATCH (n:DataMeta)
  869. WHERE id(n) = $node_id
  870. RETURN n
  871. """
  872. result = session.run(check_cypher, node_id=node_id)
  873. node = result.single()
  874. if not node or not node["n"]:
  875. return jsonify(failed("节点不存在"))
  876. # 获取当前节点属性
  877. current_node = dict(node["n"])
  878. # 处理每个可能的更新字段
  879. fields_to_update = {
  880. "name_zh": request.json.get("name_zh"),
  881. "category": request.json.get("category"),
  882. "alias": request.json.get("alias"),
  883. "affiliation": request.json.get("affiliation"),
  884. "data_type": request.json.get("data_type"),
  885. "describe": request.json.get("describe"),
  886. "status": request.json.get("status"),
  887. "name_en": request.json.get("name_en"),
  888. }
  889. # 计算更新后的值(用于冗余检测)
  890. updated_name_zh = (
  891. fields_to_update["name_zh"]
  892. if fields_to_update["name_zh"] is not None
  893. else current_node.get("name_zh", "")
  894. )
  895. updated_name_en = (
  896. fields_to_update["name_en"]
  897. if fields_to_update["name_en"] is not None
  898. else current_node.get("name_en", "")
  899. )
  900. updated_data_type = (
  901. fields_to_update["data_type"]
  902. if fields_to_update["data_type"] is not None
  903. else current_node.get("data_type", "varchar(255)")
  904. )
  905. # 处理标签
  906. tag = request.json.get("tag")
  907. tag_ids = normalize_tag_inputs(tag) if tag is not None else []
  908. # ========== 冗余检测(仅当修改了 name_zh 或 name_en 时)==========
  909. name_changed = (
  910. fields_to_update["name_zh"] is not None
  911. and fields_to_update["name_zh"] != current_node.get("name_zh")
  912. ) or (
  913. fields_to_update["name_en"] is not None
  914. and fields_to_update["name_en"] != current_node.get("name_en")
  915. )
  916. if name_changed and not force_update:
  917. redundancy_result = check_redundancy_for_update(
  918. node_id=node_id,
  919. name_zh=updated_name_zh,
  920. name_en=updated_name_en,
  921. data_type=updated_data_type,
  922. tag_ids=tag_ids,
  923. )
  924. # 存在完全匹配的其他元数据
  925. if redundancy_result["has_exact_match"]:
  926. exact_id = redundancy_result["exact_match_id"]
  927. logger.warning(
  928. f"更新后元数据与其他节点完全匹配: "
  929. f"node_id={node_id}, existing_id={exact_id}"
  930. )
  931. return jsonify(
  932. failed(
  933. f"更新后的元数据与已有节点(ID={exact_id})完全相同,"
  934. f"请检查是否需要合并或修改名称。"
  935. )
  936. )
  937. # 存在疑似重复的元数据,已创建审核记录
  938. if redundancy_result["review_created"]:
  939. candidates = redundancy_result["candidates"]
  940. candidate_names = [c.get("name_zh", "") for c in candidates[:3]]
  941. logger.info(
  942. f"更新元数据发现疑似重复: node_id={node_id}, "
  943. f"candidates={candidate_names}"
  944. )
  945. return jsonify(
  946. failed(
  947. f"发现疑似重复元数据,已创建审核记录。"
  948. f"疑似重复: {', '.join(candidate_names)}。"
  949. f"请前往元数据审核页面处理,或使用 force_update=true 强制更新。"
  950. )
  951. )
  952. # ========== 执行更新 ==========
  953. # 构建更新语句,只更新提供的属性
  954. update_cypher = """
  955. MATCH (n:DataMeta)
  956. WHERE id(n) = $node_id
  957. SET n.updateTime = $update_time
  958. """
  959. # 准备更新参数
  960. update_params = {"node_id": node_id, "update_time": get_formatted_time()}
  961. # 只更新提供了新值的字段
  962. for field, new_value in fields_to_update.items():
  963. if new_value is not None:
  964. # 特殊处理 data_type 字段映射
  965. if field == "data_type":
  966. update_cypher += f", n.data_type = ${field}\n"
  967. else:
  968. update_cypher += f", n.{field} = ${field}\n"
  969. update_params[field] = new_value
  970. update_cypher += "RETURN n"
  971. result = session.run(
  972. update_cypher, # type: ignore[arg-type]
  973. **update_params,
  974. )
  975. updated_node = result.single()
  976. if updated_node and updated_node["n"]:
  977. node_data = dict(updated_node["n"])
  978. node_data["id"] = updated_node["n"].id
  979. # 如果更新了标签,处理标签关系(支持列表)
  980. if tag is not None:
  981. # 先删除现有标签关系
  982. delete_tag_cypher = """
  983. MATCH (n:DataMeta)-[r:LABEL]->(t:DataLabel)
  984. WHERE id(n) = $node_id
  985. DELETE r
  986. """
  987. session.run(delete_tag_cypher, node_id=node_id)
  988. for tag_id in tag_ids:
  989. create_tag_cypher = """
  990. MATCH (n:DataMeta), (t:DataLabel)
  991. WHERE id(n) = $node_id AND id(t) = $tag_id
  992. MERGE (n)-[r:LABEL]->(t)
  993. RETURN r
  994. """
  995. session.run(create_tag_cypher, node_id=node_id, tag_id=tag_id)
  996. logger.info(f"成功更新元数据节点: ID={node_data['id']}")
  997. return jsonify(success(node_data))
  998. else:
  999. logger.error(f"更新元数据节点失败: ID={node_id}")
  1000. return jsonify(failed("更新元数据节点失败"))
  1001. except Exception as e:
  1002. logger.error(f"更新元数据失败: {str(e)}")
  1003. return jsonify(failed(str(e)))
  1004. @bp.route("/review/list", methods=["POST"])
  1005. def metadata_review_list():
  1006. """
  1007. 审核记录列表:疑似冗余/变动
  1008. Body:
  1009. - current: 页码(默认1)
  1010. - size: 每页数量(默认10)
  1011. - record_type: redundancy|change(可选)
  1012. - status: pending|resolved|ignored(可选)
  1013. - business_domain_id: 业务领域ID(可选)
  1014. - keyword: 关键字(可选,匹配 new_meta.name_zh/name_en)
  1015. """
  1016. try:
  1017. payload = request.get_json() or {}
  1018. if not isinstance(payload, dict):
  1019. return jsonify(failed("请求数据格式错误,应为 JSON 对象"))
  1020. def to_int(value, default):
  1021. try:
  1022. return int(value)
  1023. except (TypeError, ValueError):
  1024. return default
  1025. page = to_int(payload.get("current", 1), 1)
  1026. page_size = to_int(payload.get("size", 10), 10)
  1027. record_type = payload.get("record_type")
  1028. status = payload.get("status")
  1029. business_domain_id = payload.get("business_domain_id")
  1030. keyword = (payload.get("keyword") or "").strip()
  1031. query = MetadataReviewRecord.query
  1032. if record_type:
  1033. query = query.filter(MetadataReviewRecord.record_type == record_type)
  1034. if status:
  1035. query = query.filter(MetadataReviewRecord.status == status)
  1036. if business_domain_id is not None and str(business_domain_id).strip() != "":
  1037. bd_id_int = int(business_domain_id)
  1038. query = query.filter(MetadataReviewRecord.business_domain_id == bd_id_int)
  1039. if keyword:
  1040. # 兼容:使用JSONB ->> 提取进行模糊匹配
  1041. name_zh_col = MetadataReviewRecord.new_meta["name_zh"].astext
  1042. name_en_col = MetadataReviewRecord.new_meta["name_en"].astext
  1043. query = query.filter(
  1044. or_(
  1045. name_zh_col.contains(keyword),
  1046. name_en_col.contains(keyword),
  1047. )
  1048. )
  1049. total = query.count()
  1050. records = (
  1051. query.order_by(MetadataReviewRecord.created_at.desc())
  1052. .offset((page - 1) * page_size)
  1053. .limit(page_size)
  1054. .all()
  1055. )
  1056. # 将 tag_ids 转换为 tags
  1057. records_data = [convert_tag_ids_to_tags(r.to_dict()) for r in records]
  1058. return jsonify(
  1059. success(
  1060. {
  1061. "records": records_data,
  1062. "total": total,
  1063. "size": page_size,
  1064. "current": page,
  1065. }
  1066. )
  1067. )
  1068. except Exception as e:
  1069. logger.error(f"审核记录列表查询失败: {str(e)}")
  1070. return jsonify(failed("审核记录列表查询失败", error=str(e)))
  1071. @bp.route("/review/create", methods=["POST"])
  1072. def metadata_review_create():
  1073. """
  1074. 创建元数据审核记录
  1075. Body:
  1076. - record_type: 审核记录类型(redundancy: 疑似重复 / change: 疑似变动 / merge: 合并请求)
  1077. - source: 触发来源(默认 "manual")
  1078. - meta1: 第一个元数据信息
  1079. - id: 节点ID
  1080. - name_zh: 中文名
  1081. - name_en: 英文名
  1082. - data_type: 数据类型
  1083. - status: 状态
  1084. - meta2: 第二个元数据信息
  1085. - id: 节点ID
  1086. - name_zh: 中文名
  1087. - name_en: 英文名
  1088. - data_type: 数据类型
  1089. - status: 状态
  1090. - notes: 备注(可选)
  1091. Returns:
  1092. 创建成功的审核记录信息
  1093. """
  1094. try:
  1095. payload = request.get_json() or {}
  1096. if not isinstance(payload, dict):
  1097. return jsonify(failed("请求数据格式错误,应为 JSON 对象"))
  1098. record_type = payload.get("record_type")
  1099. source = payload.get("source", "manual")
  1100. meta1 = payload.get("meta1")
  1101. meta2 = payload.get("meta2")
  1102. notes = payload.get("notes")
  1103. # 参数校验
  1104. if not record_type:
  1105. return jsonify(failed("record_type 不能为空"))
  1106. if record_type not in ("redundancy", "change", "merge"):
  1107. return jsonify(
  1108. failed("record_type 必须是 redundancy、change 或 merge 之一")
  1109. )
  1110. if not meta1 or not isinstance(meta1, dict):
  1111. return jsonify(failed("meta1 不能为空且必须是对象"))
  1112. if not meta2 or not isinstance(meta2, dict):
  1113. return jsonify(failed("meta2 不能为空且必须是对象"))
  1114. # 校验元数据必要字段
  1115. required_fields = ["id", "name_zh", "name_en", "data_type", "status"]
  1116. for field in required_fields:
  1117. if field not in meta1:
  1118. return jsonify(failed(f"meta1 缺少必要字段: {field}"))
  1119. if field not in meta2:
  1120. return jsonify(failed(f"meta2 缺少必要字段: {field}"))
  1121. # 构建 new_meta(主元数据信息)
  1122. new_meta = {
  1123. "id": meta1.get("id"),
  1124. "name_zh": meta1.get("name_zh"),
  1125. "name_en": meta1.get("name_en"),
  1126. "data_type": meta1.get("data_type"),
  1127. "status": meta1.get("status"),
  1128. }
  1129. # 构建 candidates(候选/对比元数据列表)
  1130. candidates = [
  1131. {
  1132. "id": meta2.get("id"),
  1133. "name_zh": meta2.get("name_zh"),
  1134. "name_en": meta2.get("name_en"),
  1135. "data_type": meta2.get("data_type"),
  1136. "status": meta2.get("status"),
  1137. }
  1138. ]
  1139. # 创建审核记录
  1140. review_record = MetadataReviewRecord()
  1141. review_record.record_type = record_type
  1142. review_record.source = source
  1143. review_record.new_meta = new_meta
  1144. review_record.candidates = candidates
  1145. review_record.status = "pending"
  1146. review_record.notes = notes
  1147. db.session.add(review_record)
  1148. db.session.commit()
  1149. logger.info(
  1150. f"创建审核记录成功: id={review_record.id}, "
  1151. f"record_type={record_type}, "
  1152. f"meta1_name={meta1.get('name_zh')}, "
  1153. f"meta2_name={meta2.get('name_zh')}"
  1154. )
  1155. return jsonify(
  1156. success(
  1157. {
  1158. "record": review_record.to_dict(),
  1159. "message": "审核记录创建成功,请前往数据审核页面进行处理",
  1160. }
  1161. )
  1162. )
  1163. except Exception as e:
  1164. logger.error(f"创建审核记录失败: {str(e)}")
  1165. db.session.rollback()
  1166. return jsonify(failed("创建审核记录失败", error=str(e)))
  1167. @bp.route("/review/detail", methods=["GET"])
  1168. def metadata_review_detail():
  1169. """
  1170. 审核记录详情
  1171. Query:
  1172. - id: 记录ID
  1173. """
  1174. try:
  1175. record_id = request.args.get("id")
  1176. if not record_id:
  1177. return jsonify(failed("缺少id参数"))
  1178. record = MetadataReviewRecord.query.get(int(record_id))
  1179. if not record:
  1180. return jsonify(failed("记录不存在"))
  1181. # 将 tag_ids 转换为 tags
  1182. data = convert_tag_ids_to_tags(record.to_dict())
  1183. # change 场景:返回受影响元数据的影响关系图谱(若有 meta_id)
  1184. impact_graph = None
  1185. if record.record_type == "change":
  1186. old_meta = record.old_meta or {}
  1187. meta_id = old_meta.get("meta_id")
  1188. if meta_id is not None and str(meta_id).strip() != "":
  1189. try:
  1190. impact_graph = meta_impact_graph(int(meta_id))
  1191. except Exception as e:
  1192. logger.warning(f"获取影响图谱失败: {e}")
  1193. data["impact_graph"] = impact_graph
  1194. return jsonify(success(data))
  1195. except Exception as e:
  1196. logger.error(f"审核记录详情查询失败: {str(e)}")
  1197. return jsonify(failed("审核记录详情查询失败", error=str(e)))
  1198. @bp.route("/review/resolve", methods=["POST"])
  1199. def metadata_review_resolve():
  1200. """
  1201. 处理审核记录
  1202. Body:
  1203. - id: 记录ID
  1204. - action: alias | create_new | accept_change | reject_change | ignore
  1205. - payload: 动作参数(可选)
  1206. - resolved_by: 处理人(可选)
  1207. - notes: 备注(可选)
  1208. action=alias:
  1209. payload: { primary_meta_id: int, alias_meta_id: int }
  1210. 行为:在 DataMeta 节点之间重建 ALIAS 关系
  1211. - 创建 (alias_meta)-[:ALIAS]->(primary_meta) 关系
  1212. - 将所有指向 alias_meta 的 ALIAS 关系转移到 primary_meta
  1213. - primary_meta 已有的 ALIAS 关系保持不变
  1214. - BusinessDomain 的 INCLUDES 关系不受影响
  1215. action=create_new:
  1216. payload: { new_name_zh: str }
  1217. 行为:创建新的 DataMeta(中文名区分)并关联业务领域
  1218. action=accept_change:
  1219. payload: { meta_id?: int }
  1220. 行为:把 new_meta 写回目标 DataMeta,并写入 metadata_version_history(PG)
  1221. action=reject_change/ignore:
  1222. 行为:仅更新审核记录状态
  1223. """
  1224. try:
  1225. payload = request.get_json() or {}
  1226. if not isinstance(payload, dict):
  1227. return jsonify(failed("请求数据格式错误,应为 JSON 对象"))
  1228. record_id = payload.get("id")
  1229. action = payload.get("action")
  1230. action_payload = payload.get("payload") or {}
  1231. resolved_by = payload.get("resolved_by")
  1232. notes = payload.get("notes")
  1233. if not record_id:
  1234. return jsonify(failed("id 不能为空"))
  1235. if not action:
  1236. return jsonify(failed("action 不能为空"))
  1237. record = MetadataReviewRecord.query.get(int(record_id))
  1238. if not record:
  1239. return jsonify(failed("记录不存在"))
  1240. if record.status != "pending":
  1241. return jsonify(failed("记录已处理,无法重复处理"))
  1242. # 需要业务领域上下文的动作
  1243. bd_id = record.business_domain_id
  1244. new_meta = record.new_meta or {}
  1245. if action == "alias":
  1246. primary_meta_id = action_payload.get("primary_meta_id")
  1247. alias_meta_id = action_payload.get("alias_meta_id")
  1248. if not primary_meta_id:
  1249. return jsonify(failed("payload.primary_meta_id 不能为空"))
  1250. if not alias_meta_id:
  1251. return jsonify(failed("payload.alias_meta_id 不能为空"))
  1252. if int(primary_meta_id) == int(alias_meta_id):
  1253. return jsonify(failed("primary_meta_id 和 alias_meta_id 不能相同"))
  1254. # 写入 Neo4j:重建 DataMeta 节点间的 ALIAS 关系
  1255. from app.services.neo4j_driver import neo4j_driver
  1256. with neo4j_driver.get_session() as session:
  1257. # Step 1: 将所有指向 alias_meta 的 ALIAS 关系转移到 primary_meta
  1258. # 查找所有以 alias_meta 为目标的 ALIAS 关系,创建新关系指向 primary_meta,然后删除旧关系
  1259. session.run(
  1260. """
  1261. MATCH (other:DataMeta)-[old_rel:ALIAS]->(alias_meta:DataMeta)
  1262. WHERE id(alias_meta) = $alias_meta_id
  1263. WITH other, old_rel
  1264. MATCH (primary_meta:DataMeta)
  1265. WHERE id(primary_meta) = $primary_meta_id
  1266. MERGE (other)-[:ALIAS]->(primary_meta)
  1267. DELETE old_rel
  1268. """,
  1269. {
  1270. "alias_meta_id": int(alias_meta_id),
  1271. "primary_meta_id": int(primary_meta_id),
  1272. },
  1273. )
  1274. # Step 2: 创建 alias_meta 指向 primary_meta 的 ALIAS 关系
  1275. session.run(
  1276. """
  1277. MATCH (alias_meta:DataMeta), (primary_meta:DataMeta)
  1278. WHERE id(alias_meta) = $alias_meta_id AND id(primary_meta) = $primary_meta_id
  1279. MERGE (alias_meta)-[:ALIAS]->(primary_meta)
  1280. """,
  1281. {
  1282. "alias_meta_id": int(alias_meta_id),
  1283. "primary_meta_id": int(primary_meta_id),
  1284. },
  1285. )
  1286. update_review_record_resolution(
  1287. record,
  1288. action="alias",
  1289. payload={
  1290. "primary_meta_id": int(primary_meta_id),
  1291. "alias_meta_id": int(alias_meta_id),
  1292. },
  1293. resolved_by=resolved_by,
  1294. notes=notes,
  1295. )
  1296. db.session.commit()
  1297. return jsonify(success(record.to_dict()))
  1298. if action == "create_new":
  1299. new_name_zh = (action_payload.get("new_name_zh") or "").strip()
  1300. if not bd_id:
  1301. return jsonify(
  1302. failed("记录缺少 business_domain_id,无法执行 create_new")
  1303. )
  1304. if not new_name_zh:
  1305. return jsonify(failed("payload.new_name_zh 不能为空"))
  1306. from app.core.meta_data import get_formatted_time
  1307. from app.services.neo4j_driver import neo4j_driver
  1308. with neo4j_driver.get_session() as session:
  1309. # 创建新 DataMeta(避免覆盖旧节点)
  1310. result = session.run(
  1311. """
  1312. CREATE (m:DataMeta {
  1313. name_zh: $name_zh,
  1314. name_en: $name_en,
  1315. data_type: $data_type,
  1316. create_time: $create_time,
  1317. status: true
  1318. })
  1319. RETURN m
  1320. """,
  1321. {
  1322. "name_zh": new_name_zh,
  1323. "name_en": (new_meta.get("name_en") or "").strip(),
  1324. "data_type": (new_meta.get("data_type") or "varchar(255)"),
  1325. "create_time": get_formatted_time(),
  1326. },
  1327. ).single()
  1328. if not result or not result.get("m"):
  1329. return jsonify(failed("创建新元数据失败"))
  1330. new_meta_id = int(result["m"].id)
  1331. session.run(
  1332. """
  1333. MATCH (n:BusinessDomain), (m:DataMeta)
  1334. WHERE id(n) = $domain_id AND id(m) = $meta_id
  1335. MERGE (n)-[:INCLUDES]->(m)
  1336. """,
  1337. {"domain_id": int(bd_id), "meta_id": new_meta_id},
  1338. )
  1339. update_review_record_resolution(
  1340. record,
  1341. action="create_new",
  1342. payload={"new_name_zh": new_name_zh},
  1343. resolved_by=resolved_by,
  1344. notes=notes,
  1345. )
  1346. db.session.commit()
  1347. return jsonify(success(record.to_dict()))
  1348. if action == "accept_change":
  1349. old_meta = record.old_meta or {}
  1350. meta_id = action_payload.get("meta_id") or old_meta.get("meta_id")
  1351. if not meta_id:
  1352. return jsonify(failed("无法确定需要更新的 meta_id"))
  1353. from app.core.meta_data import get_formatted_time
  1354. from app.services.neo4j_driver import neo4j_driver
  1355. before_snapshot = old_meta.get("snapshot") or {}
  1356. after_snapshot = new_meta
  1357. # 写入 Neo4j:更新 DataMeta 属性,并尝试同步标签集合
  1358. with neo4j_driver.get_session() as session:
  1359. name_zh_val = (
  1360. after_snapshot.get("name_zh")
  1361. or before_snapshot.get("name_zh")
  1362. or ""
  1363. ).strip()
  1364. name_en_val = (after_snapshot.get("name_en") or "").strip()
  1365. data_type_val = after_snapshot.get("data_type") or "varchar(255)"
  1366. session.run(
  1367. """
  1368. MATCH (m:DataMeta)
  1369. WHERE id(m) = $meta_id
  1370. SET m.name_zh = $name_zh,
  1371. m.name_en = $name_en,
  1372. m.data_type = $data_type,
  1373. m.updateTime = $update_time,
  1374. m.status = true
  1375. """,
  1376. {
  1377. "meta_id": int(meta_id),
  1378. "name_zh": name_zh_val,
  1379. "name_en": name_en_val,
  1380. "data_type": data_type_val,
  1381. "update_time": get_formatted_time(),
  1382. },
  1383. )
  1384. tag_ids = after_snapshot.get("tag_ids") or []
  1385. tag_ids = [int(t) for t in tag_ids if t is not None]
  1386. if tag_ids:
  1387. session.run(
  1388. """
  1389. MATCH (m:DataMeta)-[r:LABEL]->(:DataLabel)
  1390. WHERE id(m) = $meta_id
  1391. DELETE r
  1392. """,
  1393. {"meta_id": int(meta_id)},
  1394. )
  1395. session.run(
  1396. """
  1397. MATCH (m:DataMeta)
  1398. WHERE id(m) = $meta_id
  1399. WITH m
  1400. UNWIND $tag_ids AS tid
  1401. MATCH (t:DataLabel) WHERE id(t) = tid
  1402. MERGE (m)-[:LABEL]->(t)
  1403. """,
  1404. {"meta_id": int(meta_id), "tag_ids": tag_ids},
  1405. )
  1406. # 写入版本历史(PG)
  1407. history = MetadataVersionHistory()
  1408. history.meta_id = int(meta_id) if meta_id is not None else 0
  1409. history.change_source = "ddl"
  1410. history.before_snapshot = (
  1411. before_snapshot if before_snapshot is not None else {}
  1412. )
  1413. history.after_snapshot = (
  1414. after_snapshot if after_snapshot is not None else {}
  1415. )
  1416. history.created_by = resolved_by if resolved_by is not None else ""
  1417. db.session.add(history)
  1418. update_review_record_resolution(
  1419. record,
  1420. action="accept_change",
  1421. payload={"meta_id": int(meta_id)},
  1422. resolved_by=resolved_by,
  1423. notes=notes,
  1424. )
  1425. db.session.commit()
  1426. return jsonify(success(record.to_dict()))
  1427. if action in ("reject_change", "ignore"):
  1428. update_review_record_resolution(
  1429. record,
  1430. action=action,
  1431. payload=action_payload,
  1432. resolved_by=resolved_by,
  1433. notes=notes,
  1434. )
  1435. db.session.commit()
  1436. return jsonify(success(record.to_dict()))
  1437. return jsonify(failed(f"不支持的action: {action}"))
  1438. except Exception as e:
  1439. logger.error(f"处理审核记录失败: {str(e)}")
  1440. db.session.rollback()
  1441. return jsonify(failed("处理审核记录失败", error=str(e)))