<commit_before># -*- coding: UTF-8 -*-
"""
@author: <EMAIL>
@time: 8/10/17
@desc: virtual card service
"""
from datetime import datetime

from server.database.model import VirtualCard
from server.database.model import ConsumeRecord
from server.database.model import Battery
from server.service import refund_table_service
from server.service import battery_rent_service

from server.utility.constant.custom_constant import get_custom_const
from server.utility.exception import *


# from playhouse.shortcuts import model_to_dict
# from server.utility.json_utility import models_to_json


def add(**kwargs):
    """
    add new virtual card to database
    :param kwargs: parameters to insert to database
    :return:
    """
    virtual_card = VirtualCard.create(**kwargs)
    return virtual_card


def get_all():
    virtual_cards = VirtualCard.select()
    return virtual_cards


# def get_deposit(card_no):
#     """
#     check if the card is deposited
#     :param card_no: card number
#     :return: True of False
#     """
#     # 检查电池租用情况，超过一个月冻结账户
#
#     if not battery_rent_service.check_on_load_batter_rent_date(
#             username=card_no):
#         # 冻结账号
#         result = freeze(card_no)
#         if not result:
#             print("冻结失败", card_no)
#         raise Error("账号已冻结")
#     virtual_card = VirtualCard.get(VirtualCard.card_no == card_no)
#     return virtual_card.deposit


def pre_pay_deposit(**kwargs):
    card_no = kwargs["card_no"]

    deposit_fee = get_custom_const("DEFAULT_DEPOSIT")
    virtual_card = VirtualCard.get(VirtualCard.card_no == card_no)
    deposit = virtual_card.deposit
    if deposit >= deposit_fee:
        # 充值押金失败
        ConsumeRecord.create(
            card=card_no,
            consume_event="deposit failed",
            consume_date_time=datetime.utcnow(),
            consume_fee=deposit_fee,
            balance=virtual_card.balance)

        raise Error("无需充值押金")
    return deposit_fee


def pay_deposit(**kwargs):
    """
    pay deposit
    :param data:
        card_no: card number
        deposit_fee: deposit amount
    :return:
    """
    card_no = kwargs["card_no"]
    deposit_fee = float(kwargs["deposit_fee"])

    out_trade_no = kwargs["out_trade_no"]

    virtual_card = VirtualCard.get(VirtualCard.card_no == card_no)

    virtual_card.deposit = deposit_fee
    result = virtual_card.save()
    record = ConsumeRecord.create(
        card=card_no,
        consume_event="deposit",
        consume_date_time=datetime.utcnow(),
        consume_fee=deposit_fee,
        balance=virtual_card.balance,
        out_trade_no=out_trade_no
    )
    # 给用户虚拟卡也存一个订单号
    virtual_card = VirtualCard.get(card_no=card_no)
    virtual_card.out_trade_no = out_trade_no
    virtual_card.save()

    return result, record


def pre_top_up(**kwargs):
    """
    1. check
    :param kwargs:
    :type kwargs:
    :return:
    :rtype:
    """
    card_no = kwargs["card_no"]
    virtual_card = VirtualCard.get(VirtualCard.card_no == card_no)
    if virtual_card.deposit < get_custom_const("DEFAULT_DEPOSIT"):
        raise Error("No deposit")


def top_up(**kwargs):
    """

    2. top up virtual card
    :param data:
        card_no: card number
        top_up_fee: top up amount
    :return:
    """
    card_no = kwargs["card_no"]
    top_up_fee = float(kwargs["top_up_fee"])

    virtual_card = VirtualCard.get(VirtualCard.card_no == card_no)

    if virtual_card.deposit < get_custom_const("DEFAULT_DEPOSIT"):
        raise Error("No deposit")
    else:
        balance = virtual_card.balance + top_up_fee
        virtual_card.balance = balance
        result = virtual_card.save()
        record = ConsumeRecord.create(
            card=card_no,
            consume_event="top up",
            consume_date_time=datetime.utcnow(),
            consume_fee=top_up_fee,
            balance=balance)
        return result, record


# def get_card_balance(card_no):
#     """
#     get card balance
#     :param card_no: card number
#     :return: balance
#     """
#     virtual_card = VirtualCard.get(VirtualCard.card_no == card_no)
#     return virtual_card.balance


