From 96a0d97b720c28766661afd99edb287e2fe43593 Mon Sep 17 00:00:00 2001 From: 10205501415 <10205501415@stu.ecnu.edu.cn> Date: Mon, 5 Dec 2022 15:22:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=AE=A2=E5=8D=95=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E4=BF=AE=E6=94=B9=E6=93=8D=E4=BD=9C=EF=BC=8C=E7=AD=89?= =?UTF-8?q?=E5=BE=85=E5=90=88=E5=B9=B6=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- be/Config.py | 0 be/creatTB.py | 5 - be/model/store.py | 0 be/postgreSQLORM.py | 121 -------------- be/testconn.py | 22 --- doc/buyer.md | 121 ++++++++++++++ doc/seller.md | 38 +++++ modified/be/Config.py | 0 modified/be/creatTB.py | 5 - modified/be/model/buyer.py | 83 +++++++++- modified/be/model/postgreSQLORM.py | 5 + modified/be/model/seller.py | 27 ++++ modified/be/model/store.py | 0 modified/be/postgreSQLORM.py | 121 -------------- modified/be/testconn.py | 22 --- modified/be/view/buyer.py | 22 +++ modified/be/view/seller.py | 10 ++ report.md | 316 ++++++++++++++++++++++++++++++++++++- 18 files changed, 617 insertions(+), 301 deletions(-) delete mode 100644 be/Config.py delete mode 100644 be/creatTB.py delete mode 100644 be/model/store.py delete mode 100644 be/postgreSQLORM.py delete mode 100644 be/testconn.py delete mode 100644 modified/be/Config.py delete mode 100644 modified/be/creatTB.py delete mode 100644 modified/be/model/store.py delete mode 100644 modified/be/postgreSQLORM.py delete mode 100644 modified/be/testconn.py diff --git a/be/Config.py b/be/Config.py deleted file mode 100644 index e69de29..0000000 diff --git a/be/creatTB.py b/be/creatTB.py deleted file mode 100644 index cca8c93..0000000 --- a/be/creatTB.py +++ /dev/null @@ -1,5 +0,0 @@ -from postgreSQLORM import Base,engine - -def createTable(): - # 创建所有继承于Base的类对应的表 - Base.metadata.create_all(engine) diff --git a/be/model/store.py b/be/model/store.py deleted file mode 100644 index e69de29..0000000 diff --git a/be/postgreSQLORM.py b/be/postgreSQLORM.py deleted file mode 100644 index 23c9b71..0000000 --- a/be/postgreSQLORM.py +++ /dev/null @@ -1,121 +0,0 @@ -from sqlalchemy import create_engine,MetaData -from sqlalchemy import Integer,String,ForeignKey,Column,TEXT -from sqlalchemy.orm import relationship -from sqlalchemy.ext.declarative import declarative_base -from sqlalchemy.orm import sessionmaker -from sqlalchemy import PrimaryKeyConstraint,UniqueConstraint -import sqlalchemy -Base=declarative_base() -class con: - def connect(): - '''Returns a connection and a metadata object''' - # We connect with the help of the PostgreSQL URL - - url = 'postgresql://stu10205501415:Stu10205501415@dase-cdms-2022-pub.pg.rds.aliyuncs.com:5432/stu10205501415' - - # The return value of create_engine() is our connection object - con = create_engine(url, client_encoding='utf8') - - # We then bind the connection to MetaData() - meta = MetaData(bind=con) - - return con, meta - - -class User(Base): - __tablename__ = 'user' - user_id = Column(TEXT, primary_key=True, comment="主键") - password = Column(TEXT, nullable=False, comment="密码") - balance = Column(Integer, nullable=False, comment="") - token = Column(TEXT, comment="缓存的令牌") - terminal = Column(TEXT, comment="终端代码") - -class Store(Base): - __tablename__ = 'store' - store_id = Column(TEXT, primary_key=True,comment="主键") - stock_level = Column(Integer, comment = "货存") - -class Store_Book(Base): - __tablename__ = 'store_book' - store_id = Column(TEXT, comment="主键") - book_id = Column(TEXT, comment="主键") - book_info = Column(TEXT, comment="书籍信息") - stock_level = Column(Integer, comment = "货存") - - __table_args__ = ( - PrimaryKeyConstraint('store_id', 'book_id'), - ) - -class User_Store(Base): - __tablename__ = 'user_store' - - id = Column(Integer, primary_key=True, autoincrement=True, comment="主键") - - fk_user_id = Column( - TEXT, - ForeignKey( - "user.user_id", - ondelete="CASCADE", - onupdate="CASCADE", - ), - nullable=False, - comment="user外键" - ) - fk_store_id = Column( - TEXT, - ForeignKey( - "store.store_id", - ondelete="CASCADE", - onupdate="CASCADE", - ), - nullable=False, - comment="store外键" - ) - # 多对多关系的中间表必须使用联合唯一约束,防止出现重复数据 - __table_args__ = ( - UniqueConstraint("fk_user_id", "fk_store_id"), - ) - -class New_Order(Base): - __tablename__ = 'new_order' - order_id = Column(TEXT, primary_key = True, comment = '订单id') - - fk_user_id = Column( - TEXT, - ForeignKey( - "user.user_id", - ondelete="CASCADE", - onupdate="CASCADE", - ), - nullable=False, - comment="user外键" - ) - fk_store_id = Column( - TEXT, - ForeignKey( - "store.store_id", - ondelete="CASCADE", - onupdate="CASCADE", - ), - nullable=False, - comment="store外键" - ) - -class New_Order_Detail(Base): - __tablename__ = 'new_order_detail' - order_id = Column(TEXT, comment='订单id') - book_id = Column(TEXT, comment='订单书籍') - count = Column(Integer, comment='购买书籍数') - price = Column(Integer, comment='单价') - - __table_args__ = ( - PrimaryKeyConstraint('order_id','book_id'), - ) - -engine, meta = con.connect() - -Base.metadata.bind = engine - -DBSession = sessionmaker(bind=engine) - -session = DBSession() diff --git a/be/testconn.py b/be/testconn.py deleted file mode 100644 index f880222..0000000 --- a/be/testconn.py +++ /dev/null @@ -1,22 +0,0 @@ -from flask import Blueprint -from flask import Flask, abort, request, jsonify -import json -from flask import jsonify -from sqlalchemy.ext.serializer import loads, dumps -import postgreSQLORM -from postgreSQLORM import User,Store,Store_Book,User_Store,New_Order,New_Order_Detail - -bp_testconn = Blueprint("testconn",__name__,url_prefix="/testconn") - -@bp_testconn.route("/test",methods=['GET']) -def test(): - user_id: str = request.args.get("user_id") - print(user_id) - try: - ss = postgreSQLORM.session.query(User).filter(User.user_id==user_id).all() - for i in ss: - print('user_id',i.user_id) - except: - print("error") - return "connect success" - diff --git a/doc/buyer.md b/doc/buyer.md index 5f415d2..ef2ddc1 100644 --- a/doc/buyer.md +++ b/doc/buyer.md @@ -142,3 +142,124 @@ Status Code: 200 | 充值成功 401 | 授权失败 5XX | 无效参数 + + +## 买家收货 + +#### URL + +POST http://[address]/buyer/take_over + +#### Request +Headers: + +key | 类型 | 描述 | 是否可为空 +---|---|---|--- +token | string | 登录产生的会话标识 | N + +Body: + +```json +{ + "user_id": "$seller id$", + "order_id": "$store id$", +} +``` +key | 类型 | 描述 | 是否可为空 +---|---|---|--- +user_id | String | 买家用户ID | N +order_id | String | 订单号 | N + +Response + +Status Code: + +码 | 描述 +--- | --- +200 | 收货成功 +5XX | 买家用户ID不存在 +5XX | 无效参数 + + +## 买家取消订单 + +#### URL + +POST http://[address]/buyer/order_cancel + +#### Request +Headers: + +key | 类型 | 描述 | 是否可为空 +---|---|---|--- +token | string | 登录产生的会话标识 | N + +Body: + +```json +{ + "user_id": "$seller id$", + "order_id": "$store id$", +} +``` +key | 类型 | 描述 | 是否可为空 +---|---|---|--- +user_id | String | 买家用户ID | N +order_id | String | 订单号 | N + +#### Response + +Status Code: + +码 | 描述 +--- | --- +200 | 收货成功 +5XX | 买家用户ID不存在 +5XX | 无效参数 + + +## 历史订单查询 + +#### URL + +POST http://[address]/buyer/history_order + +#### Request +Headers: + +key | 类型 | 描述 | 是否可为空 +---|---|---|--- +token | string | 登录产生的会话标识 | N + +Body: + +```json +{ + "user_id": "$buyer id$" +} +``` +key | 类型 | 描述 | 是否可为空 +---|---|---|--- +user_id | String | 买家用户ID | N + +#### Response + +Status Code: + +码 | 描述 +--- | --- +200 | 查询成功 +5XX | 买家用户ID不存在 +5XX | 无效参数 + +Body: + +```json +{ + "order_id": ["uuid"] +} +``` + +变量名 | 类型 | 描述 | 是否可为空 +---|---|---|--- +order_id | string | 订单号,只有返回200时才有效 | N \ No newline at end of file diff --git a/doc/seller.md b/doc/seller.md index 0513122..0d05ad3 100644 --- a/doc/seller.md +++ b/doc/seller.md @@ -176,3 +176,41 @@ Status Code: 200 | 创建商铺成功 5XX | 商铺ID不存在 5XX | 图书ID不存在 + + +## 商家发货 + +#### URL + +POST http://[address]/seller/send_out + +#### Request + +Headers: + +key | 类型 | 描述 | 是否可为空 +---|---|---|--- +token | string | 登录产生的会话标识 | N + +Body: + +```json +{ + "user_id": "$seller id$", + "order_id": "$store id$", +} +``` +key | 类型 | 描述 | 是否可为空 +---|---|---|--- +user_id | String | 卖家用户ID | N +order_id | String | 订单号 | N + +#### Response + +Status Code: + +码 | 描述 +--- | --- +200 | 发货成功 +5XX | 买家用户ID不存在 +5XX | 无效参数 \ No newline at end of file diff --git a/modified/be/Config.py b/modified/be/Config.py deleted file mode 100644 index e69de29..0000000 diff --git a/modified/be/creatTB.py b/modified/be/creatTB.py deleted file mode 100644 index d0f51f0..0000000 --- a/modified/be/creatTB.py +++ /dev/null @@ -1,5 +0,0 @@ -from postgreSQLORM import Base,engine - -def createTable(): - # 创建所有继承于Base的类对应的表 - Base.metadata.create_all(engine) diff --git a/modified/be/model/buyer.py b/modified/be/model/buyer.py index fe1f8b1..70afe9b 100644 --- a/modified/be/model/buyer.py +++ b/modified/be/model/buyer.py @@ -7,8 +7,10 @@ from model.postgreSQLORM import New_Order_Detail from model.postgreSQLORM import New_Order from model.postgreSQLORM import User import uuid + import json import logging +import time from model import db_conn from model import error @@ -59,6 +61,7 @@ class Buyer(db_conn.DBConn): return error.error_stock_level_low(book_id) + (order_id, ) # 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) @@ -68,7 +71,8 @@ class Buyer(db_conn.DBConn): # "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) + creat_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) ## 订单创建时间 + new_order_entity = New_Order(order_id=uid,fk_store_id=store_id,fk_user_id=user_id,creat_time=creat_time,status=0) self.session.add(new_order_entity) # self.conn.execute( # "INSERT INTO new_order(order_id, store_id, user_id) " @@ -163,13 +167,14 @@ class Buyer(db_conn.DBConn): if row == 0: return error.error_non_exist_user_id(buyer_id) # print('touch2') - row = session.query(New_Order).filter(New_Order.order_id==order_id).delete() + # row = session.query(New_Order).filter(New_Order.order_id==order_id).delete() 不在删除而是修改订单状态 + row = session.query(New_Order).filter(New_Order.order_id==order_id).update({'status':1}) # 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) - row = session.query(New_Order_Detail).filter(New_Order_Detail.order_id==order_id).delete() + # 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: @@ -213,3 +218,75 @@ class Buyer(db_conn.DBConn): return 530, "{}".format(str(e)) return 200, "ok" + + def take_over(self, user_id, order_id): + session = self.session + try: + if not self.user_id_exist(user_id): + return error.error_non_exist_user_id(user_id) + + row = session.query(New_Order).filter(and_(New_Order.order_id==order_id,New_Order.fk_user_id==user_id)).first() + if row is None: + return error.error_invalid_order_id(order_id) + if row.status != 2: + return error.error_invalid_order_id(order_id) + + row = session.query(New_Order).filter(and_(New_Order.order_id==order_id,New_Order.fk_user_id==user_id)).update({'status':3}) + + if row == 0: + return error.error_invalid_order_id(order_id) + + session.commit() + + except SQLAlchemyError as e: + return 528, "{}".format(str(e)) + except BaseException as e: + # print('touch3') + return 530, "{}".format(str(e)) + return 200, "ok" + + def order_cancel(self, user_id, order_id): + session = self.session + try: + if not self.user_id_exist(user_id): + return error.error_non_exist_user_id(user_id) + + row = session.query(New_Order).filter(and_(New_Order.order_id==order_id,New_Order.fk_user_id==user_id)).first() + if row is None: + return error.error_invalid_order_id(order_id) + if row.status != 0: + return error.error_invalid_order_id(order_id) + + row = session.query(New_Order).filter(and_(New_Order.order_id==order_id,New_Order.fk_user_id==user_id)).update({'status':-1}) + + if row == 0: + return error.error_invalid_order_id(order_id) + + session.commit() + + except SQLAlchemyError as e: + return 528, "{}".format(str(e)) + except BaseException as e: + # print('touch3') + return 530, "{}".format(str(e)) + return 200, "ok" + + def history_order(self, user_id): + session = self.session + try: + if not self.user_id_exist(user_id): + return error.error_non_exist_user_id(user_id) + row = session.query(New_Order).filter(New_Order.fk_user_id==user_id).first() + if row is None: + return error.error_non_exist_user_id(user_id) + row = session.query(New_Order).filter(New_Order.fk_user_id==user_id).order_by(New_Order.creat_time.desc()).all() + order_ids = [] + for i in row: + order_ids.append(i.order_id) + session.commit() + except SQLAlchemyError as e: + return 528, "{}".format(str(e)) + except BaseException as e: + # print('touch3') + return 530, "{}".format(str(e)) + return 200, "ok", order_ids diff --git a/modified/be/model/postgreSQLORM.py b/modified/be/model/postgreSQLORM.py index 7f3ef54..d8f9c5c 100644 --- a/modified/be/model/postgreSQLORM.py +++ b/modified/be/model/postgreSQLORM.py @@ -5,6 +5,9 @@ from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker from sqlalchemy import PrimaryKeyConstraint,UniqueConstraint import sqlalchemy + +import time + Base=declarative_base() class con: def connect(): @@ -100,6 +103,8 @@ class New_Order(Base): nullable=False, comment="store外键" ) + creat_time = Column(TEXT, nullable=False,comment="订单创建时间") + status = Column(Integer,nullable=False,comment="订单状态") class New_Order_Detail(Base): __tablename__ = 'new_order_detail' diff --git a/modified/be/model/seller.py b/modified/be/model/seller.py index 4e8b341..ef38308 100644 --- a/modified/be/model/seller.py +++ b/modified/be/model/seller.py @@ -1,6 +1,7 @@ from sqlalchemy.exc import SQLAlchemyError from model import error from model import postgreSQLORM +from model.postgreSQLORM import New_Order from model import db_conn from sqlalchemy import and_ @@ -75,3 +76,29 @@ class Seller(db_conn.DBConn): # print('touch3') return 530, "{}".format(str(e)) return 200, "ok" + + def send_out(self, order_id:str, user_id:str): + session = self.session + try: + if not self.user_id_exist(user_id): + return error.error_non_exist_user_id(user_id) + + row = session.query(New_Order).filter(New_Order.order_id==order_id).first() + if row is None: + return error.error_invalid_order_id(order_id) + if row.status != 1: + return error.error_invalid_order_id(order_id) + + row = session.query(New_Order).filter(New_Order.order_id==order_id).update({'status':2}) + + if row == 0: + return error.error_invalid_order_id(order_id) + + session.commit() + + except SQLAlchemyError as e: + return 528, "{}".format(str(e)) + except BaseException as e: + # print('touch3') + return 530, "{}".format(str(e)) + return 200, "ok" \ No newline at end of file diff --git a/modified/be/model/store.py b/modified/be/model/store.py deleted file mode 100644 index e69de29..0000000 diff --git a/modified/be/postgreSQLORM.py b/modified/be/postgreSQLORM.py deleted file mode 100644 index 7f3ef54..0000000 --- a/modified/be/postgreSQLORM.py +++ /dev/null @@ -1,121 +0,0 @@ -from sqlalchemy import create_engine,MetaData -from sqlalchemy import Integer,String,ForeignKey,Column,TEXT -from sqlalchemy.orm import relationship -from sqlalchemy.ext.declarative import declarative_base -from sqlalchemy.orm import sessionmaker -from sqlalchemy import PrimaryKeyConstraint,UniqueConstraint -import sqlalchemy -Base=declarative_base() -class con: - def connect(): - '''Returns a connection and a metadata object''' - # We connect with the help of the PostgreSQL URL - - url = 'postgresql://stu10205501415:Stu10205501415@dase-cdms-2022-pub.pg.rds.aliyuncs.com:5432/stu10205501415' - - # The return value of create_engine() is our connection object - con = create_engine(url, client_encoding='utf8') - - # We then bind the connection to MetaData() - meta = MetaData(bind=con) - - return con, meta - - -class User(Base): - __tablename__ = 'user' - user_id = Column(TEXT, primary_key=True, comment="主键") - password = Column(TEXT, nullable=False, comment="密码") - balance = Column(Integer, nullable=False, comment="") - token = Column(TEXT, comment="缓存的令牌") - terminal = Column(TEXT, comment="终端代码") - -class Store(Base): - __tablename__ = 'store' - store_id = Column(TEXT, primary_key=True,comment="主键") - stock_level = Column(Integer, comment = "货存") - -class Store_Book(Base): - __tablename__ = 'store_book' - store_id = Column(TEXT, comment="主键") - book_id = Column(TEXT, comment="主键") - book_info = Column(TEXT, comment="书籍信息") - stock_level = Column(Integer, comment = "货存") - - __table_args__ = ( - PrimaryKeyConstraint('store_id', 'book_id'), - ) - -class User_Store(Base): - __tablename__ = 'user_store' - - id = Column(Integer, primary_key=True, autoincrement=True, comment="主键") - - fk_user_id = Column( - TEXT, - ForeignKey( - "user.user_id", - ondelete="CASCADE", - onupdate="CASCADE", - ), - nullable=False, - comment="user外键" - ) - fk_store_id = Column( - TEXT, - ForeignKey( - "store.store_id", - ondelete="CASCADE", - onupdate="CASCADE", - ), - nullable=False, - comment="store外键" - ) - # 多对多关系的中间表必须使用联合唯一约束,防止出现重复数据 - __table_args__ = ( - UniqueConstraint("fk_user_id", "fk_store_id"), - ) - -class New_Order(Base): - __tablename__ = 'new_order' - order_id = Column(TEXT, primary_key = True, comment = '订单id') - - fk_user_id = Column( - TEXT, - ForeignKey( - "user.user_id", - ondelete="CASCADE", - onupdate="CASCADE", - ), - nullable=False, - comment="user外键" - ) - fk_store_id = Column( - TEXT, - ForeignKey( - "store.store_id", - ondelete="CASCADE", - onupdate="CASCADE", - ), - nullable=False, - comment="store外键" - ) - -class New_Order_Detail(Base): - __tablename__ = 'new_order_detail' - order_id = Column(TEXT, comment='订单id') - book_id = Column(TEXT, comment='订单书籍') - count = Column(Integer, comment='购买书籍数') - price = Column(Integer, comment='单价') - - __table_args__ = ( - PrimaryKeyConstraint('order_id','book_id'), - ) - -engine, meta = con.connect() - -Base.metadata.bind = engine - -DBSession = sessionmaker(bind=engine) - -session = DBSession() diff --git a/modified/be/testconn.py b/modified/be/testconn.py deleted file mode 100644 index a43855a..0000000 --- a/modified/be/testconn.py +++ /dev/null @@ -1,22 +0,0 @@ -from flask import Blueprint -from flask import Flask, abort, request, jsonify -import json -from flask import jsonify -from sqlalchemy.ext.serializer import loads, dumps -import postgreSQLORM -from postgreSQLORM import User,Store,Store_Book,User_Store,New_Order,New_Order_Detail - -bp_testconn = Blueprint("testconn",__name__,url_prefix="/testconn") - -@bp_testconn.route("/test",methods=['GET']) -def test(): - user_id: str = request.args.get("user_id") - print(user_id) - try: - ss = postgreSQLORM.session.query(User).filter(User.user_id==user_id).all() - for i in ss: - print('user_id',i.user_id) - except: - print("error") - return "connect success" - diff --git a/modified/be/view/buyer.py b/modified/be/view/buyer.py index 3ef1a88..6d4ada8 100644 --- a/modified/be/view/buyer.py +++ b/modified/be/view/buyer.py @@ -40,3 +40,25 @@ def add_funds(): code, message = b.add_funds(user_id, password, add_value) return jsonify({"message": message}), code +@bp_buyer.route("/take_over", methods=["POST"]) +def take_over(): + user_id = request.json.get("user_id") + order_id = request.json.get("order_id") + b = Buyer() + code, message = b.take_over(user_id, order_id) + return jsonify({"message": message}), code + +@bp_buyer.route("/order_cancel", methods=["POST"]) +def order_cancel(): + user_id = request.json.get("user_id") + order_id = request.json.get("order_id") + b = Buyer() + code, message = b.order_cancel(user_id, order_id) + return jsonify({"message": message}), code + +@bp_buyer.route("/history_order", methods=["POST"]) +def history_order(): + user_id = request.json.get("user_id") + b = Buyer() + code, message, order_ids = b.history_order(user_id) + return jsonify({"message": message,"order_id": order_ids}), code diff --git a/modified/be/view/seller.py b/modified/be/view/seller.py index c839e2a..13b162f 100644 --- a/modified/be/view/seller.py +++ b/modified/be/view/seller.py @@ -39,3 +39,13 @@ def add_stock_level(): code, message = s.add_stock_level(user_id, store_id, book_id, add_num) return jsonify({"message": message}), code + +@bp_seller.route("/send_out",methods=["POST"]) +def send_out(): + order_id: str = request.json.get("order_id") + user_id: str = request.json.get("user_id") + s = seller.Seller() + code, message = s.send_out(order_id,user_id) + + return jsonify({"message": message}), code + diff --git a/report.md b/report.md index 814fb67..e6be630 100644 --- a/report.md +++ b/report.md @@ -47,7 +47,7 @@   -#### 二. 依据上述分析构建数据库table +#### 二. 依据上述分析构建数据库table(前60%) 1. 利用sqlalchemy连接远程的aliyun数据库并创建上述table [postgreSQLORM.py](./modified/be/postgreSQLORM.py) ```python class con: @@ -271,4 +271,316 @@ ![avatar](./figure_require/addfunds_test.png) ![avatar](./figure_require/payment_test.png) - 10. \ No newline at end of file +#### 三、根据要求实现后续的40%的功能并为其编写测试接口 + 1. 对数据库结构进行改造(添加table的列) + + 为了实现发货,收获,订单状态的查询,可以在new_order的订单的table中添加status,并利用不同的状态码来表示当前次订单的状态 + + |status code|status| + |---|---| + |-1|取消| + |0|初始值(未付款)| + |1|已付款| + |2|已发货| + |3|已收货| + + 因此修改postgreSQLORM.py文件中New_Order的类 + + 2. 修改对应payment接口中删除订单的操作未修改订单状态未1 + ```python + row = session.query(New_Order).filter(New_Order.order_id==order_id).update({'status':1}) + ``` + + 3. 为seller路由新增发货(修改订单状态为2)接口、buyer路由新增收货(修改订单状态为3)接口、buyer路由新增取消订单(修改订单状态为-1)接口 +```python + ## /view/seller.py + @bp_seller.routr("/send_out",methods=["POST"]) + def send_out(): + order_id: str = request.json.get("order_id") + user_id: str = request.json.get("user_id") + s = seller.Seller() + code, message = s.send_out(order_id) + + return jsonify({"message": message}), code +``` +```python + ## /model/seller.py + def send_out(self, order_id:str): + session = self.session + try: + if not self.user_id_exist(user_id): + return error.error_non_exist_user_id(user_id) + + row = session.query(New_Order).filter(New_Order.order_id==order_id).first() + if row is None: + return error.error_invalid_order_id(order_id) + if row.status != 1: + return error.error_invalid_order_id(order_id) + + row = session.query(New_Order).filter(New_Order.order_id==order_id).update({'status':2}) + + if row == 0: + return error.error_invalid_order_id(order_id) + + session.commit() + + except SQLAlchemyError as e: + return 528, "{}".format(str(e)) + except BaseException as e: + # print('touch3') + return 530, "{}".format(str(e)) + return 200, "ok" +``` + +商家发货 + +URL + +POST http://[address]/seller/send_out + +Request +Headers: + +key | 类型 | 描述 | 是否可为空 +---|---|---|--- +token | string | 登录产生的会话标识 | N + +Body: + +```json +{ + "user_id": "$seller id$", + "order_id": "$store id$", +} +``` +key | 类型 | 描述 | 是否可为空 +---|---|---|--- +user_id | String | 卖家用户ID | N +order_id | String | 订单号 | N + +Response + +Status Code: + +码 | 描述 +--- | --- +200 | 发货成功 +5XX | 买家用户ID不存在 +5XX | 无效参数 + + +```python +## /view/buyer.py +@bp_buyer.route("/take_over", methods=["POST"]) +def take_over(): + user_id = request.json.get("user_id") + order_id = request.json.get("order_id") + b = Buyer() + code, message = b.take_over(user_id, order_id) + return jsonify({"message": message}), code + +``` + +```python +## /model/buyer.py +def take_over(self, user_id, order_id): + session = self.session + try: + if not self.user_id_exist(user_id): + return error.error_non_exist_user_id(user_id) + + row = session.query(New_Order).filter(and_(New_Order.order_id==order_id,New_Order.fk_user_id==user_id)).first() + if row is None: + return error.error_invalid_order_id(order_id) + if row.status != 2: + return error.error_invalid_order_id(order_id) + + row = session.query(New_Order).filter(and_(New_Order.order_id==order_id,New_Order.fk_user_id==user_id)).update({'status':3}) + + if row == 0: + return error.error_invalid_order_id(order_id) + + session.commit() + + except SQLAlchemyError as e: + return 528, "{}".format(str(e)) + except BaseException as e: + # print('touch3') + return 530, "{}".format(str(e)) + return 200, "ok" + +``` +卖家收货 + +URL + +POST http://[address]/buyer/take_over + +Request +Headers: + +key | 类型 | 描述 | 是否可为空 +---|---|---|--- +token | string | 登录产生的会话标识 | N + +Body: + +```json +{ + "user_id": "$seller id$", + "order_id": "$store id$", +} +``` +key | 类型 | 描述 | 是否可为空 +---|---|---|--- +user_id | String | 买家用户ID | N +order_id | String | 订单号 | N + +Response + +Status Code: + +码 | 描述 +--- | --- +200 | 收货成功 +5XX | 买家用户ID不存在 +5XX | 无效参数 + +```python +## /view/buyer.py +@bp_buyer.route("/order_cancel", methods=["POST"]) +def take_over(): + user_id = request.json.get("user_id") + order_id = request.json.get("order_id") + b = Buyer() + code, message = b.order_cancel(user_id, order_id) + return jsonify({"message": message}), code +``` +```python +## /model/buyer.py +def order_cancel(self, user_id, order_id): + session = self.session + try: + if not self.user_id_exist(user_id): + return error.error_non_exist_user_id(user_id) + + row = session.query(New_Order).filter(and_(New_Order.order_id==order_id,New_Order.fk_user_id==user_id)).first() + if row is None: + return error.error_invalid_order_id(order_id) + if row.status != 0: + return error.error_invalid_order_id(order_id) + + row = session.query(New_Order).filter(and_(New_Order.order_id==order_id,New_Order.fk_user_id==user_id)).update({'status':-1}) + + if row == 0: + return error.error_invalid_order_id(order_id) + + session.commit() + + except SQLAlchemyError as e: + return 528, "{}".format(str(e)) + except BaseException as e: + # print('touch3') + return 530, "{}".format(str(e)) + return 200, "ok" +``` + +买家取消订单 + +URL + +POST http://[address]/buyer/order_cancel + +Request +Headers: + +key | 类型 | 描述 | 是否可为空 +---|---|---|--- +token | string | 登录产生的会话标识 | N + +Body: + +```json +{ + "user_id": "$seller id$", + "order_id": "$store id$", +} +``` +key | 类型 | 描述 | 是否可为空 +---|---|---|--- +user_id | String | 买家用户ID | N +order_id | String | 订单号 | N + +Response + +Status Code: + +码 | 描述 +--- | --- +200 | 收货成功 +5XX | 买家用户ID不存在 +5XX | 无效参数 + + + 5. 像buyer路由中添加一个查询历史订单的接口,为此首先需要在原来的New_Order表中添加creat_time列,然后为buyer路由添加history_order接口 + +```python +## /view/buyer.py +@bp_buyer.route("/history_order", methods=["POST"]) +def take_over(): + user_id = request.json.get("user_id") + b = Buyer() + code, message = b.history_order(user_id) + return jsonify({"message": message}), code +``` +```python +## /model/buyer.py + +``` + +历史订单查询 + +URL + +POST http://[address]/buyer/history_order + +Request +Headers: + +key | 类型 | 描述 | 是否可为空 +---|---|---|--- +token | string | 登录产生的会话标识 | N + +Body: + +```json +{ + "user_id": "$buyer id$" +} +``` +key | 类型 | 描述 | 是否可为空 +---|---|---|--- +user_id | String | 买家用户ID | N + +Response + +Status Code: + +码 | 描述 +--- | --- +200 | 查询成功 +5XX | 买家用户ID不存在 +5XX | 无效参数 + +Body: + +```json +{ + "order_id": ["uuid"] +} +``` + +变量名 | 类型 | 描述 | 是否可为空 +---|---|---|--- +order_id | string | 订单号,只有返回200时才有效 | N + 1. \ No newline at end of file