فهرست منبع

```
feat(customer-template): 添加配置名称获取功能以支持图片重命名

- 引入数据库模型DeviceConfig、SysConfigs、SqlQuery、CRUD、select、
DeviceConfigTabs和PhotoRecord用于数据查询
- 新增get_config_name方法,通过货号查询配置名称并返回重命名列表
- 在处理配置数据时调用get_config_name方法获取重命名规则
- 根据配置名称动态更新图片键名,实现图片的自定义命名
- 添加索引控制

rambo 1 هفته پیش
والد
کامیت
b20d93f040
1فایلهای تغییر یافته به همراه61 افزوده شده و 0 حذف شده
  1. 61 0
      python/service/customer_template_service.py

+ 61 - 0
python/service/customer_template_service.py

@@ -9,6 +9,9 @@ import base64,shutil
 from logger import logger
 from natsort import ns, natsorted
 from service.base import get_images, check_path, get_image_mask
+from databases import DeviceConfig, SysConfigs, SqlQuery, CRUD, select, DeviceConfigTabs
+from model import PhotoRecord
+from sqlalchemy import and_, asc, desc
 '''前端生图接口'''
 # 
 generate_templace = "/generate"
@@ -196,6 +199,60 @@ class CustomerTemplateService:
             y_offset += resized_img.height
 
         return concatenated_image
+    def get_config_name(self,good_art_no):
+        session = SqlQuery()
+        query = (
+            select(PhotoRecord, DeviceConfig.action_name)
+            .outerjoin(DeviceConfig, PhotoRecord.action_id == DeviceConfig.id)
+            .where(PhotoRecord.goods_art_no == good_art_no)
+            .where(PhotoRecord.delete_time == None)
+            .order_by(PhotoRecord.goods_art_no, asc("id"))  # 按货号分组并按ID倒序
+        )
+        all_items = session.exec(query).mappings().all()
+        if len(all_items) == 0:  # 如果没有记录则返回
+            # raise UnicornException("暂无可用货号")
+            return None
+        goods_art_rename_list = []
+        for index,item in enumerate(all_items):
+            image_orders = [
+                "俯视",
+                "侧视",
+                "后跟",
+                "鞋底",
+                "内里",
+                "组合",
+                "组合2",
+                "组合3",
+                "组合4",
+                "组合5",
+                "组合6",
+                "组合7",
+                "组合8",
+                "组合9",
+                "组合10",
+                "组合11",
+                "组合12",
+                "组合13",
+                "组合14",
+                "组合15",
+                "组合16",
+                "组合17",
+                "组合18",
+                "组合19",
+                "组合20",
+                "组合21",
+                "组合22",
+                "组合23",
+                "组合24",
+                "组合25",
+                "组合26",
+            ]
+            if item["action_name"] == None:
+                continue
+            data = {"goods_art_no": item.PhotoRecord.goods_art_no,"old_name":image_orders[index], "action_name": item["action_name"],"image_index":item.PhotoRecord.image_index}
+            goods_art_rename_list.append(data)
+        session.close()
+        return goods_art_rename_list
     def __handler_config_data(self,config_data,scp_path):
         '''
         处理配置数据,返回一个新的数据对象
@@ -218,6 +275,7 @@ class CustomerTemplateService:
             pics = product.get("pics", {})
             goods_art_no = product.get("货号",None)
             goods_art_lens = len(new_config_data.get("货号资料", []))
+            goods_art_no_rename_list = self.get_config_name(goods_art_no)
             concat_shuffix = "" if goods_art_lens == 1 else f"_{goods_art_no}"
             if not model_image:
                 model_image_path = product.get("模特图", None)
@@ -230,6 +288,7 @@ class CustomerTemplateService:
                     scene_image = scene_image_path
                     self.copyImage(scene_image_path, f"{scp_path}/场景图{concat_shuffix}.jpg")
             new_pics = {}
+            index = 0
             for pic_key, pic_path in pics.items():
                 if "-抠图" in pic_key:
                     # 读取图片并转换为 Base64
@@ -237,6 +296,7 @@ class CustomerTemplateService:
                         base64_data = self.crop_image_and_convert_to_base64(pic_path)
                         # 新增字段(去除 -抠图)
                         new_key = pic_key.replace("-抠图", "")
+                        new_key = goods_art_no_rename_list[index].get("action_name")
                         new_pics[new_key] = base64_data
                     except Exception as e:
                         print(f"读取图片失败: {pic_path}, 错误: {e}")
@@ -245,6 +305,7 @@ class CustomerTemplateService:
                     new_pics[pic_key] = pic_path
                 # 更新 pics 字段
                 product["pics"] = new_pics
+                index += 1
             
                 # 构建目标结构
             #     result.append({key: item})