示例#1
0
def order():

    order_dict = request.form

    house_id = order_dict.get('house_id')
    start_time = datetime.strptime(order_dict.get('start_time'), '%Y-%m-%d')
    end_time = datetime.strptime(order_dict.get('end_time'), '%Y-%m-%d')

    if not all([house_id, start_time, end_time]):
        return jsonify(status_code.PARAMS_ERROR)

    if start_time > end_time:
        return jsonify(status_code.ORDER_START_TIME_GT_END_TIME)

    house = House.query.get(house_id)

    order = Order()
    order.user_id = session['user_id']
    order.house_id = house_id
    order.begin_date = start_time
    order.end_date = end_time
    order.house_price = house.price
    order.days = (end_time - start_time).days + 1
    order.amount = order.days * order.house_price

    try:
        order.add_update()
        return jsonify(code=status_code.OK)
    except:
        return jsonify(status_code.DATABASE_ERROR)
示例#2
0
def create_order():
    # 创建,提交订单
    #:house_id, ⼊入住时间和离开时间
    json_dict = request.json
    house_id = json_dict.get('house_id')
    start_date_str = json_dict.get('start_date')
    end_date_str = json_dict.get('end_date')
    # 判断参数是否缺少
    if not all([house_id, start_date_str, end_date_str]):
        return jsonify(errno=RET.PARAMERR, errmsg='缺少参数')
    # 校验房屋是否存在
    try:
        house = House.query.get(house_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询房屋数据失败')
    if not house:
        return jsonify(errno=RET.NODATA, errmsg='房屋不不存在')
    # 对⼊入住和离开时间进⾏行行校验
    try:
        start_date = datetime.datetime.strptime(start_date_str, '%Y-%m-%d')
        end_date = datetime.datetime.strptime(end_date_str, '%Y-%m-%d')
        if start_date and end_date:
            # 断言:入住时间一定小于离开时间,如果不满足,就抛出异常
            assert start_date < end_date, Exception('入住时间有误')
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg='⼊入住时间有误')

    try:
        conflict_orders = Order.query.filter(
            Order.house_id == house_id, end_date > Order.begin_date,
            start_date < Order.end_date).all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询冲突订单失败')
    if conflict_orders:
        return jsonify(errno=RET.DATAERR, errmsg='该房屋已被预订')

    order = Order()
    order.user_id = g.user_id
    order.house_id = house_id
    order.begin_date = start_date
    order.end_date = end_date
    order.days = (end_date - start_date).days
    order.house_price = house.price
    order.amount = house.price * order.days

    try:
        db.session.add(order)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DATAERR, errmsg='保存订单数据到数据库失败')
    # 6.响应结果
    return jsonify(errno=RET.OK, errmsg='OK')
