示例#1
0
    def get(self):
        """
        后台管理员操作日志
        :return:
        """
        if not request.args:
            return ResponseSuccess(
                message="参数规则:?account=panda&date=20190901&export=1"
            ).as_response()

        account = request.args['account']
        if account:
            user = AdminUser.query_user(account=account)
            if not user:
                return ResponseSuccess(message="用户不存在,请检查参数。account:%s" %
                                       account).as_response()

        try:
            date = request.args.get('date')
            if date:
                date = DateTimeKit.str_to_datetime(
                    date, DateTimeFormatEnum.TIGHT_DAY_FORMAT, to_date=True)
            else:
                date = DateTimeKit.get_cur_date()
        except:
            return ResponseSuccess(
                message="请输入有效的查询日期,格式为:20190901").as_response()

        events = AdminLog.query_by_date(date)
        if account:
            events = events.filter_by(account=account)

        rst = list()

        for event in events:
            rst.append(
                dict(
                    create_time=event.create_time,
                    account=event.account,
                    url=event.url,
                    ip=event.ip,
                    module=event.module.desc,
                    model=event.model,
                    model_id=event.model_id,
                    data_before=event.data_before,
                    data_after=event.data_after,
                ))

        rst = sorted(rst, key=lambda x: x['create_time'], reverse=True)
        for x in rst:
            x['create_time'] = DateTimeKit.datetime_to_str(x['create_time'])

        if rst and request.args.get('export'):
            filename = 'admin_log_%s.csv' % DateTimeKit.datetime_to_str(
                date, DateTimeFormatEnum.TIGHT_DAY_FORMAT)
            return CsvKit.send_csv(rst,
                                   filename=filename,
                                   fields=rst[0].keys())

        return ResponseSuccess(bs_data=rst).as_response()
示例#2
0
 def get_clean_date(cls):
     """
     获取清理日期
     :return:
     """
     return DateTimeKit.get_cur_date() - DateTimeKit.time_delta(
         days=cls.HOT_DAYS)
示例#3
0
 def validate_end_datetime(self, value):
     if value.data == '':
         self.end_datetime.data = DateTimeKit.get_day_begin_end(
             DateTimeKit.get_cur_date())[1]
     else:
         try:
             self.end_datetime.data = DateTimeKit.str_to_datetime(
                 value.data, DateTimeFormatEnum.SECONDS_FORMAT)
         except Exception as e:
             raise StopValidation('查询日期格式有误')
示例#4
0
    def get(self):
        """
        检查热表数据
        :return:
        """
        if not request.args:
            return ResponseSuccess(
                message="参数规则:?merchant=test&date=20190901").as_response()

        try:
            merchant = MerchantEnum.from_name(request.args['merchant'])
        except:
            return ResponseSuccess(message="请输入正确的商户名称,有效的商户名称包括:%s" %
                                   MerchantEnum.get_names()).as_response()

        try:
            date = request.args.get('date')
            if date:
                date = DateTimeKit.str_to_datetime(
                    date, DateTimeFormatEnum.TIGHT_DAY_FORMAT, to_date=True)
            else:
                date = DateTimeKit.get_cur_date()
        except:
            return ResponseSuccess(
                message="请输入有效的查询日期,格式为:20190901").as_response()

        kwargs = dict(
            date=date,
            merchant=merchant,
            only_hot=request.args.get('only_hot'),
            only_cold=request.args.get('only_cold'),
        )

        rst = dict(
            OrderDeposit=OrderDeposit.query_by_date(date, **kwargs).count(),
            OrderWithdraw=OrderWithdraw.query_by_date(date, **kwargs).count(),
            OrderDetailDeposit=OrderDetailDeposit.query_by_date(
                date, **kwargs).count(),
            OrderDetailWithdraw=OrderDetailWithdraw.query_by_date(
                date, **kwargs).count(),
        )
        kwargs['merchant'] = merchant.name
        kwargs['date'] = DateTimeKit.datetime_to_str(date)
        rst['kwargs'] = kwargs

        return ResponseSuccess(bs_data=rst).as_response()
