Ejemplo n.º 1
0
 def validate_amount_max(self, value):
     try:
         if value.data:
             self.amount_max.data = Decimal(str(value.data))
             BalanceKit.multiple_hundred(self.amount_max.data)
         else:
             self.amount_max.data = 0
     except Exception as e:
         raise StopValidation("无效的 amount_max")
Ejemplo n.º 2
0
 def amount_max(self, value):
     if value:
         self._amount_max = BalanceKit.multiple_hundred(value)
     else:
         self._amount_max = 0
Ejemplo n.º 3
0
 def limit_day_max(self, value):
     if value != 0:
         self._limit_day_max = BalanceKit.multiple_hundred(value)
     else:
         self._limit_day_max = 0
Ejemplo n.º 4
0
 def limit_per_max(self, value):
     self._limit_per_max = BalanceKit.multiple_hundred(value)
Ejemplo n.º 5
0
 def fee(self, value):
     self._fee = BalanceKit.multiple_hundred(value)
Ejemplo n.º 6
0
 def validate_amount(self, value):
     try:
         self.amount.data = Decimal(value.data)
         BalanceKit.multiple_hundred(self.amount.data)
     except Exception as e:
         raise StopValidation("无效的 amount,必须是整数或浮点数字符串,最多保留2位小数")
Ejemplo n.º 7
0
 def tx_amount(self, value):
     self._tx_amount = BalanceKit.multiple_hundred(value)
Ejemplo n.º 8
0
 def offer(self, value):
     self._offer = BalanceKit.multiple_hundred(value)
Ejemplo n.º 9
0
 def cost(self, value):
     self._cost = BalanceKit.multiple_hundred(value)
Ejemplo n.º 10
0
 def profit(self, value):
     self._profit = BalanceKit.multiple_hundred(value)
Ejemplo n.º 11
0
 def incr_day_amount(self, channel: ChannelConfigEnum, value: Decimal):
     value = BalanceKit.multiple_hundred(value)
     ret = self.hincr(channel.name, value)
     self.update_expiration()
     return ret
Ejemplo n.º 12
0
 def value(self, value):
     self._value = BalanceKit.multiple_hundred(value)
Ejemplo n.º 13
0
    def update_balance(cls,
                       merchant: MerchantEnum,
                       ref_id,
                       source: OrderSourceEnum,
                       bl_type: BalanceTypeEnum,
                       value: float,
                       order_type: PayTypeEnum,
                       tx_id,
                       ad_type: BalanceAdjustTypeEnum = None,
                       comment: str = '',
                       create_time=None,
                       commit=True):
        """
        修改余额
        :param merchant:
        :param ref_id: 32位的md5ID
        :param source: 操作来源
        :param order_type: 订单类型
        :param bl_type: 余额类型
        :param ad_type: 当人工操作时,必填调整类型;订单类型的调整不需要填写ad_type
        :param value: 始终传入正整数
        :param comment: 当人工调整时,必填备注信息
        :param create_time:
        :param tx_id: 交易流水号
        :param commit: 是否立即提交事务
        :return: (result, msg)
        """
        # 记录一个参数日志, locals自动收集这行代码之前出现过的局部变量
        params = copy.deepcopy(locals())
        params.pop('cls')

        # current_app.config['SENTRY_DSN'] and current_app.logger.info('update_balance, params: %s', params)

        rst, msg = cls.__check_event_params(params)
        if rst != 0:
            return rst, msg

        if ad_type is None:
            if source == PayTypeEnum.DEPOSIT:
                # 存款+
                ad_type = BalanceAdjustTypeEnum.PLUS
            else:
                # 提款-
                ad_type = BalanceAdjustTypeEnum.MINUS

        # 金额入库乘100转换为分钱
        value = BalanceKit.multiple_hundred(value)

        if ad_type == BalanceAdjustTypeEnum.MINUS:
            # 做减法
            value = -value

        try:
            with db.auto_commit(commit):
                # 生成修改事件
                create_time = create_time or DateTimeKit.get_cur_datetime()
                fields = dict(
                    source=source,
                    order_type=order_type,
                    bl_type=bl_type,
                    value=value,
                    tx_id=tx_id,
                    ad_type=ad_type,
                    comment=comment,
                    ref_id=ref_id,
                    create_time=create_time,
                    extra=dict(),
                )
                rst = cls.add_model(fields,
                                    merchant=merchant,
                                    date=create_time)
                db.session.add(rst['model'])

                # 更新商户余额
                before_merchant, after_merchant = cls.__update_balance(
                    merchant, source, order_type, bl_type, ad_type, value)
                # 添加admin日志
                if source == OrderSourceEnum.MANUALLY:
                    log_model = cls.add_admin_log(
                        data_before=before_merchant.get_before_fields(),
                        data_after=after_merchant.get_after_fields(),
                    )
                    log_model and db.session.add(log_model)

        except RuntimeError as e:
            # 捕获异常,返回失败
            msg = "%s, params: %s" % (str(e), params)
            current_app.config['SENTRY_DSN'] and current_app.logger.fatal(msg)
            return -100, str(e)

        except InsufficientBalanceException as e:
            msg = str(e)
            if bl_type == BalanceTypeEnum.AVAILABLE:
                msg = "可用余额不足"
            if bl_type == BalanceTypeEnum.INCOME:
                msg = "在途余额不足"
            if bl_type == BalanceTypeEnum.FROZEN:
                if ad_type == BalanceAdjustTypeEnum.PLUS:
                    msg = "可用余额不足"
                else:
                    msg = "冻结余额不足"
            return -101, msg

        return 0, ''