material.py 16 KB

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