tasks2.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import time
  2. from bson.objectid import ObjectId
  3. from celery_app2 import celery_app2
  4. from pymongo import MongoClient
  5. import requests
  6. import json
  7. uri="mongodb://owner:Pheecian1@47.101.198.30:27017/baojia"
  8. mongoclient=MongoClient(uri)
  9. db = mongoclient['baojia']
  10. dbcollection=db['Dwgc']
  11. qdcollection=db['qdxm']
  12. biaocollection=db['jingjibiao']
  13. djcollection=db['Djcs']
  14. from redis.exceptions import TimeoutError
  15. @celery_app2.task(autoretry_for=(TimeoutError,))
  16. def process_data(data:dict)-> dict:
  17. print(data['name'])
  18. print(data['detail'])##dwgcbh
  19. biao = biaocollection.find_one({'_id': ObjectId(data['name'])})
  20. Dxgcxx = biao['Dxgcxx']
  21. label=""
  22. city = biao['city']
  23. for dxgc in Dxgcxx:
  24. Dwgc = dxgc['Dwgc']
  25. for child in Dwgc:
  26. if child['Dwgcbh'] == data['detail']:
  27. label = child['Dwgcmc']
  28. qd = qdcollection.find({'biao_id': data['name'], 'Dwgcbh': data['detail']})
  29. qds = {}
  30. qds2 = {}
  31. djs = {}
  32. djs2 = {}
  33. for entry in qd:
  34. qds[entry['清单编码']]= 0
  35. qds2[entry['清单编码']]= 0
  36. dj = djcollection.find({'biao_id': data['name'], 'Dwgcbh': data['detail']})
  37. for entry in dj:
  38. djs[entry['清单编码']]= 0
  39. djs2[entry['清单编码']]= 0
  40. result = dbcollection.update_one({'biao_id': data['name'], 'Dwgcbh': data['detail']},
  41. {'$set': {'status': '开始', 'qd_detail': qds, 'dj_detail': djs}})
  42. maxN = 0
  43. for entry in qds:
  44. maxN = maxN + 1
  45. url = 'http://0.0.0.0:8000/submit/'
  46. qd_ = qdcollection.find_one({'biao_id': data['name'], 'Dwgcbh': data['detail'], '清单编码': entry})
  47. response = requests.post(url, json={
  48. 'bianma': entry,
  49. 'mc': qd_['名称'],
  50. 'tz': qd_['项目特征'],
  51. 'dw': qd_['单位'],
  52. 'sl': qd_['数量'],
  53. 'n': maxN,
  54. 'label': label,
  55. 'name': data['name'],
  56. 'bh': data['detail'],
  57. 'city': city
  58. })
  59. text = response.text
  60. id_ = json.loads(text)['id']
  61. elapse = 0
  62. result = None
  63. while elapse < 300:
  64. time.sleep(10)
  65. elapse = elapse + 10
  66. response = requests.get('http://0.0.0.0:8000/check/'+id_)
  67. text = json.loads(response.text)
  68. result = text['result']
  69. if text['status'] in ['SUCCESS', 'FAILURE']:
  70. break
  71. if result is not None and text['status'] == 'SUCCESS':
  72. qds2[entry]=1
  73. result['result'][0]['biao_id'] = qd_['biao_id']
  74. result['result'][0]['Dwgcbh'] = qd_['Dwgcbh']
  75. result['result'][0]['bt'] = qd_['bt']
  76. response = requests.post('http://www.xiaozaotongxue.com/api/save2/', json={
  77. 'name': json.dumps(result['result']),
  78. })
  79. print(response.text)
  80. else:
  81. qds2[entry]=-1
  82. result = dbcollection.update_one({'biao_id': data['name'], 'Dwgcbh': data['detail']},
  83. {'$set': {'qd_detail': qds2}})
  84. break
  85. maxN = 0
  86. for entry in djs:
  87. maxN = maxN + 1
  88. url = 'http://0.0.0.0:8000/submit/'
  89. qd_ = djcollection.find_one({'biao_id': data['name'], 'Dwgcbh': data['detail'], '清单编码': entry})
  90. response = requests.post(url, json={
  91. 'bianma': entry,
  92. 'mc': qd_['名称'],
  93. 'tz': qd_['项目特征'],
  94. 'dw': qd_['单位'],
  95. 'sl': qd_['数量'],
  96. 'n': maxN,
  97. 'label': label,
  98. 'name': data['name'],
  99. 'bh': data['detail'],
  100. 'city' : city
  101. })
  102. text = response.text
  103. id_ = json.loads(text)['id']
  104. elapse = 0
  105. result = None
  106. while elapse < 300:
  107. time.sleep(10)
  108. elapse = elapse + 10
  109. response = requests.get('http://0.0.0.0:8000/check/'+id_)
  110. text = json.loads(response.text)
  111. result = text['result']
  112. if text['status'] in ['SUCCESS', 'FAILURE']:
  113. break
  114. if result is not None and text['status'] == 'SUCCESS':
  115. djs2[entry]=1
  116. result['result'][0]['biao_id'] = qd_['biao_id']
  117. result['result'][0]['Dwgcbh'] = qd_['Dwgcbh']
  118. response = requests.post('http://www.xiaozaotongxue.com/api/savedjcs2/', json={
  119. 'name': json.dumps(result['result']),
  120. })
  121. print(response.text)
  122. else:
  123. djs2[entry]=-1
  124. result = dbcollection.update_one({'biao_id': data['name'], 'Dwgcbh': data['detail']},
  125. {'$set': {'dj_detail': djs2}})
  126. break
  127. return {"result": []}