예제 #1
0
 def covert_data_to_dict(cls, value):
     data = dict()
     for k, v in value.items():
         if isinstance(v, Enum):
             v = v.to_dict()
         elif isinstance(v, Decimal):
             v = str(v)
         elif isinstance(v, (datetime.datetime, datetime.date)):
             v = DateTimeKit.datetime_to_timestamp(v)
         elif isinstance(v, dict):
             v = cls.covert_data_to_dict(v)
         elif isinstance(v, list):
             v_list = list()
             for x in v:
                 if isinstance(x, Enum):
                     x = x.to_dict()
                 elif isinstance(x, Decimal):
                     x = str(x)
                 elif isinstance(x, (datetime.datetime, datetime.date)):
                     x = DateTimeKit.datetime_to_timestamp(v)
                 elif isinstance(x, dict):
                     x = cls.covert_data_to_dict(x)
                 v_list.append(x)
             v = v_list
         data[k] = v
     return data
예제 #2
0
    def query_transfer_log_by_user_info(cls,
                                        deposit_cardnumber,
                                        amount,
                                        client_accountname=None,
                                        deposit_time=None):

        query_params = dict(in_account=deposit_cardnumber,
                            amount=amount,
                            state=OrderStateEnum.INIT)
        if client_accountname:
            query_params['out_name'] = client_accountname
        if deposit_time:
            begin_time = DateTimeKit.datetime_to_timestamp(
                DateTimeKit.str_to_datetime(deposit_time) +
                DateTimeKit.time_delta(minutes=-180))
            end_time = DateTimeKit.datetime_to_timestamp(
                DateTimeKit.str_to_datetime(deposit_time) +
                DateTimeKit.time_delta(minutes=80))
            if client_accountname:
                return cls.query.filter(
                    cls._create_time >= begin_time,
                    cls._create_time <= end_time
                ).filter(cls.in_account == deposit_cardnumber).filter(
                    cls._amount == int(Decimal(amount) * Decimal(100))).filter(
                        cls._state == OrderStateEnum.INIT.value).filter(
                            cls.out_name == client_accountname).all()
            else:
                return cls.query.filter(
                    cls._create_time >= begin_time,
                    cls._create_time <= end_time
                ).filter(cls.in_account == deposit_cardnumber).filter(
                    cls._amount == int(Decimal(amount) * Decimal(100))).filter(
                        cls._state == OrderStateEnum.INIT.value).all()

        return cls.query_model(query_fields=query_params).all()
예제 #3
0
파일: base.py 프로젝트: LyanJin/check-pay
 def query_by_create_time(cls, begin_time, end_time, **kwargs):
     """
     根据时间查询数据,包含 begin_time/end_time 边界值
     :param begin_time:
     :param end_time:
     :return: 返回查询对象
     """
     begin_time = DateTimeKit.datetime_to_timestamp(begin_time)
     end_time = DateTimeKit.datetime_to_timestamp(end_time)
     return cls.query.filter(cls._create_time.between(begin_time, end_time))
예제 #4
0
    def post(self):
        """
        用户列表
        :return:
        """
        form, error = UserListSelectForm.request_validate()
        if error:
            return error.as_response()

        kwargs = dict()
        if form.phone_number.data:
            phone_number = str(form.phone_number.data)
            if phone_number.find("+") < 0:
                phone_number = "{}{}".format("+", phone_number)
            kwargs['account'] = phone_number

        user_query = User.query.filter_by(**kwargs)
        if form.start_datetime.data:
            user_query = user_query.filter(
                User._create_time >= DateTimeKit.datetime_to_timestamp(
                    form.start_datetime.data))
        if form.end_datetime.data:
            user_query = user_query.filter(
                User._create_time <= DateTimeKit.datetime_to_timestamp(
                    form.end_datetime.data))
        paginate = user_query.paginate(form.page_index.data,
                                       form.page_size.data, False)

        total = paginate.total
        user_lst = paginate.items

        uid_lst = [u.id for u in user_lst]
        uid_merchant = UserBalance.query.filter(
            UserBalance.id.in_(uid_lst)).all()
        mer_bl_uid = {
            uid.id: dict(balance=uid.real_balance, merchant=uid.merchant)
            for uid in uid_merchant
        }

        data = [
            dict(user_id=u.id,
                 phone_number=PhoneNumberParser.hide_number(u.account),
                 type='测试用户' if u.is_test_user else "普通用户",
                 source=mer_bl_uid[u.id]['merchant'].name,
                 available_bl=mer_bl_uid[u.id]['balance'],
                 register_datetime=u.str_create_time,
                 state=u.state.name) for u in user_lst
        ]

        return UserListResult(
            bs_data=dict(entries=data, total=total)).as_response()
