Ejemplo n.º 1
0
    def new_order(self, user_id: str, store_id: str,
                  id_and_count: [(str, int)]) -> (int, str, str):  # 箭头后表示返回类型
        order_id = ""
        if not user_id_exist(user_id):
            return error.error_non_exist_user_id(user_id) + (order_id, )
        if not 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:
            store_detail = session.query(Store_detail).filter(
                Store_detail.store_id == store_id,
                Store_detail.book_id == book_id).first()
            if (store_detail is None):
                return error.error_non_exist_book_id(book_id) + (order_id, )

            stock_level = store_detail.stock_level

            if stock_level < count:
                return error.error_stock_level_low(book_id) + (order_id, )

            store_detail.stock_level -= count
            session.add(
                Order_detail(order_id=uid, book_id=book_id, count=count))
        session.add(
            Order_to_Pay(order_id=uid,
                         user_id=user_id,
                         store_id=store_id,
                         paytime=datetime.now()))
        session.commit()
        order_id = uid
        return 200, "ok", order_id
Ejemplo n.º 2
0
 def new_order(self, user_id: str, store_id: str, id_and_count: [(str, int)]) -> (int, str, str):
     order_id = ""
     try:
         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()))
         order_id = uid
         for book_id, count in id_and_count:
             row = self.Session.query(Store).filter(Store.store_id==store_id,Store.book_id==book_id).first()
             if row is None:
                 return error.error_non_exist_book_id(book_id) + (order_id, )
             book_info_json = json.loads(row.book_info)
             price = book_info_json.get("price")
             if row.stock_level < count:
                 return error.error_stock_level_low(book_id) + (order_id,)
             else:
                 row.stock_level -= count
             new_order = New_order_detail(order_id=uid, book_id=book_id, count=count, price=price)
             self.Session.add(new_order)
         # print('插入订单')
         # 插入订单更新:添加两个属性
         new_ord = New_order(order_id=uid, store_id=store_id, user_id=user_id, state=0, create_time=time.time(), delivery_time=0 )
         self.Session.add(new_ord)
         self.Session.commit()
     except sqlalchemy.exc.IntegrityError as e:
         logging.info("528, {}".format(str(e)))
         return 528, "{}".format(str(e)), ""
     except BaseException as e:
         logging.info("530, {}".format(str(e)))
         return 530, "{}".format(str(e)), ""
     return 200, "ok", order_id
Ejemplo n.º 3
0
    def new_order(self, user_id: str, store_id: str,
                  id_and_count: [(str, int)]) -> (int, str, str):
        order_id = ""
        try:
            if self.session.query(User).filter_by(
                    user_id=user_id).first() is None:
                return error.error_non_exist_user_id(user_id) + (order_id, )
            if self.session.query(Store).filter_by(
                    store_id=store_id).first() is None:
                return error.error_non_exist_store_id(store_id) + (order_id, )
            uid = "{}_{}_{}".format(user_id, store_id, str(uuid.uuid1()))

            pt = datetime.datetime.now()

            new_order = Order(id=uid,
                              status=Order_status.pending,
                              buyer_id=user_id,
                              store_id=store_id,
                              pt=pt)
            self.session.add(new_order)

            for book_id, count in id_and_count:
                cursor = self.session.query(Book_info).filter_by(
                    id=book_id, store_id=store_id)
                row = cursor.first()
                if row is None:
                    return error.error_non_exist_book_id(book_id) + (
                        order_id, )

                inventory_count = row.inventory_count
                price = row.price

                if inventory_count < count:
                    return error.error_stock_level_low(book_id) + (order_id, )

                cursor = self.session.query(Book_info).filter(
                    Book_info.id == book_id, Book_info.store_id == store_id,
                    Book_info.inventory_count >= count)
                rowcount = cursor.update({
                    Book_info.inventory_count:
                    Book_info.inventory_count - count
                })
                if rowcount == 0:
                    return error.error_stock_level_low(book_id) + (order_id, )

                new_order_info = Order_info(order_id=uid,
                                            book_id=book_id,
                                            count=count,
                                            price=price)
                self.session.add(new_order_info)

            self.session.commit()
            self.session.close()
            order_id = uid
        except BaseException as e:
            logging.info("530, {}".format(str(e)))
            return 530, "{}".format(str(e)), ""

        return 200, "ok", order_id