示例#5
0
    def test_datetime_kit(self):
        t = DateTimeKit.gen_midnight_timestamp()
        self.print("gen_midnight_timestamp, t: %s" % t)
        self.assertIsInstance(t, int)

        d = DateTimeKit.timestamp_to_datetime(t)
        self.print("timestamp_to_datetime, d: %s" % d)
        self.assertIsInstance(d, datetime.datetime)

        t = DateTimeKit.datetime_to_timestamp(d)
        self.print("datetime_to_timestamp, t: %s" % t)
        self.assertIsInstance(t, int)

        s = DateTimeKit.datetime_to_str(d)
        self.print("datetime_to_str, s: %s" % s)
        self.assertIsInstance(s, str)

        d = DateTimeKit.str_to_datetime(s)
        self.print("str_to_datetime, d: %s" % d)
        self.assertIsInstance(d, datetime.datetime)

        d = DateTimeKit.get_cur_date()
        self.print("get_cur_date, d: %s" % d)
        self.assertIsInstance(d, datetime.date)

        t = DateTimeKit.datetime_to_timestamp(d)
        self.print("datetime_to_timestamp, t: %s" % t)
        self.assertIsInstance(t, int)

        s = DateTimeKit.datetime_to_str(d)
        self.print("datetime_to_str, s: %s" % s)
        self.assertIsInstance(s, str)

        cur_time = DateTimeKit.get_cur_datetime()
        rst = DateTimeKit.is_month_begin_time(cur_time)
        self.assertFalse(rst)

        someday = DateTimeKit.to_date(year=2019, month=1, day=1)
        rst = DateTimeKit.is_month_begin_time(someday)
        self.assertTrue(rst)

        someday = DateTimeKit.to_date(year=2019, month=2, day=2)
        rst = DateTimeKit.is_month_begin_time(someday)
        self.assertFalse(rst)
示例#6
0
    def test_order_transfer_log(self):
        data = dict(
            order_id=1,
            amount=Decimal("199.03"),
            in_account="239814112797121247",
            out_name='张三',
        )

        def check_data(_data, _model):
            self.assertEqual(_data['order_id'], _model.order_id)
            self.assertEqual(_data['amount'], _model.amount)
            self.assertEqual(_data['in_account'], _model.in_account)
            self.assertEqual(_data['out_name'], _model.out_name)

        model = OrderTransferLog.insert_transfer_log(**data)
        self.assertIsNotNone(model)
        check_data(data, model)

        model = OrderTransferLog.query_transfer_log(data['order_id'])
        self.assertIsNotNone(model)
        check_data(data, model)

        data['order_id'] = 1000
        model = OrderTransferLog.insert_transfer_log(**data)
        self.assertIsNotNone(model)
        check_data(data, model)

        model = OrderTransferLog.query_transfer_log(data['order_id'])
        self.assertIsNotNone(model)
        check_data(data, model)

        rst = OrderTransferLog.query_all()
        self.assertEqual(2, len(rst))

        rst = OrderTransferLog.delete_transfer_log_by_date(
            DateTimeKit.get_cur_date())
        self.assertEqual(2, rst)
        model = OrderTransferLog.query_transfer_log(data['order_id'])
        self.assertIsNone(model)

        rst = OrderTransferLog.query_all()
        self.assertFalse(rst)
示例#7
0
    def get(self):
        """
        订单修改日志
        :return:
        """
        if not request.args:
            return ResponseSuccess(
                message=
                "参数规则:?merchant=test&date=20190901&order_id=123&uid=123&ref_id=xxx&export=1,"
                "必填参数:merchant,"
                "可选参数:date,order_id,uid,ref_id,export,"
                "当不填写date时,默认查询当天所有的数据").as_response()

        try:
            date = request.args.get('date')
            if date:
                date = DateTimeKit.str_to_datetime(
                    date, DateTimeFormatEnum.TIGHT_DAY_FORMAT, to_date=True)
            else:
                date = DateTimeKit.get_cur_date()
        except:
            return ResponseSuccess(
                message="请输入有效的查询日期,格式为:20190901").as_response()

        q_params = dict()
        order_id = request.args.get('order_id')
        try:
            order_id = OrderUtils.parse_tx_id(order_id)
        except:
            pass
        if order_id:
            q_params['order_id'] = order_id
        uid = request.args.get('uid')
        if uid:
            q_params['uid'] = uid
        ref_id = request.args.get('ref_id')
        if ref_id:
            q_params['ref_id'] = ref_id
        if not q_params:
            return ResponseSuccess(
                message="必须输入 order_id/uid/ref_id 其中一个或多个参数").as_response()

        events = OrderEvent.query_model(query_fields=q_params, date=date)

        rst = list()

        for event in events:
            rst.append(
                dict(
                    create_time=event.create_time,
                    order_id=event.order_id,
                    uid=event.uid,
                    ref_id=event.ref_id,
                    data_before=event.data_before,
                    data_after=event.data_after,
                ))

        rst = sorted(rst, key=lambda x: x['create_time'], reverse=True)
        for x in rst:
            x['create_time'] = DateTimeKit.datetime_to_str(x['create_time'])

        if rst and request.args.get('export'):
            filename = 'order_events_%s.csv' % DateTimeKit.datetime_to_str(
                date, DateTimeFormatEnum.TIGHT_DAY_FORMAT)
            return CsvKit.send_csv(rst,
                                   filename=filename,
                                   fields=rst[0].keys())

        return ResponseSuccess(bs_data=rst).as_response()
