material.py 18 KB

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