LiveDataMapper.xml 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. <!-- 首页推荐:正在直播的直播间,仅查 liveId、liveType、封面、标题 -->
  51. <select id="selectLivingLivesForApp" resultType="com.fs.live.vo.LiveAppSimpleVO">
  52. SELECT live_id AS liveId, live_type AS liveType, live_img_url AS liveImgUrl, live_name AS liveName
  53. FROM live
  54. WHERE status = 2 AND is_show = 1 AND (is_del = 0 OR is_del IS NULL) AND is_audit = 1
  55. ORDER BY start_time DESC
  56. </select>
  57. <select id="getRecentLive" resultType="com.fs.live.vo.RecentLiveDataVo">
  58. 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
  59. FROM live_data A LEFT JOIN live B ON A.live_id = B.live_id ORDER BY B.start_time LIMIT 4
  60. </select>
  61. <select id="getCompanyRecentLive" resultType="com.fs.live.vo.RecentLiveDataVo">
  62. 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
  63. FROM live_data A LEFT JOIN live B ON A.live_id = B.live_id
  64. <if test="companyId != null">
  65. where b.company_id = #{companyId}
  66. </if>
  67. ORDER BY B.start_time LIMIT 4
  68. </select>
  69. <select id="getLiveTop" resultType="com.fs.live.vo.RecentLiveDataVo">
  70. 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,
  71. B.live_name AS liveName,B.live_img_url AS liveImgUrl
  72. FROM live_data A LEFT JOIN live B ON A.live_id = B.live_id
  73. <choose>
  74. <when test="rankType == 'highFlow'">
  75. ORDER BY A.page_views DESC
  76. </when>
  77. <when test="rankType == 'highView'">
  78. ORDER BY A.total_views DESC
  79. </when>
  80. <when test="rankType == 'lowFlow'">
  81. ORDER BY A.page_views ASC
  82. </when>
  83. <when test="rankType == 'lowView'">
  84. ORDER BY A.total_views ASC
  85. </when>
  86. <otherwise>
  87. ORDER BY A.page_views DESC
  88. </otherwise>
  89. </choose>
  90. LIMIT 10
  91. </select>
  92. <select id="getCompanyLiveTop" resultType="com.fs.live.vo.RecentLiveDataVo">
  93. 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,
  94. B.live_name AS liveName,B.live_img_url AS liveImgUrl
  95. FROM live_data A LEFT JOIN live B ON A.live_id = B.live_id
  96. <if test="companyId != null">
  97. where b.company_id = #{companyId}
  98. </if>
  99. <choose>
  100. <when test="rankType == 'highFlow'">
  101. ORDER BY A.page_views DESC
  102. </when>
  103. <when test="rankType == 'highView'">
  104. ORDER BY A.total_views DESC
  105. </when>
  106. <when test="rankType == 'lowFlow'">
  107. ORDER BY A.page_views ASC
  108. </when>
  109. <when test="rankType == 'lowView'">
  110. ORDER BY A.total_views ASC
  111. </when>
  112. <otherwise>
  113. ORDER BY A.page_views DESC
  114. </otherwise>
  115. </choose>
  116. LIMIT 10
  117. </select>
  118. <select id="getCurrentData" resultType="com.fs.live.vo.TrendDataVO">
  119. SELECT
  120. SUM(ld.page_views) AS views,
  121. SUM(ld.unique_visitors) AS visitors,
  122. COUNT(l.live_id) AS streams,
  123. SUM(ld.total_views) AS pv,
  124. SUM(ld.unique_viewers) AS uv
  125. FROM live_data ld
  126. JOIN live l ON ld.live_id = l.live_id
  127. WHERE DATE(l.start_time) BETWEEN #{startDate} AND #{endDate};
  128. </select>
  129. <select id="getChartData" resultType="java.util.Map">
  130. SELECT
  131. CASE
  132. WHEN #{format} = 'day' THEN DATE(l.start_time)
  133. WHEN #{format} = 'week' THEN
  134. CONCAT(
  135. DATE_FORMAT(DATE_SUB(l.start_time, INTERVAL WEEKDAY(l.start_time) DAY), '%m/%d'),
  136. '-',
  137. DATE_FORMAT(DATE_ADD(l.start_time, INTERVAL (6 - WEEKDAY(l.start_time)) DAY), '%m/%d')
  138. )
  139. WHEN #{format} = 'month' THEN DATE_FORMAT(l.start_time, '%Y-%m')
  140. END AS date_range,
  141. <choose>
  142. <when test="category == 'streams'"> COUNT(l.live_id) </when>
  143. <otherwise> SUM(ld.${category}) </otherwise>
  144. </choose>
  145. AS views
  146. FROM live_data ld
  147. LEFT JOIN live l ON ld.live_id = l.live_id
  148. WHERE DATE(l.start_time) BETWEEN #{chartStartDate} AND #{chartEndDate}
  149. GROUP BY date_range
  150. ORDER BY date_range;
  151. </select>
  152. <select id="getPreviousData" resultType="com.fs.live.vo.TrendDataVO">
  153. SELECT
  154. SUM(ld.page_views) AS prevViews,
  155. SUM(ld.unique_visitors) AS prevVisitors,
  156. COUNT(l.live_id) AS prevStreams,
  157. SUM(ld.total_views) AS prevPv,
  158. SUM(ld.unique_viewers) AS prevUv
  159. FROM live_data ld
  160. LEFT JOIN live l ON ld.live_id = l.live_id
  161. WHERE DATE(l.start_time) BETWEEN #{prevStartDate} AND #{prevStartDate};
  162. </select>
  163. <select id="getCompanyCurrentData" resultType="com.fs.live.vo.TrendDataVO">
  164. SELECT
  165. SUM(ld.page_views) AS views,
  166. SUM(ld.unique_visitors) AS visitors,
  167. COUNT(l.live_id) AS streams,
  168. SUM(ld.total_views) AS pv,
  169. SUM(ld.unique_viewers) AS uv
  170. FROM live_data ld
  171. JOIN live l ON ld.live_id = l.live_id
  172. WHERE DATE(l.start_time) BETWEEN #{startDate} AND #{endDate}
  173. <if test="companyId != null">
  174. and l.company_id = #{companyId}
  175. </if>
  176. ;
  177. </select>
  178. <select id="getCompanyChartData" resultType="java.util.Map">
  179. SELECT
  180. CASE
  181. WHEN #{format} = 'day' THEN DATE(l.start_time)
  182. WHEN #{format} = 'week' THEN
  183. CONCAT(
  184. DATE_FORMAT(DATE_SUB(l.start_time, INTERVAL WEEKDAY(l.start_time) DAY), '%m/%d'),
  185. '-',
  186. DATE_FORMAT(DATE_ADD(l.start_time, INTERVAL (6 - WEEKDAY(l.start_time)) DAY), '%m/%d')
  187. )
  188. WHEN #{format} = 'month' THEN DATE_FORMAT(l.start_time, '%Y-%m')
  189. END AS date_range,
  190. <choose>
  191. <when test="category == 'streams'"> COUNT(l.live_id) </when>
  192. <otherwise> SUM(ld.${category}) </otherwise>
  193. </choose>
  194. AS views
  195. FROM live_data ld
  196. LEFT JOIN live l ON ld.live_id = l.live_id
  197. WHERE DATE(l.start_time) BETWEEN #{chartStartDate} AND #{chartEndDate}
  198. <if test="companyId != null">
  199. and l.company_id = #{companyId}
  200. </if>
  201. GROUP BY date_range
  202. ORDER BY date_range;
  203. </select>
  204. <select id="getCompanyPreviousData" resultType="com.fs.live.vo.TrendDataVO">
  205. SELECT
  206. SUM(ld.page_views) AS prevViews,
  207. SUM(ld.unique_visitors) AS prevVisitors,
  208. COUNT(l.live_id) AS prevStreams,
  209. SUM(ld.total_views) AS prevPv,
  210. SUM(ld.unique_viewers) AS prevUv
  211. FROM live_data ld
  212. LEFT JOIN live l ON ld.live_id = l.live_id
  213. WHERE DATE(l.start_time) BETWEEN #{prevStartDate} AND #{prevStartDate}
  214. <if test="companyId != null">
  215. and l.company_id = #{companyId}
  216. </if>
  217. ;
  218. </select>
  219. <insert id="insertLiveData" parameterType="LiveData">
  220. insert into live_data
  221. <trim prefix="(" suffix=")" suffixOverrides=",">
  222. <if test="liveId != null">live_id,</if>
  223. <if test="pageViews != null">page_views,</if>
  224. <if test="uniqueVisitors != null">unique_visitors,</if>
  225. <if test="totalViews != null">total_views,</if>
  226. <if test="uniqueViewers != null">unique_viewers,</if>
  227. <if test="peakConcurrentViewers != null">peak_concurrent_viewers,</if>
  228. </trim>
  229. <trim prefix="values (" suffix=")" suffixOverrides=",">
  230. <if test="liveId != null">#{liveId},</if>
  231. <if test="pageViews != null">#{pageViews},</if>
  232. <if test="uniqueVisitors != null">#{uniqueVisitors},</if>
  233. <if test="totalViews != null">#{totalViews},</if>
  234. <if test="uniqueViewers != null">#{uniqueViewers},</if>
  235. <if test="peakConcurrentViewers != null">#{peakConcurrentViewers},</if>
  236. </trim>
  237. </insert>
  238. <update id="updateLiveData" parameterType="LiveData">
  239. update live_data
  240. <trim prefix="SET" suffixOverrides=",">
  241. <if test="pageViews != null">page_views = #{pageViews},</if>
  242. <if test="uniqueVisitors != null">unique_visitors = #{uniqueVisitors},</if>
  243. <if test="totalViews != null">total_views = #{totalViews},</if>
  244. <if test="uniqueViewers != null">unique_viewers = #{uniqueViewers},</if>
  245. <if test="peakConcurrentViewers != null">peak_concurrent_viewers = #{peakConcurrentViewers},</if>
  246. <if test="likes != null">likes = #{likes},</if>
  247. <if test="favouriteNum != null">favourite_num = #{favouriteNum},</if>
  248. <if test="followNum != null">follow_num = #{followNum},</if>
  249. </trim>
  250. where live_id = #{liveId}
  251. </update>
  252. <delete id="deleteLiveDataByLiveId" parameterType="Long">
  253. delete from live_data where live_id = #{liveId}
  254. </delete>
  255. <delete id="deleteLiveDataByLiveIds" parameterType="String">
  256. delete from live_data where live_id in
  257. <foreach item="liveId" collection="array" open="(" separator="," close=")">
  258. #{liveId}
  259. </foreach>
  260. </delete>
  261. <update id="updateBatchLiveData">
  262. <foreach collection="liveDatas" item="liveData" separator=";">
  263. UPDATE live_data
  264. <set>
  265. <if test="liveData.pageViews != null">page_views = #{liveData.pageViews},</if>
  266. <if test="liveData.uniqueVisitors != null">unique_visitors = #{liveData.uniqueVisitors},</if>
  267. <if test="liveData.totalViews != null">total_views = #{liveData.totalViews},</if>
  268. <if test="liveData.uniqueViewers != null">unique_viewers = #{liveData.uniqueViewers},</if>
  269. <if test="liveData.peakConcurrentViewers != null">peak_concurrent_viewers = #{liveData.peakConcurrentViewers},</if>
  270. <if test="liveData.likes != null">likes = #{liveData.likes},</if>
  271. <if test="liveData.favouriteNum != null">favourite_num = #{liveData.favouriteNum},</if>
  272. <if test="liveData.followNum != null">follow_num = #{liveData.followNum},</if>
  273. </set>
  274. WHERE live_id = #{liveData.liveId}
  275. </foreach>
  276. </update>
  277. <!-- 查询直播间统计数据 -->
  278. <select id="selectLiveDataStatistics" resultType="com.fs.live.vo.LiveDataStatisticsVo">
  279. SELECT
  280. COUNT( distinct lwu.user_id) AS totalViewers,
  281. COUNT( CASE WHEN lwu.live_flag = 1 and lwu.replay_flag = 0 THEN lwu.user_id END) AS liveViewers,
  282. COUNT( CASE WHEN lwu.live_flag = 0 and lwu.replay_flag = 1 THEN lwu.user_id END) AS playbackViewers,
  283. COALESCE(AVG(CASE WHEN lwu.live_flag = 1 and lwu.replay_flag = 0 THEN lwu.online_seconds END), 0) AS liveAvgDuration,
  284. COALESCE(AVG(CASE WHEN lwu.live_flag = 0 and lwu.replay_flag = 1 THEN lwu.online_seconds END), 0) AS playbackAvgDuration,
  285. COUNT( CASE
  286. WHEN lwu.online_seconds >= COALESCE(video_duration.total_duration, 0) AND video_duration.total_duration > 0
  287. THEN lwu.user_id
  288. END) AS totalCompletedCourses,
  289. COUNT( CASE
  290. WHEN lwu.live_flag = 1 and lwu.replay_flag = 0 AND lwu.online_seconds >= COALESCE(video_duration.total_duration, 0) AND video_duration.total_duration > 0
  291. THEN lwu.user_id
  292. END) AS liveCompletedCourses,
  293. COUNT( CASE
  294. WHEN lwu.live_flag = 0 and lwu.replay_flag = 1 AND lwu.online_seconds >= COALESCE(video_duration.total_duration, 0) AND video_duration.total_duration > 0
  295. THEN lwu.user_id
  296. END) AS playbackCompletedCourses,
  297. COALESCE((
  298. SELECT SUM( pay_price)
  299. FROM fs_store_order_scrm
  300. WHERE order_type = 2
  301. AND remark IN
  302. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  303. #{liveId}
  304. </foreach>
  305. AND (paid = 1 OR paid = '1')
  306. ), 0) AS gmv,
  307. COALESCE((
  308. select sum(acs.paid) from (SELECT COUNT(DISTINCT user_id) as paid
  309. FROM fs_store_order_scrm
  310. WHERE order_type = 2
  311. AND remark IN
  312. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  313. #{liveId}
  314. </foreach>
  315. AND (paid = 1 OR paid = '1')
  316. group by remark
  317. ) acs
  318. ), 0) AS paidUsers,
  319. COALESCE((
  320. SELECT COUNT(DISTINCT id)
  321. FROM fs_store_order_scrm
  322. WHERE order_type = 2
  323. AND remark IN
  324. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  325. #{liveId}
  326. </foreach>
  327. AND (paid = 1 OR paid = '1')
  328. ), 0) AS paidOrders,
  329. COALESCE((
  330. SELECT COUNT(DISTINCT id)
  331. FROM fs_store_order_scrm
  332. WHERE order_type = 2
  333. AND remark IN
  334. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  335. #{liveId}
  336. </foreach>
  337. ), 0) AS salesCount
  338. FROM live l
  339. LEFT JOIN live_watch_user lwu ON l.live_id = lwu.live_id
  340. LEFT JOIN (
  341. SELECT live_id, SUM(COALESCE(duration, 0)) AS total_duration
  342. FROM live_video
  343. WHERE video_type IN (1, 2)
  344. GROUP BY live_id
  345. ) video_duration ON l.live_id = video_duration.live_id
  346. WHERE l.live_id IN
  347. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  348. #{liveId}
  349. </foreach>
  350. </select>
  351. <!-- 查询直播间列表数据 -->
  352. <select id="selectLiveDataListByLiveIds" resultType="com.fs.live.vo.LiveDataListVo">
  353. SELECT
  354. l.live_id AS liveId,
  355. l.live_name AS liveName,
  356. l.live_type AS liveType,
  357. l.status AS status,
  358. l.start_time AS startTime,
  359. l.finish_time AS finishTime,
  360. COUNT(distinct lwu.user_id) AS totalViewers,
  361. COUNT(CASE WHEN lwu.live_flag = 1 and lwu.replay_flag = 0 THEN lwu.user_id END) AS liveViewers,
  362. COUNT(CASE WHEN lwu.live_flag = 0 and lwu.replay_flag = 1 THEN lwu.user_id END) AS playbackViewers,
  363. COALESCE(AVG(CASE WHEN lwu.live_flag = 1 and lwu.replay_flag = 0 THEN lwu.online_seconds END), 0) AS liveAvgDuration,
  364. COALESCE(AVG(CASE WHEN lwu.live_flag = 0 and lwu.replay_flag = 1 THEN lwu.online_seconds END), 0) AS playbackAvgDuration,
  365. COUNT(CASE
  366. WHEN lwu.online_seconds >= COALESCE(video_duration.total_duration, 0) AND video_duration.total_duration > 0
  367. THEN lwu.user_id
  368. END) AS totalCompletedCourses,
  369. COUNT(CASE
  370. WHEN lwu.live_flag = 1 and lwu.replay_flag = 0 AND lwu.online_seconds >= COALESCE(video_duration.total_duration, 0) AND video_duration.total_duration > 0
  371. THEN lwu.user_id
  372. END) AS liveCompletedCourses,
  373. COUNT(CASE
  374. WHEN lwu.live_flag = 0 and lwu.replay_flag = 1 AND lwu.online_seconds >= COALESCE(video_duration.total_duration, 0) AND video_duration.total_duration > 0
  375. THEN lwu.user_id
  376. END) AS playbackCompletedCourses,
  377. COALESCE(order_stats.gmv, 0) AS gmv,
  378. COALESCE(order_stats.paidUsers, 0) AS paidUsers,
  379. COALESCE(order_stats.paidOrders, 0) AS paidOrders,
  380. COALESCE(order_stats.salesCount, 0) AS salesCount
  381. FROM live l
  382. LEFT JOIN live_watch_user lwu ON l.live_id = lwu.live_id
  383. LEFT JOIN (
  384. SELECT live_id, SUM(COALESCE(duration, 0)) AS total_duration
  385. FROM live_video
  386. WHERE video_type IN (1, 2)
  387. GROUP BY live_id
  388. ) video_duration ON l.live_id = video_duration.live_id
  389. LEFT JOIN (
  390. SELECT
  391. remark AS live_id,
  392. SUM( case when (paid = 1 OR paid = '1') then pay_price else 0 end) AS gmv,
  393. COUNT(distinct CASE WHEN (paid = 1 OR paid = '1') THEN user_id END) AS paidUsers,
  394. sum(CASE WHEN (paid = 1 OR paid = '1') THEN 1 else 0 END) AS paidOrders,
  395. COUNT(DISTINCT id) AS salesCount
  396. FROM fs_store_order_scrm
  397. WHERE order_type = 2
  398. AND remark IN
  399. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  400. #{liveId}
  401. </foreach>
  402. GROUP BY remark
  403. ) order_stats ON l.live_id = order_stats.live_id
  404. WHERE l.live_id IN
  405. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  406. #{liveId}
  407. </foreach>
  408. GROUP BY l.live_id, l.live_name, l.live_type, l.status, l.start_time, l.finish_time,
  409. order_stats.gmv, order_stats.paidUsers, order_stats.paidOrders, order_stats.salesCount
  410. ORDER BY l.start_time DESC
  411. </select>
  412. <!-- 查询直播间详情数据(SQL方式) -->
  413. <select id="selectLiveDataDetailBySql" resultType="com.fs.live.vo.LiveDataDetailVo">
  414. SELECT
  415. COALESCE(video_duration.total_duration, 0) AS videoDuration,
  416. COUNT(DISTINCT lwu.user_id) AS totalViewers,
  417. COUNT(DISTINCT CASE
  418. WHEN COALESCE(user_duration.total_duration, 0) >= 1200
  419. THEN lwu.user_id
  420. END) AS totalCompletedCourses,
  421. CASE
  422. WHEN COUNT(DISTINCT lwu.user_id) > 0 THEN
  423. ROUND(COUNT(DISTINCT CASE
  424. WHEN COALESCE(user_duration.total_duration, 0) >= 1200
  425. THEN lwu.user_id
  426. END) * 100.0 / COUNT(DISTINCT lwu.user_id), 2)
  427. ELSE 0
  428. END AS totalCompletionRate,
  429. COUNT(DISTINCT CASE WHEN lwu.live_flag = 1 AND lwu.replay_flag = 0 and lwu.online_seconds > 0 THEN lwu.user_id END) AS liveViewers,
  430. COUNT(DISTINCT CASE WHEN lwu.live_flag = 1 AND lwu.replay_flag = 0 AND lwu.online_seconds >= 1200 THEN lwu.user_id END) AS liveOver20Minutes,
  431. COUNT(DISTINCT CASE WHEN lwu.live_flag = 1 AND lwu.replay_flag = 0 AND lwu.online_seconds >= 1800 THEN lwu.user_id END) AS liveOver30Minutes,
  432. CASE
  433. WHEN COUNT(DISTINCT CASE WHEN lwu.live_flag = 1 AND lwu.replay_flag = 0 THEN lwu.user_id END) > 0 THEN
  434. ROUND(COUNT(DISTINCT CASE WHEN lwu.live_flag = 1 AND lwu.replay_flag = 0 AND lwu.online_seconds >= 1200 THEN lwu.user_id END) * 100.0 / COUNT(DISTINCT CASE WHEN lwu.live_flag = 1 AND lwu.replay_flag = 0 and lwu.online_seconds > 0 THEN lwu.user_id END), 2)
  435. ELSE 0
  436. END AS liveCompletionRate20,
  437. CASE
  438. WHEN COUNT(DISTINCT CASE WHEN lwu.live_flag = 1 AND lwu.replay_flag = 0 THEN lwu.user_id END) > 0 THEN
  439. ROUND(COUNT(DISTINCT CASE WHEN lwu.live_flag = 1 AND lwu.replay_flag = 0 AND lwu.online_seconds >= 1800 THEN lwu.user_id END) * 100.0 / COUNT(DISTINCT CASE WHEN lwu.live_flag = 1 AND lwu.replay_flag = 0 and lwu.online_seconds > 0 THEN lwu.user_id END), 2)
  440. ELSE 0
  441. END AS liveCompletionRate30,
  442. COUNT(DISTINCT CASE WHEN lwu.live_flag = 0 AND lwu.replay_flag = 1 and lwu.online_seconds > 0 THEN lwu.user_id END) AS playbackViewers,
  443. COUNT(DISTINCT CASE WHEN lwu.live_flag = 0 AND lwu.replay_flag = 1 AND lwu.online_seconds >= 1200 THEN lwu.user_id END) AS playbackOver20Minutes,
  444. COUNT(DISTINCT CASE WHEN lwu.live_flag = 0 AND lwu.replay_flag = 1 AND lwu.online_seconds >= 1800 THEN lwu.user_id END) AS playbackOver30Minutes,
  445. CASE
  446. WHEN COUNT(DISTINCT CASE WHEN lwu.live_flag = 0 AND lwu.replay_flag = 1 THEN lwu.user_id END) > 0 THEN
  447. ROUND(COUNT(DISTINCT CASE WHEN lwu.live_flag = 0 AND lwu.replay_flag = 1 AND lwu.online_seconds >= 1200 THEN lwu.user_id END) * 100.0 / COUNT(DISTINCT CASE WHEN lwu.live_flag = 0 AND lwu.replay_flag = 1 and lwu.online_seconds > 0 THEN lwu.user_id END), 2)
  448. ELSE 0
  449. END AS playbackCompletionRate20,
  450. CASE
  451. WHEN COUNT(DISTINCT CASE WHEN lwu.live_flag = 0 AND lwu.replay_flag = 1 THEN lwu.user_id END) > 0 THEN
  452. ROUND(COUNT(DISTINCT CASE WHEN lwu.live_flag = 0 AND lwu.replay_flag = 1 AND lwu.online_seconds >= 1800 THEN lwu.user_id END) * 100.0 / COUNT(DISTINCT CASE WHEN lwu.live_flag = 0 AND lwu.replay_flag = 1 and lwu.online_seconds > 0 THEN lwu.user_id END), 2)
  453. ELSE 0
  454. END AS playbackCompletionRate30,
  455. COALESCE(ld.peak_concurrent_viewers, 0) AS livePeak,
  456. COALESCE(AVG(CASE WHEN lwu.live_flag = 1 AND lwu.replay_flag = 0 THEN lwu.online_seconds END), 0) AS liveAvgDuration,
  457. COALESCE(AVG(CASE WHEN lwu.live_flag = 0 AND lwu.replay_flag = 1 THEN lwu.online_seconds END), 0) AS playbackAvgDuration,
  458. CASE
  459. WHEN COALESCE(video_duration.total_duration, 0) > 0 THEN
  460. ROUND(COALESCE(AVG(CASE WHEN lwu.live_flag = 0 AND lwu.replay_flag = 1 THEN lwu.online_seconds END), 0) * 100.0 / video_duration.total_duration, 2)
  461. ELSE 0
  462. END AS playbackFinishRate,
  463. COALESCE(order_stats.gmv, 0) AS gmv,
  464. COALESCE(order_stats.paidUsers, 0) AS paidUsers,
  465. COALESCE(order_stats.paidOrders, 0) AS paidOrders,
  466. CASE
  467. WHEN COALESCE(ld.peak_concurrent_viewers, 0) > 0 THEN
  468. ROUND(order_stats.paidUsers * 100.0 / ld.peak_concurrent_viewers, 2)
  469. ELSE 0
  470. END AS peakConversionRate,
  471. CASE
  472. WHEN COUNT(DISTINCT lwu.user_id) > 0 THEN
  473. ROUND(order_stats.paidUsers * 100.0 / COUNT(DISTINCT lwu.user_id), 2)
  474. ELSE 0
  475. END AS totalViewerConversionRate,
  476. CASE
  477. WHEN COUNT(DISTINCT CASE
  478. WHEN COALESCE(user_duration.total_duration, 0) >= 1800
  479. THEN lwu.user_id
  480. END) > 0 THEN
  481. ROUND(order_stats.paidUsers * 100.0 / COUNT(DISTINCT CASE
  482. WHEN COALESCE(user_duration.total_duration, 0) >= 1800
  483. THEN lwu.user_id
  484. END), 2)
  485. ELSE 0
  486. END AS completion30MinConversionRate,
  487. CASE
  488. WHEN COALESCE(ld.peak_concurrent_viewers, 0) > 0 THEN
  489. ROUND(order_stats.gmv / ld.peak_concurrent_viewers, 2)
  490. ELSE 0
  491. END AS peakRValue,
  492. CASE
  493. WHEN COUNT(DISTINCT CASE
  494. WHEN COALESCE(user_duration.total_duration, 0) >= 1800
  495. THEN lwu.user_id
  496. END) > 0 THEN
  497. ROUND(order_stats.gmv / COUNT(DISTINCT CASE
  498. WHEN COALESCE(user_duration.total_duration, 0) >= 1800
  499. THEN lwu.user_id
  500. END), 2)
  501. ELSE 0
  502. END AS completionRValue
  503. FROM live l
  504. LEFT JOIN live_data ld ON l.live_id = ld.live_id
  505. LEFT JOIN live_watch_user lwu ON l.live_id = lwu.live_id
  506. LEFT JOIN (
  507. SELECT live_id, SUM(COALESCE(duration, 0)) AS total_duration
  508. FROM live_video
  509. WHERE video_type IN (1, 2)
  510. GROUP BY live_id
  511. ) video_duration ON l.live_id = video_duration.live_id
  512. LEFT JOIN (
  513. SELECT
  514. live_id,
  515. user_id,
  516. COALESCE(SUM(CASE WHEN live_flag = 1 AND replay_flag = 0 THEN COALESCE(online_seconds, 0) ELSE 0 END), 0) +
  517. COALESCE(SUM(CASE WHEN live_flag = 0 AND replay_flag = 1 THEN COALESCE(online_seconds, 0) ELSE 0 END), 0) AS total_duration
  518. FROM live_watch_user
  519. WHERE live_id = #{liveId}
  520. GROUP BY live_id, user_id
  521. ) user_duration ON l.live_id = user_duration.live_id AND lwu.user_id = user_duration.user_id
  522. LEFT JOIN (
  523. SELECT
  524. remark AS live_id,
  525. SUM(CASE WHEN (paid = 1 OR paid = '1') THEN pay_price ELSE 0 END) AS gmv,
  526. COUNT(DISTINCT CASE WHEN (paid = 1 OR paid = '1') THEN user_id END) AS paidUsers,
  527. SUM(CASE WHEN (paid = 1 OR paid = '1') THEN 1 ELSE 0 END) AS paidOrders
  528. FROM fs_store_order_scrm
  529. WHERE order_type = 2 AND remark = #{liveId}
  530. GROUP BY remark
  531. ) order_stats ON l.live_id = order_stats.live_id
  532. WHERE l.live_id = #{liveId}
  533. </select>
  534. <!-- 查询直播间用户详情列表(SQL方式) -->
  535. <select id="selectLiveUserDetailListBySql" resultType="com.fs.live.vo.LiveUserDetailVo">
  536. SELECT
  537. u.user_id AS userId,
  538. COALESCE(u.nickname,u.nick_name, '未知用户') AS userName,
  539. COALESCE(SUM(CASE WHEN lwu.live_flag = 1 AND lwu.replay_flag = 0 THEN lwu.online_seconds ELSE 0 END), 0) AS liveWatchDuration,
  540. COALESCE(SUM(CASE WHEN lwu.live_flag = 0 AND lwu.replay_flag = 1 THEN lwu.online_seconds ELSE 0 END), 0) AS playbackWatchDuration,
  541. COALESCE(order_info.orderCount, 0) AS orderCount,
  542. COALESCE(order_info.orderAmount, 0) AS orderAmount,
  543. COALESCE(c.company_name, '') AS companyName,
  544. COALESCE(cu.user_name, '') AS salesName
  545. FROM live_watch_user lwu
  546. LEFT JOIN fs_user u ON lwu.user_id = u.user_id
  547. LEFT JOIN (
  548. SELECT
  549. user_id,
  550. COUNT(DISTINCT id) AS orderCount,
  551. SUM(CASE WHEN (paid = 1 OR paid = '1') THEN pay_price ELSE 0 END) AS orderAmount
  552. FROM fs_store_order_scrm
  553. WHERE order_type = 2 AND remark = #{liveId} AND user_id IS NOT NULL
  554. GROUP BY user_id
  555. ) order_info ON lwu.user_id = order_info.user_id
  556. left join live_user_first_entry lufe on lwu.live_id = lufe.live_id and lwu.user_id = lufe.user_id
  557. LEFT JOIN company c ON lufe.company_id = c.company_id
  558. LEFT JOIN company_user cu ON lufe.company_user_id = cu.user_id
  559. WHERE lwu.live_id = #{liveId}
  560. <if test="companyId != null">
  561. and lufe.company_id = #{companyId}
  562. </if>
  563. <if test="companyUserId != null">
  564. and lufe.company_user_id = #{companyUserId}
  565. </if>
  566. GROUP BY u.user_id, u.nick_name, u.nickname, order_info.orderCount, order_info.orderAmount, c.company_name, cu.user_name
  567. ORDER BY order_info.orderAmount DESC, liveWatchDuration DESC
  568. </select>
  569. <!-- 查询满足条件的直播间ID -->
  570. <select id="selectLiveIdsByCompanyParam" parameterType="com.fs.live.param.LiveDataCompanyParam" resultType="java.lang.Long">
  571. SELECT distinct l.live_id
  572. FROM live l
  573. LEFT JOIN company c ON l.company_id = c.company_id
  574. WHERE l.is_del = 0
  575. AND l.is_audit = 1
  576. AND l.is_show = 1
  577. <if test="startDate != null">
  578. AND l.start_time &gt;= #{startDate}
  579. </if>
  580. <if test="endDate != null">
  581. AND l.start_time &lt;= #{endDate}
  582. </if>
  583. ORDER BY l.live_id
  584. </select>
  585. <!-- 分公司总到课人数 -->
  586. <select id="selectAttendanceCountByCompany" resultType="com.fs.live.vo.LiveDataCompanyVO">
  587. SELECT
  588. lufe.company_id AS companyId,
  589. COALESCE(c.company_name, '总台') AS companyName,
  590. COALESCE(COUNT(DISTINCT lwu.user_id), 0) AS totalAttendanceCount
  591. FROM live_watch_user lwu
  592. LEFT JOIN live_user_first_entry lufe ON lwu.live_id = lufe.live_id AND lwu.user_id = lufe.user_id
  593. LEFT JOIN company c ON lufe.company_id = c.company_id
  594. WHERE lwu.live_id IN
  595. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  596. #{liveId}
  597. </foreach>
  598. <if test="companyIds != null and companyIds.size() > 0">
  599. AND lufe.company_id IN
  600. <foreach collection="companyIds" item="cid" open="(" separator="," close=")">
  601. #{cid}
  602. </foreach>
  603. </if>
  604. GROUP BY lufe.company_id, c.company_name
  605. </select>
  606. <!-- 分公司总完课人数 -->
  607. <select id="selectCompleteCountByCompany" resultType="com.fs.live.vo.LiveDataCompanyVO">
  608. SELECT
  609. lufe.company_id AS companyId,
  610. COALESCE(c.company_name, '总台') AS companyName,
  611. COALESCE(COUNT(DISTINCT CASE
  612. WHEN lwu.online_seconds >= 1200
  613. <!-- AND lwu.online_seconds >= COALESCE(vd.total_duration, 0)-->
  614. AND COALESCE(vd.total_duration, 0) > 0
  615. THEN lwu.user_id END), 0) AS totalCompleteCount
  616. FROM live_watch_user lwu
  617. LEFT JOIN live_user_first_entry lufe ON lwu.live_id = lufe.live_id AND lwu.user_id = lufe.user_id
  618. LEFT JOIN company c ON lufe.company_id = c.company_id
  619. LEFT JOIN (
  620. SELECT live_id, SUM(COALESCE(duration, 0)) AS total_duration
  621. FROM live_video
  622. WHERE video_type IN (1, 2)
  623. GROUP BY live_id
  624. ) vd ON lwu.live_id = vd.live_id
  625. WHERE lwu.live_id IN
  626. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  627. #{liveId}
  628. </foreach>
  629. <if test="companyIds != null and companyIds.size() > 0">
  630. AND lufe.company_id IN
  631. <foreach collection="companyIds" item="cid" open="(" separator="," close=")">
  632. #{cid}
  633. </foreach>
  634. </if>
  635. GROUP BY lufe.company_id, c.company_name
  636. </select>
  637. <!-- 分公司直播课人数 -->
  638. <select id="selectLiveAttendCountByCompany" resultType="com.fs.live.vo.LiveDataCompanyVO">
  639. SELECT
  640. lufe.company_id AS companyId,
  641. COALESCE(c.company_name, '总台') AS companyName,
  642. COALESCE(COUNT(DISTINCT CASE WHEN lwu.live_flag = 1 AND lwu.replay_flag = 0 THEN lwu.user_id END), 0) AS liveAttendanceCount
  643. FROM live_watch_user lwu
  644. LEFT JOIN live_user_first_entry lufe ON lwu.live_id = lufe.live_id AND lwu.user_id = lufe.user_id
  645. LEFT JOIN company c ON lufe.company_id = c.company_id
  646. WHERE lwu.live_id IN
  647. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  648. #{liveId}
  649. </foreach>
  650. <if test="companyIds != null and companyIds.size() > 0">
  651. AND lufe.company_id IN
  652. <foreach collection="companyIds" item="cid" open="(" separator="," close=")">
  653. #{cid}
  654. </foreach>
  655. </if>
  656. GROUP BY lufe.company_id, c.company_name
  657. </select>
  658. <!-- 分公司直播完课人数 -->
  659. <select id="selectLiveCompleteCountByCompany" resultType="com.fs.live.vo.LiveDataCompanyVO">
  660. SELECT
  661. lufe.company_id AS companyId,
  662. COALESCE(c.company_name, '总台') AS companyName,
  663. COALESCE(COUNT(DISTINCT CASE
  664. WHEN lwu.live_flag = 1
  665. AND lwu.replay_flag = 0
  666. AND lwu.online_seconds >= 1200
  667. <!-- AND lwu.online_seconds >= COALESCE(vd.total_duration, 0)-->
  668. AND COALESCE(vd.total_duration, 0) > 0
  669. THEN lwu.user_id END), 0) AS liveCompleteCount
  670. FROM live_watch_user lwu
  671. LEFT JOIN live_user_first_entry lufe ON lwu.live_id = lufe.live_id AND lwu.user_id = lufe.user_id
  672. LEFT JOIN company c ON lufe.company_id = c.company_id
  673. LEFT JOIN (
  674. SELECT live_id, SUM(COALESCE(duration, 0)) AS total_duration
  675. FROM live_video
  676. WHERE video_type IN (1, 2)
  677. GROUP BY live_id
  678. ) vd ON lwu.live_id = vd.live_id
  679. WHERE lwu.live_id IN
  680. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  681. #{liveId}
  682. </foreach>
  683. <if test="companyIds != null and companyIds.size() > 0">
  684. AND lufe.company_id IN
  685. <foreach collection="companyIds" item="cid" open="(" separator="," close=")">
  686. #{cid}
  687. </foreach>
  688. </if>
  689. GROUP BY lufe.company_id, c.company_name
  690. </select>
  691. <!-- 分公司回放课人数 -->
  692. <select id="selectReplayAttendCountByCompany" resultType="com.fs.live.vo.LiveDataCompanyVO">
  693. SELECT
  694. lufe.company_id AS companyId,
  695. COALESCE(c.company_name, '总台') AS companyName,
  696. COALESCE(COUNT(DISTINCT CASE WHEN lwu.live_flag = 0 AND lwu.replay_flag = 1 THEN lwu.user_id END), 0) AS replayAttendanceCount
  697. FROM live_watch_user lwu
  698. LEFT JOIN live_user_first_entry lufe ON lwu.live_id = lufe.live_id AND lwu.user_id = lufe.user_id
  699. LEFT JOIN company c ON lufe.company_id = c.company_id
  700. WHERE lwu.live_id IN
  701. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  702. #{liveId}
  703. </foreach>
  704. <if test="companyIds != null and companyIds.size() > 0">
  705. AND lufe.company_id IN
  706. <foreach collection="companyIds" item="cid" open="(" separator="," close=")">
  707. #{cid}
  708. </foreach>
  709. </if>
  710. GROUP BY lufe.company_id, c.company_name
  711. </select>
  712. <!-- 分公司回放完课人数 -->
  713. <select id="selectReplayCompleteCountByCompany" resultType="com.fs.live.vo.LiveDataCompanyVO">
  714. SELECT
  715. lufe.company_id AS companyId,
  716. COALESCE(c.company_name, '总台') AS companyName,
  717. COALESCE(COUNT(DISTINCT CASE
  718. WHEN lwu.live_flag = 0
  719. AND lwu.replay_flag = 1
  720. AND lwu.online_seconds >= 1200
  721. <!-- AND lwu.online_seconds >= COALESCE(vd.total_duration, 0)-->
  722. AND COALESCE(vd.total_duration, 0) > 0
  723. THEN lwu.user_id END), 0) AS replayCompleteCount
  724. FROM live_watch_user lwu
  725. LEFT JOIN live_user_first_entry lufe ON lwu.live_id = lufe.live_id AND lwu.user_id = lufe.user_id
  726. LEFT JOIN company c ON lufe.company_id = c.company_id
  727. LEFT JOIN (
  728. SELECT live_id, SUM(COALESCE(duration, 0)) AS total_duration
  729. FROM live_video
  730. WHERE video_type IN (1, 2)
  731. GROUP BY live_id
  732. ) vd ON lwu.live_id = vd.live_id
  733. WHERE lwu.live_id IN
  734. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  735. #{liveId}
  736. </foreach>
  737. <if test="companyIds != null and companyIds.size() > 0">
  738. AND lufe.company_id IN
  739. <foreach collection="companyIds" item="cid" open="(" separator="," close=")">
  740. #{cid}
  741. </foreach>
  742. </if>
  743. GROUP BY lufe.company_id, c.company_name
  744. </select>
  745. <!-- 分公司订单/GMV统计 -->
  746. <select id="selectCompanyOrderAndGmv" resultType="com.fs.live.vo.LiveDataCompanyVO">
  747. SELECT
  748. lo.company_id AS companyId,
  749. COALESCE(c.company_name, '总台') AS companyName,
  750. COALESCE(SUM(CASE WHEN lo.paid = '1' THEN lo.pay_price ELSE 0 END), 0) AS gmv,
  751. COALESCE(COUNT(DISTINCT lo.id), 0) AS orderCount,
  752. COALESCE(COUNT(DISTINCT lo.user_id), 0) AS orderUserCount
  753. FROM fs_store_order_scrm lo
  754. LEFT JOIN company c ON lo.company_id = c.company_id
  755. WHERE lo.order_type = 2 and lo.remark IN
  756. <foreach collection="liveIds" item="liveId" open="(" separator="," close=")">
  757. #{liveId}
  758. </foreach>
  759. <if test="companyIds != null and companyIds.size() > 0">
  760. AND lo.company_id IN
  761. <foreach collection="companyIds" item="cid" open="(" separator="," close=")">
  762. #{cid}
  763. </foreach>
  764. </if>
  765. GROUP BY lo.company_id, c.company_name
  766. </select>
  767. <!-- 分公司员工数量统计 -->
  768. <select id="selectCompanyEmployeeCountByLiveIds" resultType="com.fs.live.vo.LiveDataCompanyVO">
  769. SELECT
  770. cu.company_id AS companyId,
  771. COALESCE(c.company_name, '总台') AS companyName,
  772. COALESCE(COUNT(DISTINCT cu.user_id), 0) AS employeeCount
  773. FROM company_user cu
  774. LEFT JOIN company c ON cu.company_id = c.company_id
  775. WHERE
  776. cu.status = 0
  777. AND cu.del_flag = 0
  778. <if test="companyIds != null and companyIds.size() > 0">
  779. AND cu.company_id IN
  780. <foreach collection="companyIds" item="cid" open="(" separator="," close=")">
  781. #{cid}
  782. </foreach>
  783. </if>
  784. GROUP BY cu.company_id, c.company_name
  785. </select>
  786. </mapper>