def return_deposit(**kwargs):
    """
    return deposit
    :param kwargs:
        card_no: card number
    :return:
    """
    card_no = kwargs["card_no"]
    # 获取支付押金订单号
    out_trade_no = VirtualCard.get(card_no=card_no).out_trade_no

    on_loan = Battery.select().where(Battery.user == card_no)
    if on_loan:
        raise Error("Battery in use")

    virtual_card = VirtualCard.get(VirtualCard.card_no == card_no)
    deposit = virtual_card.deposit
    if deposit <= 0:
        raise Error("无可退还押金")
    else:
        virtual_card.deposit = 0
        result = virtual_card.save()
        record = ConsumeRecord.create(
            card=card_no,
            consume_event="return deposit",
            consume_date_time=datetime.utcnow(),
            consume_fee=-deposit,<commit_msg>修改error message<commit_after># -*- coding: UTF-8 -*-
"""
@author: <EMAIL>
@time: 8/10/17
@desc: virtual card service
"""
from datetime import datetime

from server.database.model import VirtualCard
from server.database.model import ConsumeRecord
from server.database.model import Battery
from server.service import refund_table_service
from server.service import battery_rent_service

from server.utility.constant.custom_constant import get_custom_const
from server.utility.exception import *


# from playhouse.shortcuts import model_to_dict
# from server.utility.json_utility import models_to_json


def add(**kwargs):
    """
    add new virtual card to database
    :param kwargs: parameters to insert to database
    :return:
    """
    virtual_card = VirtualCard.create(**kwargs)
    return virtual_card


def get_all():
    virtual_cards = VirtualCard.select()
    return virtual_cards


# def get_deposit(card_no):
#     """
#     check if the card is deposited
#     :param card_no: card number
#     :return: True of False
#     """
#     # 检查电池租用情况，超过一个月冻结账户
#
#     if not battery_rent_service.check_on_load_batter_rent_date(
#             username=card_no):
#         # 冻结账号
#         result = freeze(card_no)
#         if not result:
#             print("冻结失败", card_no)
#         raise Error("账号已冻结")
#     virtual_card = VirtualCard.get(VirtualCard.card_no == card_no)
#     return virtual_card.deposit


def pre_pay_deposit(**kwargs):
    card_no = kwargs["card_no"]

    deposit_fee = get_custom_const("DEFAULT_DEPOSIT")
    virtual_card = VirtualCard.get(VirtualCard.card_no == card_no)
    deposit = virtual_card.deposit
    if deposit >= deposit_fee:
        # 充值押金失败
        ConsumeRecord.create(
            card=card_no,
            consume_event="deposit failed",
            consume_date_time=datetime.utcnow(),
            consume_fee=deposit_fee,
            balance=virtual_card.balance)

        raise Error("无需充值押金")
    return deposit_fee


def pay_deposit(**kwargs):
    """
    pay deposit
    :param data:
        card_no: card number
        deposit_fee: deposit amount
    :return:
    """
    card_no = kwargs["card_no"]
    deposit_fee = float(kwargs["deposit_fee"])

    out_trade_no = kwargs["out_trade_no"]

    virtual_card = VirtualCard.get(VirtualCard.card_no == card_no)

    virtual_card.deposit = deposit_fee
    result = virtual_card.save()
    record = ConsumeRecord.create(
        card=card_no,
        consume_event="deposit",
        consume_date_time=datetime.utcnow(),
        consume_fee=deposit_fee,
        balance=virtual_card.balance,
        out_trade_no=out_trade_no
    )
    # 给用户虚拟卡也存一个订单号
    virtual_card = VirtualCard.get(card_no=card_no)
    virtual_card.out_trade_no = out_trade_no
    virtual_card.save()

    return result, record


def pre_top_up(**kwargs):
    """
    1. check
    :param kwargs:
    :type kwargs:
    :return:
    :rtype:
    """
    card_no = kwargs["card_no"]
    virtual_card = VirtualCard.get(VirtualCard.card_no == card_no)
    if virtual_card.deposit < get_custom_const("DEFAULT_DEPOSIT"):
        raise Error("No deposit")


def top_up(**kwargs):
    """

    2. top up virtual card
    :param data:
        card_no: card number
        top_up_fee: top up amount
    :return:
    """
    card_no = kwargs["card_no"]
    top_up_fee = float(kwargs["top_up_fee"])

    virtual_card = VirtualCard.get(VirtualCard.card_no == card_no)

    if virtual_card.deposit < get_custom_const("DEFAULT_DEPOSIT"):
        raise Error("No deposit")
    else:
        balance = virtual_card.balance + top_up_fee
        virtual_card.balance = balance
        result = virtual_card.save()
        record = ConsumeRecord.create(
            card=card_no,
            consume_event="top up",
            consume_date_time=datetime.utcnow(),
            consume_fee=top_up_fee,
            balance=balance)
        return result, record


# def get_card_balance(card_no):
#     """
#     get card balance
#     :param card_no: card number
#     :return: balance
#     """
#     virtual_card = VirtualCard.get(VirtualCard.card_no == card_no)
#     return virtual_card.balance


def return_deposit(**kwargs):
    """
    return deposit
    :param kwargs:
        card_no: card number
    :return:
    """
    card_no = kwargs["card_no"]
    # 获取支付押金订单号
    out_trade_no = VirtualCard.get(card_no=card_no).out_trade_no

    on_loan = Battery.select().where(Battery.user == card_no)
    if on_loan:
        raise Error("电池使用中")

    virtual_card = VirtualCard.get(VirtualCard.card_no == card_no)
    deposit = virtual_card.deposit
    if deposit <= 0:
        raise Error("无可退还押金")
    else:
        virtual_card.deposit = 0
        result = virtual_card.save()
        record = ConsumeRecord.create(
            card=card_no,
            consume_event="return deposit",
            consume_date_time=datetime.utcnow(),
            consume_fee=-deposit,
{'fraction_non_alphanumeric': 0.10059831660075043, 'fraction_numerical': 0.0020281918669506137, 'mean_word_length': 2.8433359314107562, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6336790', 'n_tokens_mistral': 3299, 'n_tokens_neox': 3260, 'n_words': 624}