Browse Source

开发查询商户的综合评分接口

jinshihui 9 hours ago
parent
commit
d9a376f553

+ 22 - 2
nightFragrance-massage/src/main/java/com/ylx/order/controller/OrderCommentController.java

@@ -3,14 +3,17 @@ package com.ylx.order.controller;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.ylx.common.core.domain.R;
 import com.ylx.order.domain.dto.MerchantMyCommentQueryDTO;
+import com.ylx.order.domain.vo.MerchantCompositeScoreVO;
 import com.ylx.order.domain.vo.MerchantMyCommentVO;
 import com.ylx.order.service.TCommentUserService;
 import com.ylx.order.domain.dto.OrderCommentDTO;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -25,6 +28,7 @@ import org.springframework.web.bind.annotation.RestController;
  */
 
 @Api(tags = "订单评论")
+@Slf4j
 @RestController
 @RequestMapping("/order/comment")
 @RequiredArgsConstructor
@@ -40,12 +44,12 @@ public class OrderCommentController {
     }
 
     /**
-     * 商户端我的评分分页查询接口
+     * 商户端我的评分分页查询
      *
      * @param dto
      * @return R<Page<MerchantMyCommentVO>>
      */
-    @ApiOperation("商户端我的评分分页查询接口")
+    @ApiOperation("商户端我的评分分页查询")
     @PostMapping("/merchant/my/page")
     public R<Page<MerchantMyCommentVO>> getMerchantMyCommentPage(@Validated @RequestBody MerchantMyCommentQueryDTO dto) {
         try {
@@ -56,4 +60,20 @@ public class OrderCommentController {
         }
     }
 
+    /**
+     * 查询商户的综合评分
+     *
+     * @return R<MerchantCompositeScoreVO>
+     */
+    @ApiOperation("查询商户的综合评分")
+    @GetMapping("/merchant/my/score")
+    public R<MerchantCompositeScoreVO> getMerchantCompositeScore() {
+        try {
+            return R.ok(tCommentUserService.getMerchantCompositeScore());
+        } catch (Exception e) {
+            log.error("查询商户综合评分失败", e);
+            return R.fail("查询商户综合评分失败");
+        }
+    }
+
 }

+ 7 - 0
nightFragrance-massage/src/main/java/com/ylx/order/domain/TCommentUser.java

@@ -10,6 +10,7 @@ import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 
 import java.io.Serializable;
+import java.math.BigDecimal;
 import java.util.Date;
 
 /**
@@ -123,6 +124,12 @@ public class TCommentUser {
     @ApiModelProperty("仪表仪容等级1-5")
     private Integer groomingComment;
 
+    /**
+     * 订单的综合评分
+     */
+    @ApiModelProperty("订单的综合评分")
+    private BigDecimal score;
+
     /**
      * 审核状态:0待审核 1通过 2拒绝
      */

+ 42 - 0
nightFragrance-massage/src/main/java/com/ylx/order/domain/vo/MerchantCompositeScoreVO.java

@@ -0,0 +1,42 @@
+package com.ylx.order.domain.vo;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+
+/**
+ * 商户综合评分。
+ */
+@Data
+@ApiModel("商户综合评分VO")
+public class MerchantCompositeScoreVO implements Serializable {
+
+    private static final long serialVersionUID = 3996309269130487383L;
+
+    /**
+     * 商户ID。
+     */
+    @ApiModelProperty("商户ID")
+    private Integer merchantId;
+
+    /**
+     * 商户所有订单评分总和。
+     */
+    @ApiModelProperty("商户所有订单评分总和")
+    private BigDecimal scoreSum;
+
+    /**
+     * 商户订单量。
+     */
+    @ApiModelProperty("商户订单量")
+    private Long orderCount;
+
+    /**
+     * 综合评分,评分总和 / 订单量。
+     */
+    @ApiModelProperty("综合评分")
+    private BigDecimal comprehensiveScore;
+}

+ 10 - 1
nightFragrance-massage/src/main/java/com/ylx/order/mapper/TCommentUserMapper.java

@@ -6,6 +6,7 @@ import com.ylx.merchant.domain.dto.MerchantCommentDTO;
 import com.ylx.merchant.domain.vo.MerchantCommentVO;
 import org.apache.ibatis.annotations.Param;
 import com.ylx.order.domain.TCommentUser;
