| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- <?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">
- <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 = #{userIds}
- ),
- 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 = #{userIds}
- ),
- 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 = #{userIds} ),
- 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 = #{userIds} ),
- 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 = #{userIds} )
- SELECT * FROM t1,t2,t3,t4,t5
- </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>
- <select id="getStatisticNumByPersonal" resultType="com.fs.company.domain.ComprehensiveDailyStats">
- select <include refid="Base_Column_List"/> from user_daily_stats as uds
- <where>
- uds.user_id = #{userIds[0]}
- and statistics_time >=date_format(#{startTime},'%y%m%d')
- and statistics_time <= date_format(#{endTime},'%y%m%d')
- </where>
- </select>
- <select id="getStatisticNumByDeptId" resultType="com.fs.company.domain.ComprehensiveDailyStats">
- select <include refid="Base_Column_List"/> from user_daily_stats as uds
- <where>
- uds.dept_id = #{deptIds[0]}
- and statistics_time >=date_format(#{startTime},'%y%m%d')
- and statistics_time <= date_format(#{endTime},'%y%m%d')
- </where>
- </select>
- <!-- 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,存在则更新,不存在则插入) 已经删除了索引,有时候用户id是空的 -->
- <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 = date_format(#{statisticsTime},'%y%m%d')
- </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 =date_format(#{statisticsTime},'%y%m%d')
- </select>
- <!-- 6. 根据用户ID和统计日期查询 -->
- <select id="countByUserAndDate" resultType="java.lang.Integer">
- SELECT count(id)
- FROM user_daily_stats
- WHERE user_id = #{userId} AND statistics_time = date_format(#{statisticsTime},'%y%m%d')
- </select>
- <!-- 8. 根据部门ID和日期查询 -->
- <select id="selectByDeptAndDate" resultType="com.fs.company.domain.ComprehensiveDailyStats">
- SELECT <include refid="Base_Column_List"/>
- FROM user_daily_stats
- WHERE dept_id = #{deptId}
- AND statistics_time = date_format(#{statisticsTime},'%y%m%d')
- </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>
- <!-- 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>
- <!-- 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 = date_format(#{statisticsTime},'%y%m%d')
- </delete>
- <!-- 上面的方法需要逐个删除 -->
- <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="parent_id" property="parentId"/>
- <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,
- cd.parent_id,
- 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="getCompanyInfo" resultMap="CompanyDeptUserListMap">
- SELECT
- ci.company_id,
- ci.company_name
- FROM
- company AS ci
- WHERE
- ci.is_del = 0
- </select>
- <select id="getSearchDeptInfo" resultMap="CompanyDeptUserListMap">
- SELECT
- ci.company_id,
- ci.company_name,
- cd.parent_id,
- cd.dept_id,
- cd.dept_name
- FROM company AS ci
- LEFT JOIN company_dept AS cd ON ci.company_id = cd.company_id AND cd.STATUS = 0
- where ci.company_id = #{id}
- </select>
- <select id="getSearchUserInfo" resultMap="CompanyDeptUserListMap">
- SELECT
- cd.parent_id,
- 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
- and (cd.dept_id = #{id} OR FIND_IN_SET(#{id}, cd.ancestors) > 0)
- order by cd.dept_id
- </select>
- <select id="getCourseWatchLogData" resultType="com.fs.company.domain.CourseWatchLogData">
- SELECT
- fcwl.company_user_id,
- fcwl.video_id,
- fcwl.course_id,
- fuc.project,
- fcwl.log_type,
- fcwl.create_time
- FROM
- fs_course_watch_log as fcwl
- left join fs_user_course as fuc on fcwl.course_id = fuc.course_id and fuc.is_del = 0
- WHERE
- fcwl.company_user_id = #{companyUserId}
- and DATE_FORMAT(fcwl.create_time, '%Y-%m-%d') = DATE_FORMAT(#{dateTime}, '%Y-%m-%d')
- </select>
- <select id="getAnswerAndRedPacketData" resultType="com.fs.company.domain.CourseWatchLogData">
- WITH t1 as (
- SELECT
- count(DISTINCT fcal.watch_log_id) as answer_num
- FROM
- fs_course_answer_logs AS fcal
- WHERE
- fcal.company_user_id = #{companyUserId}
- and fcal.video_id = #{videoId}
- AND DATE_FORMAT( fcal.create_time, '%Y-%m-%d') = DATE_FORMAT(#{dateTime}, '%Y-%m-%d')
- ),t2 as (
- SELECT
- fcrpl.log_id,
- fcrpl.amount
- FROM
- fs_course_red_packet_log AS fcrpl
- WHERE
- fcrpl.company_user_id = #{companyUserId}
- AND fcrpl.video_id = #{videoId}
- AND DATE_FORMAT(fcrpl.create_time, '%Y-%m-%d') = DATE_FORMAT(#{dateTime}, '%Y-%m-%d')
- ),
- t3 AS ( SELECT count(t2.log_id) as red_packet_num FROM t2 ),
- t4 AS ( SELECT sum(t2.amount) as red_packet_amount FROM t2 )
- SELECT #{companyUserId}, t1.* , t3.* , t4.* FROM t1, t3, t4
- </select>
- <select id="getStatisticDataByDeptId" resultType="com.fs.statis.dto.ComprehensiveStatisticsDTO">
- SELECT
- SUM(CASE WHEN fs.log_type != 2 THEN 0 ELSE fs.send_count END) AS completeNum,
- SUM(IFNULL(fs.send_count, 0)) as sendCount,
- SUM(IFNULL(fs.log_type, 0)) as logType,
- SUM(IFNULL(fs.answer_num, 0)) as answerNum,
- SUM(IFNULL(fs.red_packet_num, 0)) as redPacketNum,
- SUM(IFNULL(fs.red_packet_amount, 0)) as redPacketAmount,
- fs.statistics_time
- FROM
- fs_statistics AS fs
- WHERE
- DATE_FORMAT(fs.statistics_time, '%Y-%m-%d' ) >= DATE_FORMAT(#{startTime}, '%Y-%m-%d' )
- ANd DATE_FORMAT(fs.statistics_time, '%Y-%m-%d' ) <= DATE_FORMAT(#{endTime}, '%Y-%m-%d' )
- <if test="companyId != null">
- and company_id = #{companyId}
- </if>
- <if test="deptId != null">
- and fs.dept_id = #{deptId}
- </if>
- <if test="userId != null">
- and fs.company_user_id =#{userId}
- </if>
- <if test="videoId != null">
- and fs.video_id = #{videoId}
- </if>
- <if test="projectId != null">
- and fs.project_id= #{projectId}
- </if>
- <if test="logType != null">
- and fs.log_type = #{logType}
- </if>
- <if test="courseId != null">
- and fs.course_id = #{courseId}
- </if>
- <if test="timeGroupFlag">
- GROUP BY fs.statistics_time
- </if>
- order by fs.statistics_time
- <if test="dimension != null">
- <if test="dimension == 2">
- , fs.company_id
- </if>
- <if test="dimension == 3">
- , fs.dept_id
- </if>
- <if test="dimension == 1">
- , fs.company_user_id
- </if>
- </if>
- </select>
- <select id="getUserInfoByUserId" resultMap="CompanyDeptUserListMap">
- select
- c.company_id,
- c.company_name,
- cd.dept_id,
- cd.dept_name,
- cd.parent_id,
- cu.user_id,
- cu.user_name,
- cu.nick_name
- from company_user as cu
- left join company_dept as cd on cu.dept_id = cd.dept_id AND cd.STATUS = 0
- left join company as c on c.company_id = cd.company_id and c.is_del = 0
- where cu.user_id = #{userId}
- </select>
- <select id="getUserInfoByDeptId" resultType="com.fs.company.domain.CompanyDeptUserInfo">
- select
- c.company_id,
- c.company_name,
- cd.dept_id,
- cd.dept_name,
- cd.parent_id,
- cu.user_id,
- cu.user_name,
- cu.nick_name
- from company_dept as cd
- left join company as c on cd.company_id = c.company_id and c.is_del = 0
- left join company_user as cu on cu.dept_id = cd.dept_id AND cu.del_flag = 0
- where cd.dept_id = #{deptId}
- </select>
- </mapper>
|