from flask import request, jsonify from utils.database import get_db, db_transaction from utils.auth import get_user_id_from_token from utils.helpers import error_response, build_search_conditions def register_rating_routes(app): @app.route('/api/admin/ratings', methods=['GET']) def get_admin_ratings(): search = request.args.get('search') page = int(request.args.get('page', 1)) limit = int(request.args.get('limit', 10)) offset = (page - 1) * limit conn = get_db() cursor = conn.cursor() try: query_base = ''' SELECT r.*, t.title as task_title, u.nickname as acceptor_name, u.phone as acceptor_phone, p.nickname as publisher_name, p.phone as publisher_phone FROM task_ratings r LEFT JOIN tasks t ON r.task_id = t.id LEFT JOIN users u ON r.acceptor_id = u.id LEFT JOIN users p ON r.publisher_id = p.id ''' count_query = 'SELECT COUNT(*) FROM task_ratings r' params = [] count_params = [] if search: search_fields = ['t.title', 'u.nickname', 'u.phone', 'p.nickname', 'p.phone', 'r.comment'] search_cond, search_params = build_search_conditions(search_fields, search) if search_cond: query_base += search_cond count_query += ' LEFT JOIN tasks t ON r.task_id = t.id LEFT JOIN users u ON r.acceptor_id = u.id LEFT JOIN users p ON r.publisher_id = p.id' + search_cond count_params.extend(search_params) cursor.execute(count_query, count_params) total = cursor.fetchone()[0] query = query_base + ' ORDER BY r.created_at DESC LIMIT ? OFFSET ?' params.extend([limit, offset]) cursor.execute(query, params) rows = cursor.fetchall() items = [] for row in rows: rating = dict(row) items.append(rating) return jsonify({'items': items, 'total': total, 'page': page, 'limit': limit}) finally: conn.close() @app.route('/api/admin/ratings/', methods=['GET']) def get_admin_rating(rating_id): conn = get_db() cursor = conn.cursor() try: cursor.execute(''' SELECT r.*, t.title as task_title, u.nickname as acceptor_name, u.phone as acceptor_phone, p.nickname as publisher_name, p.phone as publisher_phone FROM task_ratings r LEFT JOIN tasks t ON r.task_id = t.id LEFT JOIN users u ON r.acceptor_id = u.id LEFT JOIN users p ON r.publisher_id = p.id WHERE r.id = ? ''', (rating_id,)) row = cursor.fetchone() if not row: return error_response('评价不存在') return jsonify(dict(row)) finally: conn.close() @app.route('/api/tasks//rating', methods=['POST']) def create_rating(task_id): auth_header = request.headers.get('Authorization') if not auth_header or not auth_header.startswith('Bearer '): return error_response('未登录', 401) token = auth_header.split(' ')[1] user_id = get_user_id_from_token(token) if not user_id: return error_response('登录已过期', 401) data = request.json conn = get_db() cursor = conn.cursor() try: cursor.execute('SELECT * FROM tasks WHERE id = ?', (task_id,)) task = cursor.fetchone() if not task: return error_response('任务不存在') if task['status'] != 'completed': return error_response('任务未完成,无法评价') rating_type = '' rated_user_id = 0 if task['publisher_id'] == user_id: rating_type = 'publisher_to_acceptor' rated_user_id = task['acceptor_id'] elif task['acceptor_id'] == user_id: rating_type = 'acceptor_to_publisher' rated_user_id = task['publisher_id'] else: return error_response('只能评价自己参与的任务', 403) if not rated_user_id: return error_response('对方用户不存在') cursor.execute('SELECT * FROM task_ratings WHERE task_id = ? AND rating_type = ?', (task_id, rating_type)) existing_rating = cursor.fetchone() if existing_rating: return error_response('已评价过该任务') cursor.execute(''' INSERT INTO task_ratings (task_id, publisher_id, acceptor_id, rating, comment, rating_type) VALUES (?, ?, ?, ?, ?, ?) ''', (task_id, task['publisher_id'], task['acceptor_id'], data.get('rating'), data.get('comment'), rating_type)) conn.commit() return jsonify({'success': True, 'message': '评价成功'}) except Exception as e: conn.rollback() raise e finally: conn.close()