Quellcode durchsuchen

修改统计接口,按照个人,公司,部门的维度去展示

Guos vor 1 Woche
Ursprung
Commit
a5aceb993a

+ 6 - 2
fs-admin/src/main/java/com/fs/api/controller/StatisticManageController.java

@@ -3,8 +3,11 @@ package com.fs.api.controller;
 
 import com.fs.common.core.domain.R;
 import com.fs.company.service.IStatisticManageService;
+import com.fs.statis.param.ComprehensiveStatisticsParam;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.util.Assert;
 import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
@@ -24,7 +27,8 @@ public class StatisticManageController {
     private IStatisticManageService statisticManageService;
 
     @PostMapping("/statisticMain")
-    public R statisticMain() {
-        return R.ok().put("data", statisticManageService.statisticMain());
+    public R statisticMain(@RequestBody ComprehensiveStatisticsParam param) {
+        Assert.notNull(param.getDimension(), "请选择统计维度");
+        return R.ok().put("data", statisticManageService.statisticMain(param));
     }
 }

+ 52 - 0
fs-common/src/main/java/com/fs/common/enums/DimensionEnum.java

@@ -0,0 +1,52 @@
+package com.fs.common.enums;
+
+/**
+ * @description: 统计维度枚举
+ * @author: Guos
+ * @time: 2025/11/3 上午11:26
+ */
+public enum DimensionEnum {
+
+    /**
+     * 个人维度
+     */
+    PERSONAL(1, "个人"),
+
+    /**
+     * 公司维度
+     */
+    COMPANY(2, "公司"),
+
+    /**
+     * 部门维度
+     */
+    DEPARTMENT(3, "部门");
+
+    private final Integer value;
+    private final String description;
+
+    DimensionEnum(Integer value, String description) {
+        this.value = value;
+        this.description = description;
+    }
+
+    public Integer getValue() {
+        return value;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    /**
+     * 根据值获取枚举
+     */
+    public static DimensionEnum fromValue(Integer value) {
+        for (DimensionEnum dimension : DimensionEnum.values()) {
+            if (dimension.getValue().equals(value)) {
+                return dimension;
+            }
+        }
+        return null;
+    }
+}

+ 1 - 1
fs-service/src/main/java/com/fs/company/domain/CompanyDeptUserInfo.java

@@ -13,7 +13,7 @@ public class CompanyDeptUserInfo {
    /**
     * 公司id
     */
-   private Integer companyId;
+   private Long companyId;
 
    /**
     * 公司名称

+ 1 - 1
fs-service/src/main/java/com/fs/company/dto/CompanyDeptUserInfoDTO.java

@@ -13,7 +13,7 @@ public class CompanyDeptUserInfoDTO {
     /**
      * 公司id
      */
-    private Integer companyId;
+    private Long companyId;
 
     /**
      * 公司名称

+ 13 - 1
fs-service/src/main/java/com/fs/company/mapper/StatisticManageMapper.java

@@ -4,6 +4,7 @@ import com.fs.company.domain.CompanyDeptUserInfo;
 import com.fs.company.dto.CompanyDeptUserInfoDTO;
 import org.apache.ibatis.annotations.Param;
 
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -15,8 +16,19 @@ public interface StatisticManageMapper {
 
 
     //获取公司、部门、员工信息
-    List<CompanyDeptUserInfo> getCompanyAndDeptAndDeptUserList();
+    List<CompanyDeptUserInfo> getCompanyAndDeptAndDeptUserList(@Param("companyId") Long companyId);
+
+    List<CompanyDeptUserInfo> getCompanyInfo();
 
 
     CompanyDeptUserInfoDTO getStatisticNum(@Param("userIds") Long[] userIds);
+
+    /**
+     * 按照个人统计
+     * @param dimension 维度
+     * @param startTime
+     * @param endTime
+     * @param userIds
+     */
+    void getStatisticNumByPersonal(@Param("dimension")Integer dimension,@Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("userIds") Long... userIds);
 }

+ 7 - 1
fs-service/src/main/java/com/fs/company/service/IStatisticManageService.java

@@ -1,6 +1,6 @@
 package com.fs.company.service;
 
-import com.fs.company.dto.CompanyDeptUserInfoDTO;
+import com.fs.statis.param.ComprehensiveStatisticsParam;
 
 import java.util.List;
 import java.util.Map;
@@ -14,4 +14,10 @@ public interface IStatisticManageService {
 
     List<Map<String, Object>> statisticMain();
 
+    List<Map<String, Object>> statisticMain(ComprehensiveStatisticsParam param);
+
+
+
+
+
 }

+ 60 - 3
fs-service/src/main/java/com/fs/company/service/impl/StatisticManageServiceImpl.java

@@ -1,17 +1,22 @@
 package com.fs.company.service.impl;
 
+import cn.hutool.core.util.ObjectUtil;
+import com.fs.common.enums.DimensionEnum;
 import com.fs.company.domain.CompanyDeptUserInfo;
 import com.fs.company.dto.CompanyDeptUserInfoDTO;
 import com.fs.company.mapper.StatisticManageMapper;
 import com.fs.company.service.IStatisticManageService;
+import com.fs.statis.param.ComprehensiveStatisticsParam;
 import com.google.common.collect.Maps;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
 import com.google.common.collect.Lists;
 import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
+import org.springframework.util.Assert;
 
 import javax.annotation.Resource;
+import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
@@ -28,15 +33,67 @@ public class StatisticManageServiceImpl implements IStatisticManageService {
     @Resource
     private StatisticManageMapper statisticManageMapper;
 
-
     /**
      * 统计
      * 按照部门分组情况
      * @return
      */
     @Override
+    public List<Map<String, Object>> statisticMain(ComprehensiveStatisticsParam param) {
+        if (param.getDimension() == DimensionEnum.PERSONAL.getValue()){
+            Assert.notNull(param.getId(), "按个人展示查询条件不能为空!");
+            getStatisticNumByPersonal( DimensionEnum.PERSONAL.getValue(), param.getStartTime(), param.getEndTime(), param.getId());
+        }
+        if(param.getDimension() == DimensionEnum.COMPANY.getValue()){
+            //按照公司统计,如果id为空就要展示全部公司,如果不为空就展示id查询的公司
+            if(ObjectUtil.isEmpty(param.getId())){
+                //得到所有公司信息
+                List<CompanyDeptUserInfo> companyInfo = statisticManageMapper.getCompanyInfo();
+                for (CompanyDeptUserInfo companyDeptUserInfo : companyInfo){
+                    getStatisticNumByPersonal(DimensionEnum.COMPANY.getValue(), param.getStartTime(), param.getEndTime(),
+                            companyDeptUserInfo.getCompanyId());
+                }
+            }
+            //else{
+              //  getStatisticNumByPersonal(DimensionEnum.COMPANY.getValue(), param.getStartTime(), param.getEndTime(),
+                //        param.getId());
+            //}
+        }
+        if(param.getDimension() == DimensionEnum.DEPARTMENT.getValue()){
+            Assert.notNull(param.getId(), "按部门展示公司不能为空!");
+            List<CompanyDeptUserInfo> companyDeptdUserList = statisticManageMapper.getCompanyAndDeptAndDeptUserList(param.getId());
+            //按照部门分组
+            Map<Long, List<CompanyDeptUserInfo>> deptInfos = companyDeptdUserList.stream()
+                    .collect(Collectors.groupingBy(CompanyDeptUserInfo::getDeptId));
+            deptInfos.forEach((deptId, companyDeptUserInfos) -> {
+                Long[] userIds = companyDeptUserInfos.stream().map(CompanyDeptUserInfo::getUserId).toArray(Long[]::new);
+                getStatisticNumByPersonal(DimensionEnum.PERSONAL.getValue(), param.getStartTime(), param.getEndTime(),userIds);
+            });
+        }
+        return null;
+    }
+
+    /**
+     * 获取统计数据
+     * @param dimension 维度
+     * @param startTime 开始时间
+     * @param endTime 结束时间
+     * @param userIds 用户id
+     * @return
+     */
+    public void getStatisticNumByPersonal(Integer dimension, Date startTime, Date endTime, Long... userIds) {
+        statisticManageMapper.getStatisticNumByPersonal(dimension,startTime, endTime, userIds);
+    }
+
+
+    /**
+     * 统计1.0
+     * 按照部门分组情况
+     * @return
+     */
+    @Override
     public List<Map<String, Object>> statisticMain() {
-        List<CompanyDeptUserInfo> companyDeptdUserList = statisticManageMapper.getCompanyAndDeptAndDeptUserList();
+        List<CompanyDeptUserInfo> companyDeptdUserList = statisticManageMapper.getCompanyAndDeptAndDeptUserList(null);
         if(CollectionUtils.isEmpty(companyDeptdUserList)){return null;}
         //按照部门分组
         Map<Long, List<CompanyDeptUserInfo>> deptInfos = companyDeptdUserList.stream()
@@ -94,7 +151,7 @@ public class StatisticManageServiceImpl implements IStatisticManageService {
 
     private List<Map<String, Object>> groupByCompanyAndCompanyName(List<CompanyDeptUserInfoDTO> source){
         //按照companyId和companyName分组
-        Map<Integer, List<CompanyDeptUserInfoDTO>> companyInfos = source.stream()
+        Map<Long, List<CompanyDeptUserInfoDTO>> companyInfos = source.stream()
                 .collect(Collectors.groupingBy(CompanyDeptUserInfoDTO::getCompanyId));
         List<Map<String, Object>> resultList = Lists.newArrayList();
         companyInfos.forEach((companyId, companyDeptUserInfoDTOS) -> {

+ 40 - 0
fs-service/src/main/java/com/fs/statis/param/ComprehensiveStatisticsParam.java

@@ -0,0 +1,40 @@
+package com.fs.statis.param;
+
+import lombok.Data;
+
+import java.util.Date;
+
+/**
+ * @description: 综合统计入参
+ * @author: Guos
+ * @time: 2025/11/3 上午11:22
+ */
+@Data
+public class ComprehensiveStatisticsParam {
+
+    /**
+     * 统计维度
+     */
+    private Integer dimension;
+
+    /**
+     * 开始时间
+     */
+    private Date startTime;
+
+    /**
+     * 结束时间
+     */
+    private Date endTime;
+
+    /**
+     * 名称
+     */
+    private String name;
+
+    /**
+     * id 在不同的维度下,id代表的意义不同
+     */
+    private Long id;
+}
+

+ 181 - 0
fs-service/src/main/resources/mapper/company/StatisticManageMapper.xml

@@ -27,6 +27,9 @@
         LEFT JOIN company_dept AS cd ON ci.company_id = cd.company_id AND cd.STATUS = 0
         LEFT JOIN company_user AS cu ON cu.dept_id = cd.dept_id AND cu.del_flag = 0
         WHERE ci.is_del = 0 AND cd.del_flag = 0
+        <if test="companyId != null">
+            and ci.company_id = #{companyId}
+        </if>
     </select>
 
     <select id="getStatisticNum" resultType="com.fs.company.dto.CompanyDeptUserInfoDTO">
@@ -61,5 +64,183 @@
         select t2.lineNum, t3.activeNum, t4.completeNum, t5.answerNum, t6.redPacketNum from t2, t3, t4, t5, t6
     </select>
 
+    <select id="getStatisticNumByPersonal">
+        WITH RECURSIVE date_range AS (
+            SELECT #{startTime} AS dt
+            UNION ALL
+            SELECT DATE_ADD(dt, INTERVAL 1 DAY)
+            FROM date_range
+            WHERE dt &lt; #{endTime}
+        ),
+        t1 AS (
+            SELECT
+            COUNT(qec.id) AS t1_count,
+            d.dt AS create_time
+            FROM date_range d
+            LEFT JOIN qw_external_contact qec
+            ON DATE(qec.create_time) = d.dt
+        <if test="userIds != null">
+            <if test="dimension == 1">
+                <choose>
+                    <when test="userIds.length > 1 ">
+                        AND qec.company_user_id IN (
+                        <foreach collection="userIds" item="i" separator=",">
+                            #{i}
+                        </foreach>
+                        )
+                    </when>
+                    <otherwise>
+                        AND qec.company_user_id = #{userIds[0]}
+                    </otherwise>
+                </choose>
+            </if>
+            <if test="dimension == 2">
+                AND qec.company_id = #{userIds[0]}
+            </if>
+        </if>
+            GROUP BY d.dt
+        ),
+        t2 AS (
+            SELECT
+            COUNT(qec.id) AS t2_count,
+            d.dt AS create_time
+            FROM date_range d
+            LEFT JOIN qw_external_contact qec
+            ON DATE(qec.create_time) = d.dt
+        <if test="userIds != null">
+            <if test="dimension == 1">
+                <choose>
+                    <when test="userIds.length > 1 ">
+                        AND qec.company_user_id IN (
+                        <foreach collection="userIds" item="i" separator=",">
+                            #{i}
+                        </foreach>
+                        )
+                    </when>
+                    <otherwise>
+                        AND qec.company_user_id = #{userIds[0]}
+                    </otherwise>
+                </choose>
+            </if>
+            <if test="dimension == 2">
+                AND qec.company_id = #{userIds[0]}
+            </if>
+        </if>
+            AND qec.fs_user_id IS NOT NULL
+            GROUP BY d.dt
+        ),
+        t4 AS (
+            SELECT
+            d.dt AS create_time,
+            COUNT(fcwl.qw_external_contact_id) AS completeNum
+            FROM
+            date_range d
+            LEFT JOIN fs_course_watch_log AS fcwl
+            ON DATE(fcwl.create_time) = d.dt
+            AND fcwl.log_type = 2
+        <if test="userIds != null">
+            <if test="dimension == 1">
+                <choose>
+                    <when test="userIds.length > 1 ">
+                        AND fcwl.company_user_id IN (
+                        <foreach collection="userIds" item="i" separator=",">
+                            #{i}
+                        </foreach>
+                        )
+                    </when>
+                    <otherwise>
+                        AND fcwl.company_user_id = #{userIds[0]}
+                    </otherwise>
+                </choose>
+            </if>
+            <if test="dimension == 2">
+                AND fcwl.company_id = #{userIds[0]}
+            </if>
+        </if>
+            GROUP BY d.dt
+        ),
+        t5 AS (
+            SELECT
+            d.dt AS create_time,
+            COUNT(fcal.log_id) AS answerNum
+            FROM
+            date_range d
+            LEFT JOIN fs_course_answer_logs AS fcal
+            ON DATE(fcal.create_time) = d.dt
+        <if test="userIds != null">
+            <if test="dimension == 1">
+                <choose>
+                    <when test="userIds.length > 1 ">
+                        AND fcal.company_user_id IN (
+                        <foreach collection="userIds" item="i" separator=",">
+                            #{i}
+                        </foreach>
+                        )
+                    </when>
+                    <otherwise>
+                        AND fcal.company_user_id = #{userIds[0]}
+                    </otherwise>
+                </choose>
+            </if>
+            <if test="dimension == 2">
+                AND fcal.company_id = #{userIds[0]}
+            </if>
+        </if>
+            GROUP BY d.dt
+        ),
+        t6 AS (
+            SELECT
+            d.dt AS create_time,
+            COUNT(fcrpl.log_id) AS redPacketNum
+            FROM
+            date_range d
+            LEFT JOIN fs_course_red_packet_log AS fcrpl
+            ON DATE(fcrpl.create_time) = d.dt
+        <if test="userIds != null">
+            <if test="dimension == 1">
+                <choose>
+                    <when test="userIds.length > 1 ">
+                        AND fcrpl.company_user_id IN (
+                        <foreach collection="userIds" item="i" separator=",">
+                            #{i}
+                        </foreach>
+                        )
+                    </when>
+                    <otherwise>
+                        AND fcrpl.company_user_id = #{userIds[0]}
+                    </otherwise>
+                </choose>
+            </if>
+            <if test="dimension == 2">
+                AND fcrpl.company_id = #{userIds[0]}
+            </if>
+        </if>
+            GROUP BY d.dt
+        )
+        SELECT
+            t1.create_time,
+            t1.t1_count as lineNum,
+            t2.t2_count as activeNum,
+            t4.completeNum,
+            t5.answerNum,
+            t6.redPacketNum
+        FROM t1
+        INNER JOIN t2 ON t1.create_time = t2.create_time
+        INNER JOIN t4 ON t1.create_time = t4.create_time
+        INNER JOIN t5 ON t1.create_time = t5.create_time
+        INNER JOIN t6 ON t1.create_time = t6.create_time
+        ORDER BY t1.create_time
+    </select>
+
+    <select id="getCompanyInfo" resultType="com.fs.company.domain.CompanyDeptUserInfo">
+        SELECT
+            ci.company_id,
+            ci.company_name
+        FROM
+            company AS ci
+        WHERE
+            ci.is_del = 0
+    </select>
+
 
 </mapper>