Преглед на файлове

消息列表统计sql优化

yzc преди 2 години
родител
ревизия
63a52d3634

+ 10 - 0
sckw-modules/sckw-message/src/main/java/com/sckw/message/dao/KwmMessageMapper.java

@@ -1,6 +1,7 @@
 package com.sckw.message.dao;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.sckw.core.model.vo.TableTop;
 import com.sckw.message.model.KwmMessage;
 import com.sckw.message.model.dto.MessageListDTO;
 import com.sckw.message.model.dto.SelectMessagesDTO;
@@ -24,4 +25,13 @@ public interface KwmMessageMapper extends BaseMapper<KwmMessage> {
      * @return java.util.List<com.sckw.message.model.dto.MessageListDTO>
      */
     List<MessageListDTO> findPage(@Param("item") SelectMessagesDTO item);
+
+    /**
+     * @desc: 统计查询
+     * @author: yzc
+     * @date: 2023-09-06 9:21
+     * @Param item:
+     * @return: java.util.List<com.sckw.core.model.vo.TableTop>
+     */
+    List<TableTop> statistics(@Param("item") SelectMessagesDTO item);
 }

+ 3 - 0
sckw-modules/sckw-message/src/main/java/com/sckw/message/model/vo/req/StatisticsMessagesReqVO.java

@@ -1,5 +1,6 @@
 package com.sckw.message.model.vo.req;
 
+import com.fasterxml.jackson.annotation.JsonFormat;
 import lombok.Getter;
 import lombok.Setter;
 import lombok.ToString;
