Exemple #1
0
def pprint(*arg):
    print((safe_join(arg)))
Exemple #2
0
def parttime_settlement():
    logging.info("parttime_settlement start...")
    man_conn = mongodb_client['profile']['man']
    express = mongodb_client['aeolus']['express']

    m_type = "parttime"

    log = []
    settlements = []

    for man_info in man_conn.find({"job_description": m_type}):
        man = format_man(man_info, m_type)

        if man['tel'] not in man_list:
            continue

        settlement = Settlement.objects(man__id=man['id'],
                                        man__m_type=m_type).first()
        start_date = _start_arrow.datetime
        if settlement:
            # 如果有数据,则从数据的后一天开始算
            start_date = arrow.get(settlement.create_time,
                                   "utc").replace(days=1).to("local").datetime

        # 最多计算到昨天
        end_date = arrow.now().replace(days=-1).datetime
        # 从开始日期开始到昨天都需要计算

        for day in arrow.Arrow.range("day", start_date, end_date):
            start_time, end_time = day.span("day")
            start_time = start_time.datetime
            end_time = end_time.datetime
            # 收件提成
            result = express.aggregate([{
                "$match": {
                    "watchers.0.id": man['id'],
                    "watchers.0.m_type": man['m_type'],
                    "times.sj_time": {
                        "$gte": start_time,
                        "$lte": end_time
                    },
                    "times.zj_time": {
                        "$exists": True
                    },
                    "fee.sj_profit": {
                        "$exists": True
                    },
                }
            }, {
                "$group": {
                    "_id": "sum",
                    "profit": {
                        "$sum": "$fee.sj_profit"
                    }
                }
            }])

            result = list(result)
            sj_profit = 0
            if result:
                sj_profit = result[0]['profit']

            # 派件提成
            result = express.aggregate([{
                "$match": {
                    "assignee.id": man['id'],
                    "assignee.m_type": man['m_type'],
                    "times.parttime_tt_time": {
                        "$gte": start_time,
                        "$lte": end_time
                    },
                    "fee.pj_profit": {
                        "$exists": True
                    },
                }
            }, {
                "$group": {
                    "_id": "",
                    "profit": {
                        "$sum": "$fee.pj_profit"
                    }
                }
            }])

            result = list(result)
            pj_profit = 0
            if result:
                pj_profit = result[0]['profit']

            # 先将时间加上本地tzinfo, 然后再转换时区, 直接指定tzinfo会出错, 差6分钟, 故意躲加1小时
            create_time = day.floor("day").replace(hours=1).to("utc").datetime
            reward = Rewards.objects(man__id=man['id'],
                                     man__m_type=man['m_type'],
                                     create_time__gte=start_time,
                                     create_time__lte=end_time,
                                     money__gt=0).aggregate_sum("money")

            punishment = Rewards.objects(man__id=man['id'],
                                         man__m_type=man['m_type'],
                                         create_time__gte=start_time,
                                         create_time__lte=end_time,
                                         money__lt=0).aggregate_sum("money")

            # 如果今天的分成为0, 则说明没有手牌
            if sj_profit + pj_profit > 0:
                base = get_man_base(man['tel'])
            else:
                base = 0

            insurance = 0
            total = base + insurance + reward + punishment + sj_profit + pj_profit

            log.append([
                m_type, day, man['tel'], man['name'], sj_profit, pj_profit,
                reward, punishment
            ])

            settlements.append(
                Settlement(man=man,
                           sj_profit=sj_profit,
                           pj_profit=pj_profit,
                           base=base,
                           insurance=insurance,
                           reward=reward,
                           punishment=punishment,
                           total=total,
                           create_time=create_time))
    # =============================================

    for _ in settlements:
        _.save()

    for _ in log:
        logging.info(safe_join(_, ", "))

    logging.info("parttime_settlement end...")