예제 #5
0
    def query_by_create_time(cls, begin_time, end_time, *args, **kwargs):
        """
        根据时间查询数据,包含begin_time, end_time,[begin_time, end_time]
        :param begin_time:
        :param end_time:
        :return: 返回查询对象
        """
        begin_ts = DateTimeKit.datetime_to_timestamp(begin_time)
        end_ts = DateTimeKit.datetime_to_timestamp(end_time)

        model_cls = kwargs.get('model_cls') or cls.get_model_cls(
            *args, **kwargs)
        return model_cls.query.filter(model_cls._create_time >= begin_ts,
                                      model_cls._create_time <= end_ts)
예제 #6
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)
예제 #7
0
파일: base.py 프로젝트: LyanJin/check-pay
 def create_time(self, value):
     self._create_time = DateTimeKit.datetime_to_timestamp(value)
예제 #8
0
 def maintain_end(self, value):
     if value:
         self._maintain_end = DateTimeKit.datetime_to_timestamp(value)
     else:
         self._maintain_end = 0
예제 #9
0
 def maintain_begin(self, value):
     if value:
         self._maintain_begin = DateTimeKit.datetime_to_timestamp(value)
     else:
         self._maintain_begin = 0
예제 #10
0
 def done_time(self, value):
     self._done_time = DateTimeKit.datetime_to_timestamp(value)
예제 #11
0
 def alloc_time(self, value):
     self._alloc_time = DateTimeKit.datetime_to_timestamp(value)
예제 #12
0
    def clean_hot_table(cls, seconds=0, clean_date=None):
        """
        清理热数据
        :param seconds: 从db删除一次后等待多少秒
        :param clean_date: 清理哪一天的数据
        :return:
        """
        clean_result = list()

        if clean_date:
            clean_date = DateTimeKit.to_date(clean_date)
            if clean_date > cls.get_clean_date():
                raise RuntimeError('invalid clean date, input: %s, fix: %s',
                                   clean_date, cls.get_clean_date())
        else:
            # 清理 HOT_DAYS 天之前的那一天的数据
            clean_date = cls.get_clean_date()

        for kls in cls._cls_mapper.values():

            if not kls.is_hot_active():
                # 未开启冷/热库的跳过
                continue

            if kls.is_cold_table():
                # 跳过冷库
                continue

            def get_query_obj(_begin_ts, _end_ts):
                return kls.query.filter(kls._create_time >= _begin_ts,
                                        kls._create_time <= _end_ts)

            begin_time, end_time = DateTimeKit.get_day_begin_end(clean_date)
            begin_ts = DateTimeKit.datetime_to_timestamp(begin_time)
            end_ts = DateTimeKit.datetime_to_timestamp(end_time)

            # 先查出总共多少条数据
            counts = get_query_obj(begin_ts, end_ts).count()
            if counts == 0:
                continue

            current_app.logger.info(
                'begin, clean_date: %s, kls: %s, counts: %s', clean_date,
                kls.inspect(), counts)

            with db.auto_commit():

                # 分批按小时删除
                begin_ts_1 = begin_ts
                for end_time in DateTimeKit.gen_hour_range(clean_date):
                    end_ts_1 = DateTimeKit.datetime_to_timestamp(end_time)
                    get_query_obj(begin_ts_1,
                                  end_ts_1).delete(synchronize_session="fetch")
                    begin_ts_1 = end_ts_1
                    seconds and time.sleep(seconds)

                # 删除剩余的
                get_query_obj(begin_ts,
                              end_ts).delete(synchronize_session="fetch")

            counts = get_query_obj(begin_ts, end_ts).count()
            if counts != 0:
                current_app.logger.error(
                    'clean hot orders failed, clean_date: %s, kls: %s, counts: %s',
                    clean_date, kls.inspect(), counts)
            else:
                current_app.logger.info(
                    'end, clean_date: %s, kls: %s, counts: %s', clean_date,
                    kls.inspect(), counts)

            seconds and time.sleep(seconds)

            clean_result.append(
                dict(
                    kls=kls.inspect(),
                    clean_date=clean_date,
                    clean_total=counts,
                ))

        return clean_result