示例#3
0
def save_order_info():

    req_dict = request.json
    house_id = req_dict.get('house_id')
    start_date = req_dict.get('start_date')
    end_date = req_dict.get('end_date')

    if not all([house_id, start_date, end_date]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')

    try:
        start_date = datetime.strptime(start_date, '%Y-%m-%d')
        end_date = datetime.strptime(end_date, '%Y-%m-%d')
        assert start_date < end_date, Exception('起始时间大于结束时间')
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg='参数错误')

    try:
        house = House.query.get(house_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询房屋信息失败')

    if not house:
        return jsonify(errno=RET.NODATA, errmsg='房屋不存在')
    try:
        conflict_orders_count = Order.query.filter(
            end_date > Order.begin_date, start_date < Order.end_date,
            Order.house_id == house_id).count()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询冲突订单失败')

    if conflict_orders_count > 0:
        return jsonify(errno=RET.DATAERR, errmsg='房屋已被预定')
    days = (end_date - start_date).days
    order = Order()
    order.user_id = g.user_id
    order.house_id = house_id
    order.begin_date = start_date
    order.end_date = end_date
    order.days = days
    order.house_price = house.price
    order.amount = house.price * days

    house.order_count += 1
    try:
        db.session.add(order)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存订单信息失败')

    return jsonify(errno=RET.OK, errmsg='ok')
示例#4
0
文件: order.py 项目: kindaoqd/iHome
def create_order():
    """创建订单"""
    user_id = g.user_id
    request_dict = request.json
    house_id = request_dict.get('house_id')
    sd = request_dict.get('start_date')
    ed = request_dict.get('end_date')
    if not all([house_id, sd, ed]):
        return jsonify(errno=RET.PARAMERR, errmsg=u'参数不完整')
    try:
        start_date = datetime.strptime(sd, '%Y-%m-%d')
        end_date = datetime.strptime(ed, '%Y-%m-%d')
        assert end_date > start_date, Exception(u'时间输入有误')
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg=u'时间格式有误')
    # 判断订房时间与订单中时间是否冲突及房屋是否存在
    try:
        conflict_orders = Order.query.filter(Order.house_id == house_id, start_date < Order.end_date,
                                             end_date > Order.begin_date).all()
        if conflict_orders:
            return jsonify(errno=RET.DATAEXIST, errmsg=u'房屋已被订')
        house = House.query.get(house_id)
        if not house:
            return jsonify(errno=RET.NODATA, errmsg=u'房屋不存在')
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg=u'数据库查询失败')
    # 新建并储存订单
    price = house.price
    days = (end_date - start_date).days
    new_order = Order()
    new_order.house_id = house_id
    new_order.user_id = user_id
    new_order.begin_date = start_date
    new_order.end_date = end_date
    new_order.house_price = price
    new_order.days = days
    new_order.amount = price * days
    try:
        db.session.add(new_order)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg=u'存储新订单失败')
    return jsonify(errno=RET.OK, errmsg='OK')
示例#5
0
文件: order.py 项目: xiangzaizi/iHome
def create_order():
    """创建、提交订单
    0.判断用户是否登录
    1.接受参数,house_id, 入住时间和离开时间
    2.校验参数,判断入住时间和离开是否符合逻辑,校验房屋是否存在
    3.判断当前房屋有没有被预定
    4.创建订单模型对象,并存储订单数据
    5.保存到数据库
    6.响应结果
    """

    # 1.接受参数,house_id, 入住时间和离开时间
    json_dict = request.json
    house_id = json_dict.get('house_id')
    start_date_str = json_dict.get('start_date')
    end_date_str = json_dict.get('end_date')
    # 判断是否缺少参数
    if not all([house_id,start_date_str,end_date_str]):
        return jsonify(errno=RET.PARAMERR, errmsg='缺少参数')

    # 2.校验参数,判断入住时间和离开是否符合逻辑,校验房屋是否存在
    try:
        start_date = datetime.datetime.strptime(start_date_str, '%Y-%m-%d')
        end_date = datetime.datetime.strptime(end_date_str, '%Y-%m-%d')
        # 自己校验入住时间是否小于离开的时间
        if start_date and end_date:
            # 断言:入住时间一定小于离开时间,如果不满足,就抛出异常
            assert start_date < end_date, Exception('入住时间有误')
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg='入住时间有误')

    # 判断房屋是否存在
    try:
        house = House.query.get(house_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询房屋数据失败')
    if not house:
        return jsonify(errno=RET.NODATA, errmsg='房屋不存在')

    # 3.判断当前房屋有没有被预定
    try:
        conflict_orders = Order.query.filter(Order.house_id==house_id,end_date > Order.begin_date, start_date < Order.end_date).all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询冲突订单失败')
    # 如果有值,说明要预订的房屋在该时间节点,已经在订单中,说明被预定
    if conflict_orders:
        return jsonify(errno=RET.DATAERR, errmsg='房屋已被预订')

    # 4.创建订单模型对象,并存储订单数据
    days = (end_date-start_date).days # 计算时间段之间的天数
    order = Order()
    order.user_id = g.user_id
    order.house_id = house_id
    order.begin_date = start_date
    order.end_date = end_date
    order.days = days
    order.house_price = house.price
    order.amount = house.price * days

    # 5.保存到数据库
    try:
        db.session.add(order)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg='保存订单数据失败')

    # 6.响应结果
    return jsonify(errno=RET.OK, errmsg='OK')