Exemple #3
0
def driver_settlement():
    logging.info("driver_settlement start...")
    man_conn = mongodb_client['profile']['man']

    m_type = "city_driver"
    log = []
    settlements = []

    for man_info in man_conn.find({"job_description": m_type}):
        man = format_man(man_info, m_type)

        if man['tel'] not in driver_list:
            continue

        settlement = Settlement.objects(
            man__id=man['id'],
            man__m_type=m_type).order_by("-create_time").first()
        start_date = _start_arrow.datetime
        if settlement:
            # 如果有数据,则从数据的后一天开始算
            start_date = arrow.get(settlement.create_time,
                                   "utc").replace(days=1).to("local").datetime

        # 最多计算到昨天
        end_date = arrow.now().replace(days=-1).datetime
        # 从开始日期开始到昨天都需要计算

        for day in arrow.Arrow.range("day", start_date, end_date):
            start_time, end_time = day.span("day")
            start_time = start_time.datetime
            end_time = end_time.datetime

            reward = Rewards.objects(man__id=man['id'],
                                     man__m_type=man['m_type'],
                                     create_time__gte=start_time,
                                     create_time__lte=end_time,
                                     money__gt=0).aggregate_sum("money")

            punishment = Rewards.objects(man__id=man['id'],
                                         man__m_type=man['m_type'],
                                         create_time__gte=start_time,
                                         create_time__lte=end_time,
                                         money__lt=0).aggregate_sum("money")

            # 先将时间加上本地tzinfo, 然后再转换时区, 直接指定tzinfo会出错, 差6分钟, 故意多加1小时
            create_time = day.floor("day").replace(hours=1).to("utc").datetime

            insurance = 0
            total = DRIVER_BASE + insurance + reward + punishment

            log.append(
                [m_type, day, man['tel'], man['name'], reward, punishment])
            settlements.append(
                Settlement(man=man,
                           base=DRIVER_BASE,
                           insurance=insurance,
                           reward=reward,
                           punishment=punishment,
                           total=total,
                           create_time=create_time))

    # =============================================

    for _ in settlements:
        _.save()

    for _ in log:
        logging.info(safe_join(_, ", "))

    logging.info("driver_settlement end...")
Exemple #4
0
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, os.pardir)))
import logging
from settings import *
from pymongo import MongoClient
from handlers.models import *
from tools_lib.common_util.sstring import safe_join

imp.reload(sys)
sys.setdefaultencoding("utf8")

PROD_MONGODB_OUTER_IP = '123.56.117.75'
MONGODB_CONFIG = {
    # 'host': DEV_OUTER_IP,
    'host': PROD_MONGODB_OUTER_IP,
    'port': 27017,
}

logging.root.level = logging.NOTSET

# 初始化链接
client = MongoClient(**MONGODB_CONFIG)
crontab_conn = client['aeolus']['crontab']

if __name__ == '__main__':
    print((MONGODB_NAME, MONGODB_CONFIG))
    shop_conn = client['profile']['shop']
    for doc in shop_conn.find():
        print((safe_join([doc.get('name', "空"), doc['tel'], doc.get("fee", {}).get("fh_base", 15)])))
