python简易在线产品采购系统开发

应某大型炼化企业要求,为其开发(简易)在线产品采购系统,所需功能如下。
用户登录和注销功能
系统有顾客、商家和管理员三种用户,其中顾客账户数量不限,商家和管理员账户仅有 一个。每个用户都有各自的用户名和密码,输入用户名和密码即可登录为对应角色,登录后 可随时注销。
为简化考虑,注册和修改密码功能完全由管理员账户承担,登录界面不做这两个功能的 要求。
顾客功能
顾客登录后,须具备以下功能:

  1. 按不同类别查看化工产品的名称、价格、库存量和组分信息,界面清楚且按一定规
    则排序;

  2. 将化工产品添加至进货单;

  3. 查看或结算进货单;

  4. 选择某一化工产品,查看其平均打分和全部反馈及回复;

  5. 选择某一化工产品(不限定是否购买过),给其打分并发布反馈。
    商家功能
    商家登录后,须具备以下功能:

  6. 按不同类别查看化工产品的名称、价格、库存量和组分信息,界面清楚且按一定规
    则排序;

  7. 增减化工产品种类;

  8. 修改化工产品价格、库存量和组分信息;

  9. 选择某一化工产品,再选择某一顾客的反馈,发布回复。
    管理员功能
    管理员作为超级用户,除了具备商家的全部功能外,还须具备以下功能: 1. 增加顾客账户;

  10. 删除顾客账户,同时一并删除其所有打分和反馈;

  11. 修改顾客或商家账户的密码;

  12. 选择某一化工产品,选择某一打分、反馈或回复,修改其内容或将其删除。

可以回答你的问题,但是回答以前,你先给咱们说说你是怎么拿到大型炼化企业的合同的。

【以下回答由 GPT 生成】

简易的在线产品采购系统开发思路

用户登录和注销功能

  1. 将用户账户信息存储在数据库中,包含用户名和密码字段。
  2. 提供登录界面,接收用户输入的用户名和密码。
  3. 查询数据库,验证用户名和密码是否匹配。
  4. 如果验证成功,将用户信息保存在会话中,表示用户已登录。
  5. 提供注销功能,将会话中用户信息删除,表示用户已注销。

代码示例:

from flask import Flask, session, redirect, url_for, request

app = Flask(__name__)
app.secret_key = "mysecretkey"

# 用户账户信息的数据库
users = [
    {'username': 'customer1', 'password': 'password1'},
    {'username': 'customer2', 'password': 'password2'},
    {'username': 'merchant', 'password': 'password'},
    {'username': 'admin', 'password': 'password'}
]

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']

        # 验证用户名和密码
        for user in users:
            if user['username'] == username and user['password'] == password:
                session['username'] = username
                return redirect(url_for('index'))

        return 'Invalid username or password'

    return '''
        <form method="post">
            <input type="text" name="username" placeholder="Username"><br>
            <input type="password" name="password" placeholder="Password"><br>
            <input type="submit" value="Login">
        </form>
    '''

@app.route('/logout')
def logout():
    session.pop('username', None)
    return redirect(url_for('index'))

@app.route('/')
def index():
    if 'username' in session:
        return 'Logged in as {}'.format(session['username'])
    return 'Not logged in'

if __name__ == '__main__':
    app.run()

顾客功能

查看产品信息

  1. 将产品信息存储在数据库中,包括名称、价格、库存量、组分和打分等字段。
  2. 提供按类别分类、排序和展示产品信息的功能,可以使用数据库查询语句实现。

代码示例:

import sqlite3

# 连接到数据库
conn = sqlite3.connect('products.db')
cursor = conn.cursor()

# 创建产品信息表
cursor.execute('''
    CREATE TABLE IF NOT EXISTS products (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        name TEXT,
        price REAL,
        stock INTEGER,
        composition TEXT,
        rating REAL
    )
''')

# 插入示例数据
products = [
    ('Product A', 10.0, 5, 'Component A,B,C', 4.5),
    ('Product B', 15.0, 3, 'Component A,B,D', 3.8),
    ('Product C', 12.0, 8, 'Component B,C,D', 4.2)
]

cursor.executemany('INSERT INTO products (name, price, stock, composition, rating) VALUES (?, ?, ?, ?, ?)', products)

# 查询产品信息
category = 'Category A'
order_by = 'price DESC'
cursor.execute('SELECT * FROM products WHERE category = ? ORDER BY ?', (category, order_by))
products = cursor.fetchall()