示例#6
0
def add_order():
    """添加订单
    0.判断是否登录
    1.接收参数,房屋id,用户id
    2.校验参数
    3.计算入住天数
    4.判断房屋是否存在
    5.判断房屋是否是当前登录用户的
    6.查询是否存在冲突的订单
    7.生成订单模型,提交订单
    8.响应请求
    """

    # 1 判断是否登录
    # 2 接收参数,房屋id,用户id
    # 获取当前用户的id
    user_id = g.user_id
    # 获取传入的参数
    params = request.json
    start_date_str = params.get('start_date')
    end_date_str = params.get('end_date')
    house_id = params.get('house_id')
    # 3 校验参数
    if not all([start_date_str, end_date_str, house_id]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数不能为空")
    try:
        start_date = datetime.datetime.strptime(start_date_str, '%Y-%m-%d')
        end_date = datetime.datetime.strptime(end_date_str, '%Y-%m-%d')
        assert start_date < end_date, Exception('开始日期大于结束日期')
        # 4 计算入住天数
        days = (end_date - start_date).days
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg="⼊住时间有误")

    # 5 判断房屋是否存在
    try:
        house = House.query.get(house_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询房屋数据失败")
    if not house:
        return jsonify(errno=RET.NODATA, errmsg="房屋不存在")

    # 6 判断房屋是否是当前登录用户的
    if user_id == house.user_id:
        return jsonify(errno=RET.ROLEERR, errmsg="不能预定自己的房屋哦")

    # 7 查询是否存在冲突的订单
    try:
        conflict_orders = Order.query.filter(
            Order.house_id == house_id, end_date > Order.begin_date,
            start_date < Order.end_date).all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询冲突订单失败")
    if conflict_orders:
        return jsonify(errno=RET.DATAERR, errmsg="该房屋已被预订")

    # 8 生成订单模型,提交订单
    order = Order()
    order.user_id = user_id  # 下订单的用户编号
    order.house_id = house_id  # 预订的房间编号
    order.begin_date = start_date  # 预订的起始时间
    order.end_date = end_date  # 预订的结束时间
    order.days = days  # 预订的总天数
    order.house_price = house.price  # 房屋的单价
    order.amount = house.price * days  # 订单的总金额

    try:
        db.session.add(order)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DATAERR, errmsg="保存订单数据到数据库失败")

    # 9 响应请求
    return jsonify(errno=RET.OK, errmsg="OK")
示例#7
0
def create_order():
    """创建、提交订单
        0.判断用户是否登录
        1.获取参数:house_id, start_date, end_date
        2.判断参数是否为空
        3.校验时间格式是否合法
        4.通过house_id,判断要提交的房屋是否存在
        5.判断当前房屋是否已经被预定了
        6.创建订单模型对象,并赋值
        7.将数据保存到数据库
        8.响应提交订单结果
        """

    # 1.获取参数:house_id, start_date, end_date
    json_dict = request.json
    house_id = json_dict.get('house_id')
    start_data_str = json_dict.get('start_data')
    end_data_str = json_dict.get('end_data')

    # 2.判断参数是否为空
    if not all([house_id, start_data_str, end_data_str]):
        return jsonify(errno=RET.PARAMERR, errmsg='缺少参数')

    # 3.校验时间格式是否合法
    try:
        start_data = datetime.datetime.strptime(start_data_str, '%Y-%m-%d')
        end_data = datetime.datetime.strptime(end_data_str, '%Y-%m-%d')
        if start_data and end_data:
            assert start_data < end_data, Exception('入住时间有误')
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg='入住时间有误')

    # 4.通过house_id,判断要提交的房屋是否存在
    try:
        house = House.query.get(house_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询房屋数据失败')

    if not house:
        return jsonify(errno=RET.NODATA, errmsg='房屋不存在')

    # 5.判断当前房屋是否已经被预定了
    try:
        conflict_orders = Order.query.filter(
            Order.house_id == house_id, end_data > Order.begin_date,
            start_data < Order.end_date).all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询冲突订单失败')
    if conflict_orders:
        return jsonify(errno=RET.PARAMERR, errmsg='该房屋已经被预订')

    # 6.创建订单模型对象,并赋值
    order = Order()
    order.user_id = g.user_id
    order.house_id = house_id
    order.begin_date = start_data
    order.end_date = end_data
    order.days = (end_data - start_data).days  # days是datetime模块中的属性
    order.house_price = house.price
    order.amount = order.days * house.price

    # 7.将数据保存到数据库
    try:
        db.session.add(order)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg='保存订单到数据库失败')

    return jsonify(errno=RET.OK, errmsg='OK')