Exemple #5
0
def driver(days=0):
    local = arrow.now().replace(days=days)
    if local.hour < 23:
        return False

    # 初始化时间
    node_conn = psycopg2.connect(cursor_factory=psycopg2.extras.DictCursor, database="tlbs", **CONFIG_POSTGRESQL)

    need_punish = True if local > punish_time else False
    punish_m_type = "driver_sign_in"
    man_m_type = "city_driver"

    absent_money = 300
    late_money = 30
    if need_punish:
        late_msg = "%s年%s月%s日你在{}{}点{}分迟到,现处以%s元的罚款;奖惩明细可以到风先生软件内的数据页面查看" % (
            local.year, local.month, local.day, late_money)

        absent_msg = "%s年%s月%s日你旷工,现处以%s元的罚款,如你当日已经请假,请尽快向上级反映;奖惩明细可以到风先生软件内的数据页面查看" % (
            local.year, local.month, local.day, absent_money)
    else:
        late_msg = "%s年%s月%s日你在{}{}点{}分迟到,予以警告处理;2016年6月1日起此行为会被处以%s元/次的罚款,请注意不要再犯。" % (
            local.year, local.month, local.day, late_money)
        absent_msg = "%s年%s月%s日你旷工,予以警告处理;2016年6月1日起此行为会被处以%s元/次的罚款,请注意不要再犯。" % (
            local.year, local.month, local.day, absent_money)

    log = []
    windchat = []
    punishment = []
    logging.info("sign_in driver start...")

    # ===================== processing =============================
    def process(tel):
        man_conn = mongodb_client['profile']['man']
        cursor = node_conn.cursor()

        # 检查是否有这个人
        man = man_conn.find_one({"tel": tel})
        if not man:
            logging.info("tel[%s], 这个司机没有注册", tel)
            return
        man = format_man(man, man_m_type)

        node_list = find_driver_node(cursor, man['id'])
        if not node_list:
            log.append([tel, man['name'], "这个司机没有线路网点信息, 请管理人员设置"])
            return

        # ============= 旷工 =============
        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 not doc or int(doc[0]) == 0:
            log.append([tel, man['name'], "旷工"])
            windchat.append(dict(man=man, title="惩罚提醒", content=absent_msg, summary=absent_msg, description="惩罚提醒"))
            punishment.append(
                Rewards(man=man, m_type=punish_m_type, title="旷工惩罚", desc="旷工", money=-absent_money,
                        create_time=local.datetime)
            )
            punishment.append(
                Rewards(man=man, m_type=punish_m_type, title="旷工惩罚", desc="因为旷工扣除今天的基础服务费", money=-DRIVER_BASE,
                        create_time=local.datetime)
            )

            return
        # ============================

        for node in node_list:

            node_name, lng, lat, hour, minute = node

            msg = late_msg.format(node_name, hour, minute)
            time = local.replace(hour=hour, minute=minute).floor('minute')
            e = time.datetime
            s = time.replace(minutes=-20).datetime

            sign = driver_find_first_valid_sign(cursor, man['id'], s.strftime(TIME_PATTERN), e.strftime(TIME_PATTERN),
                                                lng, lat)
            if not sign:
                log.append([tel, man['name'], "迟到", node_name, "%02d:%02d" % (hour, minute)])
                windchat.append(dict(man=man, title="惩罚提醒", content=msg, summary=msg, description="惩罚提醒"))
                desc = "在%s%s点%s分迟到" % (node_name, hour, minute)
                punishment.append(Rewards(man=man, m_type=punish_m_type, title="迟到惩罚", desc=desc, money=-late_money,
                                          create_time=local.datetime))

                continue

            # 已经转好时区
            t = sign['create_time']

            log.append([tel, man['name'], "签到成功", node_name, "%02d:%02d" % (hour, minute), t.strftime(TIME_PATTERN)])

    # =========================================

    # main process
    for tel in driver_list:
        process(tel)
    # ==========================================
    # 风信
    from tools_lib.windchat import shortcuts, conf
    for wind in windchat:
        man = wind.pop('man')
        # 获取风信ID
        wc_id_list = shortcuts.account_query(
            account={"account_id": man['id'], "account_type": conf.ACCOUNT_TYPE_MAN})
        if not wc_id_list:
            log.append([man['tel'], man['name'], "无风信账户"])
            continue
        client_id = wc_id_list[0]

        shortcuts.channel_send_message(client_id, **wind)

    # 惩罚
    for _ in punishment:
        _.save()

    # 日志和邮件
    msg = [["电话", "姓名", "标记", "站点", "需要到达时间", "签到时间"]]

    for _ in log:
        logging.info(safe_join(_))
        msg.append(_)

    try:
        if DEBUG:
            send_mail(mail_list, "[测试]司机签到报表", "这是测试服务器发出的测试数据\n数据日期:" + str(local), file_name="data.xls",
                      file_stream=xls_writer(msg))
        else:
            send_mail(mail_list, "司机签到报表", "这是线上服务器发出的线上数据\n数据日期:" + str(local), file_name="data.xls",
                      file_stream=xls_writer(msg))
    except Exception as e:
        logging.exception(e.message)

    logging.info("sign_in driver end...")

    return True