# 打印产品信息
for product in products:
    print('Name: {}, Price: {}, Stock: {}, Composition: {}, Rating: {}'.format(product[1], product[2], product[3], product[4], product[5]))

# 关闭数据库连接
conn.close()

添加产品至进货单

  1. 在用户登录会话中添加进货单列表。
  2. 提供添加产品至进货单的功能,将产品添加到进货单列表中。

代码示例:

@app.route('/add_to_cart/<int:product_id>')
def add_to_cart(product_id):
    if 'cart' not in session:
        session['cart'] = []

    # 添加产品至进货单
    session['cart'].append(product_id)

    return redirect(url_for('view_cart'))

@app.route('/view_cart')
def view_cart():
    cart = session.get('cart')

    # 根据进货单中产品ID查询产品信息
    products = []
    for product_id in cart:
        product = get_product_by_id(product_id)
        if product:
            products.append(product)

    return render_template('cart.html', products=products)

查看和结算进货单

  1. 提供查看进货单功能,将进货单中的产品信息展示给用户。

代码示例:

@app.route('/view_cart')
def view_cart():
    cart = session.get('cart')

    # 根据进货单中产品ID查询产品信息
    products = []
    for product_id in cart:
        product = get_product_by_id(product_id)
        if product:
            products.append(product)

    return render_template('cart.html', products=products)

@app.route('/checkout')
def checkout():
    # 根据进货单中产品ID计算总价格
    total_price = 0
    cart = session.get('cart')
    for product_id in cart:
        product = get_product_by_id(product_id)
        if product:
            total_price += product['price']

    return render_template('checkout.html', total_price=total_price)

查看产品评分和反馈

  1. 创建评分和反馈表,建立与产品表的关联。
  2. 计算产品的平均打分。
  3. 查询产品的全部反馈及回复。

代码示例:

# 创建评分和反馈表
cursor.execute('''
    CREATE TABLE IF NOT EXISTS ratings (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        product_id INTEGER,
        rating REAL,
        feedback TEXT,
        reply TEXT
    )
''')

# 插入示例数据
ratings = [
    (1, 4.5, 'Good product', 'Thank you for your feedback'),
    (1, 5.0, 'Excellent product', 'We appreciate your positive feedback'),
    (2, 3.5, 'Average product', 'We apologize for any inconvenience caused')
]

cursor.executemany('INSERT INTO ratings (product_id, rating, feedback, reply) VALUES (?, ?, ?, ?)', ratings)

# 计算产品的平均打分
average_rating = cursor.execute('SELECT AVG(rating) FROM ratings WHERE product_id = ?', (product_id,)).fetchone()[0]
print('Average rating: {}'.format(average_rating))

# 查询产品的全部反馈及回复
feedbacks = cursor.execute('SELECT feedback, reply FROM ratings WHERE product_id = ?', (product_id,)).fetchall()
for feedback in feedbacks:
    print('Feedback: {}, Reply: {}'.format(feedback[0], feedback[1]))

# 关闭数据库连接
conn.close()

给产品打分和发布反馈

  1. 提供给产品打分和发布反馈的界面。
  2. 将用户输入的打分和反馈存储到数据库中。

代码示例:

@app.route('/rate_product/<int:product_id>', methods=['GET', 'POST'])
def rate_product(product_id):
    if request.method == 'POST':
        rating = request.form['rating']
        feedback = request.form['feedback']

        # 将打分和反馈存储到数据库
        save_rating(product_id, rating, feedback)

        return redirect(url_for('view_product', product_id=product_id))

    return '''
        <form method="post">
            <input type="number" name="rating" min="1" max="5" step="0.1" placeholder="Rating"><br>
            <textarea name="feedback" placeholder="Feedback"></textarea><br>
            <input type="submit" value="Submit">
        </form>
    '''

商家功能

增加和删除产品种类

  1. 在数据库产品表中增加类别字段。
  2. 提供增加产品种类的功能,将新的产品类别插入数据库。
  3. 提供删除产品种类的功能,从数据库中删除指定的产品类别。

代码示例:

# 增加产品种类字段
cursor.execute('ALTER TABLE products ADD COLUMN category TEXT')

# 插入示例数据
categories = ['Category A', 'Category B', 'Category C']
for category in categories:
    cursor.execute('INSERT INTO products (category) VALUES (?)', (category,))

