def create_store(self, user_id: str, store_id: str) -> (int, str): try: if not self.user_id_exist(user_id): return error.error_non_exist_user_id(user_id) if self.store_id_exist(store_id): return error.error_exist_store_id(store_id) #将store_id,user_id插入user_store self.cursor = self.conn.cursor() self.cursor.execute("INSERT into user_store(store_id, user_id)" "VALUES ('%s', '%s')" % (store_id, user_id)) self.conn.commit() except psycopg2.errors.UniqueViolation: return error.error_exist_store_id(store_id) return 200, "ok"
def create_store(self, user_id: str, store_id: str) -> (int, str): if not user_id_exist(user_id): return error.error_non_exist_user_id(user_id) if store_id_exist(store_id): return error.error_exist_store_id(store_id) store_one = Store(user_id=user_id, store_id=store_id) session.add(store_one) session.commit() return 200, "ok"
def new_order(self, user_id: str, store_id: str, id_and_count: [(str, int)]) -> (int, str, str): order_id = "" try: #判断用户id和门店id是否存在 if not self.user_id_exist(user_id): return error.error_non_exist_user_id(user_id) + (order_id, ) 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())) for book_id, count in id_and_count: #对每个书的id和数量 self.cursor = self.conn.cursor() self.cursor.execute( #查询书的id,数量,信息,从store "SELECT book_id, stock_level, book_info FROM store " "WHERE store_id = '%s' AND book_id = '%s'" % (store_id, book_id)) #从前端获取store_id和book_id row = self.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] #写为json book_info_json = json.loads(book_info) #从json中获取价格 price = book_info_json.get("price") #如果存货量小于订单量,则返回错误 if stock_level < count: return error.error_stock_level_low(book_id) + (order_id, ) #更新存货量,stocklevel - count self.cursor.execute( "UPDATE store set stock_level = stock_level-%d " "WHERE store_id = '%s' and book_id = '%s' and stock_level >= %d" % (count, store_id, book_id, count)) if self.cursor.rowcount == 0: return error.error_stock_level_low(book_id) + (order_id, ) #新增order(细节) self.cursor.execute( "INSERT INTO new_order_detail(order_id, book_id, count, price) " "VALUES('%s', '%s', %d, %d)" % (uid, book_id, count, price)) #新增order self.cursor = self.conn.cursor() self.cursor.execute( "INSERT INTO new_order(order_id, store_id, user_id) " "VALUES('%s', '%s', '%s')" % (uid, store_id, user_id)) self.conn.commit() order_id = uid except psycopg2.errors.UniqueViolation: return error.error_exist_store_id(store_id) return 200, "ok", order_id
def create_store(self,user_id: str, store_id: str) -> (int, str): try: if not user_id_exist(user_id): return error.error_non_exist_user_id(user_id) if store_id_exist(store_id): return error.error_exist_store_id(store_id) store_one = Store(user_id=user_id,store_id=store_id) session.add(store_one) session.commit() except BaseException as e: return 530, "{}".format(str(e)) return 200, "ok"
def create_store(self, user_id: str, store_id: str) -> (int, str): try: if not self.user_id_exist(user_id): return error.error_non_exist_user_id(user_id) if self.store_id_exist(store_id): return error.error_exist_store_id(store_id) store_obj = User_store(store_id=store_id, user_id=user_id) self.Session.add(store_obj) self.Session.commit() except sqlalchemy.exc.IntegrityError as e: return 528, "{}".format(str(e)) except BaseException as e: return 530, "{}".format(str(e)) return 200, "ok"
def create_store(self, user_id: str, store_id: str) -> (int, str): try: if not self.user_id_exist(user_id): return error.error_non_exist_user_id(user_id) if self.store_id_exist(store_id): return error.error_exist_store_id(store_id) self.cursor.execute( "INSERT into user_store(store_id, user_id)" "VALUES (%s, %s)", (store_id, user_id)) self.conn.commit() except pymysql.Error as e: return 528, "{}".format(str(e)) except BaseException as e: return 530, "{}".format(str(e)) return 200, "ok"
def create_store(self, user_id: str, store_id: str) -> (int, str): try: if not self.user_id_exist(user_id): return error.error_non_exist_user_id(user_id) if self.store_id_exist(store_id): return error.error_exist_store_id(store_id) self.conn.execute( "INSERT into user_store(user_id, store_id,s_balance)VALUES('%s','%s',0)" % (user_id, store_id)) self.conn.commit() except sqlalchemy.exc.IntegrityError as e: return 528, "{}".format(str(e)) except BaseException as e: print(e) return 530, "{}".format(str(e)) return 200, "ok"
def create_store(self, user_id: str, store_id: str) -> (int, str): try: if not self.user_id_exist(user_id): return error.error_non_exist_user_id(user_id) if self.store_id_exist(store_id): return error.error_exist_store_id(store_id) # self.conn.execute("INSERT into user_store(store_id, user_id)" # "VALUES (?, ?)", (store_id, user_id)) # self.conn.commit() user_store = User_Store(user_id=user_id, store_id=store_id) db_session.add(user_store) db_session.commit() except BaseException as e: return 530, "{}".format(str(e)) return 200, "ok"
def deliver_books(self, user_id: str, store_id: str, order_id: str) -> (int, str): try: if not self.user_id_exist(user_id): return error.error_non_exist_user_id(user_id) if not self.store_id_exist(store_id): return error.error_exist_store_id(store_id) if not self.order_id_exist(order_id): return error.error_non_exist_order_id(order_id) self.cursor.execute( "SELECT state FROM new_order " "WHERE order_id = %s", order_id) row = self.cursor.fetchone() if row[0] != 'undelivered': return error.error_order_state(order_id) self.cursor.execute( "UPDATE new_order SET state = 'delivering' " "WHERE order_id = %s", order_id) self.conn.commit() except pymysql.Error as e: return 528, "{}".format(str(e)) except BaseException as e: return 530, "{}".format(str(e)) return 200, "ok"