示例#8
0
def create_order():
    """创建,提交订单
    0,判断用户是否登录
    1,获取参数:house_id,start_date,end_date
    2,判断参数是否为空
    3,校验时间格式是否合法
    4,通过house_id,判断要提交的房屋是否存在
    5,判断当前的房屋是否被预定
    6,创建订单模型对象,并赋值
    7,将数据保存到数据库
    8,响应提交订单结果
    """
    # 1
    json_dict = request.json
    house_id = json_dict.get('house_id')
    start_date_str = json_dict.get('start_date')
    end_date_str = json_dict.get('end_date')

    # 2
    if not all([house_id, start_date_str, end_date_str]):
        return jsonify(errno=RET.PARAMERR, errmsg='缺少参数')

    # 3
    try:
        start_date = datetime.strptime(start_date_str, '%Y-%m-%d')
        end_date = datetime.strptime(end_date_str, '%Y-%m-%d')
        if start_date and end_date:
            assert start_date < end_date, Exception('入住时间有误')

    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg='入住时间有误')

    # 4
    try:
        house = House.query.get(house_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询房屋信息失败')
    if not house_id:
        return jsonify(errno=RET.NODATA, errmsg='房屋不存在')

    # 5
    try:
        conflict_orders = Order.query.filter(
            Order.house_id == house_id, Order.begin_date < end_date,
            Order.end_date > start_date).all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询冲突订单失败')
    if conflict_orders:
        return jsonify(errno=RET.PARAMERR, errmsg='该房屋已经被预定了')

    # 6
    order = Order()
    order.user_id = g.user_id
    order.house_id = house_id
    order.begin_date = start_date
    order.end_date = end_date
    order.days = (end_date - start_date).days  # days是datetime模块中的属性
    order.house_price = house.price
    order.amount = order.days * house.price

    # 7.将数据保存到数据库
    try:
        db.session.add(order)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.DBERR, errmsg='保存订单数据失败')

    # 8.响应提交订单结果
    return jsonify(errno=RET.OK, errmsg='OK')
