| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- <?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.fs.company.mapper.StatisticManageMapper">
- <resultMap id="CompanyDeptUserListMap" type="com.fs.company.domain.CompanyDeptUserInfo">
- <id column="company_id" property="companyId" />
- <result column="company_name" property="companyName"/>
- <result column="dept_id" property="deptId"/>
- <result column="dept_name" property="deptName"/>
- <result column="user_id" property="userId"/>
- <result column="user_name" property="userName"/>
- <result column="nick_name" property="nickName"/>
- </resultMap>
- <select id="getCompanyAndDeptAndDeptUserList" resultMap="CompanyDeptUserListMap">
- SELECT
- ci.company_id,
- ci.company_name,
- cd.dept_id,
- cd.dept_name,
- cu.user_id,
- cu.user_name,
- cu.nick_name
- FROM company AS ci
- 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">
- WITH t1 AS (
- SELECT
- count(*) AS lineNum
- FROM
- qw_external_contact AS qec
- WHERE
- date_format( qec.create_time, '%y%m%d' ) = date_format(now(), '%y%m%d' )
- AND qec.company_user_id = 327
- ),
- t2 AS (
- SELECT
- count( qec.fs_user_id ) AS activeNum
- FROM
- qw_external_contact AS qec
- WHERE
- date_format( qec.create_time, '%y%m%d' ) = date_format(now(), '%y%m%d' )
- AND qec.fs_user_id IS NOT NULL
- AND qec.company_user_id = 327
- ),
- t3 AS ( SELECT count(*) AS completeNum FROM fs_course_watch_log AS fcwl WHERE date_format(fcwl.create_time, '%y%m%d' ) = date_format( now(), '%y%m%d') and fcwl.log_type = 2 AND fcwl.company_user_id = 327 ),
- t4 AS ( SELECT count(*) AS answerNum FROM fs_course_answer_logs AS fcal WHERE date_format(fcal.create_time, '%y%m%d' ) = date_format( now(), '%y%m%d') and fcal.company_user_id = 327 ),
- t5 AS ( SELECT count(*) AS redPacketNum FROM fs_course_red_packet_log AS fcrpl WHERE date_format(fcrpl.create_time, '%y%m%d' ) = date_format( now(), '%y%m%d' ) and fcrpl.company_user_id = 327 )
- SELECT * FROM t1,t2,t3,t4,t5
- </select>
- <select id="getStatisticNumByPersonal" resultType="com.fs.company.dto.ComprehensiveStatisticsDTO">
- WITH RECURSIVE date_range AS (
- SELECT #{startTime} AS dt
- UNION ALL
- SELECT DATE_ADD(dt, INTERVAL 1 DAY)
- FROM date_range
- WHERE dt < #{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 as dateStr,
- 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>
- <!-- 基础字段映射(复用) -->
- <sql id="Base_Column_List">
- id, company_id, company_name, dept_id, dept_name,
- user_id, user_name, nick_name, statistics_time,
- line_num, active_num, complete_num, answer_num,
- red_packet_num, red_packet_amount, create_time, update_time
- </sql>
- <!-- 1. 插入数据(全字段插入) -->
- <insert id="insert" parameterType="com.fs.company.domain.ComprehensiveDailyStats">
- INSERT INTO user_daily_stats (
- company_id, company_name, dept_id, dept_name,
- user_id, user_name, nick_name, statistics_time,
- line_num, active_num, complete_num, answer_num,
- red_packet_num, red_packet_amount, create_time, update_time
- ) VALUES (
- #{companyId}, #{companyName}, #{deptId}, #{deptName},
- #{userId}, #{userName}, #{nickName}, #{statisticsTime},
- #{lineNum}, #{activeNum}, #{completeNum}, #{answerNum},
- #{redPacketNum}, #{redPacketAmount}, #{createTime}, #{updateTime}
- )
- </insert>
- <!-- 2. 插入或更新(根据唯一索引uk_user_date,存在则更新,不存在则插入) -->
- <insert id="insertOrUpdate" parameterType="com.fs.company.domain.ComprehensiveDailyStats">
- INSERT INTO user_daily_stats (
- company_id, company_name, dept_id, dept_name,
- user_id, user_name, nick_name, statistics_time,
- line_num, active_num, complete_num, answer_num,
- red_packet_num, red_packet_amount, create_time, update_time
- ) VALUES (
- #{companyId}, #{companyName}, #{deptId}, #{deptName},
- #{userId}, #{userName}, #{nickName}, #{statisticsTime},
- #{lineNum}, #{activeNum}, #{completeNum}, #{answerNum},
- #{redPacketNum}, #{redPacketAmount}, NOW(), NOW()
- ) ON DUPLICATE KEY UPDATE
- company_id = VALUES(company_id),
- company_name = VALUES(company_name),
- dept_id = VALUES(dept_id),
- dept_name = VALUES(dept_name),
- user_name = VALUES(user_name),
- nick_name = VALUES(nick_name),
- line_num = VALUES(line_num),
- active_num = VALUES(active_num),
- complete_num = VALUES(complete_num),
- answer_num = VALUES(answer_num),
- red_packet_num = VALUES(red_packet_num),
- red_packet_amount = VALUES(red_packet_amount),
- update_time = NOW()
- </insert>
- <!-- 3. 根据ID更新数据(全字段更新) -->
- <update id="updateById" parameterType="com.fs.company.domain.ComprehensiveDailyStats">
- UPDATE user_daily_stats
- SET
- company_id = #{companyId},
- company_name = #{companyName},
- dept_id = #{deptId},
- dept_name = #{deptName},
- user_id = #{userId},
- user_name = #{userName},
- nick_name = #{nickName},
- statistics_time = #{statisticsTime},
- line_num = #{lineNum},
- active_num = #{activeNum},
- complete_num = #{completeNum},
- answer_num = #{answerNum},
- red_packet_num = #{redPacketNum},
- red_packet_amount = #{redPacketAmount},
- update_time = NOW()
- WHERE id = #{id}
- </update>
- <!-- 4. 根据用户ID和统计日期更新(部分字段更新,按需调整) -->
- <update id="updateByUserAndDate" parameterType="com.fs.company.domain.ComprehensiveDailyStats">
- UPDATE user_daily_stats
- SET
- line_num = #{lineNum},
- active_num = #{activeNum},
- complete_num = #{completeNum},
- answer_num = #{answerNum},
- red_packet_num = #{redPacketNum},
- red_packet_amount = #{redPacketAmount},
- update_time = NOW()
- WHERE user_id = #{userId} AND statistics_time = #{statisticsTime}
- </update>
- <!-- 5. 根据ID查询 -->
- <select id="selectById" resultType="com.fs.company.domain.ComprehensiveDailyStats" parameterType="java.lang.Long">
- SELECT <include refid="Base_Column_List"/> FROM user_daily_stats WHERE id = #{id}
- </select>
- <!-- 6. 根据用户ID和统计日期查询 -->
- <select id="selectByUserAndDate" resultType="com.fs.company.domain.ComprehensiveDailyStats">
- SELECT <include refid="Base_Column_List"/>
- FROM user_daily_stats
- WHERE user_id = #{userId} AND statistics_time = #{statisticsTime}
- </select>
- <!-- 7. 根据公司ID和日期范围查询 -->
- <select id="selectByCompanyAndDateRange" resultType="com.fs.company.domain.ComprehensiveDailyStats">
- SELECT <include refid="Base_Column_List"/>
- FROM user_daily_stats
- WHERE company_id = #{companyId}
- AND statistics_time BETWEEN #{startTime} AND #{endTime}
- ORDER BY statistics_time ASC
- </select>
- <!-- 8. 根据部门ID和日期范围查询 -->
- <select id="selectByDeptAndDateRange" resultType="com.fs.company.domain.ComprehensiveDailyStats">
- SELECT <include refid="Base_Column_List"/>
- FROM user_daily_stats
- WHERE dept_id = #{deptId}
- AND statistics_time BETWEEN #{startTime} AND #{endTime}
- ORDER BY statistics_time ASC
- </select>
- <!-- 9. 删除数据(根据ID) -->
- <delete id="deleteById" parameterType="java.lang.Long">
- DELETE FROM user_daily_stats WHERE id = #{id}
- </delete>
- <!-- 10. 删除数据(根据用户ID和统计日期) -->
- <delete id="deleteByUserAndDate">
- DELETE FROM user_daily_stats WHERE user_id = #{userId} AND statistics_time = #{statisticsTime}
- </delete>
- </mapper>
|