FsUserCourseCompanyStatisticsMapper.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.course.mapper.FsUserCourseCompanyStatisticsMapper">
  6. <resultMap type="FsUserCourseCompanyStatistics" id="FsUserCourseCompanyStatisticsResult">
  7. <result property="id" column="id" />
  8. <result property="projectId" column="project_id" />
  9. <result property="watchCount" column="watch_count" />
  10. <result property="answerCount" column="answer_count" />
  11. <result property="correctCount" column="correct_count" />
  12. <result property="receiveCount" column="receive_count" />
  13. <result property="receiveAmount" column="receive_amount" />
  14. <result property="userCount" column="user_count" />
  15. <result property="userBlacklistCount" column="user_blacklist_count" />
  16. <result property="companyId" column="company_id" />
  17. <result property="companyName" column="company_name" />
  18. <result property="createDate" column="create_date" />
  19. <result property="createTime" column="create_time" />
  20. <result property="updateTime" column="update_time" />
  21. <result property="createBy" column="create_by" />
  22. <result property="updateBy" column="update_by" />
  23. </resultMap>
  24. <sql id="selectFsUserCourseCompanyStatisticsVo">
  25. select id, project_id, complete_watch_count, watch_count, answer_count, correct_count,receive_count, receive_amount, user_count, user_blacklist_count, company_id, company_name, create_date, create_time, update_time, create_by, update_by from fs_user_course_company_statistics
  26. </sql>
  27. <select id="selectFsUserCourseCompanyStatisticsList" parameterType="FsUserCourseCompanyStatistics" resultMap="FsUserCourseCompanyStatisticsResult">
  28. <include refid="selectFsUserCourseCompanyStatisticsVo"/>
  29. <where>
  30. <if test="projectId != null "> and project_id = #{projectId}</if>
  31. <if test="watchCount != null "> and watch_count = #{watchCount}</if>
  32. <if test="answerCount != null "> and answer_count = #{answerCount}</if>
  33. <if test="correctCount != null "> and correct_count = #{correctCount}</if>
  34. <if test="receiveCount != null "> and receive_count = #{receiveCount}</if>
  35. <if test="receiveAmount != null "> and receive_amount = #{receiveAmount}</if>
  36. <if test="userCount != null "> and user_count = #{userCount}</if>
  37. <if test="userBlacklistCount != null "> and user_blacklist_count = #{userBlacklistCount}</if>
  38. <if test="companyId != null "> and company_id = #{companyId}</if>
  39. <if test="companyName != null and companyName != ''"> and company_name like concat('%', #{companyName}, '%')</if>
  40. <if test="createDate != null "> and create_date = #{createDate}</if>
  41. </where>
  42. </select>
  43. <select id="selectFsUserCourseCompanyStatisticsById" parameterType="Long" resultMap="FsUserCourseCompanyStatisticsResult">
  44. <include refid="selectFsUserCourseCompanyStatisticsVo"/>
  45. where id = #{id}
  46. </select>
  47. <insert id="insertFsUserCourseCompanyStatistics" parameterType="FsUserCourseCompanyStatistics" useGeneratedKeys="true" keyProperty="id">
  48. insert into fs_user_course_company_statistics
  49. <trim prefix="(" suffix=")" suffixOverrides=",">
  50. <if test="projectId != null">project_id,</if>
  51. <if test="watchCount != null">watch_count,</if>
  52. <if test="completeWatchCount != null">complete_watch_count,</if>
  53. <if test="answerCount != null">answer_count,</if>
  54. <if test="correctCount != null">correct_count,</if>
  55. <if test="receiveCount != null">receive_count,</if>
  56. <if test="receiveAmount != null">receive_amount,</if>
  57. <if test="userCount != null">user_count,</if>
  58. <if test="userBlacklistCount != null">user_blacklist_count,</if>
  59. <if test="companyId != null">company_id,</if>
  60. <if test="companyName != null">company_name,</if>
  61. <if test="createDate != null">create_date,</if>
  62. <if test="createTime != null">create_time,</if>
  63. <if test="updateTime != null">update_time,</if>
  64. <if test="createBy != null">create_by,</if>
  65. <if test="updateBy != null">update_by,</if>
  66. </trim>
  67. <trim prefix="values (" suffix=")" suffixOverrides=",">
  68. <if test="projectId != null">#{projectId},</if>
  69. <if test="watchCount != null">#{watchCount},</if>
  70. <if test="completeWatchCount != null">#{completeWatchCount},</if>
  71. <if test="answerCount != null">#{answerCount},</if>
  72. <if test="correctCount != null">#{correctCount},</if>
  73. <if test="receiveCount != null">#{receiveCount},</if>
  74. <if test="receiveAmount != null">#{receiveAmount},</if>
  75. <if test="userCount != null">#{userCount},</if>
  76. <if test="userBlacklistCount != null">#{userBlacklistCount},</if>
  77. <if test="companyId != null">#{companyId},</if>
  78. <if test="companyName != null">#{companyName},</if>
  79. <if test="createDate != null">#{createDate},</if>
  80. <if test="createTime != null">#{createTime},</if>
  81. <if test="updateTime != null">#{updateTime},</if>
  82. <if test="createBy != null">#{createBy},</if>
  83. <if test="updateBy != null">#{updateBy},</if>
  84. </trim>
  85. </insert>
  86. <update id="updateFsUserCourseCompanyStatistics" parameterType="FsUserCourseCompanyStatistics">
  87. update fs_user_course_company_statistics
  88. <trim prefix="SET" suffixOverrides=",">
  89. <if test="projectId != null">project_id = #{projectId},</if>
  90. <if test="watchCount != null">watch_count = #{watchCount},</if>
  91. <if test="answerCount != null">answer_count = #{answerCount},</if>
  92. <if test="correctCount != null">correct_count = #{correctCount},</if>
  93. <if test="receiveCount != null">receive_count = #{receiveCount},</if>
  94. <if test="receiveAmount != null">receive_amount = #{receiveAmount},</if>
  95. <if test="userCount != null">user_count = #{userCount},</if>
  96. <if test="userBlacklistCount != null">user_blacklist_count = #{userBlacklistCount},</if>
  97. <if test="companyId != null">company_id = #{companyId},</if>
  98. <if test="companyName != null">company_name = #{companyName},</if>
  99. <if test="createDate != null">create_date = #{createDate},</if>
  100. <if test="createTime != null">create_time = #{createTime},</if>
  101. <if test="updateTime != null">update_time = #{updateTime},</if>
  102. <if test="createBy != null">create_by = #{createBy},</if>
  103. <if test="updateBy != null">update_by = #{updateBy},</if>
  104. </trim>
  105. where id = #{id}
  106. </update>
  107. <delete id="deleteFsUserCourseCompanyStatisticsById" parameterType="Long">
  108. delete from fs_user_course_company_statistics where id = #{id}
  109. </delete>
  110. <delete id="deleteFsUserCourseCompanyStatisticsByIds" parameterType="String">
  111. delete from fs_user_course_company_statistics where id in
  112. <foreach item="id" collection="array" open="(" separator="," close=")">
  113. #{id}
  114. </foreach>
  115. </delete>
  116. <select id="selectStatisticsByDate" resultType="FsUserCourseCompanyStatistics">
  117. WITH watch_stats AS (
  118. <!-- 看课统计-->
  119. SELECT
  120. project,
  121. COUNT(DISTINCT CASE WHEN log_type = 2 THEN user_id END) AS courseCompleteNum,
  122. COUNT(DISTINCT CASE WHEN log_type != 3 THEN user_id END) AS courseWatchNum
  123. FROM fs_course_watch_log
  124. WHERE create_time &gt; #{startTime}
  125. AND create_time &lt; #{endTime}
  126. AND company_id = #{companyId}
  127. GROUP BY project
  128. ),
  129. answer_stats AS (
  130. <!-- 答题统计-->
  131. SELECT
  132. l.project,
  133. COUNT(DISTINCT a.user_id) as answerNum,
  134. COUNT(DISTINCT CASE WHEN a.is_right = 1 THEN a.user_id END) as answerRightNum
  135. FROM fs_course_answer_logs a
  136. INNER JOIN fs_course_watch_log l ON a.watch_log_id = l.log_id
  137. WHERE a.create_time &gt;= #{startTime}
  138. AND a.create_time &lt; #{endTime}
  139. AND a.company_id = #{companyId}
  140. GROUP BY l.project
  141. ),
  142. redpacket_stats AS (
  143. <!-- 红包统计-->
  144. SELECT
  145. l.project,
  146. COUNT(r.log_id) as redPacketNum,
  147. IFNULL(SUM(r.amount), 0) as redPacketAmount
  148. FROM fs_course_red_packet_log r
  149. INNER JOIN fs_course_watch_log l ON r.watch_log_id = l.log_id
  150. WHERE r.create_time &gt;= #{startTime}
  151. AND r.create_time &lt; #{endTime}
  152. AND r.company_id = #{companyId}
  153. GROUP BY l.project
  154. ),
  155. user_stats AS (
  156. <!-- 用户统计-->
  157. SELECT
  158. project_id as project,
  159. COUNT(DISTINCT user_id) as userCount,
  160. COUNT(DISTINCT CASE WHEN status = 2 THEN user_id END) as blacklist
  161. FROM fs_user_company_user
  162. WHERE create_time &gt;= #{startTime}
  163. AND create_time &lt; #{endTime}
  164. AND company_id = #{companyId}
  165. GROUP BY project_id
  166. )
  167. <!-- 合并数据-->
  168. SELECT
  169. w.project AS projectId,
  170. w.courseCompleteNum AS completeWatchCount,
  171. w.courseWatchNum AS watchCount,
  172. COALESCE(a.answerNum, 0) AS answerCount,
  173. COALESCE(a.answerRightNum, 0) AS correctCount,
  174. COALESCE(r.redPacketNum, 0) AS receiveCount,
  175. COALESCE(r.redPacketAmount, 0) AS receiveAmount,
  176. COALESCE(u.userCount, 0) AS userCount,
  177. COALESCE(u.blacklist, 0) AS userBlacklistCount
  178. FROM watch_stats w
  179. LEFT JOIN answer_stats a ON w.project = a.project
  180. LEFT JOIN redpacket_stats r ON w.project = r.project
  181. LEFT JOIN user_stats u ON w.project = u.project
  182. </select>
  183. <select id="selectFsUserCourseCompanyStatisticsTotal" parameterType="FsUserCourseCompanyStatistics" resultType="FsUserCourseCompanyStatistics">
  184. SELECT
  185. id,
  186. project_id,
  187. COALESCE(SUM(complete_watch_count), 0) AS complete_watch_count,
  188. COALESCE(SUM(watch_count), 0) AS watch_count,
  189. COALESCE(SUM(answer_count), 0) AS answer_count,
  190. COALESCE(SUM(correct_count), 0) AS correct_count,
  191. COALESCE(SUM(receive_count), 0) AS receive_count,
  192. COALESCE(SUM(receive_amount), 0) AS receive_amount,
  193. COALESCE(SUM(user_count), 0) AS user_count,
  194. COALESCE(SUM(user_blacklist_count), 0) AS user_blacklist_count,
  195. company_id,
  196. company_name,
  197. create_date,
  198. create_time,
  199. update_time,
  200. create_by,
  201. update_by
  202. FROM fs_user_course_company_statistics
  203. <where>
  204. <if test="projectId != null">AND project_id = #{projectId}</if>
  205. <if test="watchCount != null">AND watch_count = #{watchCount}</if>
  206. <if test="answerCount != null">AND answer_count = #{answerCount}</if>
  207. <if test="correctCount != null">AND correct_count = #{correctCount}</if>
  208. <if test="receiveCount != null">AND receive_count = #{receiveCount}</if>
  209. <if test="receiveAmount != null">AND receive_amount = #{receiveAmount}</if>
  210. <if test="userCount != null">AND user_count = #{userCount}</if>
  211. <if test="userBlacklistCount != null">AND user_blacklist_count = #{userBlacklistCount}</if>
  212. <if test="companyId != null">AND company_id = #{companyId}</if>
  213. <if test="companyName != null and companyName != ''">
  214. AND company_name LIKE concat('%', #{companyName}, '%')
  215. </if>
  216. <if test="createDate != null">AND create_date = #{createDate}</if>
  217. <!-- ✅ 新增时间筛选 -->
  218. <if test="beginTime != null and beginTime != ''">
  219. AND DATE(create_date) <![CDATA[ >= ]]> #{beginTime}
  220. </if>
  221. <if test="endTime != null and endTime != ''">
  222. AND DATE(create_date) <![CDATA[ <= ]]> #{endTime}
  223. </if>
  224. </where>
  225. group by company_id,DATE(create_date)
  226. <!-- 排序:合计行放最后 -->
  227. ORDER BY create_time DESC
  228. </select>
  229. </mapper>