material.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from fastapi import APIRouter, Request, Depends, Query, HTTPException, status,WebSocket,WebSocketDisconnect
  4. from common.security import valid_access_token,valid_websocket_token
  5. from fastapi.responses import JSONResponse
  6. from sqlalchemy.orm import Session
  7. from sqlalchemy.sql import func
  8. from common.auth_user import *
  9. from sqlalchemy import text
  10. from pydantic import BaseModel
  11. from common.BigDataCenterAPI import *
  12. from database import get_db
  13. from typing import List
  14. from models import *
  15. from utils import *
  16. from utils.spatial import *
  17. from utils.ry_system_util import *
  18. from common.websocketManager import *
  19. import json
  20. import traceback
  21. router = APIRouter()
  22. def warehouse_room_id_get_warehouse_room_info(db,id):
  23. query = db.query(ResourceProvisionWarehouseRoomInfo)
  24. query = query.filter_by(id = id,del_flag = '0')
  25. return query.first()
  26. def warehouse_id_get_warehouse_info(db,id):
  27. query = db.query(ResourceProvisionWarehouseInfo)
  28. query = query.filter_by(warehouse_id = id,del_flag = '0')
  29. return query.first()
  30. def type_id_get_material_type_info(db,id):
  31. query = db.query(ResourceProvisionMaterialType)
  32. query = query.filter_by(id = id,del_flag = '0')
  33. return query.first()
  34. def material_id_get_material_info(db,id):
  35. query = db.query(ResourceProvisionMaterialInfo)
  36. query = query.filter_by(material_id = id,del_flag = '0')
  37. return query.first()
  38. def delete_resource_provision_file(db,from_scenario,foreign_key):
  39. file_query = db.query(ResourceProvisionFile)
  40. file_query = file_query.filter(ResourceProvisionFile.del_flag != '2')
  41. file_query = file_query.filter(ResourceProvisionFile.from_scenario == from_scenario)
  42. file_query = file_query.filter(ResourceProvisionFile.foreign_key == foreign_key)
  43. files = file_query.all()
  44. for file in files:
  45. file.del_flag = '2'
  46. def get_resource_provision_file_query_fun(db,from_scenario,foreign_key):
  47. file_query = db.query(ResourceProvisionFile)
  48. file_query = file_query.filter(ResourceProvisionFile.del_flag != '2')
  49. file_query = file_query.filter(ResourceProvisionFile.from_scenario == from_scenario)
  50. file_query = file_query.filter(ResourceProvisionFile.foreign_key == foreign_key)
  51. files = file_query.all()
  52. result = [{
  53. "uid": file.file_id,
  54. "status": file.status,
  55. "name": file.file_name_desc,
  56. "url": file.file_name #"/api/file/download/%s" %
  57. } for file in files]
  58. return result
  59. @router.post("/create")
  60. async def create_pattern(
  61. user_id=Depends(valid_access_token),
  62. body = Depends(remove_xss_json),
  63. db: Session = Depends(get_db)
  64. ):
  65. try:
  66. new_material = ResourceProvisionMaterialInfo(
  67. # id = new_guid(),
  68. material_name=body['material_name'],
  69. warehouse_id=body['warehouse_id'],
  70. inventory=body['inventory'],
  71. specification=body['specification'],
  72. model=body['model'],
  73. category_name=body['category_name'],
  74. material_type=body['material_type_id'],
  75. unit_name=body['unit_name'],
  76. brand_name=body['brand_name'],
  77. length=body['length'],
  78. width=body['width'],
  79. height=body['height'],
  80. volume=body['volume'],
  81. gross_weight=body['gross_weight'],
  82. net_weight=body['net_weight'],
  83. manufacturer=body['manufacturer'],
  84. origin=body['origin'],
  85. status=body['status'],
  86. room_id=body['room_id'],
  87. package_quantity=body['package_quantity'],
  88. package_volume=body['package_volume'],
  89. package_weight=body['package_weight'],
  90. price=body['price'],
  91. selling_price=body['selling_price'],
  92. value=body['value'],
  93. cost_price=body['cost_price'],
  94. disaster_types=body['disaster_types'],
  95. maintenance=body['maintenance'],
  96. supplier_name=body['supplier_name'],
  97. special_transportation_requirements=body['special_transportation_requirements'],
  98. material=body['material'],
  99. shelf_life=body['shelf_life'],
  100. inventory_warning_pusher=body['inventory_warning_pusher'],
  101. inventory_warning_quantity=body['inventory_warning_quantity'],
  102. shelf_life_warning_days=body['shelf_life_warning_days'],
  103. shelf_life_warning_pusher=body['shelf_life_warning_pusher'],
  104. from_sys=body['from_sys'],
  105. create_by = user_id
  106. )
  107. db.add(new_material)
  108. new_file_list = body['fileList']
  109. db.commit()
  110. db.refresh(new_material)
  111. for file in new_file_list:
  112. file_name = file['url']
  113. file_name_desc = file['name']
  114. status = file['status']
  115. uid = file['uid']
  116. new_file = ResourceProvisionFile(
  117. file_id=uid,
  118. foreign_key=new_material.material_id,
  119. from_scenario='ResourceProvisionMaterialInfo',
  120. file_name=file_name,
  121. file_name_desc=file_name_desc,
  122. status=status
  123. )
  124. db.add(new_file)
  125. db.commit()
  126. return {"code": 200, "msg": "创建成功", "data": None}
  127. except Exception as e:
  128. traceback.print_exc()
  129. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  130. @router.put("/update/{id}")
  131. async def update_pattern(
  132. id :str ,
  133. user_id=Depends(valid_access_token),
  134. body=Depends(remove_xss_json),
  135. db: Session = Depends(get_db)
  136. ):
  137. try:
  138. info = material_id_get_material_info(db,id)
  139. if not info:
  140. return JSONResponse(status_code=404,content={"code":404,"msg":"warehouse room not found"})
  141. # info.room_name = body['room_name']
  142. info.material_name = body['material_name']
  143. info.warehouse_id = body['warehouse_id']
  144. info.inventory = body['inventory']
  145. info.specification = body['specification']
  146. info.model = body['model']
  147. info.category_name = body['category_name']
  148. info.material_type = body['material_type_id']
  149. info.unit_name = body['unit_name']
  150. info.brand_name = body['brand_name']
  151. info.length = body['length']
  152. info.width = body['width']
  153. info.height = body['height']
  154. info.volume = body['volume']
  155. info.gross_weight = body['gross_weight']
  156. info.net_weight = body['net_weight']
  157. info.manufacturer = body['manufacturer']
  158. info.origin = body['origin']
  159. info.status = body['status']
  160. info.room_id = body['room_id']
  161. info.package_quantity = body['package_quantity']
  162. info.package_volume = body['package_volume']
  163. info.package_weight = body['package_weight']
  164. info.price = body['price']
  165. info.selling_price = body['selling_price']
  166. info.value = body['value']
  167. info.cost_price = body['cost_price']
  168. info.disaster_types = body['disaster_types']
  169. info.maintenance = body['maintenance']
  170. info.supplier_name = body['supplier_name']
  171. info.special_transportation_requirements = body['special_transportation_requirements']
  172. info.material = body['material']
  173. info.shelf_life = body['shelf_life']
  174. info.inventory_warning_pusher = body['inventory_warning_pusher']
  175. info.inventory_warning_quantity = body['inventory_warning_quantity']
  176. info.shelf_life_warning_days = body['shelf_life_warning_days']
  177. info.shelf_life_warning_pusher = body['shelf_life_warning_pusher']
  178. info.from_sys = body['from_sys']
  179. info.update_by = user_id
  180. delete_resource_provision_file(db,'ResourceProvisionMaterialInfo',id)
  181. new_file_list = body['fileList']
  182. for file in new_file_list:
  183. file_name = file['url']
  184. file_name_desc = file['name']
  185. status = file['status']
  186. uid = file['uid']
  187. new_file = ResourceProvisionFile(
  188. file_id=uid,
  189. foreign_key=id,
  190. from_scenario='ResourceProvisionMaterialInfo',
  191. file_name=file_name,
  192. file_name_desc=file_name_desc,
  193. status=status
  194. )
  195. db.add(new_file)
  196. db.commit()
  197. return {"code": 200, "msg": "更新成功"}
  198. except Exception as e:
  199. traceback.print_exc()
  200. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  201. @router.get("/info/{id}")
  202. async def get_pattern_info(
  203. id: str,
  204. db: Session = Depends(get_db)
  205. ):
  206. try:
  207. info = material_id_get_material_info(db,id)
  208. if not info:
  209. return JSONResponse(status_code=404,content={"code":404,"msg":"warehouse room not found"})
  210. warehouse_info = warehouse_id_get_warehouse_info(db,info.warehouse_id)
  211. if warehouse_info:
  212. warehouse_name = warehouse_info.warehouse_name
  213. else:
  214. warehouse_name=None
  215. material_type_info = type_id_get_material_type_info(db,info.material_type)
  216. if material_type_info:
  217. material_category_name = material_type_info.material_category_name
  218. else:
  219. material_category_name=None
  220. warehouse_room_info = warehouse_room_id_get_warehouse_room_info(db, info.room_id)
  221. if warehouse_room_info:
  222. room_name = warehouse_room_info.room_name
  223. else:
  224. room_name = None
  225. data = {
  226. "material_id": info.material_id,
  227. "material_name": info.material_name,
  228. "warehouse_id": info.warehouse_id,
  229. "warehouse_name": warehouse_name,
  230. "inventory": info.inventory,
  231. "specification": info.specification,
  232. "model": info.model,
  233. "category_name": info.category_name,
  234. "material_type_id": info.material_type,
  235. "material_type_name": material_category_name,
  236. "unit_name": info.unit_name,
  237. "brand_name": info.brand_name,
  238. "length": info.length,
  239. "width": info.width,
  240. "height": info.height,
  241. "volume": info.volume,
  242. "gross_weight": info.gross_weight,
  243. "net_weight": info.net_weight,
  244. "manufacturer": info.manufacturer,
  245. "origin": info.origin,
  246. "status": info.status,
  247. "room_id": info.room_id,
  248. "room_name":room_name,
  249. "package_quantity": info.package_quantity,
  250. "package_volume": info.package_volume,
  251. "package_weight": info.package_weight,
  252. "price": info.price,
  253. "selling_price": info.selling_price,
  254. "value": info.value,
  255. "cost_price": info.cost_price,
  256. "disaster_types": info.disaster_types,
  257. "maintenance": info.maintenance,
  258. "supplier_name": info.supplier_name,
  259. "special_transportation_requirements": info.special_transportation_requirements,
  260. "material": info.material,
  261. "shelf_life": info.shelf_life,
  262. "inventory_warning_pusher": info.inventory_warning_pusher,
  263. "inventory_warning_quantity": info.inventory_warning_quantity,
  264. "shelf_life_warning_days": info.shelf_life_warning_days,
  265. "shelf_life_warning_pusher": info.shelf_life_warning_pusher,
  266. "from_sys": info.from_sys,
  267. "create_time":info.create_time,
  268. "fileList": get_resource_provision_file_query_fun(db=db,from_scenario='ResourceProvisionMaterialInfo', foreign_key=info.material_id)}
  269. return {"code": 200, "msg": "获取成功", "data": data}
  270. except Exception as e:
  271. traceback.print_exc()
  272. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  273. @router.get("/list")
  274. async def get_pattern_list(
  275. material_id: str = Query(None, description='名称'),
  276. material_name: str = Query(None, description='名称'),
  277. warehouse_id: str = Query(None, description='名称'),
  278. from_sys: str = Query(None, description='名称'),
  279. page: int = Query(1, gt=0, description='页码'),
  280. pageSize: int = Query(10, gt=0, description='每页条目数量'),
  281. db: Session = Depends(get_db)
  282. ):
  283. try:
  284. query = db.query(ResourceProvisionMaterialInfo)
  285. query = query.filter_by(del_flag='0')
  286. if material_id:
  287. query = query.filter(ResourceProvisionMaterialInfo.material_id.like(f'%{material_id}%'))
  288. if material_name:
  289. query = query.filter(ResourceProvisionMaterialInfo.material_name.like(f'%{material_name}%'))
  290. if warehouse_id:
  291. query = query.filter(ResourceProvisionMaterialInfo.warehouse_id==warehouse_id)
  292. if from_sys:
  293. query = query.filter(ResourceProvisionMaterialInfo.from_sys==from_sys)
  294. total_items = query.count()
  295. # 排序
  296. query = query.order_by(ResourceProvisionMaterialInfo.create_time.desc())
  297. # 执行分页查询
  298. lists = query.offset((page - 1) * pageSize).limit(pageSize).all()
  299. data = []
  300. for info in lists:
  301. warehouse_info = warehouse_id_get_warehouse_info(db, info.warehouse_id)
  302. if warehouse_info:
  303. warehouse_name = warehouse_info.warehouse_name
  304. else:
  305. warehouse_name = None
  306. material_type_info = type_id_get_material_type_info(db, info.material_type)
  307. if material_type_info:
  308. material_category_name = material_type_info.material_category_name
  309. else:
  310. material_category_name = None
  311. warehouse_room_info = warehouse_room_id_get_warehouse_room_info(db, info.room_id)
  312. if warehouse_room_info:
  313. room_name = warehouse_room_info.room_name
  314. else:
  315. room_name = None
  316. data.append({
  317. "material_id": info.material_id,
  318. "material_name": info.material_name,
  319. "warehouse_id": info.warehouse_id,
  320. "warehouse_name": warehouse_name,
  321. "inventory": info.inventory,
  322. "specification": info.specification,
  323. "model": info.model,
  324. "category_name": info.category_name,
  325. "material_type_id": info.material_type,
  326. "material_type_name": material_category_name,
  327. "unit_name": info.unit_name,
  328. "brand_name": info.brand_name,
  329. "length": info.length,
  330. "width": info.width,
  331. "height": info.height,
  332. "volume": info.volume,
  333. "gross_weight": info.gross_weight,
  334. "net_weight": info.net_weight,
  335. "manufacturer": info.manufacturer,
  336. "origin": info.origin,
  337. "status": info.status,
  338. "room_id": info.room_id,
  339. "room_name":room_name,
  340. "package_quantity": info.package_quantity,
  341. "package_volume": info.package_volume,
  342. "package_weight": info.package_weight,
  343. "price": info.price,
  344. "selling_price": info.selling_price,
  345. "value": info.value,
  346. "cost_price": info.cost_price,
  347. "disaster_types": info.disaster_types,
  348. "maintenance": info.maintenance,
  349. "supplier_name": info.supplier_name,
  350. "special_transportation_requirements": info.special_transportation_requirements,
  351. "material": info.material,
  352. "shelf_life": info.shelf_life,
  353. "inventory_warning_pusher": info.inventory_warning_pusher,
  354. "inventory_warning_quantity": info.inventory_warning_quantity,
  355. "shelf_life_warning_days": info.shelf_life_warning_days,
  356. "shelf_life_warning_pusher": info.shelf_life_warning_pusher,
  357. "from_sys": info.from_sys,
  358. "create_time":info.create_time,
  359. "fileList": get_resource_provision_file_query_fun(db=db,from_scenario='ResourceProvisionMaterialInfo', foreign_key=info.material_id)})
  360. return {"code": 200, "msg": "查询成功", "data": data,
  361. "total": total_items,
  362. "page": page,
  363. "pageSize": pageSize,
  364. "totalPages": (total_items + pageSize - 1) // pageSize
  365. }
  366. except Exception as e:
  367. traceback.print_exc()
  368. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  369. @router.delete("/delete/{id}")
  370. async def delete_pattern(
  371. id: str,
  372. db: Session = Depends(get_db)
  373. ):
  374. try:
  375. # 检查图案是否存在
  376. info = material_id_get_material_info(db, id)
  377. if not info:
  378. return JSONResponse(status_code=404, content={"code": 404, "msg": "warehouse room not found"})
  379. info.del_flag='2'
  380. delete_resource_provision_file(db, 'ResourceProvisionMaterialInfo', id)
  381. db.commit()
  382. return {"code": 200, "msg": "删除成功"}
  383. except Exception as e:
  384. traceback.print_exc()
  385. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")