瀏覽代碼

完成商品品牌和单位的关联重构

tsurumure 1 月之前
父節點
當前提交
251b9f7601

+ 21 - 23
db/b2c_good_brand.sql

@@ -12,30 +12,28 @@ CREATE TABLE `b2c_good_brand` (
     `brand_name` VARCHAR(100) NOT NULL COMMENT '商品品牌名称',
     `brand_name_en` VARCHAR(100) NOT NULL COMMENT '商品品牌名称 (英文)',
     `sort` INT DEFAULT '1' COMMENT '排序',
-    `status` TINYINT(1) DEFAULT '1' COMMENT '商品品牌状态 (-1禁用, 1启用)'
+    `status` TINYINT(1) DEFAULT '1' COMMENT '商品品牌状态 (-1禁用, 1启用)',
 ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='商品品牌表';
 
 INSERT INTO b2c_good_brand(parent_id, brand_name, brand_name_en, sort) VALUES
-    (-1, '优衣库', 'UNIQLO', 1),
-    (-1, 'ZARA', 'ZARA', 2),
-    (-1, 'H&M', 'H&M', 3),
-    (-1, '耐克', 'Nike', 4),
-        (4, 'Air Jordan', 'Air Jordan', 11),       -- 属于 Nike
-        (4, 'Nike SB', 'Nike Skateboarding', 12),  -- 属于 Nike
-    (-1, '阿迪达斯', 'Adidas', 5),
-    (-1, '路易威登', 'Louis Vuitton', 6),
-    (-1, '古驰', 'Gucci', 7),
-    (-1, '香奈儿', 'Chanel', 8),
-    (-1, '彪马', 'Puma', 9),
-    (-1, '李宁', 'Li-Ning', 10),
-    (-1, '安踏', 'ANTA', 11),
-    (-1, '太平鸟', 'PEACEBIRD', 12),
-    (-1, '杰克琼斯', 'Jack & Jones', 13),
-    (-1, 'Only', 'Only', 14),
-    (-1, 'Vero Moda', 'Vero Moda', 15),
-    (-1, '森马', 'Semir', 16),
-    (-1, '美特斯邦威', 'Meters/bonwe', 17),
-    (-1, '海澜之家', 'HLA', 18),
-    (-1, '七匹狼', 'Septwolves', 19),
-    (-1, '波司登', 'Bosideng', 20)
+    (-1, '优衣库', 'UNIQLO', 20),
+    (-1, 'ZARA', 'ZARA', 19),
+    (-1, 'H&M', 'H&M', 18),
+    (-1, '耐克', 'Nike', 17),
+        (4, 'Air Jordan', 'Air Jordan', 16),
+        (4, 'Nike SB', 'Nike Skateboarding', 15),
+    (-1, '阿迪达斯', 'Adidas', 14),
+    (-1, '路易威登', 'Louis Vuitton', 13),
+    (-1, '古驰', 'Gucci', 12),
+    (-1, '香奈儿', 'Chanel', 11),
+    (-1, '彪马', 'Puma', 10),
+    (-1, '李宁', 'Li-Ning', 9),
+    (-1, '安踏', 'ANTA', 8),
+    (-1, '太平鸟', 'PEACEBIRD', 7),
+    (-1, '杰克琼斯', 'Jack & Jones', 6),
+    (-1, 'Only', 'Only', 5),
+    (-1, 'Vero Moda', 'Vero Moda', 4),
+    (-1, '森马', 'Semir', 3),
+    (-1, '美特斯邦威', 'Meters/bonwe', 2),
+    (-1, '海澜之家', 'HLA', 1)
 ;

+ 29 - 0
src/main/java/com/backendsys/modules/b2c/good/controller/GoodBrandController.java

@@ -0,0 +1,29 @@
+package com.backendsys.modules.b2c.good.controller;
+
+import com.backendsys.modules.b2c.good.entity.GoodBrand;
+import com.backendsys.modules.b2c.good.service.GoodBrandService;
+import com.backendsys.modules.common.config.security.annotations.Anonymous;
+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
+@RestController
+@Tag(name = "商品品牌管理")
+public class GoodBrandController {
+
+    @Autowired
+    private GoodBrandService goodBrandService;
+
+    @Anonymous
+    @Operation(summary = "获取商品品牌列表")
+    @GetMapping("/api/b2c/good/getGoodBrandList")
+    public Result selectGoodBrandList(GoodBrand goodBrand) {
+        return Result.success().put("data", goodBrandService.selectGoodBrandList(goodBrand));
+    }
+
+}

+ 2 - 2
src/main/java/com/backendsys/modules/b2c/good/controller/GoodUnitController.java

@@ -21,8 +21,8 @@ public class GoodUnitController {
     @Anonymous
     @Operation(summary = "获取商品单位列表")
     @GetMapping("/api/b2c/good/getGoodUnitList")
-    public Result getGood(String unit_category) {
-        return Result.success().put("data", goodUnitService.selectGoodUnit(unit_category));
+    public Result selectGoodUnitList(String unit_category) {
+        return Result.success().put("data", goodUnitService.selectGoodUnitList(unit_category));
     }
 
 }

+ 5 - 0
src/main/java/com/backendsys/modules/b2c/good/dao/GoodBrandDao.java

@@ -4,6 +4,11 @@ import com.backendsys.modules.b2c.good.entity.GoodBrand;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import org.apache.ibatis.annotations.Mapper;
 
+import java.util.List;
+
 @Mapper
 public interface GoodBrandDao extends BaseMapper<GoodBrand> {
+
+    List<GoodBrand> selectGoodBrandList(GoodBrand goodBrand);
+
 }

+ 7 - 0
src/main/java/com/backendsys/modules/b2c/good/entity/GoodBrand.java

@@ -1,10 +1,13 @@
 package com.backendsys.modules.b2c.good.entity;
 
 import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
 import lombok.Data;
 
+import java.util.List;
+
 @Data
 @TableName("b2c_good_brand")
 public class GoodBrand {
@@ -14,6 +17,10 @@ public class GoodBrand {
     private Long parent_id;
     private String brand_name;
     private String brand_name_en;
+
+    @TableField(exist = false)
+    private List<GoodBrand> children;
+
     private Integer sort;
     private Integer status;
 

+ 12 - 0
src/main/java/com/backendsys/modules/b2c/good/service/GoodBrandService.java

@@ -0,0 +1,12 @@
+package com.backendsys.modules.b2c.good.service;
+
+import com.backendsys.modules.b2c.good.entity.GoodBrand;
+
+import java.util.List;
+
+public interface GoodBrandService {
+
+    // 获取商品单位
+    List<GoodBrand> selectGoodBrandList(GoodBrand goodBrand);
+
+}

+ 1 - 1
src/main/java/com/backendsys/modules/b2c/good/service/GoodUnitService.java

@@ -7,6 +7,6 @@ import java.util.List;
 public interface GoodUnitService {
 
     // 获取商品单位
-    List<GoodUnit> selectGoodUnit(String unit_category);
+    List<GoodUnit> selectGoodUnitList(String unit_category);
 
 }

+ 51 - 0
src/main/java/com/backendsys/modules/b2c/good/service/impl/GoodBrandServiceImpl.java

@@ -0,0 +1,51 @@
+package com.backendsys.modules.b2c.good.service.impl;
+
+import cn.hutool.core.util.StrUtil;
+import com.backendsys.modules.b2c.good.dao.GoodBrandDao;
+import com.backendsys.modules.b2c.good.entity.GoodBrand;
+import com.backendsys.modules.b2c.good.entity.GoodUnit;
+import com.backendsys.modules.b2c.good.service.GoodBrandService;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Service
+public class GoodBrandServiceImpl implements GoodBrandService {
+
+    @Autowired
+    private GoodBrandDao goodBrandDao;
+
+    // 获取商品单位
+    @Override
+    public List<GoodBrand> selectGoodBrandList(GoodBrand goodBrand) {
+        List<GoodBrand> goodBrandList = goodBrandDao.selectGoodBrandList(goodBrand);
+        return buildTree(goodBrandList, -1L);
+    }
+
+    /**
+     * 递归构建树形结构
+     * @param all_brands 所有品牌数据
+     * @param parent_id  当前父级ID
+     * @return 树形结构列表
+     */
+    private List<GoodBrand> buildTree(List<GoodBrand> all_brands, Long parent_id) {
+        List<GoodBrand> treeList = new ArrayList<>();
+
+        for (GoodBrand brand : all_brands) {
+            if (parent_id.equals(brand.getParent_id())) {
+                // 递归查询子节点
+                List<GoodBrand> children = buildTree(all_brands, brand.getId());
+                if (!children.isEmpty()) {
+                    brand.setChildren(children);
+                }
+                treeList.add(brand);
+            }
+        }
+
+        return treeList;
+    }
+
+}

+ 2 - 1
src/main/java/com/backendsys/modules/b2c/good/service/impl/GoodUnitServiceImpl.java

@@ -30,11 +30,12 @@ public class GoodUnitServiceImpl implements GoodUnitService {
      *   - 其他单位: other
      */
     @Override
-    public List<GoodUnit> selectGoodUnit(String unit_category) {
+    public List<GoodUnit> selectGoodUnitList(String unit_category) {
         LambdaQueryWrapper<GoodUnit> wrapper = new LambdaQueryWrapper<>();
         if (StrUtil.isNotEmpty(unit_category)) {
             wrapper.eq(GoodUnit::getUnit_category, unit_category);
         }
+        wrapper.orderByAsc(GoodUnit::getSort);
         return goodUnitDao.selectList(wrapper);
     }
 

+ 40 - 0
src/main/resources/mapper/b2c/good/GoodBrandDao.xml

@@ -0,0 +1,40 @@
+<?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.b2c.good.dao.GoodBrandDao">
+
+    <sql id="includeGoodBrand">
+        id,
+        COALESCE(parent_id, -1) parent_id,
+        COALESCE(brand_name, "") brand_name,
+        COALESCE(brand_name_en, "") brand_name_en,
+        sort,
+        status
+    </sql>
+
+    <resultMap id="resultMapGoodBrand" type="com.backendsys.modules.b2c.good.entity.GoodBrand">
+        <id property="id" column="id" jdbcType="BIGINT" />
+        <result property="parent_id" column="parent_id" javaType="java.lang.Long" />
+        <result property="brand_name" column="brand_name" />
+        <result property="brand_name_en" column="brand_name_en" />
+        <result property="sort" column="sort" javaType="java.lang.Integer" />
+        <result property="status" column="status" javaType="java.lang.Integer" />
+    </resultMap>
+
+    <select id="selectGoodBrandList" resultMap="resultMapGoodBrand">
+        SELECT <include refid="includeGoodBrand" />
+        FROM b2c_good_brand
+        <where>
+            <if test="brand_name != null and brand_name != ''">
+                AND brand_name = #{brand_name}
+            </if>
+            <if test="brand_name_en != null and brand_name_en != ''">
+                AND brand_name_en = #{brand_name_en}
+            </if>
+            <if test="status != null and status != ''">
+                AND status = #{status}
+            </if>
+        </where>
+        ORDER BY sort DESC
+    </select>
+
+</mapper>

+ 36 - 3
src/main/resources/mapper/b2c/good/GoodDao.xml

@@ -8,8 +8,14 @@
         g.uid uid,
         gc.id category_id,
         gc.category_name category_name,
+
         COALESCE(g.brand_id, -1) brand_id,
+        COALESCE(gb.brand_name, '') brand_name,
+        COALESCE(gb.brand_name_en, '') brand_name_en,
         COALESCE(g.unit_id, -1) unit_id,
+        COALESCE(gu.unit_name, '') unit_name,
+        COALESCE(gu.unit_name_en, '') unit_name_en,
+
         COALESCE(g.good_thumb, '') good_thumb,
         COALESCE(g.good_images, '') good_images,
         gt.good_name good_name,
@@ -26,8 +32,14 @@
         g.uid uid,
         gc.id category_id,
         gc.category_name category_name,
+
         COALESCE(g.brand_id, -1) brand_id,
+        COALESCE(gb.brand_name, '') brand_name,
+        COALESCE(gb.brand_name_en, '') brand_name_en,
         COALESCE(g.unit_id, -1) unit_id,
+        COALESCE(gu.unit_name, '') unit_name,
+        COALESCE(gu.unit_name_en, '') unit_name_en,
+
         COALESCE(g.good_thumb, '') good_thumb,
         COALESCE(g.good_images, '') good_images,
         g.status status,
@@ -49,9 +61,10 @@
     <sql id="leftJoinCategory">
         LEFT JOIN b2c_good_category_i18n gc ON gc.good_category_id = g.category_id AND gc.language = #{lang}
     </sql>
-    <sql id="leftJoinTranslations">
-        LEFT JOIN b2c_good_i18n gt ON g.id = gt.good_id
-    </sql>
+    <sql id="leftJoinTranslations">LEFT JOIN b2c_good_i18n gt ON g.id = gt.good_id</sql>
+    <sql id="leftJoinUnit">LEFT JOIN b2c_good_unit gu ON g.unit_id = gu.id</sql>
+    <sql id="leftJoinBrand">LEFT JOIN b2c_good_brand gb ON g.brand_id = gb.id</sql>
+
 
     <sql id="includeGoodDetailFull">
         g.id id,
@@ -61,8 +74,14 @@
 
         gc.id category_id,
         gc.category_name category_name,
+
         COALESCE(g.brand_id, -1) brand_id,
+        COALESCE(gb.brand_name, '') brand_name,
+        COALESCE(gb.brand_name_en, '') brand_name_en,
         COALESCE(g.unit_id, -1) unit_id,
+        COALESCE(gu.unit_name, '') unit_name,
+        COALESCE(gu.unit_name_en, '') unit_name_en,
+
         COALESCE(g.good_thumb, '') good_thumb,
         COALESCE(g.good_images, '') good_images,
 
@@ -83,7 +102,11 @@
         <result property="category_id" column="category_id" javaType="java.lang.Long" />
         <result property="category_name" column="category_name" />
         <result property="brand_id" column="brand_id" javaType="java.lang.Long" />
+        <result property="brand_name" column="brand_name" />
+        <result property="brand_name_en" column="brand_name_en" />
         <result property="unit_id" column="unit_id" javaType="java.lang.Long" />
+        <result property="unit_name" column="unit_name" />
+        <result property="unit_name_en" column="unit_name_en" />
         <result property="good_thumb" column="good_thumb" />
         <result property="good_images" column="good_images" />
         <result property="good_name" column="good_name" />
@@ -101,7 +124,11 @@
         <result property="category_id" column="category_id" javaType="java.lang.Long" />
         <result property="category_name" column="category_name" />
         <result property="brand_id" column="brand_id" javaType="java.lang.Long" />
+        <result property="brand_name" column="brand_name" />
+        <result property="brand_name_en" column="brand_name_en" />
         <result property="unit_id" column="unit_id" javaType="java.lang.Long" />
+        <result property="unit_name" column="unit_name" />
+        <result property="unit_name_en" column="unit_name_en" />
         <result property="good_thumb" column="good_thumb" />
         <result property="good_images" column="good_images" />
         <result property="status" column="status" javaType="java.lang.Integer" />
@@ -146,6 +173,8 @@
         SELECT <include refid="includeGood" /> FROM b2c_good g
         <include refid="leftJoinCategory" />
         <include refid="leftJoinTranslations" />
+        <include refid="leftJoinUnit" />
+        <include refid="leftJoinBrand" />
         <where>
             gt.language = #{lang}
             <if test="uid != null and uid != ''">
@@ -172,6 +201,8 @@
         SELECT <include refid="includeGoodDetail" />
         FROM b2c_good g
         <include refid="leftJoinCategory" />
+        <include refid="leftJoinUnit" />
+        <include refid="leftJoinBrand" />
         <where>
             <if test="uid != null and uid != ''">
                 AND g.uid = #{uid}
@@ -188,6 +219,8 @@
         FROM b2c_good g
         <include refid="leftJoinCategory" />
         <include refid="leftJoinTranslations" />
+        <include refid="leftJoinUnit" />
+        <include refid="leftJoinBrand" />
         WHERE
             gt.language = #{lang}
             AND g.uid = #{uid}