def expire_at_this_end_of_month(self, key): # 本月月底过期 from tools_lib.common_util.archived.gtz import TimeZone local_now = TimeZone.local_now() end_of_this_month = TimeZone.utc_to_local( TimeZone.month_range(local_now.year, local_now.month)[1]) self.expireat(key, end_of_this_month) self.delete()
def sign_in(): sign_in = client['profile']['sign_in'] start, end = TimeZone.day_range(value=TimeZone.local_now()) pprint( "姓名", "电话", "时间", "地址", "POI", "经度", "维度", ) for doc in sign_in.find({"create_time": {"$gte": start, "$lte": end}}): pprint(doc['name'], doc['tel'], TimeZone.utc_to_local(doc['create_time']), doc['loc']['addr'], doc['loc']['name'], doc['loc']['lng'], doc['loc']['lat'])
def op_data(): logging.info("op_data start...") man_conn = mongodb_client['profile']['man'] shop_conn = mongodb_client['profile']['shop'] express = mongodb_client['aeolus']['express'] opdata = OpData.objects.order_by("-create_time").first() start_date = date(2016, 5, 20) if opdata: start_date = TimeZone.increment_days( TimeZone.utc_to_local(opdata.create_time), 1).date() end_date = TimeZone.local_now().date() for day in TimeZone.date_xrange(start_date, end_date): start_time, end_time = TimeZone.day_range(value=day) logging.info("op data %s", day) data = {} # ============= 资金 ============== data['zj_top_up'], data['zj_pay'] = get_zj_statistics( day, TimeZone.increment_days(day)) # ============= 订单 ============== data['dd_count'] = express.find({ "create_time": { "$gte": start_time, "$lte": end_time } }).count() data['dd_sj_count'] = express.find({ "times.zj_time": { "$gte": start_time, "$lte": end_time } }).count() data['dd_tt_count'] = express.find({ "times.parttime_tt_time": { "$gte": start_time, "$lte": end_time } }).count() data['dd_error_count'] = express.find({ "times.yc_time": { "$gte": start_time, "$lte": end_time } }).count() # ============= 客户 ============== data['sh_register'] = shop_conn.find({ "create_time": { "$gte": start_time, "$lte": end_time } }).count() data['sh_order'] = 0 result = express.aggregate([{ "$match": { "create_time": { "$gte": start_time, "$lte": end_time } } }, { "$group": { "_id": "$creator.id", } }, { "$group": { "_id": "sum", "count": { "$sum": 1 } } }]) for doc in result: data['sh_order'] += doc['count'] # ============== 人力 =============== data['hr_on_job'] = man_conn.find({ "job_description": "parttime", "status": "STATUS_WORKING" }).count() data['hr_active'] = 0 # 在规定时间内收件的人 result = express.aggregate([{ "$match": { "times.sj_time": { "$gte": start_time, "$lte": end_time } } }, { "$project": { "man": { "$arrayElemAt": ["$watchers", 0] } } }, { "$group": { "_id": "$man.id" } }]) active_list = [_['_id'] for _ in result] result = express.aggregate([{ "$match": { "times.parttime_tt_time": { "$gte": start_time, "$lte": end_time } } }, { "$group": { "_id": "$assignee.id", } }]) # 在规定时间内妥投的人 active_list2 = [_['_id'] for _ in result] temp = set(active_list + active_list2) data['hr_active'] = len(temp) # 司机出勤 man_list = man_conn.find({"tel": {"$in": driver_list}}) node_conn = psycopg2.connect(cursor_factory=psycopg2.extras.DictCursor, database="tlbs", **CONFIG_POSTGRESQL) cursor = node_conn.cursor() data['cl_depart'] = 0 man_id_list = [str(_['_id']) for _ in man_list] for man_id in man_id_list: local = arrow.get(day, "local") cursor.execute( """SELECT count(*) from trans.bs_trans_deliver_sign_record where deliver_id = %s and create_time >= %s and create_time <= %s """, (man_id, local.floor("day").strftime(TIME_PATTERN), local.ceil("day").strftime(TIME_PATTERN))) doc = cursor.fetchone() if doc and int(doc[0]) > 0: data['cl_depart'] += 1 cursor.execute( """ SELECT count(*) FROM trans.bs_trans_bind_node_info WHERE deliver_id = ANY(%s) """, (man_id_list, )) doc = cursor.fetchone() if not doc: data['cl_shift'] = 0 else: data['cl_shift'] = int(doc[0]) # ======================================= create_time = TimeZone.datetime_to_utc( TimeZone.naive_to_aware(datetime(day.year, day.month, day.day, 1))) data["create_time"] = create_time OpData(**data).save() logging.info("op_data end...")
def shop_reward(): logging.info("invitation shop reward start...") man_conn = mongodb_client['profile']['man'] shop_conn = mongodb_client['profile']['shop'] express = mongodb_client['aeolus']['express'] now = TimeZone.utc_now() local = TimeZone.local_now() cursor = shop_conn.find({ "recommended_by.tel": { "$exists": True }, "recommended_by.time": { "$gt": TimeZone.decrement_months(now, 2) } }) m_type = "shop_invitation" for doc in cursor: man = man_conn.find_one({"tel": doc['recommended_by']['tel']}) if not man: logging.warning("no man tel:%s", doc['recommended_by']['tel']) continue shop_id = str(doc['_id']) shop_name = utf8(doc.get("name", "新客户")) man = format_man(man, 'parttime') invite_time = TimeZone.naive_to_aware(doc['recommended_by']['time'], pytz.utc) # 拿出这个人对应商户所有的奖励, 用来计算已经奖励了多少钱 reward_list = Rewards.objects(man__id=man['id'], source__shop_id=shop_id, m_type=m_type) # 计算还剩多少可以奖励 count = 0 for r in reward_list: count += r.money count = int(count) left = 100 - count # 如果没剩了, 不能再奖励了 if left <= 0: continue # 计算最近一次需要计算的日期, try: reward = reward_list[0] start_date = TimeZone.utc_to_local( TimeZone.increment_days(reward.create_time, 1)).date() except IndexError: start_date = TimeZone.utc_to_local(invite_time).date() # 最多计算到昨天 end_date = local.date() # 从开始日期开始到昨天都需要计算 for day in TimeZone.date_xrange(start_date, end_date): start_time, end_time = TimeZone.day_range(value=day) # 限制一下start_time start_time = start_time if start_time > invite_time else invite_time _count = express.find({ "creator.id": shop_id, "times.sj_time": { "$gte": start_time, "$lte": end_time }, }).count() if _count == 0: continue c = _count if _count < left else left money = c * 1.0 # 先将时间加上本地tzinfo, 然后再转换时区, 直接指定tzinfo会出错, 差6分钟, 故意躲加1小时 create_time = TimeZone.datetime_to_utc( TimeZone.naive_to_aware( datetime(day.year, day.month, day.day, 1))) Rewards(m_type=m_type, title="邀请客户", desc="%s当日下单%s单" % (shop_name, c), man=man, money=money, source={ "shop_id": shop_id, "shop_name": shop_name }, create_time=create_time).save() logging.info( "reward: m_type[%s], date[%s], man[%s], shop_id[%s], money[%s]", m_type, day, man['id'], shop_id, money) left -= c if left <= 0: break