task2.py.bak 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. result['result'][0]['序号'] = qd_['序号']
  77. response = requests.post('http://www.xiaozaotongxue.com/api/save2/', json={
  78. 'name': json.dumps(result['result']),
  79. })
  80. print(response.text)
  81. else:
  82. qds2[entry]=-1
  83. result = dbcollection.update_one({'biao_id': data['name'], 'Dwgcbh': data['detail']},
  84. {'$set': {'qd_detail': qds2}})
  85. #break
  86. maxN = 0
  87. for entry in djs:
  88. maxN = maxN + 1
  89. url = 'http://0.0.0.0:8000/submit/'
  90. qd_ = djcollection.find_one({'biao_id': data['name'], 'Dwgcbh': data['detail'], '清单编码': entry})
  91. response = requests.post(url, json={
  92. 'bianma': entry,
  93. 'mc': qd_['名称'],
  94. 'tz': qd_['项目特征'],
  95. 'dw': qd_['单位'],
  96. 'sl': qd_['数量'],
  97. 'n': maxN,
  98. 'label': label,
  99. 'name': data['name'],
  100. 'bh': data['detail'],
  101. 'city' : city
  102. })
  103. text = response.text
  104. id_ = json.loads(text)['id']
  105. elapse = 0
  106. result = None
  107. while elapse < 300:
  108. time.sleep(10)
  109. elapse = elapse + 10
  110. response = requests.get('http://0.0.0.0:8000/check/'+id_)
  111. text = json.loads(response.text)
  112. result = text['result']
  113. if text['status'] in ['SUCCESS', 'FAILURE']:
  114. break
  115. if result is not None and text['status'] == 'SUCCESS':
  116. djs2[entry]=1
  117. result['result'][0]['biao_id'] = qd_['biao_id']
  118. result['result'][0]['Dwgcbh'] = qd_['Dwgcbh']
  119. result['result'][0]['序号'] = qd_['序号']
  120. response = requests.post('http://www.xiaozaotongxue.com/api/savedjcs2/', json={
  121. 'name': json.dumps(result['result']),
  122. })
  123. print(response.text)
  124. else:
  125. djs2[entry]=-1
  126. result = dbcollection.update_one({'biao_id': data['name'], 'Dwgcbh': data['detail']},
  127. {'$set': {'dj_detail': djs2}})
  128. #break
  129. result = dbcollection.update_one({'biao_id': data['name'], 'Dwgcbh': data['detail']},
  130. {'$set': {'status': '结束'}})
  131. return {"result": []}