MAmountCalc.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from .MerchantCalc import MerchantCalc, mch_detail_paths, mch_paths, detail_paths
  2. from .algorithm import calc_morder_lack
  3. from .DataStream import EMchPosmap as pos_map
  4. import time as time
  5. import json
  6. import redis
  7. import logging
  8. logger = logging.getLogger('MAmountCalc')
  9. def earliest_time(rclient: redis.client):
  10. result = {}
  11. min_time = int(time.time())
  12. data = rclient.get('nc_refill-stat-earliest-ordertime')
  13. if data is not None:
  14. mchid_times = json.loads(data)
  15. for mchid,order_time in mchid_times.items():
  16. mchid = int(mchid)
  17. order_time = int(order_time)
  18. result[mchid] = order_time
  19. if order_time < min_time:
  20. min_time = order_time
  21. return result, min_time
  22. else:
  23. return result, min_time
  24. class MAmountCalc(MerchantCalc):
  25. def _calc_handler(self, rclient):
  26. mchid_times,earliest = earliest_time(rclient)
  27. end_time = int(time.time())
  28. reader = self._reader()
  29. days, start_time, end_time = self.calc_time(reader, earliest, end_time)
  30. day_stamp = days[0]
  31. tuple_pathes = reader.many_tuple_path(days)
  32. gen = detail_paths(reader, tuple_pathes, days)
  33. mamounts = dict()
  34. for _mchid, _card_type, _spec, _data in gen:
  35. if _mchid not in mchid_times:
  36. continue
  37. else:
  38. _start_time = mchid_times[_mchid]
  39. send_amounts, lack_amounts = calc_morder_lack(_data, pos_map, _start_time - day_stamp, end_time - day_stamp)
  40. if _mchid not in mamounts:
  41. mamounts[_mchid] = {'send_amounts': send_amounts, 'lack_amounts': lack_amounts}
  42. else:
  43. mamounts[_mchid]['send_amounts'] += send_amounts
  44. mamounts[_mchid]['lack_amounts'] += lack_amounts
  45. result = dict()
  46. for _mchid, _val in mamounts.items():
  47. _send_amounts = round(_val['send_amounts'], 2)
  48. _lack_amounts = round(_val['lack_amounts'], 2)
  49. result[_mchid] = {'send_amounts': _send_amounts, 'lack_amounts': _lack_amounts}
  50. val = json.dumps({'send_amounts': result, 'time': int(time.time())})
  51. rclient.set('nc_refill-stat-merchant-sendamount',val)
  52. logger.debug(result)
  53. return 15