Ejemplo n.º 4
0
    def new_order(self, user_id: str, store_id: str, id_and_count: [(str, int)]) -> (int, str, str):
        order_id = ""
        try:
            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()))

            total_price = 0

            for book_id, count in id_and_count:
                cursor = self.conn.execute(
                    "SELECT book_id, stock_level, book_info FROM store "
                    "WHERE store_id = '%s' AND book_id = '%s';" % (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]
                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 - %d "
                    "WHERE store_id = '%s' and book_id = '%s' and stock_level >= %d; "
                    % (count, store_id, book_id, count))
                if cursor.rowcount == 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('%s', '%s', %d, %d);"
                    % (uid, book_id, count, price))  # 添加状态0,该订单为支付

                total_price += price

            self.conn.execute(
                "INSERT INTO new_order(order_id, store_id, user_id,total_price,condition) "
                "VALUES('%s', '%s', '%s',%d,'unpaid');"
                % (uid, store_id, user_id, total_price))
            self.conn.commit()
            order_id = uid
        except sqlalchemy.exc.IntegrityError as e:
            logging.info("528, {}".format(str(e)))
            return 528, "{}".format(str(e)), ""
        except BaseException as e:
            logging.info("530, {}".format(str(e)))
            print(e)
            return 530, "{}".format(str(e)), ""

        return 200, "ok", order_id
