Bladeren bron

完成素材列表接口

tsurumure 1 maand geleden
bovenliggende
commit
3c64d4ee70

+ 13 - 13
db/ai_material.sql

@@ -25,18 +25,18 @@ CREATE TABLE `ai_material` (
     INDEX `idx_material_name` (`material_name`)
 ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='素材表';
 
-INSERT INTO ai_material(user_id, category_id, tag_ids, material_name, image_thumb_url, image_url, fla_url, psd_url, is_copyright) VALUES
-    (1, 1, '1,2', '御姐魔女-1', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1),
-    (1, 1, '1,2', '御姐魔女-2', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1),
-    (1, 1, '1,2', '御姐魔女-3', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1),
-    (1, 1, '1,2', '御姐魔女-4', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1),
-    (1, 1, '1,2', '御姐魔女-5', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1),
-    (1, 1, '1,2', '御姐魔女-6', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1),
-    (1, 1, '1,2', '御姐魔女-7', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1),
-    (1, 1, '1,2', '御姐魔女-8', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1),
-    (1, 1, '1,2', '御姐魔女-9', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1),
-    (1, 1, '1,2', '御姐魔女-10', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1),
-    (1, 1, '1,2', '御姐魔女-11', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1),
-    (1, 1, '1,2', '御姐魔女-12', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1)
+INSERT INTO ai_material(user_id, category_id, tag_ids, material_name, image_thumb_url, image_url, fla_url, psd_url, is_copyright, create_time) VALUES
+    (1, 1, '1,2', '御姐魔女-1', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1, '2025-07-28 08:30:00'),
+    (1, 1, '1,2', '御姐魔女-2', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1, '2025-07-28 08:30:01'),
+    (1, 1, '1,2', '御姐魔女-3', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1, '2025-07-28 08:30:02'),
+    (1, 1, '1,2', '御姐魔女-4', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1, '2025-07-28 08:30:03'),
+    (1, 1, '1,2', '御姐魔女-5', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1, '2025-07-28 08:30:04'),
+    (1, 1, '1,2', '御姐魔女-6', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1, '2025-07-28 08:30:05'),
+    (1, 1, '1,2', '御姐魔女-7', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1, '2025-07-28 08:30:06'),
+    (1, 1, '1,2', '御姐魔女-8', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1, '2025-07-28 08:30:07'),
+    (1, 1, '4,5', '御姐魔女-9', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1, '2025-07-28 08:30:08'),
+    (1, 1, '3,4,5', '御姐魔女-10', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1, '2025-07-28 08:30:09'),
+    (1, 1, '3', '御姐魔女-11', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1, '2025-07-28 08:30:10'),
+    (1, 1, '1,2', '御姐魔女-12', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.png', 'https://www.xxx.com/xx.fla', 'https://www.xxx.com/xx.psd', 1, '2025-07-28 08:30:11')
 ;
 

+ 11 - 1
src/main/java/com/backendsys/modules/ai/material/controller/MaterialController.java

@@ -1,9 +1,14 @@
 package com.backendsys.modules.ai.material.controller;
 
+import com.backendsys.modules.ai.material.entity.Material;
+import com.backendsys.modules.ai.material.entity.MaterialTag;
 import com.backendsys.modules.ai.material.service.MaterialService;
+import com.backendsys.modules.common.utils.Result;
+import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 @Validated
@@ -14,6 +19,11 @@ public class MaterialController {
     @Autowired
     private MaterialService materialService;
 
-
+    @Operation(summary = "获取素材列表")
+//    @PreAuthorize("@sr.hasPermission('31')")
+    @GetMapping("/api/ai/material/getMaterialList")
+    public Result getMaterialTagList(Material material) {
+        return Result.success().put("data", materialService.selectMaterialList(material));
+    }
 
 }

+ 7 - 0
src/main/java/com/backendsys/modules/ai/material/dao/MaterialDao.java

@@ -4,6 +4,13 @@ import com.backendsys.modules.ai.material.entity.Material;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import org.apache.ibatis.annotations.Mapper;
 
+import java.util.List;
+import java.util.Map;
+
 @Mapper
 public interface MaterialDao extends BaseMapper<Material> {
+
+    // 获取素材分类列表
+    List<Material> selectMaterialList(Material material);
+
 }

+ 6 - 1
src/main/java/com/backendsys/modules/ai/material/entity/Material.java

@@ -15,6 +15,7 @@ import org.hibernate.validator.constraints.Range;
 
 import java.time.LocalDateTime;
 import java.util.List;
+import java.util.Map;
 
 @Data
 @TableName("ai_material")
@@ -36,8 +37,12 @@ public class Material {
 
     @NotNull(message="素材分类ID不能为空", groups = { Create.class, Update.class })
     private Long category_id;
-    private String tag_ids;
+    @TableField(exist = false)
+    private String category_name;
 
+    private String tag_ids;
+    @TableField(exist = false)
+    private Long tag_id;
     @TableField(exist = false)
     private List<MaterialTag> tag_list;
 

+ 3 - 0
src/main/java/com/backendsys/modules/ai/material/entity/MaterialTag.java

@@ -34,6 +34,9 @@ public class MaterialTag {
     @NotEmpty(message="标签名称不能为空", groups = { Create.class, Update.class })
     private String tag_name;
 
+    @TableField(exist = false)
+    private Integer material_count;
+
     @Range(min = 1, max = 9999, message = "排序必须在 {min} 到 {max} 之间")
     private Integer sort;
 

+ 3 - 1
src/main/java/com/backendsys/modules/ai/material/service/MaterialService.java

@@ -1,9 +1,11 @@
 package com.backendsys.modules.ai.material.service;
 
+import com.backendsys.modules.ai.material.entity.Material;
 import com.backendsys.utils.response.PageEntity;
 
 public interface MaterialService {
 
-    PageEntity getMaterialList();
+    // 获取素材列表
+    PageEntity selectMaterialList(Material material);
 
 }

+ 52 - 2
src/main/java/com/backendsys/modules/ai/material/service/impl/MaterialServiceImpl.java

@@ -1,17 +1,67 @@
 package com.backendsys.modules.ai.material.service.impl;
 
+import cn.hutool.core.util.StrUtil;
+import com.backendsys.modules.ai.material.dao.MaterialDao;
+import com.backendsys.modules.ai.material.dao.MaterialTagDao;
+import com.backendsys.modules.ai.material.entity.Material;
+import com.backendsys.modules.ai.material.entity.MaterialTag;
 import com.backendsys.modules.ai.material.service.MaterialService;
+import com.backendsys.modules.upload.enums.StyleEnums;
+import com.backendsys.modules.upload.utils.UploadUtil;
 import com.backendsys.utils.response.PageEntity;
+import com.backendsys.utils.response.PageInfoResult;
 import com.backendsys.utils.v2.PageUtils;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
 @Service
 public class MaterialServiceImpl implements MaterialService {
 
+    @Autowired
+    private MaterialDao materialDao;
+    @Autowired
+    private MaterialTagDao materialTagDao;
+
+    /**
+     * 获取素材列表
+     */
     @Override
-    public PageEntity getMaterialList() {
+    public PageEntity selectMaterialList(Material material) {
         PageUtils.startPage();  // 分页
-        return null;
+
+        List<Material> list = materialDao.selectMaterialList(material);
+
+        // 1) 完成分页实体渲染
+        PageEntity pageEntity = new PageInfoResult(list).toEntity();
+
+        if (!list.isEmpty()) {
+            // 2) 分页列表格式化
+            list = list.stream().map(item -> {
+                String tag_ids = item.getTag_ids();
+                if (StrUtil.isNotEmpty(tag_ids)) {
+                    LambdaQueryWrapper<MaterialTag> wrapper = new LambdaQueryWrapper<>();
+                    wrapper.in(MaterialTag::getId, tag_ids.split(","));
+                    wrapper.orderByDesc(MaterialTag::getSort);
+                    List<MaterialTag> materialTagList = materialTagDao.selectList(wrapper);
+                    item.setTag_list(materialTagList);
+                }
+                return item;
+            }).collect(Collectors.toList());
+
+            // 3) 分页实体重新赋值
+            List<Object> objectList = list.stream().map(item -> (Object) item).collect(Collectors.toList());
+            pageEntity.setList(objectList);
+        }
+
+        return pageEntity;
+
+
+//        return new PageInfoResult(list).toEntity();
     }
 
 }

+ 1 - 3
src/main/java/com/backendsys/modules/ai/material/service/impl/MaterialTagImpl.java

@@ -33,9 +33,7 @@ public class MaterialTagImpl implements MaterialTagService {
      */
     @Override
     public List<MaterialTag> selectMaterialTagPopover(MaterialTag materialTag) {
-        LambdaQueryWrapper<MaterialTag> wrapper = new LambdaQueryWrapper<>();
-        wrapper.eq(MaterialTag::getCategory_id, materialTag.getCategory_id());
-        return materialTagDao.selectList(wrapper);
+        return materialTagDao.selectMaterialTagList(materialTag);
     }
 
 }

+ 64 - 0
src/main/resources/mapper/ai/material/MaterialDao.xml

@@ -0,0 +1,64 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
+<mapper namespace="com.backendsys.modules.ai.material.dao.MaterialDao">
+
+    <sql id="includeMaterial">
+        m.id,
+        m.id material_id,
+        m.user_id,
+        m.category_id,
+        COALESCE(mc.category_name, '') category_name,
+        COALESCE(m.tag_ids, '') tag_ids,
+        m.material_name,
+        COALESCE(m.image_thumb_url, '') image_thumb_url,
+        COALESCE(m.image_url, '') image_url,
+        COALESCE(m.fla_url, '') fla_url,
+        COALESCE(m.psd_url, '') psd_url,
+        m.is_copyright,
+        m.create_time,
+        m.update_time
+    </sql>
+    <!-- java.util.LinkedHashMap -->
+    <resultMap id="resultMapMaterial" type="com.backendsys.modules.ai.material.entity.Material">
+        <id property="id" column="id" jdbcType="BIGINT" />
+        <result property="material_id" column="id" javaType="java.lang.Long" />
+        <result property="category_id" column="category_id" javaType="java.lang.Long" />
+        <result property="category_name" column="category_name" />
+        <result property="tag_ids" column="tag_ids" />
+        <result property="material_name" column="material_name" />
+        <result property="image_thumb_url" column="image_thumb_url" />
+        <result property="image_url" column="image_url" />
+        <result property="fla_url" column="fla_url" />
+        <result property="psd_url" column="psd_url" />
+        <result property="is_copyright" column="is_copyright" javaType="java.lang.Integer" />
+        <result property="create_time" column="create_time"
+                typeHandler="com.backendsys.config.Mybatis.handler.timezone.LocalDateTimeHandler" />
+        <result property="update_time" column="update_time"
+                typeHandler="com.backendsys.config.Mybatis.handler.timezone.LocalDateTimeHandler" />
+    </resultMap>
+
+    <!-- 查 -->
+    <select id="selectMaterialList" resultMap="resultMapMaterial">
+        SELECT
+            <include refid="includeMaterial" />
+        FROM ai_material m
+        LEFT JOIN ai_material_category mc ON m.category_id = mc.id
+        <where>
+            <if test="category_id != null and category_id != ''">
+                AND m.category_id = #{category_id}
+            </if>
+            <if test="tag_id != null and tag_id != ''">
+                AND FIND_IN_SET(#{tag_id}, m.tag_ids) > 0
+            </if>
+            <if test="material_name != null and material_name != ''">
+                AND m.material_name like concat('%', #{material_name}, '%')
+            </if>
+            <if test="is_copyright != null and is_copyright != ''">
+                AND m.is_copyright = #{is_copyright}
+            </if>
+        </where>
+        ORDER BY m.create_time DESC
+    </select>
+
+
+</mapper>

+ 15 - 11
src/main/resources/mapper/ai/material/MaterialTagDao.xml

@@ -3,32 +3,36 @@
 <mapper namespace="com.backendsys.modules.ai.material.dao.MaterialTagDao">
 
     <sql id="includeMaterialTag">
-        id,
-        id tag_id,
-        category_id,
-        tag_name,
-        sort
+        mt.id,
+        mt.id tag_id,
+        mt.category_id,
+        mt.tag_name,
+        mt.sort
     </sql>
     <!-- COALESCE(content_type, '') content_type, -->
     <resultMap id="resultMapMaterialTag" type="com.backendsys.modules.ai.material.entity.MaterialTag">
         <id property="id" column="id" jdbcType="BIGINT" />
         <result property="tag_id" column="id" javaType="java.lang.Long" />
-        <result property="category_id" column="id" javaType="java.lang.Long" />
+        <result property="category_id" column="category_id" javaType="java.lang.Long" />
         <result property="tag_name" column="tag_name" />
+        <result property="material_count" column="material_count" javaType="java.lang.Integer" />
         <result property="sort" column="sort" javaType="java.lang.Integer" />
     </resultMap>
 
     <select id="selectMaterialTagList" resultMap="resultMapMaterialTag">
         SELECT
-            <include refid="includeMaterialTag" />
-        FROM ai_material_tag
+            <include refid="includeMaterialTag" />,
+            COUNT(m.id) material_count
+        FROM ai_material_tag mt
+        LEFT JOIN ai_material m ON FIND_IN_SET(mt.id, m.tag_ids) > 0
         <where>
-            category_id = #{category_id}
+            mt.category_id = #{category_id}
             <if test="tag_name != null and tag_name != ''">
-                AND tag_name like concat('%', #{tag_name}, '%')
+                AND mt.tag_name like concat('%', #{tag_name}, '%')
             </if>
         </where>
-        ORDER BY sort DESC
+        GROUP BY mt.id
+        ORDER BY mt.sort DESC
     </select>