@@ -24,11 +25,13 @@ public class StatisticsMessagesReqVO {
     /**
      * 创建时间开始(yyyy-MM-dd HH:mm:ss)
      */
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
     private Date startCreateTime;
 
     /**
      * 创建时间结束(yyyy-MM-dd HH:mm:ss)
      */
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
     private Date endCreateTime;
 
 

+ 12 - 0
sckw-modules/sckw-message/src/main/java/com/sckw/message/service/KwmMessageService.java

@@ -3,6 +3,7 @@ package com.sckw.message.service;
 import cn.hutool.core.collection.CollectionUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.sckw.core.model.vo.TableTop;
 import com.sckw.core.utils.CollectionUtils;
 import com.sckw.core.utils.StringUtils;
 import com.sckw.message.dao.KwmMessageMapper;
@@ -101,6 +102,17 @@ public class KwmMessageService {
         return kwmMessageMapper.findPage(dto);
     }
 
+    /**
+     * @desc: 统计查询
+     * @author: yzc
+     * @date: 2023-09-06 9:21
+     * @Param dto:
+     * @return: java.util.List<com.sckw.core.model.vo.TableTop>
+     */
+    public List<TableTop> statistics(SelectMessagesDTO dto) {
+        return kwmMessageMapper.statistics(dto);
+    }
+
     /**
      * @param id
      * @return com.sckw.message.model.KwmMessage

+ 20 - 10
sckw-modules/sckw-message/src/main/java/com/sckw/message/service/MessageService.java

@@ -215,22 +215,32 @@ public class MessageService {
      */
     public TableStatisticRes statistics(StatisticsMessagesReqVO params) {
         TableStatisticRes res = new TableStatisticRes();
-        List<KwmMessageListResVO> messages = this.select(BeanUtils.copyProperties(params, SelectMessagesReqVO.class));
-        Map<Integer, List<KwmMessageListResVO>> map = messages.stream().collect(Collectors.groupingBy(KwmMessageListResVO::getStatus));
+        SelectMessagesDTO dto = BeanUtils.copyProperties(params, SelectMessagesDTO.class);
+        //当前登录人是企业管理员则查看全部消息,否则只查看对应用户消息
+        if (Objects.equals(LoginUserHolder.getIsMain(), 1)) {
+            dto.setEntId(LoginUserHolder.getEntId());
+        } else {
+            dto.setUserId(LoginUserHolder.getUserId());
+        }
+        List<TableTop> statistics = kwmMessageService.statistics(dto);
+        Map<Integer, Integer> map = statistics.stream().
+                collect(Collectors.toMap(TableTop::getValue, TableTop::getTotal, (k1, k2) -> k1));
         List<TableTop> tableTops = new ArrayList<>();
-        TableTop all = new TableTop();
-        all.setName("全部").setTotal(CollectionUtils.isEmpty(messages) ? 0 : messages.size());
-        tableTops.add(all);
+        int totalCount = 0;
         List<MsgStatusEnum> enums = MsgStatusEnum.getSortList();
-        enums.forEach(e -> {
-            List<KwmMessageListResVO> list = map.get(e.getCode());
-            int total = CollectionUtils.isEmpty(list) ? 0 : list.size();
+        for (MsgStatusEnum e : enums) {
+            Integer count = map.get(e.getCode());
+            int total = Objects.isNull(count) ? 0 : count;
             TableTop tableTop = new TableTop();
             tableTop.setName(e.getMsg()).setValue(e.getCode()).setTotal(total);
             tableTops.add(tableTop);
-        });
+            totalCount = totalCount + total;
+        }
+        TableTop all = new TableTop();
+        all.setName("全部").setTotal(totalCount);
+        tableTops.add(0, all);
         TableBottom tableBottom = new TableBottom();
-        tableBottom.setTotal(CollectionUtils.isEmpty(messages) ? 0 : messages.size());
+        tableBottom.setTotal(totalCount);
         res.setTableTops(tableTops).setTableBottom(tableBottom);
         return res;
     }

+ 53 - 26
sckw-modules/sckw-message/src/main/resources/mapper/KwmMessageMapper.xml

@@ -3,35 +3,35 @@
 <mapper namespace="com.sckw.message.dao.KwmMessageMapper">
 
   <select id="findPage" resultType="com.sckw.message.model.dto.MessageListDTO" >
-    select
+      select
       mu.id, mu.ent_id entId, mu.user_id userId, mu.status, mu.remark, mu.create_by createBy, mu.create_time createTime,
       mu.update_by updateBy, mu.update_time updateTime, m.id as msgId, m.category, m.type, m.title, m.content, m.url,
       m.params, m.client_type clientType, m.remark as msgRemark
-    from kwm_message_user mu
-    left join kwm_message m
-    on mu.msg_id = m.id
-    <where>
-        m.del_flag = 0 and mu.del_flag = 0
-        <if test="item.entId != null">
-            and mu.ent_id = #{item.entId}
-        </if>
-        <if test="item.userId != null">
-            and mu.user_id = #{item.userId}
-        </if>
-        <if test="item.status != null">
-            and mu.status = #{item.status}
-        </if>
-        <if test="item.category != null and item.category != ''">
-          and m.category = #{item.category}
-        </if>
-        <if test="item.startCreateTime != null">
-            and mu.create_time &gt;= #{item.startCreateTime,jdbcType=TIMESTAMP}
-        </if>
-        <if test="item.endCreateTime != null">
-            and mu.create_time &lt;= #{item.endCreateTime,jdbcType=TIMESTAMP}
-        </if>
-    </where>
-    ORDER BY
+      from kwm_message_user mu
+      left join kwm_message m
+      on mu.msg_id = m.id
+      <where>
+          m.del_flag = 0 and mu.del_flag = 0
+          <if test="item.entId != null">
+              and mu.ent_id = #{item.entId}
+          </if>
+          <if test="item.userId != null">
+              and mu.user_id = #{item.userId}
+          </if>
+          <if test="item.status != null">
+              and mu.status = #{item.status}
+          </if>
+          <if test="item.category != null and item.category != ''">
+              and m.category = #{item.category}
+          </if>
+          <if test="item.startCreateTime != null">
+              and mu.create_time &gt;= #{item.startCreateTime,jdbcType=TIMESTAMP}
+          </if>
+          <if test="item.endCreateTime != null">
+              and mu.create_time &lt;= #{item.endCreateTime,jdbcType=TIMESTAMP}
+          </if>
+      </where>
+      ORDER BY
       <choose>
           <when test="item.sortType != null and item.sortType == 1">
               mu.status ASC, mu.create_time DESC
@@ -41,4 +41,31 @@
           </otherwise>
       </choose>
   </select>
+    <select id="statistics" resultType="com.sckw.core.model.vo.TableTop">
+        select
+        mu.status as value,count(*) as total
+        from kwm_message_user mu
+        left join kwm_message m
+        on mu.msg_id = m.id
+        <where>
+            m.del_flag = 0 and mu.del_flag = 0
+            <if test="item.entId != null">
+                and mu.ent_id = #{item.entId}
+            </if>
+            <if test="item.userId != null">
+                and mu.user_id = #{item.userId}
+            </if>
+            <if test="item.category != null and item.category != ''">
+                and m.category = #{item.category}
+            </if>
+            <if test="item.startCreateTime != null">
+                and mu.create_time &gt;= #{item.startCreateTime,jdbcType=TIMESTAMP}
+            </if>
+            <if test="item.endCreateTime != null">
+                and mu.create_time &lt;= #{item.endCreateTime,jdbcType=TIMESTAMP}
+            </if>
+        </where>
+        group by mu.status
+
+    </select>
 </mapper>