+import com.ylx.order.domain.vo.MerchantCompositeScoreVO;
 import com.ylx.order.domain.vo.MerchantMyCommentVO;
 import com.ylx.order.domain.vo.TCommentUserAuditStatusCountVO;
 
@@ -59,7 +60,15 @@ public interface TCommentUserMapper extends BaseMapper<TCommentUser> {
      *
      * @param page       分页参数
      * @param merchantId 当前登录商户ID
-     * @return 我的评分分页列表
+     * @return Page<MerchantMyCommentVO> 我的评分分页列表
      */
     Page<MerchantMyCommentVO> selectMerchantMyCommentPage(Page<MerchantMyCommentVO> page, @Param("merchantId") Integer merchantId);
+
+    /**
+     * 查询商户评分总和和订单量。
+     *
+     * @param merchantId 商户ID
+     * @return MerchantCompositeScoreVO 商户评分统计
+     */
+    MerchantCompositeScoreVO selectMerchantScoreStatistics(@Param("merchantId") Integer merchantId);
 }

+ 8 - 0
nightFragrance-massage/src/main/java/com/ylx/order/service/TCommentUserService.java

@@ -8,6 +8,7 @@ import com.ylx.merchant.domain.vo.MerchantCommentVO;
 import com.ylx.order.domain.TCommentUser;
 import com.ylx.order.domain.dto.MerchantMyCommentQueryDTO;
 import com.ylx.order.domain.dto.OrderCommentDTO;
+import com.ylx.order.domain.vo.MerchantCompositeScoreVO;
 import com.ylx.order.domain.vo.MerchantMyCommentVO;
 import com.ylx.order.domain.vo.TCommentUserAuditStatusCountVO;
 
@@ -59,4 +60,11 @@ public interface TCommentUserService extends IService<TCommentUser> {
      * @return 我的评分分页列表
      */
     Page<MerchantMyCommentVO> getMerchantMyCommentPage(MerchantMyCommentQueryDTO dto);
+
+    /**
+     * 查询当前登录商户综合评分。
+     *
+     * @return 商户综合评分
+     */
+    MerchantCompositeScoreVO getMerchantCompositeScore();
 }

+ 49 - 8
nightFragrance-massage/src/main/java/com/ylx/order/service/impl/TCommentUserServiceImpl.java

@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -23,6 +24,7 @@ import com.ylx.order.domain.dto.MerchantMyCommentQueryDTO;
 import com.ylx.order.mapper.TCommentUserMapper;
 import com.ylx.order.service.TCommentUserService;
 import com.ylx.order.domain.dto.OrderCommentDTO;
+import com.ylx.order.domain.vo.MerchantCompositeScoreVO;
 import com.ylx.order.domain.vo.MerchantMyCommentVO;
 import com.ylx.order.domain.vo.TCommentUserAuditStatusCountVO;
 import com.ylx.order.enums.AuditStatusEnum;
@@ -32,6 +34,8 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.Collections;
 import java.util.Date;
 import java.util.List;
@@ -94,6 +98,7 @@ public class TCommentUserServiceImpl extends ServiceImpl<TCommentUserMapper, TCo
         TCommentUser comment = new TCommentUser();
         String userId = wxLoginUser.getId();
         comment.setUserId(userId);
+        comment.setNickName(wxLoginUser.getNickName());
         comment.setOrderId(dto.getOrderId());
         comment.setMerchantId(Math.toIntExact(dto.getMerchantId()));
         comment.setMerchantName(dto.getMerchantName());
