StatisticManageMapper.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.fs.company.mapper.StatisticManageMapper">
  6. <resultMap id="CompanyDeptUserListMap" type="com.fs.company.domain.CompanyDeptUserInfo">
  7. <id column="company_id" property="companyId" />
  8. <result column="company_name" property="companyName"/>
  9. <result column="dept_id" property="deptId"/>
  10. <result column="dept_name" property="deptName"/>
  11. <result column="user_id" property="userId"/>
  12. <result column="user_name" property="userName"/>
  13. <result column="nick_name" property="nickName"/>
  14. </resultMap>
  15. <select id="getCompanyAndDeptAndDeptUserList" resultMap="CompanyDeptUserListMap">
  16. SELECT
  17. ci.company_id,
  18. ci.company_name,
  19. cd.dept_id,
  20. cd.dept_name,
  21. cu.user_id,
  22. cu.user_name,
  23. cu.nick_name
  24. FROM company AS ci
  25. LEFT JOIN company_dept AS cd ON ci.company_id = cd.company_id AND cd.STATUS = 0
  26. LEFT JOIN company_user AS cu ON cu.dept_id = cd.dept_id AND cu.del_flag = 0
  27. WHERE ci.is_del = 0 AND cd.del_flag = 0
  28. <if test="companyId != null">
  29. and ci.company_id = #{companyId}
  30. </if>
  31. </select>
  32. <select id="getStatisticNum" resultType="com.fs.company.dto.CompanyDeptUserInfoDTO">
  33. WITH t1 AS (
  34. SELECT
  35. count(*) AS lineNum
  36. FROM
  37. qw_external_contact AS qec
  38. WHERE
  39. date_format( qec.create_time, '%y%m%d' ) = date_format(now(), '%y%m%d' )
  40. AND qec.company_user_id = 327
  41. ),
  42. t2 AS (
  43. SELECT
  44. count( qec.fs_user_id ) AS activeNum
  45. FROM
  46. qw_external_contact AS qec
  47. WHERE
  48. date_format( qec.create_time, '%y%m%d' ) = date_format(now(), '%y%m%d' )
  49. AND qec.fs_user_id IS NOT NULL
  50. AND qec.company_user_id = 327
  51. ),
  52. 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 ),
  53. 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 ),
  54. 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 )
  55. SELECT * FROM t1,t2,t3,t4,t5
  56. </select>
  57. <select id="getStatisticNumByPersonal" resultType="com.fs.company.dto.ComprehensiveStatisticsDTO">
  58. WITH RECURSIVE date_range AS (
  59. SELECT #{startTime} AS dt
  60. UNION ALL
  61. SELECT DATE_ADD(dt, INTERVAL 1 DAY)
  62. FROM date_range
  63. WHERE dt &lt; #{endTime}
  64. ),
  65. t1 AS (
  66. SELECT
  67. COUNT(qec.id) AS t1_count,
  68. d.dt AS create_time
  69. FROM date_range d
  70. LEFT JOIN qw_external_contact qec
  71. ON DATE(qec.create_time) = d.dt
  72. <if test="userIds != null">
  73. <if test="dimension == 1">
  74. <choose>
  75. <when test="userIds.length > 1 ">
  76. AND qec.company_user_id IN (
  77. <foreach collection="userIds" item="i" separator=",">
  78. #{i}
  79. </foreach>
  80. )
  81. </when>
  82. <otherwise>
  83. AND qec.company_user_id = #{userIds[0]}
  84. </otherwise>
  85. </choose>
  86. </if>
  87. <if test="dimension == 2">
  88. AND qec.company_id = #{userIds[0]}
  89. </if>
  90. </if>
  91. GROUP BY d.dt
  92. ),
  93. t2 AS (
  94. SELECT
  95. COUNT(qec.id) AS t2_count,
  96. d.dt AS create_time
  97. FROM date_range d
  98. LEFT JOIN qw_external_contact qec
  99. ON DATE(qec.create_time) = d.dt
  100. <if test="userIds != null">
  101. <if test="dimension == 1">
  102. <choose>
  103. <when test="userIds.length > 1 ">
  104. AND qec.company_user_id IN (
  105. <foreach collection="userIds" item="i" separator=",">
  106. #{i}
  107. </foreach>
  108. )
  109. </when>
  110. <otherwise>
  111. AND qec.company_user_id = #{userIds[0]}
  112. </otherwise>
  113. </choose>
  114. </if>
  115. <if test="dimension == 2">
  116. AND qec.company_id = #{userIds[0]}
  117. </if>
  118. </if>
  119. AND qec.fs_user_id IS NOT NULL
  120. GROUP BY d.dt
  121. ),
  122. t4 AS (
  123. SELECT
  124. d.dt AS create_time,
  125. COUNT(fcwl.qw_external_contact_id) AS completeNum
  126. FROM
  127. date_range d
  128. LEFT JOIN fs_course_watch_log AS fcwl
  129. ON DATE(fcwl.create_time) = d.dt
  130. AND fcwl.log_type = 2
  131. <if test="userIds != null">
  132. <if test="dimension == 1">
  133. <choose>
  134. <when test="userIds.length > 1 ">
  135. AND fcwl.company_user_id IN (
  136. <foreach collection="userIds" item="i" separator=",">
  137. #{i}
  138. </foreach>
  139. )
  140. </when>
  141. <otherwise>
  142. AND fcwl.company_user_id = #{userIds[0]}
  143. </otherwise>
  144. </choose>
  145. </if>
  146. <if test="dimension == 2">
  147. AND fcwl.company_id = #{userIds[0]}
  148. </if>
  149. </if>
  150. GROUP BY d.dt
  151. ),
  152. t5 AS (
  153. SELECT
  154. d.dt AS create_time,
  155. COUNT(fcal.log_id) AS answerNum
  156. FROM
  157. date_range d
  158. LEFT JOIN fs_course_answer_logs AS fcal
  159. ON DATE(fcal.create_time) = d.dt
  160. <if test="userIds != null">
  161. <if test="dimension == 1">
  162. <choose>
  163. <when test="userIds.length > 1 ">
  164. AND fcal.company_user_id IN (
  165. <foreach collection="userIds" item="i" separator=",">
  166. #{i}
  167. </foreach>
  168. )
  169. </when>
  170. <otherwise>
  171. AND fcal.company_user_id = #{userIds[0]}
  172. </otherwise>
  173. </choose>
  174. </if>
  175. <if test="dimension == 2">
  176. AND fcal.company_id = #{userIds[0]}
  177. </if>
  178. </if>
  179. GROUP BY d.dt
  180. ),
  181. t6 AS (
  182. SELECT
  183. d.dt AS create_time,
  184. COUNT(fcrpl.log_id) AS redPacketNum
  185. FROM
  186. date_range d
  187. LEFT JOIN fs_course_red_packet_log AS fcrpl
  188. ON DATE(fcrpl.create_time) = d.dt
  189. <if test="userIds != null">
  190. <if test="dimension == 1">
  191. <choose>
  192. <when test="userIds.length > 1 ">
  193. AND fcrpl.company_user_id IN (
  194. <foreach collection="userIds" item="i" separator=",">
  195. #{i}
  196. </foreach>
  197. )
  198. </when>
  199. <otherwise>
  200. AND fcrpl.company_user_id = #{userIds[0]}
  201. </otherwise>
  202. </choose>
  203. </if>
  204. <if test="dimension == 2">
  205. AND fcrpl.company_id = #{userIds[0]}
  206. </if>
  207. </if>
  208. GROUP BY d.dt
  209. )
  210. SELECT
  211. t1.create_time as dateStr,
  212. t1.t1_count as lineNum,
  213. t2.t2_count as activeNum,
  214. t4.completeNum,
  215. t5.answerNum,
  216. t6.redPacketNum
  217. FROM t1
  218. INNER JOIN t2 ON t1.create_time = t2.create_time
  219. INNER JOIN t4 ON t1.create_time = t4.create_time
  220. INNER JOIN t5 ON t1.create_time = t5.create_time
  221. INNER JOIN t6 ON t1.create_time = t6.create_time
  222. ORDER BY t1.create_time
  223. </select>
  224. <select id="getCompanyInfo" resultType="com.fs.company.domain.CompanyDeptUserInfo">
  225. SELECT
  226. ci.company_id,
  227. ci.company_name
  228. FROM
  229. company AS ci
  230. WHERE
  231. ci.is_del = 0
  232. </select>
  233. <!-- 基础字段映射(复用) -->
  234. <sql id="Base_Column_List">
  235. id, company_id, company_name, dept_id, dept_name,
  236. user_id, user_name, nick_name, statistics_time,
  237. line_num, active_num, complete_num, answer_num,
  238. red_packet_num, red_packet_amount, create_time, update_time
  239. </sql>
  240. <!-- 1. 插入数据(全字段插入) -->
  241. <insert id="insert" parameterType="com.fs.company.domain.ComprehensiveDailyStats">
  242. INSERT INTO user_daily_stats (
  243. company_id, company_name, dept_id, dept_name,
  244. user_id, user_name, nick_name, statistics_time,
  245. line_num, active_num, complete_num, answer_num,
  246. red_packet_num, red_packet_amount, create_time, update_time
  247. ) VALUES (
  248. #{companyId}, #{companyName}, #{deptId}, #{deptName},
  249. #{userId}, #{userName}, #{nickName}, #{statisticsTime},
  250. #{lineNum}, #{activeNum}, #{completeNum}, #{answerNum},
  251. #{redPacketNum}, #{redPacketAmount}, #{createTime}, #{updateTime}
  252. )
  253. </insert>
  254. <!-- 2. 插入或更新(根据唯一索引uk_user_date,存在则更新,不存在则插入) -->
  255. <insert id="insertOrUpdate" parameterType="com.fs.company.domain.ComprehensiveDailyStats">
  256. INSERT INTO user_daily_stats (
  257. company_id, company_name, dept_id, dept_name,
  258. user_id, user_name, nick_name, statistics_time,
  259. line_num, active_num, complete_num, answer_num,
  260. red_packet_num, red_packet_amount, create_time, update_time
  261. ) VALUES (
  262. #{companyId}, #{companyName}, #{deptId}, #{deptName},
  263. #{userId}, #{userName}, #{nickName}, #{statisticsTime},
  264. #{lineNum}, #{activeNum}, #{completeNum}, #{answerNum},
  265. #{redPacketNum}, #{redPacketAmount}, NOW(), NOW()
  266. ) ON DUPLICATE KEY UPDATE
  267. company_id = VALUES(company_id),
  268. company_name = VALUES(company_name),
  269. dept_id = VALUES(dept_id),
  270. dept_name = VALUES(dept_name),
  271. user_name = VALUES(user_name),
  272. nick_name = VALUES(nick_name),
  273. line_num = VALUES(line_num),
  274. active_num = VALUES(active_num),
  275. complete_num = VALUES(complete_num),
  276. answer_num = VALUES(answer_num),
  277. red_packet_num = VALUES(red_packet_num),
  278. red_packet_amount = VALUES(red_packet_amount),
  279. update_time = NOW()
  280. </insert>
  281. <!-- 3. 根据ID更新数据(全字段更新) -->
  282. <update id="updateById" parameterType="com.fs.company.domain.ComprehensiveDailyStats">
  283. UPDATE user_daily_stats
  284. SET
  285. company_id = #{companyId},
  286. company_name = #{companyName},
  287. dept_id = #{deptId},
  288. dept_name = #{deptName},
  289. user_id = #{userId},
  290. user_name = #{userName},
  291. nick_name = #{nickName},
  292. statistics_time = #{statisticsTime},
  293. line_num = #{lineNum},
  294. active_num = #{activeNum},
  295. complete_num = #{completeNum},
  296. answer_num = #{answerNum},
  297. red_packet_num = #{redPacketNum},
  298. red_packet_amount = #{redPacketAmount},
  299. update_time = NOW()
  300. WHERE id = #{id}
  301. </update>
  302. <!-- 4. 根据用户ID和统计日期更新(部分字段更新,按需调整) -->
  303. <update id="updateByUserAndDate" parameterType="com.fs.company.domain.ComprehensiveDailyStats">
  304. UPDATE user_daily_stats
  305. SET
  306. line_num = #{lineNum},
  307. active_num = #{activeNum},
  308. complete_num = #{completeNum},
  309. answer_num = #{answerNum},
  310. red_packet_num = #{redPacketNum},
  311. red_packet_amount = #{redPacketAmount},
  312. update_time = NOW()
  313. WHERE user_id = #{userId} AND statistics_time = #{statisticsTime}
  314. </update>
  315. <!-- 5. 根据ID查询 -->
  316. <select id="selectById" resultType="com.fs.company.domain.ComprehensiveDailyStats" parameterType="java.lang.Long">
  317. SELECT <include refid="Base_Column_List"/> FROM user_daily_stats WHERE id = #{id}
  318. </select>
  319. <!-- 6. 根据用户ID和统计日期查询 -->
  320. <select id="selectByUserAndDate" resultType="com.fs.company.domain.ComprehensiveDailyStats">
  321. SELECT <include refid="Base_Column_List"/>
  322. FROM user_daily_stats
  323. WHERE user_id = #{userId} AND statistics_time = #{statisticsTime}
  324. </select>
  325. <!-- 7. 根据公司ID和日期范围查询 -->
  326. <select id="selectByCompanyAndDateRange" resultType="com.fs.company.domain.ComprehensiveDailyStats">
  327. SELECT <include refid="Base_Column_List"/>
  328. FROM user_daily_stats
  329. WHERE company_id = #{companyId}
  330. AND statistics_time BETWEEN #{startTime} AND #{endTime}
  331. ORDER BY statistics_time ASC
  332. </select>
  333. <!-- 8. 根据部门ID和日期范围查询 -->
  334. <select id="selectByDeptAndDateRange" resultType="com.fs.company.domain.ComprehensiveDailyStats">
  335. SELECT <include refid="Base_Column_List"/>
  336. FROM user_daily_stats
  337. WHERE dept_id = #{deptId}
  338. AND statistics_time BETWEEN #{startTime} AND #{endTime}
  339. ORDER BY statistics_time ASC
  340. </select>
  341. <!-- 9. 删除数据(根据ID) -->
  342. <delete id="deleteById" parameterType="java.lang.Long">
  343. DELETE FROM user_daily_stats WHERE id = #{id}
  344. </delete>
  345. <!-- 10. 删除数据(根据用户ID和统计日期) -->
  346. <delete id="deleteByUserAndDate">
  347. DELETE FROM user_daily_stats WHERE user_id = #{userId} AND statistics_time = #{statisticsTime}
  348. </delete>
  349. </mapper>