示例#9
0
def create_order():
    """
    添加新订单
    1. 获取参数:房屋id,开始入住时间,结束入住时间
    2. 判断参数/校验参数
    3. 判断当前房屋在当前时间段内是否已经被预订
    4. 创建订单模型并设置相关数据
    5. 添加到数据库
    6. 返回响应
    :return:
    """

    # 1. 获取参数:房屋id,开始入住时间,结束入住时间
    data_dict = request.json
    house_id = data_dict.get("house_id")
    start_date_str = data_dict.get("start_date")
    end_date_str = data_dict.get("end_date")

    # 2. 判断参数/校验参数

    if not all([house_id, start_date_str, end_date_str]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    # 判断参数
    try:
        # 转成时间对象
        start_date = datetime.datetime.strptime(start_date_str, "%Y-%m-%d")
        end_date = datetime.datetime.strptime(end_date_str, "%Y-%m-%d")
        if start_date and end_date:
            assert start_date < end_date, Exception("结束日期必须大于开始时间")
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    # 判断房屋是否存在
    try:
        house = House.query.get(house_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询数据失败")

    if not house:
        return jsonify(errno=RET.NODATA, errmsg="未查询到房屋数据")

    # 3. 判断当前房屋在当前时间段内是否已经被预订
    try:
        conflict_orders = Order.query.filter(end_date > Order.begin_date,
                                             start_date < Order.end_date,
                                             Order.house_id == house_id).all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询数据错误")

    if conflict_orders:
        return jsonify(errno=RET.DATAERR, errmsg="当前房屋已被预订")

    # 4. 创建订单模型并设置相关数据
    days = (end_date - start_date).days
    order = Order()
    order.user_id = g.user_id
    order.house_id = house_id
    order.begin_date = start_date
    order.end_date = end_date
    order.days = days
    order.house_price = house.price
    order.amount = days * house.price

    # 设置房屋的订单数量加1
    house.order_count += 1

    # 5. 添加到数据库
    try:
        db.session.add(order)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存订单失败")

    # 6. 返回响应
    return jsonify(errno=RET.OK, errmsg="OK")
示例#10
0
def save_order_info():
    """
    保存房屋预订订单的信息:
    1. 接收参数(房屋id, 起始时间,结束时间) 并进行参数校验
    2. 根据房屋id查询房屋信息(如果查不到,说明房屋信息不存在)
    3. 根据入住起始时间和结束时间查询订单是否有冲突
    4. 创建Order对象并保存订单信息
    5. 把订单信息添加进数据库
    6. 返回应答,订单创建成功
    """
    # 1. 接收参数(房屋id, 起始时间,结束时间) 并进行参数校验
    req_dict = request.json
    house_id = req_dict.get('house_id')
    start_date = req_dict.get('start_date')
    end_date = req_dict.get('end_date')

    if not all([house_id, start_date, end_date]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')

    try:
        start_date = datetime.strptime(start_date, '%Y-%m-%d')
        end_date = datetime.strptime(end_date, '%Y-%m-%d')

        assert start_date < end_date, Exception('起始时间大于结束时间')
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg='参数错误')
    # 2.根据房屋id查询房屋信息(如果查不到,说明房屋信息不存在)
    try:
        house = House.query.get(house_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询房屋信息失败')

    if not house:
        return jsonify(errno=RET.NODATA, errmsg='房屋不存在')

    # 3.根据入住起始时间和结束时间查询订单是否有冲突
    try:
        conflict_orders_count = Order.query.filter(
            end_date > Order.begin_date, start_date < Order.end_date,
            Order.house_id == house_id).count()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询冲突订单失败')

    if conflict_orders_count > 0:
        return jsonify(errno=RET.DATAERR, errmsg='房屋已被预订')

    # 4.创建Order对象并保存订单信息
    days = (end_date - start_date).days
    order = Order()
    order.user_id = g.user_id
    order.house_id = house_id
    order.begin_date = start_date
    order.end_date = end_date
    order.house_price = house.price
    order.amount = house.price * days
    order.days = days

    house.order_count += 1

    # 5.把订单信息添加进数据库
    try:
        db.session.add(order)
        db.session.commit()
    except Exception as e:
        db.session.rollback()
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='保存订单信息失败')

    # 6.返回应答,订单创建成功
    return jsonify(errno=RET.OK, errmsg='OK')
示例#11
0
文件: order.py 项目: mycyzs/iHome_pro
def sub_order():
    """创建、提交订单
    0.判断用户是否登录
    1.获取参数:house_id, start_date, end_date
    2.判断参数是否为空
    3.校验时间格式是否合法
    4.通过house_id,判断要提交的房屋是否存在
    5.判断当前房屋是否已经被预定了
    6.创建订单模型对象,并赋值
    7.将数据保存到数据库
    8.响应提交订单结果

    """
    # 获取参数
    json_dict = request.json
    house_id = json_dict.get('house_id')
    start_date_str = json_dict.get('start_date')
    end_date_str = json_dict.get('end_date')

    # 判断参数是否为空
    if not all([house_id, start_date_str, end_date_str]):
        return jsonify(reeno=RET.PARAMERR, errmsg='参数不合法')

    # 校验时间格式是否合法,看能不能转成时间格式
    try:
        start_date = datetime.datetime.strptime(start_date_str, '%Y-%m-%d')
        end_date = datetime.datetime.strptime(end_date_str, '%Y-%m-%d')
        if start_date and end_date:
            assert start_date < end_date, Exception('入住时间有误')

    except Exception as e:
        current_app.logger.error(e)
        return jsonify(reeno=RET.PARAMERR, errmsg='时间格式错误')

    # 判断房子是否粗在
    try:
        house = House.query.get(house_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(reeno=RET.DBERR, errmsg='查询出错')
    if not house:
        return jsonify(reeno=RET.NODATA, errmsg='房子不存在')

    # 判断房子是否被预订类
    try:
        conflict_orders = Order.query.filter(
            Order.house_id == house_id, start_date < Order.end_date,
            end_date > Order.begin_date).all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(reeno=RET.DBERR, errmsg='查询出错')
    if conflict_orders:
        return jsonify(reeno=RET.PARAMERR, errmsg='该房屋已经被预订类')

    # 创建订单模型对象,赋值

    order = Order()
    order.user_id = g.user_id
    order.house_id = house_id
    order.begin_date = start_date
    order.end_date = end_date
    order.days = (end_date - start_date).days  # days是datetime模块中的属性
    order.house_price = house.price
    order.amount = order.days * house.price

    # 将数据把存到数据库
    try:
        db.session.add(order)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(reeno=RET.DBERR, errmsg='查询出错')

    return jsonify(reeno=RET.OK, errmsg='ok')
示例#12
0
文件: order.py 项目: yjn1207/iHome
def add_order():
    """
    1. 获取参数 > house_id, start_date_str, end_date_str
    2. 判断和校验参数
    3. 查询指定房屋id是否存在
    4. 判断当前时间段内该房屋是否有订单
    5. 生成订单模型,设置数据
    6. 数据保存
    7. 返回
    :return:
    """
    #1. 获取参数 > house_id, start_date_str, end_date_str
    user_id = g.user_id
    data_json = request.json
    house_id = data_json.get("house_id")
    start_date_str = data_json.get("start_date")
    end_date_str = data_json.get("end_date")

    #2
    if not all([house_id, start_date_str, end_date_str]):
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    try:
        # 将日期字符串转成 datetime 对象
        start_date = datetime.datetime.strptime(start_date_str, "%Y-%m-%d")
        end_date = datetime.datetime.strptime(end_date_str, "%Y-%m-%d")
        #判断开始时间与结束时间的大小 断言
        assert start_date < end_date, Exception("结束时间必须大于开始时间")
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")
    # 3. 判断房屋是否存在
    try:
        house = House.query.get(house_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询数据错误")

    if not house:
        return jsonify(errno=RET.NODATA, errmsg="房屋不存在")

    if user_id == house.user_id:
        return jsonify(errno=RET.ROLEERR, errmsg="房东不能预订")

    # 4. 判断该房屋指定时间段内是否有冲突的订单
    try:
        filters = [
            Order.house_id == house_id, Order.end_date > start_date,
            Order.begin_date < end_date
        ]
        # 取到冲突订单的数量
        order_count = Order.query.filter(*filters).count()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询数据错误")

    # 如果冲突订单的数量大于0,代表该房屋已被预订
    if order_count > 0:
        return jsonify(errno=RET.DATAERR, errmsg="房屋已被预订")

        # 5. 生成订单模型
    order = Order()
    days = (end_date - start_date).days  #   duixiang时间对象
    order.user_id = user_id
    order.house_id = house_id
    order.begin_date = start_date
    order.end_date = end_date
    order.days = days
    order.house_price = house.price
    order.amount = days * house.price

    # 将房屋的订单数量加1
    house.order_count += 1

    try:
        db.session.add(order)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg="保存数据失败")
    return jsonify(errno=RET.OK, errmsg="OK")