Ejemplo n.º 5
0
    def new_order(self, user_id: str, store_id: str,
                  id_and_count: [(str, int)]) -> (int, str, str):
        order_id = ""
        try:
            #检查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:
                cursor = self.conn.execute(
                    "SELECT book_id, stock_level, book_info FROM store "
                    "WHERE store_id = %s AND book_id = %s;",
                    (store_id, book_id))
                row = self.conn.fetchone()
                #检查商店和书是否存在
                if row is None:
                    return error.error_non_exist_book_id(book_id) + (
                        order_id, )

                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 - %s "
                    "WHERE store_id = %s and book_id = %s and stock_level >= %s; ",
                    (count, store_id, book_id, count))
                """if cursor.rowcount == 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(%s, %s, %s, %s);", (uid, book_id, count, price))

            self.conn.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 pg.Error as e:
            logging.info("528, {}".format(str(e)))
            return 528, "{}".format(str(e)), ""
        except BaseException as e:
            logging.info("530, {}".format(str(e)))
            return 530, "{}".format(str(e)), ""

        return 200, "ok", order_id
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
    def new_order(self, user_id: str, store_id: str,
                  id_and_count: [(str, int)]) -> (int, str, str):
        order_id = ""
        try:
            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:
                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]
                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:
                    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()
            order_id = uid
        except sqlite.Error as e:
            logging.info("528, {}".format(str(e)))
            return 528, "{}".format(str(e)), ""
        except BaseException as e:
            logging.info("530, {}".format(str(e)))
            return 530, "{}".format(str(e)), ""

        return 200, "ok", order_id
Ejemplo n.º 8
0
    def add_stock_level(self, user_id: str, store_id: str, book_id: str,
                        add_stock_level: int):
        if not user_id_exist(user_id):
            return error.error_non_exist_user_id(user_id)
        if not store_id_exist(store_id):
            return error.error_non_exist_store_id(store_id)
        if not book_id_exist(store_id, book_id):
            return error.error_non_exist_book_id(book_id)

        cursor = session.query(Store_detail).filter(
            and_(Store_detail.store_id == store_id,
                 Store_detail.book_id == book_id)).first()
        cursor.stock_level = cursor.stock_level + add_stock_level
        session.commit()
        return 200, "ok"
Ejemplo n.º 9
0
    def add_stock_level(self, user_id: str, store_id: str, book_id: str, add_stock_level: int) -> (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_non_exist_store_id(store_id)
            if not self.book_id_exist(store_id, book_id):
                return error.error_non_exist_book_id(book_id)

            self.conn.execute(
                "UPDATE store SET stock_level = stock_level +%d WHERE store_id = '%s' AND book_id = '%s'" % (
                    add_stock_level, store_id, book_id))
            self.conn.commit()
        except sqlalchemy.exc.IntegrityError as e:
            return 528, "{}".format(str(e))
        except BaseException as e:
            return 530, "{}".format(str(e))
        return 200, "ok"
Ejemplo n.º 10
0
 def add_stock_level(self, user_id: str, store_id: str, book_id: str,
                     add_stock_level: int):
     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_non_exist_store_id(store_id)
         if not self.book_id_exist(store_id, book_id):
             return error.error_non_exist_book_id(book_id)
         row = self.Session.query(Store.stock_level).filter(
             Store.store_id == store_id, Store.book_id == book_id).first()
         stoke_level = row[0]
         stoke_level += add_stock_level
         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"
Ejemplo n.º 11
0
 def add_stock_level(self, user_id: str, store_id: str, book_id: str,
                     add_stock_level: int):
     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_non_exist_store_id(store_id)
         if not self.book_id_exist(store_id, book_id):
             return error.error_non_exist_book_id(book_id)
     #将stock_level的值变成stock_level+add_store_level
         self.cursor = self.conn.cursor()
         self.cursor.execute(
             "UPDATE store SET stock_level = stock_level + %d "
             "WHERE store_id = '%s' AND book_id = '%s'" %
             (add_stock_level, store_id, book_id))
         self.conn.commit()
     except psycopg2.errors.UniqueViolation:
         return error.error_exist_book_id(book_id)
     return 200, "ok"
Ejemplo n.º 12
0
    def add_stock_level(self, user_id: str, store_id: str, book_id: str,
                        add_stock_level: int):
        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_non_exist_store_id(store_id)
            if not self.book_id_exist(store_id, book_id):
                return error.error_non_exist_book_id(book_id)

            book_info = self.session.query(Book_info).filter(
                store_id == store_id, book_id == book_id).first()
            book_info.inventory_count += add_stock_level
            self.session.commit()
            self.session.close()

        except BaseException as e:
            return 530, "{}".format(str(e))
        return 200, "ok"
Ejemplo n.º 13
0
    def new_order(self, user_id: str, store_id: str,
                  id_and_count: [(str, int)]) -> (int, str, str):
        try:
            num = len(id_and_count)
            id = [i[0] for i in id_and_count]
            ids = '&'.join(id) + '&'
            count = [str(i[1]) for i in id_and_count]
            counts = '&'.join(count) + '&'
            order_id = "{}_{}_{}".format(user_id, store_id, str(uuid.uuid1()))
            self.cursor.callproc('new_order',
                                 (user_id, store_id, order_id, ids, counts,
                                  str(num), 'flag', 'msg'))
            self.conn.commit()
            # data = self.cursor.fetchall()
            self.cursor.execute("select @_new_order_6,@_new_order_7")
            return_value = self.cursor.fetchone()
            # print(return_value)
            if return_value[0] == 0:
                return 200, "ok", order_id
            if return_value[0] == 1:
                return error.error_non_exist_user_id(
                    return_value[1]) + (order_id, )
            if return_value[0] == 2:
                return error.error_non_exist_store_id(
                    return_value[1]) + (order_id, )
            if return_value[0] == 3:
                return error.error_non_exist_book_id(
                    return_value[1]) + (order_id, )
            if return_value[0] == 4:
                return error.error_stock_level_low(
                    return_value[1]) + (order_id, )
            if return_value[0] == 5:
                logging.info("528, {}".format(return_value[1]))
                return 528, "{}".format(return_value[1]), ""
        except pymysql.Error as e:
            logging.info("528, {}".format(str(e)))
            return 528, "{}".format(str(e)), ""
        except BaseException as e:
            logging.info("530, {}".format(str(e)))
            return 530, "{}".format(str(e)), ""

        return 200, "ok", order_id
Ejemplo n.º 14
0
    def add_stock_level(self, user_id: str, store_id: str, book_id: str,
                        add_stock_level: int):
        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_non_exist_store_id(store_id)
            if not self.book_id_exist(store_id, book_id):
                return error.error_non_exist_book_id(book_id)

            self.cursor.execute(
                "UPDATE store SET stock_level = stock_level + %s "
                "WHERE store_id = %s AND book_id = %s",
                (add_stock_level, store_id, book_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"
Ejemplo n.º 15
0
    def add_stock_level(self, user_id: str, store_id: str, book_id: str,
                        add_stock_level: int):
        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_non_exist_store_id(store_id)
            if not self.book_id_exist(store_id, book_id):
                return error.error_non_exist_book_id(book_id)
            print(user_id, store_id, book_id)

            # self.conn.execute("UPDATE store SET stock_level = stock_level + ? "
            #                   "WHERE store_id = ? AND book_id = ?", (add_stock_level, store_id, book_id))
            # self.conn.commit()
            db_session.query(Store).filter(
                Store.store_id == store_id, Store.book_id == book_id).update(
                    {Store.stock_level: Store.stock_level + add_stock_level})
            db_session.commit()
            # except Error as e:
            #     logging.info("528, {}".format(str(e)))
            # return 528, "{}".format(str(e)), ""
        except BaseException as e:
            return 530, "{}".format(str(e))
        return 200, "ok"
Ejemplo n.º 16
0
    def add_stock_level(self, user_id: str, store_id: str, book_id: str,
                        add_stock_level: int):
        try:
            if not self.user_store_exist(user_id, store_id):
                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_non_exist_store_id(store_id)
                return error.error_and_message(
                    520, error.error_code[520].format(user_id, store_id))

            if not self.book_id_exist(store_id, book_id):
                return error.error_non_exist_book_id(book_id)
            cursor = self.conn.cursor()
            cursor.execute(
                "UPDATE \"store\" SET stock_level = stock_level + (%s) "
                "WHERE store_id = (%s) AND book_id = (%s)",
                (add_stock_level, store_id, book_id))
            self.conn.commit()
        except (Exception, psycopg2.DatabaseError) as e:
            return 528, "{}".format(str(e))
        except BaseException as e:
            return 530, "{}".format(str(e))
        return 200, "ok"
Ejemplo n.º 17
0
    def new_order(self, user_id: str, store_id: str,
                  id_and_count: [(str, int)]) -> (int, str, str):
        order_id = ""
        try:
            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()))

            cursor = self.conn.cursor()
            for book_id, count in id_and_count:
                cursor.execute(
                    "SELECT book_id, stock_level, book_info FROM \"store\" "
                    "WHERE store_id = (%s) AND book_id = (%s) ",
                    (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]
                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.execute(
                    "UPDATE \"store\" SET stock_level = stock_level - (%s) "
                    "WHERE store_id = (%s) AND book_id = (%s) AND stock_level >= (%s)",
                    (count, store_id, book_id, count))
                # res = cursor.execute(
                #     "UPDATE store set stock_level = stock_level - %d WHERE store_id = '%s' and book_id = %d  and stock_level >=%d" % (
                #         count, store_id, book_id, count))
                if cursor.rowcount == 0:
                    return error.error_stock_level_low(book_id) + (order_id, )

                cursor.execute(
                    "INSERT into \"new_order_detail\" (order_id, book_id, count, price) "
                    "VALUES (%s, %s, %s, %s)", (uid, book_id, count, price))

            current_time = time.time()
            cursor.execute(
                "INSERT INTO \"new_order\" (order_id, store_id, user_id,pay,deliver,receive,return,refund,order_time) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s)",
                (uid, store_id, user_id, 0, 0, 0, 0, 0, current_time))

            scheduler = global_scheduler.instance_GlobalAutoCancelOrder.scheduler
            cur_time_str = time.strftime("%Y-%m-%d %H:%M:%S",
                                         time.localtime(current_time))
            cur_time_datetime = datetime.strptime(cur_time_str,
                                                  '%Y-%m-%d %H:%M:%S')
            auto_cancel_time = timedelta(seconds=conf.auto_cancel_time)
            scheduler.add_job(
                global_scheduler.instance_GlobalAutoCancelOrder.delete_order,
                'date',
                run_date=cur_time_datetime + auto_cancel_time,
                args=[uid])
            order_id = uid
            self.conn.commit()
            cursor.close()

        except (Exception, psycopg2.DatabaseError) as e:
            traceback.print_exc()
            logging.info("528, {}".format(str(e)))
            return 528, "{}".format(str(e)), ""
        except BaseException as e:
            logging.info("530, {}".format(str(e)))
            return 530, "{}".format(str(e)), ""

        return 200, "ok", order_id
Ejemplo n.º 18
0
    def new_order(self, user_id: str, store_id: str, id_and_count: [(str, int)]) -> (int, str, str):
        order_id = ""
        try:
            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:

                # cursor = self.conn.execute(
                #     "SELECT book_id, stock_level, book_info FROM store "
                #     "WHERE store_id = ? AND book_id = ?;",
                #     (store_id, book_id))
                row = Store.query.filter_by(store_id=store_id, book_id=book_id).first()

                if row is None:
                    return error.error_non_exist_book_id(book_id) + (order_id,)

                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))
                row = Store.query.filter_by(store_id=store_id, book_id=book_id, stock_level=stock_level).update(
                    {'stock_level': stock_level - count})
                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))
                row = New_Order_Detail(order_id=uid, book_id=book_id, count=count, price=price, state=0,
                                       time=time.time())
                db_session.add(row)

            # self.conn.execute(
            #     "INSERT INTO new_order(order_id, store_id, user_id) "
            #     "VALUES(?, ?, ?);",
            #     (uid, store_id, user_id))
            # self.conn.commit()
            row = New_Order(order_id=uid, store_id=store_id, user_id=user_id)
            db_session.add(row)
            order_id = uid
            db_session.commit()

        # except sqlite.Error as e:
        #     logging.info("528, {}".format(str(e)))
        #     return 528, "{}".format(str(e)), ""
        except BaseException as e:
            print(e)
            logging.info("530, {}".format(str(e)))
            return 530, "{}".format(str(e)), ""

        return 200, "ok", order_id