@@ -176,14 +181,13 @@ public class TCommentUserServiceImpl extends ServiceImpl<TCommentUserMapper, TCo
     @Override
     public Page<MerchantMyCommentVO> getMerchantMyCommentPage(MerchantMyCommentQueryDTO dto) {
         Integer merchantId = getCurrentMerchantId();
-        MerchantMyCommentQueryDTO query = dto == null ? new MerchantMyCommentQueryDTO() : dto;
-        long current = query.getCurrent() <= 0 ? 1 : query.getCurrent();
-        long size = query.getSize() <= 0 ? 10 : query.getSize();
+        long current = dto.getCurrent();
+        long size = dto.getSize();
         Page<MerchantMyCommentVO> page = new Page<>(current, size);
 
         Page<MerchantMyCommentVO> result = tCommentUserMapper.selectMerchantMyCommentPage(page, merchantId);
         if (ObjectUtil.isNull(result) || CollUtil.isEmpty(result.getRecords())) {
-            return result == null ? page : result;
+            return page;
         }
 
         List<MerchantMyCommentVO> records = result.getRecords();
@@ -191,10 +195,6 @@ public class TCommentUserServiceImpl extends ServiceImpl<TCommentUserMapper, TCo
                 .map(MerchantMyCommentVO::getId)
                 .filter(StrUtil::isNotBlank)
                 .collect(Collectors.toList());
-        if (CollUtil.isEmpty(commentIds)) {
-            records.forEach(record -> record.setPictures(Collections.emptyList()));
-            return result;
-        }
 
         List<TCommentPicture> pictures = tCommentPictureService.selectByCommentIds(commentIds);
         Map<String, List<String>> pictureMap = CollUtil.isEmpty(pictures)
@@ -208,7 +208,48 @@ public class TCommentUserServiceImpl extends ServiceImpl<TCommentUserMapper, TCo
 
         for (MerchantMyCommentVO record : records) {
             record.setPictures(pictureMap.getOrDefault(record.getId(), Collections.emptyList()));
+            //根据评价ID更新综合评分
+            LambdaUpdateWrapper<TCommentUser> updateWrapper = new LambdaUpdateWrapper<>();
+            updateWrapper.set(TCommentUser::getScore, record.getScore());
+            updateWrapper.eq(TCommentUser::getId, record.getId());
+            tCommentUserMapper.update(null, updateWrapper);
+        }
+        return result;
+    }
+
+    @Override
+    public MerchantCompositeScoreVO getMerchantCompositeScore() {
+        return getMerchantCompositeScoreByMerchantId(getCurrentMerchantId());
+    }
+
+    /**
+     * 查询商户的综合评分
+     *
+     * @param merchantId
+     * @return MerchantCompositeScoreVO
+     */
+    MerchantCompositeScoreVO getMerchantCompositeScoreByMerchantId(Integer merchantId) {
+        if (ObjectUtil.isNull(merchantId) || merchantId <= 0) {
+            throw new ServiceException("商户ID不能为空");
+        }
+
+        MerchantCompositeScoreVO statistic = tCommentUserMapper.selectMerchantScoreStatistics(merchantId);
+        BigDecimal scoreSum = ObjectUtil.isNull(statistic) || ObjectUtil.isNull(statistic.getScoreSum())
+                ? BigDecimal.ZERO
+                : statistic.getScoreSum();
+        Long orderCount = ObjectUtil.isNull(statistic) || ObjectUtil.isNull(statistic.getOrderCount())
+                ? 0L
+                : statistic.getOrderCount();
+
+        MerchantCompositeScoreVO result = new MerchantCompositeScoreVO();
+        result.setMerchantId(merchantId);
+        result.setScoreSum(scoreSum);
+        result.setOrderCount(orderCount);
+        if (orderCount <= 0) {
+            result.setComprehensiveScore(BigDecimal.ZERO.setScale(1));
+            return result;
         }
+        result.setComprehensiveScore(scoreSum.divide(BigDecimal.valueOf(orderCount), 1, RoundingMode.HALF_UP));
         return result;
     }
 

+ 31 - 0
nightFragrance-massage/src/main/resources/mapper/order/TCommentUserMapper.xml

@@ -150,4 +150,35 @@
             c.comment_time DESC
     </select>
 
+    <select id="selectMerchantScoreStatistics" resultType="com.ylx.order.domain.vo.MerchantCompositeScoreVO">
+        SELECT
+            COALESCE(comment_stat.score_sum, 0) AS scoreSum,
+            COALESCE(order_stat.order_count, 0) AS orderCount
+        FROM
+            (
+                SELECT
+                    COALESCE(SUM(
+                        CASE
+                            WHEN c.score IS NOT NULL THEN c.score
+                            ELSE 0
+                        END
+                    ), 0) AS score_sum
+                FROM
+                    t_comment_user c
+                WHERE
+                    c.merchant_id = #{merchantId}
+                AND c.is_delete = 0
+            ) comment_stat
+        CROSS JOIN
+            (
+                SELECT
+                    COUNT(1) AS order_count
+                FROM
+                    t_order o
+                WHERE
+                    o.merchant_id = #{merchantId}
+                AND o.is_delete = 0
+            ) order_stat
+    </select>
+
 </mapper>

