<commit_before>"""

@author: <NAME>

@time: 2017.08.07

@desc: service for appointment

1. appointment Add/Get/Modify/Remove

1. get by id
2. get by user
3. get by e_bike_model

4. remove by id

5. modify status
"""

from datetime import datetime
from datetime import timedelta

from server.database.model import Appointment, EBikeModel
from server.service import storage_service
from server.service import serial_number_service
from server.service import virtual_card_service
from server.service import coupon_service
from server.service import refund_table_service

# from server.service import e_bike_model_service

from server.utility.exception import WrongSerialsNumber, Error

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


# from server.utility.json_utility import models_to_json


# ***************************** buy appointment ***************************** #
# 1.生成订单
def add_appointment(**kwargs):
    user = kwargs["user"]
    e_bike_model = kwargs["e_bike_model"]
    color = kwargs["color"]
    e_bike_type = kwargs["type"]
    coupon = kwargs.get("coupon")
    rent_time_period = kwargs.get("rent_time_period")

    e_bike_model = EBikeModel.get(name=e_bike_model)

    # 检查库存量，虽然库存不足时前端会生不成订单
    if not storage_service.check_storage(model=e_bike_model, color=color):
        raise Error("not enough storage")

    # 检查该用户是否存在订单 (买车订单数）
    if not check_user_appointment(user=user, type=e_bike_type):
        raise Error("too much appointment")

    # 租车订单 检查是否存在押金
    if e_bike_type == "租车":
        if not virtual_card_service.check_deposit(username=user):
            raise Error("no deposit in virtual_card")

    if e_bike_type == "租车":
        price = e_bike_model.price[rent_time_period]
    else:
        price = e_bike_model.price

    # 库存-1
    storage_service.decrement_num(e_bike_model, color)

    # 使用优惠劵
    if coupon:
        reduced_price = coupon_service.use_coupon(user, coupon, price)
        price = price - reduced_price
        kwargs["reduced_price"] = reduced_price
    kwargs["price"] = price
    appointment = add(**kwargs)

    # 获取有效的 serial_number
    serial_number = serial_number_service.get_available_code(appointment)
    # 更改 serial_number
    appointment.serial_number = serial_number
    appointment.save()
    return appointment


# 2. 预付款成功
def appointment_payment_success(user, appointment_id):
    appointment = Appointment.get(
        id=appointment_id,
        user=user)
    appointment.status = APPOINTMENT_STATUS["1"]
    # 预付款后更改新的过期日期
    appointment.expired_date_time = \
        datetime.now() + timedelta(
            days=get_custom_const("APPOINTMENT_EXPIRED_DAYS"))
    return appointment.save()


# 3. 提车码
def upload_serial_number(user, appointment_id, serial_number):
    appointment = get(id=appointment_id, user=user)
    if check_serial_number(appointment, serial_number):
        return True
    raise WrongSerialsNumber("wrong serials number")


# 4. 付款成功
def total_payment_success(user, appointment_id):
    appointment = Appointment.get(
        id=appointment_id,
        user=user)

    # 检查是否租车
    if appointment.type == "租车":  # 租车
        rent_time_period = appointment.rent_time_period
        end_time = \
            datetime.now() + timedelta(days=RENT_TIME_PERIOD[rent_time_period])
        appointment.end_time = end_time
        status = APPOINTMENT_STATUS["2"]
        appointment.status = status
        return appointment.save()

    else:
        status = APPOINTMENT_STATUS["3"]<commit_msg>fix python bug<commit_after>"""

@author: <NAME>

@time: 2017.08.07

@desc: service for appointment

1. appointment Add/Get/Modify/Remove

1. get by id
2. get by user
3. get by e_bike_model

4. remove by id

5. modify status
"""

from datetime import datetime
from datetime import timedelta

from server.database.model import Appointment, EBikeModel
from server.service import storage_service
from server.service import serial_number_service
from server.service import virtual_card_service
from server.service import coupon_service
from server.service import refund_table_service

# from server.service import e_bike_model_service

from server.utility.exception import WrongSerialsNumber, Error

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


# from server.utility.json_utility import models_to_json


# ***************************** buy appointment ***************************** #
# 1.生成订单
def add_appointment(**kwargs):
    user = kwargs["user"]
    e_bike_model = kwargs["e_bike_model"]
    color = kwargs["color"]
    e_bike_type = kwargs["type"]
    coupon = kwargs.get("coupon")
    rent_time_period = kwargs.get("rent_time_period")

    e_bike_model = EBikeModel.get(name=e_bike_model)

    # 检查库存量，虽然库存不足时前端会生不成订单
    if not storage_service.check_storage(model=e_bike_model, color=color):
        raise Error("not enough storage")

    # 检查该用户是否存在订单 (买车订单数）
    if not check_user_appointment(user=user, type=e_bike_type):
        raise Error("too much appointment")

    # 租车订单 检查是否存在押金
    if e_bike_type == "租车":
        if not virtual_card_service.check_deposit(username=user):
            raise Error("no deposit in virtual_card")

    if e_bike_type == "租车":
        price = e_bike_model.price[rent_time_period]
    else:
        price = e_bike_model.price

    # 库存-1
    storage_service.decrement_num(e_bike_model, color)

    # 使用优惠劵
    if coupon:
        reduced_price = coupon_service.use_coupon(user, coupon, price)
        price = price - reduced_price
        kwargs["reduced_price"] = reduced_price
    kwargs["price"] = price
    appointment = add(**kwargs)

    # 获取有效的 serial_number
    serial_number = serial_number_service.get_available_code(appointment)
    # 更改 serial_number
    appointment.serial_number = serial_number
    appointment.save()
    return appointment


# 2. 预付款成功
def appointment_payment_success(user, appointment_id):
    appointment = Appointment.get(
        id=appointment_id,
        user=user)
    appointment.status = APPOINTMENT_STATUS["1"]
    # 预付款后更改新的过期日期
    appointment.expired_date_time = \
        datetime.now() + timedelta(
            days=get_custom_const("APPOINTMENT_EXPIRED_DAYS"))
    return appointment.save()


# 3. 提车码
def upload_serial_number(user, appointment_id, serial_number):
    appointment = get(id=appointment_id, user=user)
    if check_serial_number(appointment, serial_number):
        return True
    raise WrongSerialsNumber("wrong serials number")


# 4. 付款成功
def total_payment_success(user, appointment_id):
    appointment = Appointment.get(
        id=appointment_id,
        user=user)

    # 检查是否租车
    if appointment.type == "租车":  # 租车
        rent_time_period = appointment.rent_time_period
        end_time = \
            datetime.now() + timedelta(days=RENT_TIME_PERIOD[rent_time_period])
        appointment.end_time = end_time
        status = RENT_APPOINTMENT_STATUS["2"]
        appointment.status = status
        return appointment.save()

    else:
        status = APPOINTMENT_STATUS["3"]
{'fraction_non_alphanumeric': 0.10084033613445378, 'fraction_numerical': 0.0061624649859943975, 'mean_word_length': 3.7574950033311127, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 5, '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': '665523', 'n_tokens_mistral': 2428, 'n_tokens_neox': 2441, 'n_words': 479}