def setItems(member_id=0, food_id=0, number=0): if member_id < 1 or food_id < 1 or number < 1: return False # 一种是添加,一种是编辑 print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&") cart_info = MemberCart.query.filter_by(food_id=food_id, member_id=member_id).first() print(cart_info) if cart_info: print("$$$$$$$$$$$$$$$$$") print(cart_info.id) print(cart_info.member_id) model_cart = cart_info else: print("test") model_cart = MemberCart() model_cart.member_id = member_id model_cart.created_time = getCurrentDate() model_cart.food_id = food_id model_cart.quantity = number model_cart.updated_time = getCurrentDate() db.session.add(model_cart) db.session.commit() print('test/////') return True
def setCart(): resp = {'code':200, 'msg':'操作成功','data':{}} req = request.values course_id = int(req['id']) if 'id' in req else 0 number = int(req['number']) if 'number' in req else 0 if course_id <1 or number <1 : resp['code'] = -1 resp['msg'] = '课程注册失败' return jsonify(resp) member_info = session.member_info if not member_info: resp['code'] = -1 resp['msg'] ='会员信息加载失败' return jsonify(resp) course_info = Course.query.filter_by( id = course_id).first() if not course_info: resp['code'] = -1 resp['msg'] ='该课程已经下架' return jsonify(resp) if course_info.stock < number: resp['code'] = -1 resp['msg'] = '对不起,名额已经报满~~~' return jsonify(resp) #db member_id = member_info.id cart_info = MemberCart.query.filter_by(course_id=course_id, member_id=member_id).first() if cart_info: model_cart = cart_info model_cart.register_status = 1 else: model_cart = MemberCart() model_cart.member_id = member_id model_cart.created_time = getCurrentDate() model_cart.course_id = course_id model_cart.quantity = number model_cart.updated_time = getCurrentDate() db.session.add(model_cart) #报名后计算course totoal_count model_course = course_info model_course.total_count = MemberCart.query.filter_by(course_id=course_id,register_status=1).count() db.session.add(model_course) db.session.commit() return jsonify(resp)
def setItems(member_id=0,food_id=0,number=0): if member_id < 1 or food_id < 1 or number < 1: return False cart_info = MemberCart.query.filter_by(food_id=food_id,member_id=member_id).first() if cart_info: model_cart = cart_info else: model_cart = MemberCart() model_cart.member_id = member_id model_cart.created_time = getCurrentDate() model_cart.food_id = food_id model_cart.quantity = number model_cart.updated_time = getCurrentDate() db.session.add(model_cart) db.session.commit() return True
def setItems( member_id = 0,food_id = 0,number = 0 ): # 用户id,商品id,添加数量 if member_id < 1 or food_id < 1 or number < 1: # 如果任何一个值小于1 return False # 添加和更新,判断这个信息是否存在(用户和商品 id 是否相同) cart_info = MemberCart.query.filter_by( food_id = food_id, member_id= member_id ).first() if cart_info: # 如果购物车数据表,有这个添加信息。就是set更新 model_cart = cart_info else: # 没有就是,添加新的信息 model_cart = MemberCart() model_cart.member_id = member_id model_cart.created_time = getCurrentData() model_cart.food_id = food_id model_cart.quantity = number model_cart.updated_time = getCurrentData() db.session.add(model_cart) db.session.commit() return True