StatisticManageMapper.xml 16 KB

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