+ 38 - 0
nightFragrance-massage/src/test/java/com/ylx/order/mapper/TCommentUserMapperXmlTest.java

@@ -0,0 +1,38 @@
+package com.ylx.order.mapper;
+
+import org.junit.jupiter.api.Test;
+
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class TCommentUserMapperXmlTest {
+
+    @Test
+    public void merchantScoreStatisticsSqlReadsCommentScoreAndOrderCount() throws Exception {
+        String xml = readMapperXml();
+
+        assertTrue(xml.contains("selectMerchantScoreStatistics"));
+        assertTrue(xml.contains("FROM"));
+        assertTrue(xml.contains("t_comment_user c"));
+        assertTrue(xml.contains("c.merchant_id = #{merchantId}"));
+        assertTrue(xml.contains("c.is_delete = 0"));
+        assertTrue(xml.contains("t_order o"));
+        assertTrue(xml.contains("o.merchant_id = #{merchantId}"));
+        assertTrue(xml.contains("o.is_delete = 0"));
+        assertTrue(xml.contains("COALESCE(comment_stat.score_sum, 0) AS scoreSum"));
+        assertTrue(xml.contains("COALESCE(order_stat.order_count, 0) AS orderCount"));
+    }
+
+    private String readMapperXml() throws Exception {
+        try (InputStream inputStream = getClass().getClassLoader()
+                .getResourceAsStream("mapper/order/TCommentUserMapper.xml")) {
+            assertNotNull(inputStream, "TCommentUserMapper.xml should exist in test classpath");
+            byte[] bytes = new byte[inputStream.available()];
+            inputStream.read(bytes);
+            return new String(bytes, StandardCharsets.UTF_8);
+        }
+    }
+}

+ 52 - 0
nightFragrance-massage/src/test/java/com/ylx/order/service/impl/TCommentUserServiceImplTest.java

@@ -0,0 +1,52 @@
+package com.ylx.order.service.impl;
+
+import com.ylx.order.domain.vo.MerchantCompositeScoreVO;
+import com.ylx.order.mapper.TCommentUserMapper;
+import org.junit.jupiter.api.Test;
+import org.springframework.test.util.ReflectionTestUtils;
+
+import java.math.BigDecimal;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class TCommentUserServiceImplTest {
+
+    @Test
+    public void getMerchantCompositeScoreDividesCommentScoreSumByOrderCount() {
+        TCommentUserMapper mapper = mock(TCommentUserMapper.class);
+        MerchantCompositeScoreVO statistic = new MerchantCompositeScoreVO();
+        statistic.setScoreSum(new BigDecimal("18.6"));
+        statistic.setOrderCount(4L);
+        when(mapper.selectMerchantScoreStatistics(10)).thenReturn(statistic);
+
+        TCommentUserServiceImpl service = new TCommentUserServiceImpl();
+        ReflectionTestUtils.setField(service, "tCommentUserMapper", mapper);
+
+        MerchantCompositeScoreVO result = service.getMerchantCompositeScoreByMerchantId(10);
+
+        assertEquals(10, result.getMerchantId());
+        assertEquals(new BigDecimal("18.6"), result.getScoreSum());
+        assertEquals(4L, result.getOrderCount());
+        assertEquals(new BigDecimal("4.7"), result.getComprehensiveScore());
+    }
+
+    @Test
+    public void getMerchantCompositeScoreReturnsZeroWhenOrderCountIsZero() {
+        TCommentUserMapper mapper = mock(TCommentUserMapper.class);
+        MerchantCompositeScoreVO statistic = new MerchantCompositeScoreVO();
+        statistic.setScoreSum(new BigDecimal("12.0"));
+        statistic.setOrderCount(0L);
+        when(mapper.selectMerchantScoreStatistics(10)).thenReturn(statistic);
+
+        TCommentUserServiceImpl service = new TCommentUserServiceImpl();
+        ReflectionTestUtils.setField(service, "tCommentUserMapper", mapper);
+
+        MerchantCompositeScoreVO result = service.getMerchantCompositeScoreByMerchantId(10);
+
+        assertEquals(new BigDecimal("12.0"), result.getScoreSum());
+        assertEquals(0L, result.getOrderCount());
+        assertEquals(new BigDecimal("0.0"), result.getComprehensiveScore());
+    }
+}