From 192c05707af3c954d9592b34840eee52fb02463d Mon Sep 17 00:00:00 2001 From: liangweihao <734499798@qq.com> Date: Mon, 22 Dec 2025 20:34:38 +0800 Subject: [PATCH] =?UTF-8?q?1221=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fabric_manager_pro.py | 124 ++++++++++++++++++++++++++++-------------- 1 file changed, 84 insertions(+), 40 deletions(-) diff --git a/fabric_manager_pro.py b/fabric_manager_pro.py index 9c4a2e9..abb68cf 100644 --- a/fabric_manager_pro.py +++ b/fabric_manager_pro.py @@ -1014,18 +1014,23 @@ class GarmentEditDialog(QDialog): def load_materials(self): try: with self.get_conn() as conn: - cursor = conn.execute("SELECT category, usage_per_piece, unit FROM garment_materials WHERE style_number = ? ORDER BY id", (self.style_number,)) - for category, usage, unit in cursor.fetchall(): - self.add_material_row(category, usage or 0, unit or "米") + cursor = conn.execute("SELECT category, fabric_type, usage_per_piece, unit FROM garment_materials WHERE style_number = ? ORDER BY id", (self.style_number,)) + for category, fabric_type, usage, unit in cursor.fetchall(): + # 如果是旧数据(category包含"-"),需要拆分 + if category and "-" in category and not fabric_type: + parts = category.split("-", 1) + category = parts[0] + fabric_type = parts[1] if len(parts) > 1 else "" + self.add_material_row(category or "", fabric_type or "", usage or 0, unit or "米") except Exception as e: QMessageBox.critical(self, "错误", "加载材料失败: " + str(e)) def add_default_categories(self): - defaults = [("A料", "米"), ("B料", "米"), ("C料", "米"), ("D料", "米"), ("花边", "码"), ("胸杯", "一对"), ("拉链", "个"), ("辅料", "个")] - for cat, unit in defaults: - self.add_material_row(cat, 0, unit) + defaults = [("A料", "", "米"), ("B料", "", "米"), ("C料", "", "米"), ("D料", "", "米"), ("花边", "", "码"), ("胸杯", "", "一对"), ("拉链", "", "个"), ("辅料", "", "个")] + for cat, fabric_type, unit in defaults: + self.add_material_row(cat, fabric_type, 0, unit) - def add_material_row(self, category="", usage=0.0, unit="米"): + def add_material_row(self, category="", fabric_type="", usage=0.0, unit="米"): row = self.material_table.rowCount() self.material_table.insertRow(row) @@ -1036,12 +1041,32 @@ class GarmentEditDialog(QDialog): try: with self.get_conn() as conn: - cursor = conn.execute("SELECT DISTINCT category FROM fabrics WHERE category IS NOT NULL AND category != '' ORDER BY category") - categories = cursor.fetchall() - for cat_row in categories: - cat_combo.addItem(cat_row[0]) + # 只获取纯类目(提取"-"前面的部分) + cursor = conn.execute(""" + SELECT DISTINCT + CASE + WHEN category LIKE '%-%' THEN SUBSTR(category, 1, INSTR(category, '-') - 1) + ELSE category + END as major_category + FROM fabrics + WHERE category IS NOT NULL AND category != '' + ORDER BY major_category + """) + categories = set() + for cat_row in cursor.fetchall(): + if cat_row[0] and cat_row[0].strip(): + categories.add(cat_row[0]) + + # 添加默认类目 + categories.update(["布料", "辅料", "其他"]) + + for cat in sorted(categories): + cat_combo.addItem(cat) except: - pass + # 如果查询失败,使用默认类目 + cat_combo.addItem("布料") + cat_combo.addItem("辅料") + cat_combo.addItem("其他") if category: cat_combo.setCurrentText(category) @@ -1053,6 +1078,9 @@ class GarmentEditDialog(QDialog): type_combo = QComboBox() type_combo.setEditable(True) type_combo.addItem("—— 选择类型 ——") + if fabric_type: + type_combo.setCurrentText(fabric_type) + type_combo.currentTextChanged.connect(lambda text, r=row: self.on_type_changed(text, r)) self.material_table.setCellWidget(row, 1, type_combo) # 列2: 型号下拉框 @@ -1137,20 +1165,21 @@ class GarmentEditDialog(QDialog): try: with self.get_conn() as conn: - # 根据类目和类型获取对应的型号 + # 根据类目和类型获取对应的型号,现在需要匹配分开的字段或组合的旧格式 cursor = conn.execute(""" SELECT model, color, unit FROM fabrics - WHERE category = ? OR category LIKE ? + WHERE category = ? OR category = ? OR category LIKE ? ORDER BY model - """, (f"{category_text}-{type_text}", f"{category_text}-{type_text}-%")) + """, (category_text, f"{category_text}-{type_text}", f"{category_text}-{type_text}-%")) models = cursor.fetchall() for model_row in models: model, color, unit = model_row - display_text = f"{model}" - if color: - display_text += f"-{color}" + # 显示格式:型号-颜色(如果有颜色的话) + display_text = model + if color and color.strip(): + display_text = f"{model}-{color}" model_combo.addItem(display_text, model) except Exception as e: pass @@ -1203,26 +1232,30 @@ class GarmentEditDialog(QDialog): fabric_type = type_widget.currentText().strip() model = model_widget.currentText().strip() - # 构建完整的材料标识 - if model and model != "—— 选择型号 ——": - # 如果选择了具体型号,使用型号作为category存储 - model_data = model_widget.itemData(model_widget.currentIndex()) - final_category = model_data if model_data else model - elif fabric_type and fabric_type != "—— 选择类型 ——": - # 如果只选择了类型,组合类目和类型 - final_category = f"{category}-{fabric_type}" - else: - # 只有类目 - final_category = category + # 处理类目和类型 + if category == "—— 自定义类目 ——": + category = "" + if fabric_type == "—— 选择类型 ——": + fabric_type = "" - if not final_category or final_category == "—— 自定义类目 ——": + # 如果选择了具体型号,获取型号的实际值 + final_model = "" + if model and model != "—— 选择型号 ——": + model_data = model_widget.itemData(model_widget.currentIndex()) + final_model = model_data if model_data else model + + # 至少需要有类目或型号 + if not category and not final_model: continue usage = usage_widget.value() unit = unit_widget.currentText().strip() or "米" - conn.execute("INSERT INTO garment_materials (style_number, category, usage_per_piece, unit) VALUES (?, ?, ?, ?)", - (style_number, final_category, usage, unit)) + # 分别存储类目、类型和型号信息 + material_identifier = final_model if final_model else (f"{category}-{fabric_type}" if fabric_type else category) + + conn.execute("INSERT INTO garment_materials (style_number, category, fabric_type, usage_per_piece, unit) VALUES (?, ?, ?, ?, ?)", + (style_number, material_identifier, fabric_type, usage, unit)) conn.commit() QMessageBox.information(self, "成功", "保存完成") @@ -1286,16 +1319,18 @@ class PurchaseOrderDialog(QDialog): try: with self.get_conn() as conn: cursor = conn.execute(''' - SELECT category, usage_per_piece, unit + SELECT category, fabric_type, usage_per_piece, unit FROM garment_materials WHERE style_number = ? AND usage_per_piece > 0 ORDER BY id ''', (self.style_number,)) rows = cursor.fetchall() - for category, usage_per_piece, unit in rows: + for category, fabric_type, usage_per_piece, unit in rows: total_usage = usage_per_piece * self.quantity * (1 + self.loss_rate) - text += f"材料:{category}\n" + # 显示材料名称(如果有类型则显示类目-类型,否则只显示类目) + material_name = f"{category}-{fabric_type}" if fabric_type else category + text += f"材料:{material_name}\n" text += f" 单件用量:{usage_per_piece:.3f} {unit}\n" text += f" 总需采购:{total_usage:.3f} {unit}\n\n" @@ -1384,13 +1419,13 @@ class FabricManager(QMainWindow): try: with self.get_conn() as conn: cursor = conn.execute(''' - SELECT category, usage_per_piece, unit + SELECT category, fabric_type, usage_per_piece, unit FROM garment_materials WHERE style_number = ? ''', (style_number,)) rows = cursor.fetchall() inserted = 0 - for category, usage_per_piece, unit in rows: + for category, fabric_type, usage_per_piece, unit in rows: if usage_per_piece == 0: continue @@ -1475,11 +1510,13 @@ class FabricManager(QMainWindow): text = f"款号: {style_number}\n生产件数: {qty}\n损耗率: {self.loss_input.value()}%\n\n" try: with self.get_conn() as conn: - cursor = conn.execute("SELECT category, usage_per_piece, unit FROM garment_materials WHERE style_number = ? ORDER BY id", (style_number,)) - for cat, usage, unit in cursor.fetchall(): + cursor = conn.execute("SELECT category, fabric_type, usage_per_piece, unit FROM garment_materials WHERE style_number = ? ORDER BY id", (style_number,)) + for category, fabric_type, usage, unit in cursor.fetchall(): if usage: total = usage * qty * (1 + loss) - text += f"{cat}\n单件: {usage:.3f} {unit}\n总用量: {total:.3f} {unit}\n\n" + # 显示材料名称(如果有类型则显示类目-类型,否则只显示类目) + material_name = f"{category}-{fabric_type}" if fabric_type else category + text += f"{material_name}\n单件: {usage:.3f} {unit}\n总用量: {total:.3f} {unit}\n\n" except Exception as e: text += "计算失败: " + str(e) self.result_text.setText(text) @@ -1651,10 +1688,17 @@ class FabricManager(QMainWindow): id INTEGER PRIMARY KEY AUTOINCREMENT, style_number TEXT, category TEXT, + fabric_type TEXT, usage_per_piece REAL, unit TEXT DEFAULT '米' ) ''') + + # 添加fabric_type列(如果不存在) + try: + conn.execute("ALTER TABLE garment_materials ADD COLUMN fabric_type TEXT") + except: + pass # 列已存在 conn.execute(''' CREATE TABLE IF NOT EXISTS admin_settings (