Exemple #6
0
def man(days=0):
    # 派件员每天10点以后, 跑今天的数据

    local = arrow.now().replace(days=days)
    # 10点之后再运行
    if local.hour < 10:
        return False

    node_conn = psycopg2.connect(cursor_factory=psycopg2.extras.DictCursor, database="tlbs", **CONFIG_POSTGRESQL)
    # 初始化时间
    start = local.replace(hour=6, minute=30).floor("minute").datetime
    end = local.replace(hour=10, minute=0).floor("minute").datetime
    late_time = local.replace(hour=8, minute=30).floor("minute").datetime

    need_punish = True if local > punish_time else False

    punish_m_type = "man_sign_in"
    man_m_type = "parttime"

    absent_money = 300
    late_money = 50
    date_str = "%s年%s月%s日" % (local.year, local.month, local.day)

    absent_msg = "%s最终判定为旷工,现处以%s元的罚款,如你当日已经请假,请尽快向上级反映;奖惩明细可以到风先生软件内的数据页面查看" % (date_str, absent_money)
    late_msg = "%s最终判定为迟到,现处以%s元的罚款;奖惩明细可以到风先生软件内的数据页面查看" % (date_str, late_money)

    logging.info("sign_in man start...")

    log = []
    windchat = []
    punishment = []

    # ===================== processing =============================
    def process(tel):
        # 为什么在这里进行链接呢
        # 为了后面可以扩展成multiprocessing, 多线程中如果使用同一个数据库链接会出错
        man_conn = mongodb_client['profile']['man']
        sign_conn = mongodb_client['profile']['sign_in']
        cursor = node_conn.cursor()

        # 检查是否有这个人
        man = man_conn.find_one({"tel": tel})
        if not man:
            log.append([tel, "这个配送员没有注册"])
            return
        man = format_man(man, man_m_type)

        # 获取应该签到的点
        node = find_man_node(cursor, man['id'])
        if not node:
            log.append([tel, man['name'], "没有这个配送员的网点信息, 请管理人员在后台给他设置"])
            return
        node_name, lng, lat = node

        # 获取签到数据
        doc = sign_conn.find({
            "tel": tel,
            "create_time": {
                "$gte": start,
                "$lte": end,
            }
        }, sort=[['create_time', 1]])

        sign = man_find_first_valid_sign(doc, lng, lat)

        if not sign:
            log.append([tel, man['name'], "无有效的签到数据"])
            windchat.append(dict(man=man, title="惩罚提醒", content=absent_msg, summary=absent_msg,
                                 description="惩罚提醒"))
            if need_punish:
                punishment.append(
                    Rewards(man=man, m_type=punish_m_type, title="旷工惩罚", desc="无有效的签到数据", money=-absent_money,
                            create_time=local.datetime)
                )
            return

        t = arrow.get(sign['create_time'], "utc").to("local").datetime
        if t > late_time:
            log.append([tel, man['name'], "迟到", sign['loc']['addr'], sign['loc']['name'], t.strftime(TIME_PATTERN)])
            windchat.append(dict(man=man, title="惩罚提醒", content=late_msg, summary=late_msg,
                                 description="惩罚提醒"))
            if need_punish:
                punishment.append(
                    Rewards(man=man, m_type=punish_m_type, title="迟到惩罚", desc="迟到", money=-late_money,
                            create_time=local.datetime)
                )

            return

        log.append([tel, man['name'], "签到成功", sign['loc']['addr'], sign['loc']['name'], t.strftime(TIME_PATTERN)])

    # =======================================================================

    for tel in man_list:
        process(tel)

    # ====== 统一进行处理, 便于DEBUG ======
    # 风信
    from tools_lib.windchat import shortcuts, conf
    for wind in windchat:
        man = wind.pop('man')
        # 获取风信ID
        wc_id_list = shortcuts.account_query(
            account={"account_id": man['id'], "account_type": conf.ACCOUNT_TYPE_MAN})
        if not wc_id_list:
            log.append([man['tel'], man['name'], "无风信账户"])
            continue
        client_id = wc_id_list[0]

        shortcuts.channel_send_message(client_id, **wind)

    # 惩罚
    for _ in punishment:
        _.save()

    # 邮件和日志
    msg = [["电话", "姓名", "标记", "地址", "POI", "签到时间"]]

    for _ in log:
        logging.info(safe_join(_))
        msg.append(_)

    try:
        if DEBUG:
            send_mail(mail_list, "[测试]派件员签到报表", "这是测试服务器发出的测试数据\n数据日期:" + str(local), file_name="data.xls",
                      file_stream=xls_writer(msg))
        else:
            send_mail(mail_list, "派件员签到报表", "这是线上服务器发出的线上数据\n数据日期:" + str(local), file_name="data.xls",
                      file_stream=xls_writer(msg))
    except Exception as e:
        logging.exception(e.message)

    logging.info("sign_in man end...")
    return True