# 删除指定的产品种类
category_to_delete = 'Category C'
cursor.execute('DELETE FROM products WHERE category = ?', (category_to_delete,))

修改产品信息

  1. 提供修改产品价格、库存量和组分信息的界面。
  2. 根据用户的输入,更新数据库中产品的相应字段。

代码示例:

@app.route('/edit_product/<int:product_id>', methods=['GET', 'POST'])
def edit_product(product_id):
    product = get_product_by_id(product_id)
    if not product:
        return 'Product not found'

    if request.method == 'POST':
        price = request.form['price']
        stock = request.form['stock']
        composition = request.form['composition']

        # 更新产品信息
        update_product(product_id, price, stock, composition)

        return redirect(url_for('view_product', product_id=product_id))

    return '''
        <form method="post">
            <input type="number" name="price" step="0.01" placeholder="Price" value="{}"><br>
            <input type="number" name="stock" placeholder="Stock" value="{}"><br>
            <input type="text" name="composition" placeholder="Composition" value="{}"><br>
            <input type="submit" value="Save">
        </form>
    '''.format(product['price'], product['stock'], product['composition'])

回复产品反馈

  1. 提供选择产品和反馈的界面。
  2. 根据用户的选择,将回复内容更新到数据库中。

代码示例:

@app.route('/reply_feedback/<int:product_id>/<int:rating_id>', methods=['GET', 'POST'])
def reply_feedback(product_id, rating_id):
    rating = get_rating_by_id(rating_id)
    if not rating:
        return 'Rating not found'

    if request.method == 'POST':
        reply = request.form['reply']

        # 更新反馈的回复内容
        update_reply(rating_id, reply)

        return redirect(url_for('view_product', product_id=product_id))

    return '''
        <form method="post">
            <textarea name="reply" placeholder="Reply"></textarea><br>
            <input type="submit" value="Reply">
        </form>
    '''

管理员功能

增加顾客账户

  1. 提供增加顾客账户的界面。
  2. 将用户输入的用户名和密码存储到数据库中。

代码示例:

@app.route('/add_customer', methods=['GET', 'POST'])
def add_customer():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']

        # 将顾客账户存储到数据库
        save_customer(username, password)

        return redirect(url_for('index'))

    return '''
        <form method="post">
            <input type="text" name="username" placeholder="Username"><br>
            <input type="password" name="password" placeholder="Password"><br>
            <input type="submit" value="Add Customer">
        </form>
    '''

删除顾客账户

  1. 提供删除顾客账户的界面。
  2. 删除指定用户的账户信息和相关的评分和反馈。

代码示例:

@app.route('/delete_customer/<int:customer_id>')
def delete_customer(customer_id):
    # 删除用户账户和相关评分和反馈
    delete_customer_info(customer_id)

    return redirect(url_for('index'))

修改账户密码

  1. 提供修改账户密码的界面。
  2. 更新数据库中指定用户的密码字段。

代码示例:

@app.route('/change_password/<int:user_id>', methods=['GET', 'POST'])
def change_password(user_id):
    user = get_user_by_id(user_id)
    if not user:
        return 'User not found'

    if request.method == 'POST':
        password = request.form['password']

        # 更新用户密码
        update_password(user_id, password)

        return redirect(url_for('index'))

    return '''
        <form method="post">
            <input type="password" name="password" placeholder="Password"><br>
            <input type="submit" value="Change Password">
        </form>
    '''

修改产品评分、反馈和回复

  1. 提供选择产品和评分、反馈、回复的界面。
  2. 根据用户的选择,更新数据库中评分、反馈或回复的相应字段,或将其删除。

代码示例: ```python @app.route('/edit_rating//', methods=['GET', 'POST']) def edit_rating(product_id, rating_id): rating = get_rating_by_id(rating_id) if not rating: return 'Rating not found'

if request.method == 'POST':
    rating = request.form['rating']
    feedback = request.form['feedback']

    # 更新评分和反馈内容
    update_rating(rating_id, rating, feedback)

    return redirect(url_for('view_product', product_id=product_id))

return '''
    <form method="post">
        <input type="number" name="rating" min="1" max="5" step="0.1" placeholder="Rating" value="{}"><br>
        <textarea name="feedback" placeholder="Feedback">{}</textarea><br>
        <input type="submit" value="Save">
    </form>
'''.


【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^