Пример #1
0
    def post(self):
        """
        商户列表
        :return:
        """
        merchant_list = list()

        merchants = MerchantInfo.query_all()

        all_configs = MerchantFeeConfig.query_all()
        all_configs = MerchantFeeConfig.filter_latest_items(all_configs)

        merchant_all_configs = defaultdict(list)
        for fee in all_configs:
            merchant_all_configs[fee.merchant].append(fee)

        for merchant in merchants:
            fee_configs = merchant_all_configs.get(merchant.merchant)
            if not fee_configs:
                continue

            withdraw_fees = [x for x in fee_configs if x.payment_way == PayTypeEnum.WITHDRAW]
            recharge_fees = [x for x in fee_configs if x.payment_way == PayTypeEnum.DEPOSIT]

            withdraw_desc = ''
            cost_type = None
            if withdraw_fees:
                withdraw_desc = withdraw_fees[0].value_description
                cost_type = withdraw_fees[0].cost_type.name

            merchant_list.append(dict(
                id=merchant.id,
                name=merchant.merchant.name,
                balance_total=merchant.balance_total,
                balance_available=merchant.balance_available,
                balance_income=merchant.balance_income,
                balance_frozen=merchant.balance_frozen,
                type=merchant.m_type.name,
                domains='\n'.join(MerchantDomainConfig.get_domains(merchant.merchant)),
                state=merchant.state.name,
                channel_fees=dict(
                    withdraw=withdraw_desc,
                    cost_type=cost_type,
                    deposit=[
                        dict(
                            desc=x.payment_method.desc,
                            value=x.payment_method.value,
                            rate=x.value_description,
                        )
                        for x in recharge_fees
                    ]
                )
            ))

        data = dict(counts=len(merchant_list), merchants=merchant_list)

        return MerchantListResult(bs_data=data).as_response()
Пример #2
0
    def test_merchant_fee_config_model(self):
        # 添加测试
        count = 0
        latest_count = 0
        for merchant in MerchantEnum:
            for payment_way in PayTypeEnum:
                if payment_way == PayTypeEnum.DEPOSIT:
                    for payment_method in PayMethodEnum:
                        for fee_type in PaymentFeeTypeEnum:
                            params = [
                                dict(
                                    merchant=merchant,
                                    payment_way=payment_way,
                                    payment_method=payment_method,
                                    fee_type=fee_type,
                                    value=Decimal("1.22"),
                                ),
                            ]
                            self.add_one_config(merchant, params)
                            count += 1
                        latest_count += 1
                else:
                    for fee_type in PaymentFeeTypeEnum:
                        params = [
                            dict(
                                merchant=merchant,
                                payment_way=payment_way,
                                fee_type=fee_type,
                                value=Decimal("3.22"),
                            ),
                        ]
                        self.add_one_config(merchant, params)
                        count += 1
                    latest_count += 1

        all_configs = MerchantFeeConfig.query_all()
        add_num = len(list(all_configs))
        self.assertEqual(count, add_num)

        latest_configs = MerchantFeeConfig.filter_latest_items(all_configs)
        x_latest_count = len(list(latest_configs))
        self.assertEqual(len(MerchantEnum.get_names()), x_latest_count)

        # 修改测试
        count = 0
        for merchant in MerchantEnum:
            for payment_way in PayTypeEnum:
                if payment_way == PayTypeEnum.DEPOSIT:
                    for payment_method in PayMethodEnum:
                        for fee_type in PaymentFeeTypeEnum:
                            params = [
                                dict(
                                    merchant=merchant,
                                    payment_way=payment_way,
                                    payment_method=payment_method,
                                    fee_type=fee_type,
                                    value=Decimal("1.33"),
                                ),
                            ]
                            self.add_one_config(merchant, params)
                            count += 1
                else:
                    for fee_type in PaymentFeeTypeEnum:
                        params = [
                            dict(
                                merchant=merchant,
                                payment_way=payment_way,
                                fee_type=fee_type,
                                value=Decimal("2.33"),
                            ),
                        ]
                        self.add_one_config(merchant, params)
                        count += 1

        add_num += count

        # 更新不会增加条目
        all_configs = MerchantFeeConfig.query_all()
        num = len(list(all_configs))
        self.assertEqual(add_num, num)

        latest_configs = MerchantFeeConfig.filter_latest_items(all_configs)
        x_latest_count = len(list(latest_configs))
        self.assertEqual(len(MerchantEnum.get_names()), x_latest_count)

        # 批量修改测试
        count = 0
        valid_count = 0
        for merchant in MerchantEnum:
            params = []
            for payment_way in PayTypeEnum:
                if payment_way == PayTypeEnum.DEPOSIT:
                    for payment_method in PayMethodEnum:
                        params.append(dict(
                            merchant=merchant,
                            payment_way=payment_way,
                            payment_method=payment_method,
                            fee_type=PaymentFeeTypeEnum.PERCENT_PER_ORDER,
                            value=Decimal("1.22"),
                        ))
                        count += 1
                else:
                    params.append(dict(
                        merchant=merchant,
                        payment_way=payment_way,
                        fee_type=PaymentFeeTypeEnum.PERCENT_PER_ORDER,
                        value=Decimal("3.22"),
                    ))
                    count += 1

            valid_count += len(params)
            self.add_one_config(merchant, params)

        add_num += count

        all_configs = MerchantFeeConfig.query_all()
        num = len(list(all_configs))
        self.assertEqual(add_num, num)

        latest_configs = MerchantFeeConfig.filter_latest_items(all_configs)
        x_latest_count = len(list(latest_configs))
        self.assertEqual(valid_count, x_latest_count)

        MerchantFeeConfig.delete_all()
        num = len(list(MerchantFeeConfig.query_all()))
        self.assertEqual(0, num)