LiveDataMapper.xml 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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.live.mapper.LiveDataMapper">
  6. <resultMap type="LiveData" id="LiveDataResult">
  7. <result property="liveId" column="live_id" />
  8. <result property="createTime" column="create_time" />
  9. <result property="pageViews" column="page_views" />
  10. <result property="uniqueVisitors" column="unique_visitors" />
  11. <result property="totalViews" column="total_views" />
  12. <result property="uniqueViewers" column="unique_viewers" />
  13. <result property="peakConcurrentViewers" column="peak_concurrent_viewers" />
  14. <result property="favouriteNum" column="favourite_num" />
  15. <result property="followNum" column="follow_num" />
  16. </resultMap>
  17. <sql id="selectLiveDataVo">
  18. SELECT favourite_num,follow_num,likes,live_id,page_views,peak_concurrent_viewers,unique_viewers,unique_visitors,total_views FROM live_data
  19. </sql>
  20. <select id="selectLiveDataList" parameterType="LiveData" resultMap="LiveDataResult">
  21. <include refid="selectLiveDataVo"/>
  22. <where>
  23. <if test="pageViews != null "> and page_views = #{pageViews}</if>
  24. <if test="uniqueVisitors != null "> and unique_visitors = #{uniqueVisitors}</if>
  25. <if test="totalViews != null "> and total_views = #{totalViews}</if>
  26. <if test="uniqueViewers != null "> and unique_viewers = #{uniqueViewers}</if>
  27. <if test="peakConcurrentViewers != null "> and peak_concurrent_viewers = #{peakConcurrentViewers}</if>
  28. <if test="favouriteNum != null "> and favourite_num = #{favouriteNum}</if>
  29. <if test="followNum != null "> and follow_num = #{followNum}</if>
  30. <if test="likes != null "> and likes = #{likes}</if>
  31. <if test="liveId != null "> and live_id = #{live_id}</if>
  32. </where>
  33. </select>
  34. <select id="selectLiveDataByLiveId" parameterType="Long" resultMap="LiveDataResult">
  35. <include refid="selectLiveDataVo"/>
  36. where live_id = #{liveId}
  37. </select>
  38. <select id="getAllLiveIds" resultType="java.lang.Long">
  39. SELECT B.live_id
  40. FROM live B
  41. where B.status = '2'
  42. </select>
  43. <select id="getAllLiveDatas" resultMap="LiveDataResult">
  44. SELECT A.favourite_num,A.follow_num,A.likes,A.live_id,A.page_views,
  45. A.peak_concurrent_viewers,A.unique_viewers,A.unique_visitors,A.total_views
  46. FROM live_data A
  47. inner join live B on A.live_id = B.live_id
  48. where B.status in (1,2,4) and b.is_audit = 1 and b.is_show = 1 and is_del=0
  49. </select>
  50. <select id="getRecentLive" resultType="com.fs.live.vo.RecentLiveDataVo">
  51. SELECT A.page_views AS pageViews,A.unique_visitors AS uniqueVisitors, B.live_id AS liveId,B.live_name AS liveName,B.`status` AS status,B.live_img_url AS liveImgUrl,B.start_time AS startTime
  52. FROM live_data A LEFT JOIN live B ON A.live_id = B.live_id ORDER BY B.start_time LIMIT 4
  53. </select>
  54. <select id="getCompanyRecentLive" resultType="com.fs.live.vo.RecentLiveDataVo">
  55. SELECT A.page_views AS pageViews,A.unique_visitors AS uniqueVisitors, B.live_id AS liveId,B.live_name AS liveName,B.`status` AS status,B.live_img_url AS liveImgUrl,B.start_time AS startTime
  56. FROM live_data A LEFT JOIN live B ON A.live_id = B.live_id
  57. <if test="companyId != null">
  58. where b.company_id = #{companyId}
  59. </if>
  60. ORDER BY B.start_time LIMIT 4
  61. </select>
  62. <select id="getLiveTop" resultType="com.fs.live.vo.RecentLiveDataVo">
  63. SELECT A.page_views AS pageViews,A.unique_visitors AS uniqueVisitors,A.total_views AS totalViews,A.unique_viewers AS uniqueViewers,A.peak_concurrent_viewers AS peakConcurrentViewers,
  64. B.live_name AS liveName,B.live_img_url AS liveImgUrl
  65. FROM live_data A LEFT JOIN live B ON A.live_id = B.live_id
  66. <choose>
  67. <when test="rankType == 'highFlow'">
  68. ORDER BY A.page_views DESC
  69. </when>
  70. <when test="rankType == 'highView'">
  71. ORDER BY A.total_views DESC
  72. </when>
  73. <when test="rankType == 'lowFlow'">
  74. ORDER BY A.page_views ASC
  75. </when>
  76. <when test="rankType == 'lowView'">
  77. ORDER BY A.total_views ASC
  78. </when>
  79. <otherwise>
  80. ORDER BY A.page_views DESC
  81. </otherwise>
  82. </choose>
  83. LIMIT 10
  84. </select>
  85. <select id="getCompanyLiveTop" resultType="com.fs.live.vo.RecentLiveDataVo">
  86. SELECT A.page_views AS pageViews,A.unique_visitors AS uniqueVisitors,A.total_views AS totalViews,A.unique_viewers AS uniqueViewers,A.peak_concurrent_viewers AS peakConcurrentViewers,
  87. B.live_name AS liveName,B.live_img_url AS liveImgUrl
  88. FROM live_data A LEFT JOIN live B ON A.live_id = B.live_id
  89. <if test="companyId != null">
  90. where b.company_id = #{companyId}
  91. </if>
  92. <choose>
  93. <when test="rankType == 'highFlow'">
  94. ORDER BY A.page_views DESC
  95. </when>
  96. <when test="rankType == 'highView'">
  97. ORDER BY A.total_views DESC
  98. </when>
  99. <when test="rankType == 'lowFlow'">
  100. ORDER BY A.page_views ASC
  101. </when>
  102. <when test="rankType == 'lowView'">
  103. ORDER BY A.total_views ASC
  104. </when>
  105. <otherwise>
  106. ORDER BY A.page_views DESC
  107. </otherwise>
  108. </choose>
  109. LIMIT 10
  110. </select>
  111. <select id="getCurrentData" resultType="com.fs.live.vo.TrendDataVO">
  112. SELECT
  113. SUM(ld.page_views) AS views,
  114. SUM(ld.unique_visitors) AS visitors,
  115. COUNT(l.live_id) AS streams,
  116. SUM(ld.total_views) AS pv,
  117. SUM(ld.unique_viewers) AS uv
  118. FROM live_data ld
  119. JOIN live l ON ld.live_id = l.live_id
  120. WHERE DATE(l.start_time) BETWEEN #{startDate} AND #{endDate};
  121. </select>
  122. <select id="getChartData" resultType="java.util.Map">
  123. SELECT
  124. CASE
  125. WHEN #{format} = 'day' THEN DATE(l.start_time)
  126. WHEN #{format} = 'week' THEN
  127. CONCAT(
  128. DATE_FORMAT(DATE_SUB(l.start_time, INTERVAL WEEKDAY(l.start_time) DAY), '%m/%d'),
  129. '-',
  130. DATE_FORMAT(DATE_ADD(l.start_time, INTERVAL (6 - WEEKDAY(l.start_time)) DAY), '%m/%d')
  131. )
  132. WHEN #{format} = 'month' THEN DATE_FORMAT(l.start_time, '%Y-%m')
  133. END AS date_range,
  134. <choose>
  135. <when test="category == 'streams'"> COUNT(l.live_id) </when>
  136. <otherwise> SUM(ld.${category}) </otherwise>
  137. </choose>
  138. AS views
  139. FROM live_data ld
  140. LEFT JOIN live l ON ld.live_id = l.live_id
  141. WHERE DATE(l.start_time) BETWEEN #{chartStartDate} AND #{chartEndDate}
  142. GROUP BY date_range
  143. ORDER BY date_range;
  144. </select>
  145. <select id="getPreviousData" resultType="com.fs.live.vo.TrendDataVO">
  146. SELECT
  147. SUM(ld.page_views) AS prevViews,
  148. SUM(ld.unique_visitors) AS prevVisitors,
  149. COUNT(l.live_id) AS prevStreams,
  150. SUM(ld.total_views) AS prevPv,
  151. SUM(ld.unique_viewers) AS prevUv
  152. FROM live_data ld
  153. LEFT JOIN live l ON ld.live_id = l.live_id
  154. WHERE DATE(l.start_time) BETWEEN #{prevStartDate} AND #{prevStartDate};
  155. </select>
  156. <select id="getCompanyCurrentData" resultType="com.fs.live.vo.TrendDataVO">
  157. SELECT
  158. SUM(ld.page_views) AS views,
  159. SUM(ld.unique_visitors) AS visitors,
  160. COUNT(l.live_id) AS streams,
  161. SUM(ld.total_views) AS pv,
  162. SUM(ld.unique_viewers) AS uv
  163. FROM live_data ld
  164. JOIN live l ON ld.live_id = l.live_id
  165. WHERE DATE(l.start_time) BETWEEN #{startDate} AND #{endDate}
  166. <if test="companyId != null">
  167. and l.company_id = #{companyId}
  168. </if>
  169. ;
  170. </select>
  171. <select id="getCompanyChartData" resultType="java.util.Map">
  172. SELECT
  173. CASE
  174. WHEN #{format} = 'day' THEN DATE(l.start_time)
  175. WHEN #{format} = 'week' THEN
  176. CONCAT(
  177. DATE_FORMAT(DATE_SUB(l.start_time, INTERVAL WEEKDAY(l.start_time) DAY), '%m/%d'),
  178. '-',
  179. DATE_FORMAT(DATE_ADD(l.start_time, INTERVAL (6 - WEEKDAY(l.start_time)) DAY), '%m/%d')
  180. )
  181. WHEN #{format} = 'month' THEN DATE_FORMAT(l.start_time, '%Y-%m')
  182. END AS date_range,
  183. <choose>
  184. <when test="category == 'streams'"> COUNT(l.live_id) </when>
  185. <otherwise> SUM(ld.${category}) </otherwise>
  186. </choose>
  187. AS views
  188. FROM live_data ld
  189. LEFT JOIN live l ON ld.live_id = l.live_id
  190. WHERE DATE(l.start_time) BETWEEN #{chartStartDate} AND #{chartEndDate}
  191. <if test="companyId != null">
  192. and l.company_id = #{companyId}
  193. </if>
  194. GROUP BY date_range
  195. ORDER BY date_range;
  196. </select>
  197. <select id="getCompanyPreviousData" resultType="com.fs.live.vo.TrendDataVO">
  198. SELECT
  199. SUM(ld.page_views) AS prevViews,
  200. SUM(ld.unique_visitors) AS prevVisitors,
  201. COUNT(l.live_id) AS prevStreams,
  202. SUM(ld.total_views) AS prevPv,
  203. SUM(ld.unique_viewers) AS prevUv
  204. FROM live_data ld
  205. LEFT JOIN live l ON ld.live_id = l.live_id
  206. WHERE DATE(l.start_time) BETWEEN #{prevStartDate} AND #{prevStartDate}
  207. <if test="companyId != null">
  208. and l.company_id = #{companyId}
  209. </if>
  210. ;
  211. </select>
  212. <insert id="insertLiveData" parameterType="LiveData">
  213. insert into live_data
  214. <trim prefix="(" suffix=")" suffixOverrides=",">
  215. <if test="liveId != null">live_id,</if>
  216. <if test="pageViews != null">page_views,</if>
  217. <if test="uniqueVisitors != null">unique_visitors,</if>
  218. <if test="totalViews != null">total_views,</if>
  219. <if test="uniqueViewers != null">unique_viewers,</if>
  220. <if test="peakConcurrentViewers != null">peak_concurrent_viewers,</if>
  221. </trim>
  222. <trim prefix="values (" suffix=")" suffixOverrides=",">
  223. <if test="liveId != null">#{liveId},</if>
  224. <if test="pageViews != null">#{pageViews},</if>
  225. <if test="uniqueVisitors != null">#{uniqueVisitors},</if>
  226. <if test="totalViews != null">#{totalViews},</if>
  227. <if test="uniqueViewers != null">#{uniqueViewers},</if>
  228. <if test="peakConcurrentViewers != null">#{peakConcurrentViewers},</if>
  229. </trim>
  230. </insert>
  231. <update id="updateLiveData" parameterType="LiveData">
  232. update live_data
  233. <trim prefix="SET" suffixOverrides=",">
  234. <if test="pageViews != null">page_views = #{pageViews},</if>
  235. <if test="uniqueVisitors != null">unique_visitors = #{uniqueVisitors},</if>
  236. <if test="totalViews != null">total_views = #{totalViews},</if>
  237. <if test="uniqueViewers != null">unique_viewers = #{uniqueViewers},</if>
  238. <if test="peakConcurrentViewers != null">peak_concurrent_viewers = #{peakConcurrentViewers},</if>
  239. <if test="likes != null">likes = #{likes},</if>
  240. <if test="favouriteNum != null">favourite_num = #{favouriteNum},</if>
  241. <if test="followNum != null">follow_num = #{followNum},</if>
  242. </trim>
  243. where live_id = #{liveId}
  244. </update>
  245. <delete id="deleteLiveDataByLiveId" parameterType="Long">
  246. delete from live_data where live_id = #{liveId}
  247. </delete>
  248. <delete id="deleteLiveDataByLiveIds" parameterType="String">
  249. delete from live_data where live_id in
  250. <foreach item="liveId" collection="array" open="(" separator="," close=")">
  251. #{liveId}
  252. </foreach>
  253. </delete>
  254. <update id="updateBatchLiveData">
  255. <foreach collection="liveDatas" item="liveData" separator=";">
  256. UPDATE live_data
  257. <set>
  258. <if test="liveData.pageViews != null">page_views = #{liveData.pageViews},</if>
  259. <if test="liveData.uniqueVisitors != null">unique_visitors = #{liveData.uniqueVisitors},</if>
  260. <if test="liveData.totalViews != null">total_views = #{liveData.totalViews},</if>
  261. <if test="liveData.uniqueViewers != null">unique_viewers = #{liveData.uniqueViewers},</if>
  262. <if test="liveData.peakConcurrentViewers != null">peak_concurrent_viewers = #{liveData.peakConcurrentViewers},</if>
  263. <if test="liveData.likes != null">likes = #{liveData.likes},</if>
  264. <if test="liveData.favouriteNum != null">favourite_num = #{liveData.favouriteNum},</if>
  265. <if test="liveData.followNum != null">follow_num = #{liveData.followNum},</if>
  266. </set>
  267. WHERE live_id = #{liveData.liveId}
  268. </foreach>
  269. </update>
  270. <!-- 查询直播间统计数据 -->
  271. <select id="selectLiveDataStatistics" resultType="com.fs.live.vo.LiveDataStatisticsVo">
  272. SELECT
  273. COUNT(DISTINCT lwu.user_id) AS totalViewers,
  274. COUNT(DISTINCT CASE WHEN l.live_type = 1 THEN lwu.user_id END) AS liveViewers,
  275. COUNT(DISTINCT CASE WHEN l.live_type = 3 THEN lwu.user_id END) AS playbackViewers,
  276. COALESCE(AVG(CASE WHEN l.live_type = 1 THEN lwu.online_seconds END), 0) AS liveAvgDuration,
  277. COALESCE(AVG(CASE WHEN l.live_type = 3 THEN lwu.online_seconds END), 0) AS playbackAvgDuration,
  278. COUNT(DISTINCT CASE
  279. WHEN lwu.online_seconds >= COALESCE(video_duration.total_duration, 0) AND video_duration.total_duration > 0
  280. THEN lwu.user_id
  281. END) AS totalCompletedCourses,
  282. COUNT(DISTINCT CASE
  283. WHEN l.live_type = 1 AND lwu.online_seconds >= COALESCE(video_duration.total_duration, 0) AND video_duration.total_duration > 0
  284. THEN lwu.user_id
  285. END) AS liveCompletedCourses,
  286. COUNT(DISTINCT CASE
  287. WHEN l.live_type = 3 AND lwu.online_seconds >= COALESCE(video_duration.total_duration, 0) AND video_duration.total_duration > 0
  288. THEN lwu.user_id
  289. END) AS playbackCompletedCourses,
  290. COALESCE((
  291. SELECT SUM(pay_price)
  292. FROM live_order
  293. WHERE live_id IN
  294. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  295. #{liveId}
  296. </foreach>
  297. AND is_pay = '1'
  298. ), 0) AS gmv,
  299. COALESCE((
  300. SELECT COUNT(DISTINCT user_id)
  301. FROM live_order
  302. WHERE live_id IN
  303. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  304. #{liveId}
  305. </foreach>
  306. AND is_pay = '1'
  307. ), 0) AS paidUsers,
  308. COALESCE((
  309. SELECT COUNT(DISTINCT order_id)
  310. FROM live_order
  311. WHERE live_id IN
  312. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  313. #{liveId}
  314. </foreach>
  315. AND is_pay = '1'
  316. ), 0) AS paidOrders,
  317. COALESCE((
  318. SELECT COUNT(DISTINCT order_id)
  319. FROM live_order
  320. WHERE live_id IN
  321. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  322. #{liveId}
  323. </foreach>
  324. AND is_pay = '1'
  325. ), 0) AS salesCount
  326. FROM live l
  327. LEFT JOIN live_watch_user lwu ON l.live_id = lwu.live_id
  328. LEFT JOIN (
  329. SELECT live_id, SUM(COALESCE(duration, 0)) AS total_duration
  330. FROM live_video
  331. WHERE video_type IN (1, 2)
  332. GROUP BY live_id
  333. ) video_duration ON l.live_id = video_duration.live_id
  334. WHERE l.live_id IN
  335. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  336. #{liveId}
  337. </foreach>
  338. </select>
  339. <!-- 查询直播间列表数据 -->
  340. <select id="selectLiveDataListByLiveIds" resultType="com.fs.live.vo.LiveDataListVo">
  341. SELECT
  342. l.live_id AS liveId,
  343. l.live_name AS liveName,
  344. l.live_type AS liveType,
  345. l.status AS status,
  346. l.start_time AS startTime,
  347. l.finish_time AS finishTime,
  348. COUNT(DISTINCT lwu.user_id) AS totalViewers,
  349. COUNT(DISTINCT CASE WHEN l.live_type = 1 THEN lwu.user_id END) AS liveViewers,
  350. COUNT(DISTINCT CASE WHEN l.live_type = 3 THEN lwu.user_id END) AS playbackViewers,
  351. COALESCE(AVG(CASE WHEN l.live_type = 1 THEN lwu.online_seconds END), 0) AS liveAvgDuration,
  352. COALESCE(AVG(CASE WHEN l.live_type = 3 THEN lwu.online_seconds END), 0) AS playbackAvgDuration,
  353. COUNT(DISTINCT CASE
  354. WHEN lwu.online_seconds >= COALESCE(video_duration.total_duration, 0) AND video_duration.total_duration > 0
  355. THEN lwu.user_id
  356. END) AS totalCompletedCourses,
  357. COUNT(DISTINCT CASE
  358. WHEN l.live_type = 1 AND lwu.online_seconds >= COALESCE(video_duration.total_duration, 0) AND video_duration.total_duration > 0
  359. THEN lwu.user_id
  360. END) AS liveCompletedCourses,
  361. COUNT(DISTINCT CASE
  362. WHEN l.live_type = 3 AND lwu.online_seconds >= COALESCE(video_duration.total_duration, 0) AND video_duration.total_duration > 0
  363. THEN lwu.user_id
  364. END) AS playbackCompletedCourses,
  365. COALESCE(order_stats.gmv, 0) AS gmv,
  366. COALESCE(order_stats.paidUsers, 0) AS paidUsers,
  367. COALESCE(order_stats.paidOrders, 0) AS paidOrders,
  368. COALESCE(order_stats.salesCount, 0) AS salesCount
  369. FROM live l
  370. LEFT JOIN live_watch_user lwu ON l.live_id = lwu.live_id
  371. LEFT JOIN (
  372. SELECT live_id, SUM(COALESCE(duration, 0)) AS total_duration
  373. FROM live_video
  374. WHERE video_type IN (1, 2)
  375. GROUP BY live_id
  376. ) video_duration ON l.live_id = video_duration.live_id
  377. LEFT JOIN (
  378. SELECT
  379. live_id,
  380. SUM(pay_price) AS gmv,
  381. COUNT(DISTINCT user_id) AS paidUsers,
  382. COUNT(DISTINCT order_id) AS paidOrders,
  383. COUNT(DISTINCT order_id) AS salesCount
  384. FROM live_order
  385. WHERE is_pay = '1'
  386. GROUP BY live_id
  387. ) order_stats ON l.live_id = order_stats.live_id
  388. WHERE l.live_id IN
  389. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  390. #{liveId}
  391. </foreach>
  392. GROUP BY l.live_id, l.live_name, l.live_type, l.status, l.start_time, l.finish_time,
  393. order_stats.gmv, order_stats.paidUsers, order_stats.paidOrders, order_stats.salesCount
  394. ORDER BY l.start_time DESC
  395. </select>
  396. </mapper>