示例#8
0
    def get(self):
        """
        查询用户余额变更流水
        :return:
        """
        if not request.args:
            return ResponseSuccess(
                message=
                "参数规则:?merchant=test&account=8618912341234&date=20190901&export=1"
            ).as_response()

        try:
            account = request.args.get('account')
            if account:
                account = '+' + account.strip('+').strip()
                if not PhoneNumberParser.is_valid_number(account):
                    raise
        except:
            return ResponseSuccess(
                message="请输入正确的用户手机号码,必须有完整区号,不填+号,如:8613812349999"
            ).as_response()

        try:
            merchant = MerchantEnum.from_name(request.args['merchant'])
        except:
            return ResponseSuccess(message="请输入正确的商户名称,有效的商户名称包括:%s" %
                                   MerchantEnum.get_names()).as_response()

        try:
            date = request.args.get('date')
            if date:
                date = DateTimeKit.str_to_datetime(
                    date, DateTimeFormatEnum.TIGHT_DAY_FORMAT, to_date=True)
            else:
                date = DateTimeKit.get_cur_date()
        except:
            return ResponseSuccess(
                message="请输入有效的查询日期,格式为:20190901").as_response()

        rst = dict(
            data=list(),
            sum_value=0,
        )

        events = UserBalanceEvent.query_by_date(date,
                                                merchant=merchant,
                                                date=date)
        if account:
            user = User.query_user(merchant, account=account)
            if not user:
                return ResponseSuccess(message="用户不存在,请检查参数。商户:%s,手机号码:%s" %
                                       (merchant.name, account))

            user_balance = UserBalance.query_balance(user.uid,
                                                     merchant).first()
            rst.update(user=dict(
                account=user.account,
                uid=user.uid,
                user_balance=str(user_balance.real_balance),
            ))
            events = events.filter_by(uid=user.uid)

        rst['sql'] = str(events)
        for event in events:
            rst['sum_value'] += event.value_real
            rst['data'].append(
                dict(
                    create_time=event.create_time,
                    uid=event.uid,
                    ref_id=event.ref_id,
                    order_type=event.order_type.desc,
                    source=event.source.desc,
                    bl_type=event.bl_type.desc,
                    value=str(event.value_real),
                    ad_type=event.ad_type.desc,
                    tx_id=event.tx_id,  # 大整数,转为字符串
                    comment=event.comment,
                    extra=event.raw_extra,
                ))

        rst['data'] = sorted(rst['data'],
                             key=lambda x: x['create_time'],
                             reverse=True)
        for x in rst['data']:
            x['create_time'] = DateTimeKit.datetime_to_str(x['create_time'])

        if rst['data'] and request.args.get('export'):
            filename = 'user_balance_events_%s.csv' % DateTimeKit.datetime_to_str(
                date, DateTimeFormatEnum.TIGHT_DAY_FORMAT)
            return CsvKit.send_csv(rst['data'],
                                   filename=filename,
                                   fields=rst['data'][0].keys())

        rst['sum_value'] = str(rst['sum_value'])

        return ResponseSuccess(bs_data=rst).as_response()
