Ver código fonte

优化商家查询逻辑;新增商品分类类型字段;完善商家详情逻辑;

woody 1 ano atrás
pai
commit
ed109fd0a1

+ 6 - 0
vehicle-api/src/main/java/com/chelvc/cloud/vehicle/api/dto/GoodsDTO.java

@@ -4,6 +4,7 @@ import java.io.Serializable;
 import java.util.Date;
 import java.util.List;
 
+import com.chelvc.cloud.vehicle.api.constant.CategoryType;
 import com.chelvc.cloud.vehicle.api.constant.GoodsStatus;
 import com.chelvc.framework.base.model.File;
 import lombok.AllArgsConstructor;
@@ -27,6 +28,11 @@ public class GoodsDTO implements Serializable {
      */
     private Long id;
 
+    /**
+     * 分类类型
+     */
+    private CategoryType type;
+
     /**
      * 分类ID
      */

+ 5 - 4
vehicle-api/src/main/java/com/chelvc/cloud/vehicle/api/dto/MerchantDetailDTO.java

@@ -3,6 +3,7 @@ package com.chelvc.cloud.vehicle.api.dto;
 import java.io.Serializable;
 import java.util.List;
 
+import com.chelvc.cloud.vehicle.api.constant.CategoryType;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
@@ -25,12 +26,12 @@ public class MerchantDetailDTO implements Serializable {
     private MerchantDTO merchant;
 
     /**
-     * 分类列表
+     * 商品列表
      */
-    private List<CategoryDTO> categories;
+    private List<GoodsDTO> goods;
 
     /**
-     * 商品信息
+     * 分类类型列表
      */
-    private MerchantGoodsDTO goods;
+    private List<CategoryType> categories;
 }

+ 41 - 0
vehicle-api/src/main/java/com/chelvc/cloud/vehicle/api/param/GoodsQueryParam.java

@@ -0,0 +1,41 @@
+package com.chelvc.cloud.vehicle.api.param;
+
+import java.io.Serializable;
+import javax.validation.constraints.Max;
+import javax.validation.constraints.Min;
+import javax.validation.constraints.NotNull;
+
+import com.chelvc.cloud.vehicle.api.constant.CategoryType;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import lombok.experimental.SuperBuilder;
+
+/**
+ * @author Woody
+ * @date 2023/8/6
+ */
+@Data
+@SuperBuilder
+@NoArgsConstructor
+@AllArgsConstructor
+public class GoodsQueryParam implements Serializable {
+    /**
+     * 商品分类类型
+     */
+    private CategoryType type;
+
+    /**
+     * 偏移商品ID
+     */
+    @Min(value = 1, message = "偏移商品ID不能小于1")
+    private Long offset;
+
+    /**
+     * 查询数量
+     */
+    @NotNull(message = "查询数量不能为空")
+    @Min(value = 1, message = "查询数量不能小于1")
+    @Max(value = 20, message = "查询数量不能大于20")
+    private Integer size;
+}

+ 9 - 0
vehicle-api/src/main/java/com/chelvc/cloud/vehicle/api/param/MerchantQueryParam.java

@@ -1,5 +1,6 @@
 package com.chelvc.cloud.vehicle.api.param;
 
+import javax.validation.constraints.Max;
 import javax.validation.constraints.Min;
 import javax.validation.constraints.NotNull;
 import javax.validation.constraints.Size;
@@ -48,4 +49,12 @@ public class MerchantQueryParam extends LocationQueryParam {
      */
     @NotNull(message = "排序规则不能为空")
     private Sort sort;
+
+    /**
+     * 查询数量
+     */
+    @NotNull(message = "查询数量不能为空")
+    @Min(value = 1, message = "查询数量不能小于1")
+    @Max(value = 20, message = "查询数量不能大于20")
+    private Integer size;
 }

+ 23 - 0
vehicle-api/src/main/java/com/chelvc/cloud/vehicle/api/service/GoodsService.java

@@ -0,0 +1,23 @@
+package com.chelvc.cloud.vehicle.api.service;
+
+import java.util.List;
+
+import com.chelvc.cloud.vehicle.api.dto.GoodsDTO;
+import com.chelvc.cloud.vehicle.api.param.GoodsQueryParam;
+
+/**
+ * 商品业务接口
+ *
+ * @author Woody
+ * @date 2023/8/6
+ */
+public interface GoodsService {
+    /**
+     * 查询商家商品
+     *
+     * @param merchantId 商家ID
+     * @param param      查询参数
+     * @return 商品列表
+     */
+    List<GoodsDTO> listMerchantSimpleGoods(Long merchantId, GoodsQueryParam param);
+}

+ 1 - 1
vehicle-api/src/main/java/com/chelvc/cloud/vehicle/api/service/MerchantService.java

@@ -39,7 +39,7 @@ public interface MerchantService {
      * @param size  商家数量
      * @return 商家信息列表
      */
-    List<MerchantDTO> listSimpleMerchants(MerchantQueryParam param, int size);
+    List<MerchantDTO> listSimpleMerchants(MerchantQueryParam param);
 
     /**
      * 获取商家详情

+ 15 - 3
vehicle-server/src/main/java/com/chelvc/cloud/vehicle/server/dao/GoodsMapper.java

@@ -3,6 +3,9 @@ package com.chelvc.cloud.vehicle.server.dao;
 import java.util.List;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.chelvc.cloud.vehicle.api.constant.CategoryType;
+import com.chelvc.cloud.vehicle.api.dto.GoodsDTO;
+import com.chelvc.cloud.vehicle.api.param.GoodsQueryParam;
 import com.chelvc.cloud.vehicle.server.entity.Goods;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
@@ -16,10 +19,19 @@ import org.apache.ibatis.annotations.Param;
 @Mapper
 public interface GoodsMapper extends BaseMapper<Goods> {
     /**
-     * 获取商家商品分类ID
+     * 查询商家商品
      *
      * @param merchantId 商家ID
-     * @return 分类ID列表
+     * @param param      查询参数
+     * @return 商品列表
      */
-    List<Long> listMerchantGoodsCategoryIdentities(@Param("merchantId") Long merchantId);
+    List<GoodsDTO> listMerchantSimpleGoods(@Param("merchantId") Long merchantId, @Param("param") GoodsQueryParam param);
+
+    /**
+     * 获取商家商品分类类型
+     *
+     * @param merchantId 商家ID
+     * @return 分类类型列表
+     */
+    List<CategoryType> listMerchantGoodsCategoryTypes(@Param("merchantId") Long merchantId);
 }

+ 1 - 2
vehicle-server/src/main/java/com/chelvc/cloud/vehicle/server/dao/MerchantMapper.java

@@ -40,8 +40,7 @@ public interface MerchantMapper extends BaseMapper<Merchant> {
      * 查询商家
      *
      * @param param 查询参数
-     * @param size  商家数量
      * @return 商家信息列表
      */
-    List<MerchantDTO> listSimpleMerchants(@Param("param") MerchantQueryParam param, @Param("size") int size);
+    List<MerchantDTO> listSimpleMerchants(MerchantQueryParam param);
 }

+ 6 - 0
vehicle-server/src/main/java/com/chelvc/cloud/vehicle/server/entity/Goods.java

@@ -6,6 +6,7 @@ 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 com.chelvc.cloud.vehicle.api.constant.CategoryType;
 import com.chelvc.cloud.vehicle.api.constant.GoodsStatus;
 import com.chelvc.framework.base.model.File;
 import com.chelvc.framework.database.entity.ModifyEntity;
@@ -37,6 +38,11 @@ public class Goods extends ModifyEntity<Long> {
     @TableId(type = IdType.ASSIGN_ID)
     private Long id;
 
+    /**
+     * 分类类型
+     */
+    private CategoryType type;
+
     /**
      * 分类ID
      */

+ 15 - 3
vehicle-server/src/main/java/com/chelvc/cloud/vehicle/server/service/GoodsService.java

@@ -3,6 +3,9 @@ package com.chelvc.cloud.vehicle.server.service;
 import java.util.List;
 
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.chelvc.cloud.vehicle.api.constant.CategoryType;
+import com.chelvc.cloud.vehicle.api.dto.GoodsDTO;
+import com.chelvc.cloud.vehicle.api.param.GoodsQueryParam;
 import com.chelvc.cloud.vehicle.server.entity.Goods;
 
 /**
@@ -13,10 +16,19 @@ import com.chelvc.cloud.vehicle.server.entity.Goods;
  */
 public interface GoodsService extends IService<Goods> {
     /**
-     * 获取商家商品分类ID
+     * 获取商家商品分类类型
      *
      * @param merchantId 商家ID
-     * @return 分类ID列表
+     * @return 分类类型列表
      */
-    List<Long> listMerchantGoodsCategoryIdentities(Long merchantId);
+    List<CategoryType> listMerchantGoodsCategoryTypes(Long merchantId);
+
+    /**
+     * 查询商家商品
+     *
+     * @param merchantId 商家ID
+     * @param param      查询参数
+     * @return 商品列表
+     */
+    List<GoodsDTO> listMerchantSimpleGoods(Long merchantId, GoodsQueryParam param);
 }

+ 19 - 5
vehicle-server/src/main/java/com/chelvc/cloud/vehicle/server/service/impl/GoodsServiceImpl.java

@@ -3,11 +3,15 @@ package com.chelvc.cloud.vehicle.server.service.impl;
 import java.util.List;
 
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.chelvc.cloud.vehicle.api.constant.CategoryType;
+import com.chelvc.cloud.vehicle.api.dto.GoodsDTO;
+import com.chelvc.cloud.vehicle.api.param.GoodsQueryParam;
 import com.chelvc.cloud.vehicle.server.dao.GoodsMapper;
 import com.chelvc.cloud.vehicle.server.entity.Goods;
 import com.chelvc.cloud.vehicle.server.service.GoodsService;
 import lombok.NonNull;
-import org.springframework.stereotype.Service;
+import org.apache.dubbo.config.annotation.DubboService;
+import org.springframework.util.CollectionUtils;
 
 /**
  * 商品业务操作实现
@@ -15,10 +19,20 @@ import org.springframework.stereotype.Service;
  * @author Woody
  * @date 2023/7/17
  */
-@Service
-public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
+@DubboService(interfaceClass = com.chelvc.cloud.vehicle.api.service.GoodsService.class)
+public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService,
+        com.chelvc.cloud.vehicle.api.service.GoodsService {
     @Override
-    public List<Long> listMerchantGoodsCategoryIdentities(@NonNull Long merchantId) {
-        return this.baseMapper.listMerchantGoodsCategoryIdentities(merchantId);
+    public List<CategoryType> listMerchantGoodsCategoryTypes(@NonNull Long merchantId) {
+        List<CategoryType> types = this.baseMapper.listMerchantGoodsCategoryTypes(merchantId);
+        if (!CollectionUtils.isEmpty(types)) {
+            types.sort(CategoryType::compareTo);
+        }
+        return types;
+    }
+
+    @Override
+    public List<GoodsDTO> listMerchantSimpleGoods(@NonNull Long merchantId, @NonNull GoodsQueryParam param) {
+        return this.baseMapper.listMerchantSimpleGoods(merchantId, param);
     }
 }

+ 12 - 12
vehicle-server/src/main/java/com/chelvc/cloud/vehicle/server/service/impl/MerchantServiceImpl.java

@@ -3,20 +3,19 @@ package com.chelvc.cloud.vehicle.server.service.impl;
 import java.util.List;
 
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.chelvc.cloud.vehicle.api.dto.CategoryDTO;
+import com.chelvc.cloud.vehicle.api.constant.CategoryType;
+import com.chelvc.cloud.vehicle.api.dto.GoodsDTO;
 import com.chelvc.cloud.vehicle.api.dto.MerchantDTO;
 import com.chelvc.cloud.vehicle.api.dto.MerchantDetailDTO;
+import com.chelvc.cloud.vehicle.api.param.GoodsQueryParam;
 import com.chelvc.cloud.vehicle.api.param.LocationQueryParam;
 import com.chelvc.cloud.vehicle.api.param.MerchantQueryParam;
-import com.chelvc.cloud.vehicle.server.copier.CategoryCopier;
 import com.chelvc.cloud.vehicle.server.copier.MerchantCopier;
 import com.chelvc.cloud.vehicle.server.dao.MerchantMapper;
 import com.chelvc.cloud.vehicle.server.entity.Merchant;
-import com.chelvc.cloud.vehicle.server.service.CategoryService;
 import com.chelvc.cloud.vehicle.server.service.GoodsService;
 import com.chelvc.cloud.vehicle.server.service.MerchantService;
 import com.chelvc.framework.base.util.ErrorUtils;
-import com.chelvc.framework.base.util.ObjectUtils;
 import lombok.NonNull;
 import lombok.RequiredArgsConstructor;
 import org.apache.dubbo.config.annotation.DubboService;
@@ -33,7 +32,6 @@ import org.springframework.beans.factory.annotation.Autowired;
 public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements MerchantService,
         com.chelvc.cloud.vehicle.api.service.MerchantService {
     private final GoodsService goodsService;
-    private final CategoryService categoryService;
 
     @Override
     public List<MerchantDTO> listNearbyMerchants(@NonNull LocationQueryParam param, int size) {
@@ -46,8 +44,8 @@ public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> i
     }
 
     @Override
-    public List<MerchantDTO> listSimpleMerchants(@NonNull MerchantQueryParam param, int size) {
-        return this.baseMapper.listSimpleMerchants(param, size);
+    public List<MerchantDTO> listSimpleMerchants(@NonNull MerchantQueryParam param) {
+        return this.baseMapper.listSimpleMerchants(param);
     }
 
     @Override
@@ -56,11 +54,13 @@ public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> i
         MerchantDTO merchant = MerchantCopier.INSTANCE.copying(this.getById(id));
         ErrorUtils.requireResource(merchant, "商家信息不存在");
 
-        // 获取商家商品分类信息
-        List<Long> categoryIds = this.goodsService.listMerchantGoodsCategoryIdentities(id);
-        List<CategoryDTO> categories = CategoryCopier.INSTANCE.copying(
-                ObjectUtils.ifEmpty(categoryIds, this.categoryService::listByIds)
+        // 获取商家全部商品列表
+        List<GoodsDTO> goods = this.goodsService.listMerchantSimpleGoods(
+                id, GoodsQueryParam.builder().size(10).build()
         );
-        return MerchantDetailDTO.builder().merchant(merchant).categories(categories).build();
+
+        // 获取商家商品分类类型列表
+        List<CategoryType> types = this.goodsService.listMerchantGoodsCategoryTypes(id);
+        return MerchantDetailDTO.builder().merchant(merchant).goods(goods).categories(types).build();
     }
 }

+ 17 - 2
vehicle-server/src/main/resources/mapper/GoodsMapper.xml

@@ -1,8 +1,23 @@
 <?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.chelvc.cloud.vehicle.server.dao.GoodsMapper">
-    <select id="listMerchantGoodsCategoryIdentities" resultType="long">
-        select distinct category_id
+    <select id="listMerchantSimpleGoods" resultType="com.chelvc.cloud.vehicle.api.dto.GoodsDTO">
+        select id, name, logo, original_price, special_price, score, sale
+        from `goods`
+        <where>
+            merchant_id = #{merchantId}
+            <if test="param.offset != null">
+                and id > #{param.offset}
+            </if>
+            <if test="param.type != null">
+                and type = #{param.type}
+            </if>
+        </where>
+        limit #{param.size}
+    </select>
+
+    <select id="listMerchantGoodsCategoryTypes" resultType="com.chelvc.cloud.vehicle.api.constant.CategoryType">
+        select distinct type
         from `goods`
         where merchant_id = #{merchantId} and status = 'ONLINE'
     </select>

+ 15 - 15
vehicle-server/src/main/resources/mapper/MerchantMapper.xml

@@ -31,38 +31,38 @@
     <select id="listSimpleMerchants" resultType="com.chelvc.cloud.vehicle.api.dto.MerchantDTO">
         select mt.id, mt.name, mt.logo, mt.score, mt.sale, mt.address,
         round(st_distance_sphere(
-            point(mt.longitude, mt.latitude), point(#{param.longitude}, #{param.latitude})
+            point(mt.longitude, mt.latitude), point(#{longitude}, #{latitude})
         )) distance
         from `merchant` mt
-        <if test="param.categoryId != null or (param.keyword != null and !param.keyword.isEmpty())">
+        <if test="categoryId != null or (keyword != null and !keyword.isEmpty())">
             left join `goods` gs on gs.merchant_id = mt.id
-            <if test="param.categoryId != null">
-                and gs.category_id = #{param.categoryId}
+            <if test="categoryId != null">
+                and gs.category_id = #{categoryId}
             </if>
-            <if test="param.keyword != null and !param.keyword.isEmpty()">
-                and (gs.name like concat('%', #{param.keyword}, '%') or gs.description like concat('%', #{param.keyword}, '%'))
+            <if test="keyword != null and !keyword.isEmpty()">
+                and (gs.name like concat('%', #{keyword}, '%') or gs.description like concat('%', #{keyword}, '%'))
             </if>
             and gs.status = 'ONLINE'
         </if>
         <where>
-            <if test="param.offset != null">
-                mt.id > #{param.offset}
+            <if test="offset != null">
+                mt.id > #{offset}
             </if>
-            mt.region = #{param.region} and mt.status = 'ONLINE'
-            <if test="param.keyword != null and !param.keyword.isEmpty()">
-                and mt.name like concat('%', #{param.keyword}, '%')
+            mt.region = #{region} and mt.status = 'ONLINE'
+            <if test="keyword != null and !keyword.isEmpty()">
+                and mt.name like concat('%', #{keyword}, '%')
             </if>
         </where>
-        <if test="param.sort != null and param.sort.name == 'SYNTHESE'">
+        <if test="sort != null and sort.name == 'SYNTHESE'">
             order by mt.recommend desc
         </if>
-        <if test="param.sort != null and param.sort.name == 'NEAREST'">
+        <if test="sort != null and sort.name == 'NEAREST'">
             order by distance desc
         </if>
-        <if test="param.sort != null and param.sort.name == 'FAVORITE'">
+        <if test="sort != null and sort.name == 'FAVORITE'">
             order by mt.score desc
         </if>
-        <if test="param.sort != null and param.sort.name == 'BESTSELLING'">
+        <if test="sort != null and sort.name == 'BESTSELLING'">
             order by mt.sale desc
         </if>
         limit #{size}