|
|
@ -1,4 +1,11 @@ |
|
|
|
import sqlite3 as sqlite |
|
|
|
from sqlalchemy.exc import SQLAlchemyError |
|
|
|
from sqlalchemy import and_ |
|
|
|
from be.model import postgreSQLORM |
|
|
|
from be.model.postgreSQLORM import Store_Book |
|
|
|
from be.model.postgreSQLORM import New_Order_Detail |
|
|
|
from be.model.postgreSQLORM import New_Order |
|
|
|
from be.model.postgreSQLORM import User |
|
|
|
import uuid |
|
|
|
import json |
|
|
|
import logging |
|
|
@ -18,43 +25,59 @@ class Buyer(db_conn.DBConn): |
|
|
|
if not self.store_id_exist(store_id): |
|
|
|
return error.error_non_exist_store_id(store_id) + (order_id, ) |
|
|
|
uid = "{}_{}_{}".format(user_id, store_id, str(uuid.uuid1())) |
|
|
|
|
|
|
|
# print("touch0") |
|
|
|
for book_id, count in id_and_count: |
|
|
|
cursor = self.conn.execute( |
|
|
|
"SELECT book_id, stock_level, book_info FROM store " |
|
|
|
"WHERE store_id = ? AND book_id = ?;", |
|
|
|
(store_id, book_id)) |
|
|
|
row = cursor.fetchone() |
|
|
|
row = self.session.query(postgreSQLORM.Store_Book).filter(and_(postgreSQLORM.Store_Book.store_id==store_id,postgreSQLORM.Store_Book.book_id==book_id)).first() |
|
|
|
# cursor = self.conn.execute( |
|
|
|
# "SELECT book_id, stock_level, book_info FROM store " |
|
|
|
# "WHERE store_id = ? AND book_id = ?;", |
|
|
|
# (store_id, book_id)) |
|
|
|
# row = cursor.fetchone() |
|
|
|
if row is None: |
|
|
|
return error.error_non_exist_book_id(book_id) + (order_id, ) |
|
|
|
|
|
|
|
stock_level = row[1] |
|
|
|
book_info = row[2] |
|
|
|
# stock_level = row[1] |
|
|
|
# book_info = row[2] |
|
|
|
stock_level = row.stock_level |
|
|
|
book_info = row.book_info |
|
|
|
|
|
|
|
book_info_json = json.loads(book_info) |
|
|
|
price = book_info_json.get("price") |
|
|
|
|
|
|
|
if stock_level < count: |
|
|
|
return error.error_stock_level_low(book_id) + (order_id,) |
|
|
|
|
|
|
|
cursor = self.conn.execute( |
|
|
|
"UPDATE store set stock_level = stock_level - ? " |
|
|
|
"WHERE store_id = ? and book_id = ? and stock_level >= ?; ", |
|
|
|
(count, store_id, book_id, count)) |
|
|
|
if cursor.rowcount == 0: |
|
|
|
# print("touch2") |
|
|
|
row = self.session.query(Store_Book).filter(and_(Store_Book.store_id==store_id,Store_Book.book_id==book_id,Store_Book.stock_level>=count)).update({'stock_level':Store_Book.stock_level-count}) |
|
|
|
# cursor = self.conn.execute( |
|
|
|
# "UPDATE store set stock_level = stock_level - ? " |
|
|
|
# "WHERE store_id = ? and book_id = ? and stock_level >= ?; ", |
|
|
|
# (count, store_id, book_id, count)) |
|
|
|
|
|
|
|
# if cursor.rowcount == 0: |
|
|
|
# print("touch3") |
|
|
|
if row == 0: |
|
|
|
return error.error_stock_level_low(book_id) + (order_id, ) |
|
|
|
|
|
|
|
self.conn.execute( |
|
|
|
"INSERT INTO new_order_detail(order_id, book_id, count, price) " |
|
|
|
"VALUES(?, ?, ?, ?);", |
|
|
|
(uid, book_id, count, price)) |
|
|
|
|
|
|
|
self.conn.execute( |
|
|
|
"INSERT INTO new_order(order_id, store_id, user_id) " |
|
|
|
"VALUES(?, ?, ?);", |
|
|
|
(uid, store_id, user_id)) |
|
|
|
self.conn.commit() |
|
|
|
# print("touch4") |
|
|
|
new_order_detail_entity = New_Order_Detail(order_id=uid,book_id=book_id,count=count,price=price) |
|
|
|
# print("touch5") |
|
|
|
row = self.session.add(new_order_detail_entity) |
|
|
|
# self.session.commit() |
|
|
|
# self.conn.execute( |
|
|
|
# "INSERT INTO new_order_detail(order_id, book_id, count, price) " |
|
|
|
# "VALUES(?, ?, ?, ?);", |
|
|
|
# (uid, book_id, count, price)) |
|
|
|
# print("touch1") |
|
|
|
new_order_entity = New_Order(order_id=uid,fk_store_id=store_id,fk_user_id=user_id) |
|
|
|
self.session.add(new_order_entity) |
|
|
|
# self.conn.execute( |
|
|
|
# "INSERT INTO new_order(order_id, store_id, user_id) " |
|
|
|
# "VALUES(?, ?, ?);", |
|
|
|
# (uid, store_id, user_id)) |
|
|
|
# self.conn.commit() |
|
|
|
self.session.commit() |
|
|
|
order_id = uid |
|
|
|
except sqlite.Error as e: |
|
|
|
except SQLAlchemyError as e: |
|
|
|
logging.info("528, {}".format(str(e))) |
|
|
|
return 528, "{}".format(str(e)), "" |
|
|
|
except BaseException as e: |
|
|
@ -64,72 +87,98 @@ class Buyer(db_conn.DBConn): |
|
|
|
return 200, "ok", order_id |
|
|
|
|
|
|
|
def payment(self, user_id: str, password: str, order_id: str) -> (int, str): |
|
|
|
conn = self.conn |
|
|
|
# conn = self.conn |
|
|
|
session = self.session |
|
|
|
try: |
|
|
|
cursor = conn.execute("SELECT order_id, user_id, store_id FROM new_order WHERE order_id = ?", (order_id,)) |
|
|
|
row = cursor.fetchone() |
|
|
|
row = session.query(New_Order).filter(New_Order.order_id==order_id).first() |
|
|
|
# cursor = conn.execute("SELECT order_id, user_id, store_id FROM new_order WHERE order_id = ?", (order_id,)) |
|
|
|
# row = cursor.fetchone() |
|
|
|
if row is None: |
|
|
|
return error.error_invalid_order_id(order_id) |
|
|
|
|
|
|
|
order_id = row[0] |
|
|
|
buyer_id = row[1] |
|
|
|
store_id = row[2] |
|
|
|
# order_id = row[0] |
|
|
|
# buyer_id = row[1] |
|
|
|
# store_id = row[2] |
|
|
|
order_id = row.order_id |
|
|
|
buyer_id = row.fk_user_id |
|
|
|
store_id = row.fk_store_id |
|
|
|
|
|
|
|
if buyer_id != user_id: |
|
|
|
return error.error_authorization_fail() |
|
|
|
|
|
|
|
cursor = conn.execute("SELECT balance, password FROM user WHERE user_id = ?;", (buyer_id,)) |
|
|
|
row = cursor.fetchone() |
|
|
|
|
|
|
|
row = session.query(postgreSQLORM.User).filter(postgreSQLORM.User.user_id==buyer_id).first() |
|
|
|
# cursor = conn.execute("SELECT balance, password FROM user WHERE user_id = ?;", (buyer_id,)) |
|
|
|
# row = cursor.fetchone() |
|
|
|
if row is None: |
|
|
|
return error.error_non_exist_user_id(buyer_id) |
|
|
|
balance = row[0] |
|
|
|
if password != row[1]: |
|
|
|
# balance = row[0] |
|
|
|
balance = row.balance |
|
|
|
pwd = row.password |
|
|
|
if password != pwd: |
|
|
|
return error.error_authorization_fail() |
|
|
|
|
|
|
|
cursor = conn.execute("SELECT store_id, user_id FROM user_store WHERE store_id = ?;", (store_id,)) |
|
|
|
row = cursor.fetchone() |
|
|
|
row = session.query(postgreSQLORM.User_Store).filter(postgreSQLORM.User_Store.fk_store_id==store_id).first() |
|
|
|
# cursor = conn.execute("SELECT store_id, user_id FROM user_store WHERE store_id = ?;", (store_id,)) |
|
|
|
# row = cursor.fetchone() |
|
|
|
if row is None: |
|
|
|
return error.error_non_exist_store_id(store_id) |
|
|
|
|
|
|
|
seller_id = row[1] |
|
|
|
# seller_id = row[1] |
|
|
|
seller_id = row.fk_user_id |
|
|
|
|
|
|
|
if not self.user_id_exist(seller_id): |
|
|
|
return error.error_non_exist_user_id(seller_id) |
|
|
|
|
|
|
|
cursor = conn.execute("SELECT book_id, count, price FROM new_order_detail WHERE order_id = ?;", (order_id,)) |
|
|
|
row = session.query(New_Order_Detail).filter(New_Order_Detail.order_id==order_id).all() |
|
|
|
# cursor = conn.execute("SELECT book_id, count, price FROM new_order_detail WHERE order_id = ?;", (order_id,)) |
|
|
|
total_price = 0 |
|
|
|
for row in cursor: |
|
|
|
count = row[1] |
|
|
|
price = row[2] |
|
|
|
# for row in cursor: |
|
|
|
# count = row[1] |
|
|
|
# price = row[2] |
|
|
|
# total_price = total_price + price * count |
|
|
|
for i in row: |
|
|
|
count = i.count |
|
|
|
price = i.price |
|
|
|
total_price = total_price + price * count |
|
|
|
|
|
|
|
if balance < total_price: |
|
|
|
return error.error_not_sufficient_funds(order_id) |
|
|
|
|
|
|
|
cursor = conn.execute("UPDATE user set balance = balance - ?" |
|
|
|
"WHERE user_id = ? AND balance >= ?", |
|
|
|
(total_price, buyer_id, total_price)) |
|
|
|
if cursor.rowcount == 0: |
|
|
|
# print('touch0') |
|
|
|
## 买家扣钱 |
|
|
|
row = session.query(User).filter(and_(User.balance>=total_price,User.user_id==buyer_id)).update({'balance':User.balance-total_price}) |
|
|
|
# cursor = conn.execute("UPDATE user set balance = balance - ?" |
|
|
|
# "WHERE user_id = ? AND balance >= ?", |
|
|
|
# (total_price, buyer_id, total_price)) |
|
|
|
# if cursor.rowcount == 0: |
|
|
|
if row == 0: |
|
|
|
return error.error_not_sufficient_funds(order_id) |
|
|
|
|
|
|
|
cursor = conn.execute("UPDATE user set balance = balance + ?" |
|
|
|
"WHERE user_id = ?", |
|
|
|
(total_price, buyer_id)) |
|
|
|
|
|
|
|
if cursor.rowcount == 0: |
|
|
|
# print('touch1') |
|
|
|
## 卖家加钱 |
|
|
|
row = session.query(User).filter(User.user_id==seller_id).update({'balance':User.balance+total_price}) |
|
|
|
# cursor = conn.execute("UPDATE user set balance = balance + ?" |
|
|
|
# "WHERE user_id = ?", |
|
|
|
# (total_price, buyer_id)) |
|
|
|
# if cursor.rowcount == 0: |
|
|
|
if row == 0: |
|
|
|
return error.error_non_exist_user_id(buyer_id) |
|
|
|
|
|
|
|
cursor = conn.execute("DELETE FROM new_order WHERE order_id = ?", (order_id, )) |
|
|
|
if cursor.rowcount == 0: |
|
|
|
# print('touch2') |
|
|
|
row = session.query(New_Order).filter(New_Order.order_id==order_id).delete() |
|
|
|
# cursor = conn.execute("DELETE FROM new_order WHERE order_id = ?", (order_id, )) |
|
|
|
# if cursor.rowcount == 0: |
|
|
|
if row == 0: |
|
|
|
return error.error_invalid_order_id(order_id) |
|
|
|
|
|
|
|
cursor = conn.execute("DELETE FROM new_order_detail where order_id = ?", (order_id, )) |
|
|
|
if cursor.rowcount == 0: |
|
|
|
|
|
|
|
row = session.query(New_Order_Detail).filter(New_Order_Detail.order_id==order_id).delete() |
|
|
|
# cursor = conn.execute("DELETE FROM new_order_detail where order_id = ?", (order_id, )) |
|
|
|
# if cursor.rowcount == 0: |
|
|
|
if row == 0: |
|
|
|
return error.error_invalid_order_id(order_id) |
|
|
|
|
|
|
|
conn.commit() |
|
|
|
# conn.commit() |
|
|
|
session.commit() |
|
|
|
|
|
|
|
except sqlite.Error as e: |
|
|
|
except SQLAlchemyError as e: |
|
|
|
return 528, "{}".format(str(e)) |
|
|
|
|
|
|
|
except BaseException as e: |
|
|
@ -139,22 +188,26 @@ class Buyer(db_conn.DBConn): |
|
|
|
|
|
|
|
def add_funds(self, user_id, password, add_value) -> (int, str): |
|
|
|
try: |
|
|
|
cursor = self.conn.execute("SELECT password from user where user_id=?", (user_id,)) |
|
|
|
row = cursor.fetchone() |
|
|
|
row = self.session.query(postgreSQLORM.User).filter(postgreSQLORM.User.user_id==user_id).first() |
|
|
|
# cursor = self.conn.execute("SELECT password from user where user_id=?", (user_id,)) |
|
|
|
# row = cursor.fetchone() |
|
|
|
if row is None: |
|
|
|
return error.error_authorization_fail() |
|
|
|
|
|
|
|
if row[0] != password: |
|
|
|
pwd = row.password |
|
|
|
if pwd != password: |
|
|
|
return error.error_authorization_fail() |
|
|
|
|
|
|
|
cursor = self.conn.execute( |
|
|
|
"UPDATE user SET balance = balance + ? WHERE user_id = ?", |
|
|
|
(add_value, user_id)) |
|
|
|
if cursor.rowcount == 0: |
|
|
|
row = self.session.query(postgreSQLORM.User).filter_by(user_id=user_id).update({'balance':postgreSQLORM.User.balance+add_value,'user_id':user_id}) |
|
|
|
# cursor = self.conn.execute( |
|
|
|
# "UPDATE user SET balance = balance + ? WHERE user_id = ?", |
|
|
|
# (add_value, user_id)) |
|
|
|
if row == 0: |
|
|
|
return error.error_non_exist_user_id(user_id) |
|
|
|
|
|
|
|
self.conn.commit() |
|
|
|
except sqlite.Error as e: |
|
|
|
self.session.commit() |
|
|
|
# self.conn.commit() |
|
|
|
except SQLAlchemyError as e: |
|
|
|
return 528, "{}".format(str(e)) |
|
|
|
except BaseException as e: |
|
|
|
return 530, "{}".format(str(e)) |
|
|
|