"""配比重量API模块""" from flask import Flask, jsonify from database.sql_server_connection import SQLServerConnection class MixWeightAPI: def __init__(self): self.app = Flask(__name__) self.db = SQLServerConnection() self._register_routes() def _register_routes(self): """注册路由""" self.app.add_url_rule('/API/mix_weights', 'get_mix_weights', self.get_mix_weights, methods=['GET']) self.app.add_url_rule('/API/mix_weights/', 'get_mix_weight_by_code', self.get_mix_weight_by_code, methods=['GET']) def get_mix_weights(self): """ HTTP接口:获取所有配比号及其对应每方重量 返回格式:JSON数组 """ try: mix_info = self.db.get_mix_weight_info() return jsonify({ "success": True, "data": mix_info, "count": len(mix_info) }), 200 except Exception as e: return jsonify({ "success": False, "error": str(e) }), 500 def get_mix_weight_by_code(self, code): """ HTTP接口:根据配比号获取其对应每方重量 返回格式:JSON对象 """ try: mix_info = self.db.get_mix_weight_info() for item in mix_info: if item["Code"] == code: return jsonify({ "success": True, "data": item }), 200 return jsonify({ "success": False, "error": f"未找到配比号 {code}" }), 404 except Exception as e: return jsonify({ "success": False, "error": str(e) }), 500 def run(self, host='127.0.0.1', port=5001, debug=True, threaded=True): """启动Flask服务""" self.app.run(host=host, port=port, debug=debug, threaded=threaded)