示例#9
0
    def get(self):
        """
        查询商户余额变更流水
        :return:
        """
        if not request.args:
            return ResponseSuccess(
                message="参数规则:?merchant=test&date=20190901&export=1"
            ).as_response()

        try:
            merchant = MerchantEnum.from_name(request.args['merchant'])
            merchant_info = MerchantInfo.query_merchant(merchant)
            if not merchant_info:
                raise
        except:
            return ResponseSuccess(message="请输入正确的商户名称,有效的商户名称包括:%s" %
                                   MerchantEnum.get_names()).as_response()

        try:
            date = request.args.get('date')
            if date:
                date = DateTimeKit.str_to_datetime(
                    date, DateTimeFormatEnum.TIGHT_DAY_FORMAT, to_date=True)
            else:
                date = DateTimeKit.get_cur_date()
        except:
            return ResponseSuccess(
                message="请输入有效的查询日期,格式为:20190901").as_response()

        balance = dict(
            balance_total=str(merchant_info.balance_total),
            balance_available=str(merchant_info.balance_available),
            balance_income=str(merchant_info.balance_income),
            balance_frozen=str(merchant_info.balance_frozen),
        )

        events = MerchantBalanceEvent.query_by_date(date,
                                                    merchant=merchant,
                                                    date=date)

        rst = dict(
            data=list(),
            sum_value=0,
            balance=balance,
        )

        rst['sql'] = str(events)

        for event in events:
            rst['sum_value'] += event.value_real
            rst['data'].append(
                dict(
                    create_time=event.create_time,
                    ref_id=event.ref_id,
                    order_type=event.order_type.desc,
                    source=event.source.desc,
                    bl_type=event.bl_type.desc,
                    value=str(event.value_real),
                    ad_type=event.ad_type.desc,
                    tx_id=event.tx_id,
                    comment=event.comment,
                ))

        rst['data'] = sorted(rst['data'],
                             key=lambda x: x['create_time'],
                             reverse=True)
        for x in rst['data']:
            x['create_time'] = DateTimeKit.datetime_to_str(x['create_time'])

        if rst['data'] and request.args.get('export'):
            filename = 'merchant_balance_events_%s.csv' % DateTimeKit.datetime_to_str(
                date, DateTimeFormatEnum.TIGHT_DAY_FORMAT)
            return CsvKit.send_csv(rst['data'],
                                   filename=filename,
                                   fields=rst['data'][0].keys())

        rst['sum_value'] = str(rst['sum_value'])

        return ResponseSuccess(bs_data=rst).as_response()
示例#10
0
    def query_withdraw_order_list(cls, form, export=False):
        """
        查询提现订单列表
        :param form:
        :param export:
        :return:
        """

        merchant = form.merchant_name.data

        if not form.begin_time.data:
            begin_time, end_time = DateTimeKit.get_day_begin_end(DateTimeKit.get_cur_date())
        else:
            begin_time = form.begin_time.data
            end_time = form.end_time.data

        kwargs = {}
        if form.state.data != "0":
            kwargs["_state"] = form.state.data.value

        tx_id = form.order_id.data
        if tx_id:
            if OrderUtils.is_sys_tx_id(tx_id):
                kwargs["sys_tx_id"] = tx_id
            else:
                kwargs["mch_tx_id"] = tx_id

        try:
            order_list = OrderWithdraw.query_by_create_time(begin_time=begin_time, end_time=end_time,
                                                            merchant=merchant).filter_by(**kwargs)
        except MultiMonthQueryException:
            return MultiMonthQueryError().as_response()

        all_channels = dict([(x.channel_id, x) for x in ChannelConfig.query_all()])
        order_detail_dict = dict()

        order_list = OrderFilters.filter_from_order_list(order_list, filters=[
            functools.partial(OrderFilters.filter_tx_id, tx_id),
            functools.partial(OrderFilters.filter_channel, all_channels, form.channel.data),
        ])

        if order_list:
            # 订单详情列表
            order_detail_list = OrderDetailWithdraw.query_by_create_time(begin_time=begin_time, end_time=end_time,
                                                                         merchant=merchant)

            # 订单列表和订单详情列表相互过滤
            order_list, order_detail_dict = OrderFilters.filter_from_detail_list(
                order_list, order_detail_list,
                filters=[
                    functools.partial(
                        OrderFilters.filter_done_time,
                        form.done_begin_time.data,
                        form.done_end_time.data),
                ])

            # 按时间倒序
            order_list = sorted(order_list, key=itemgetter('create_time'), reverse=True)

        if export and order_list:
            return CsvOrderExport.export_withdraw_list_csv(order_list, order_detail_dict, all_channels)

        return cls.render_withdraw_list(form, order_list, order_detail_dict)