FsUserCompanyBindMapper.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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.FsUserCompanyBindMapper">
  6. <resultMap type="FsUserCompanyBind" id="FsUserCompanyBindResult">
  7. <result property="id" column="id" />
  8. <result property="fsUserId" column="fs_user_id" />
  9. <result property="companyUserId" column="company_user_id" />
  10. <result property="companyId" column="company_id" />
  11. <result property="projectId" column="project_id" />
  12. <result property="qwUserId" column="qw_user_id" />
  13. <result property="qwExternalContactId" column="qw_external_contact_id" />
  14. <result property="qwCompanyId" column="qw_company_id" />
  15. <result property="courseId" column="course_id" />
  16. <result property="videoId" column="video_id" />
  17. <result property="watchLogId" column="watch_log_id" />
  18. <result property="logType" column="log_type" />
  19. <result property="finishTime" column="finish_time" />
  20. <result property="createTime" column="create_time" />
  21. <result property="createBy" column="create_by" />
  22. <result property="updateBy" column="update_by" />
  23. <result property="updateTime" column="update_time" />
  24. <result property="remark" column="remark" />
  25. </resultMap>
  26. <sql id="selectFsUserCompanyBindVo">
  27. select id, fs_user_id, company_user_id, company_id, project_id, qw_user_id, qw_external_contact_id, qw_company_id, course_id, video_id, watch_log_id, log_type, finish_time, create_time, create_by, update_by, update_time, remark from fs_user_company_bind
  28. </sql>
  29. <select id="selectFsUserCompanyBindList" parameterType="FsUserCompanyBind" resultMap="FsUserCompanyBindResult">
  30. <include refid="selectFsUserCompanyBindVo"/>
  31. <where>
  32. <if test="fsUserId != null "> and fs_user_id = #{fsUserId}</if>
  33. <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
  34. <if test="companyId != null "> and company_id = #{companyId}</if>
  35. <if test="projectId != null "> and project_id = #{projectId}</if>
  36. <if test="qwUserId != null "> and qw_user_id = #{qwUserId}</if>
  37. <if test="qwExternalContactId != null "> and qw_external_contact_id = #{qwExternalContactId}</if>
  38. <if test="qwCompanyId != null "> and qw_company_id = #{qwCompanyId}</if>
  39. <if test="courseId != null "> and course_id = #{courseId}</if>
  40. <if test="videoId != null "> and video_id = #{videoId}</if>
  41. <if test="logType != null "> and log_type = #{logType}</if>
  42. <if test="finishTime != null "> and finish_time = #{finishTime}</if>
  43. </where>
  44. </select>
  45. <select id="selectFsUserCompanyBindById" parameterType="Long" resultMap="FsUserCompanyBindResult">
  46. <include refid="selectFsUserCompanyBindVo"/>
  47. where id = #{id}
  48. </select>
  49. <insert id="insertFsUserCompanyBind" parameterType="FsUserCompanyBind" useGeneratedKeys="true" keyProperty="id">
  50. insert into fs_user_company_bind
  51. <trim prefix="(" suffix=")" suffixOverrides=",">
  52. <if test="fsUserId != null">fs_user_id,</if>
  53. <if test="companyUserId != null">company_user_id,</if>
  54. <if test="companyId != null">company_id,</if>
  55. <if test="projectId != null">project_id,</if>
  56. <if test="qwUserId != null">qw_user_id,</if>
  57. <if test="qwExternalContactId != null">qw_external_contact_id,</if>
  58. <if test="qwCompanyId != null">qw_company_id,</if>
  59. <if test="courseId != null">course_id,</if>
  60. <if test="videoId != null">video_id,</if>
  61. <if test="watchLogId != null">watch_log_id,</if>
  62. <if test="logType != null">log_type,</if>
  63. <if test="finishTime != null">finish_time,</if>
  64. <if test="createTime != null">create_time,</if>
  65. <if test="createBy != null">create_by,</if>
  66. <if test="updateBy != null">update_by,</if>
  67. <if test="updateTime != null">update_time,</if>
  68. <if test="remark != null">remark,</if>
  69. </trim>
  70. <trim prefix="values (" suffix=")" suffixOverrides=",">
  71. <if test="fsUserId != null">#{fsUserId},</if>
  72. <if test="companyUserId != null">#{companyUserId},</if>
  73. <if test="companyId != null">#{companyId},</if>
  74. <if test="projectId != null">#{projectId},</if>
  75. <if test="qwUserId != null">#{qwUserId},</if>
  76. <if test="qwExternalContactId != null">#{qwExternalContactId},</if>
  77. <if test="qwCompanyId != null">#{qwCompanyId},</if>
  78. <if test="courseId != null">#{courseId},</if>
  79. <if test="videoId != null">#{videoId},</if>
  80. <if test="watchLogId != null">#{watchLogId},</if>
  81. <if test="logType != null">#{logType},</if>
  82. <if test="finishTime != null">#{finishTime},</if>
  83. <if test="createTime != null">#{createTime},</if>
  84. <if test="createBy != null">#{createBy},</if>
  85. <if test="updateBy != null">#{updateBy},</if>
  86. <if test="updateTime != null">#{updateTime},</if>
  87. <if test="remark != null">#{remark},</if>
  88. </trim>
  89. </insert>
  90. <update id="updateFsUserCompanyBind" parameterType="FsUserCompanyBind">
  91. update fs_user_company_bind
  92. <trim prefix="SET" suffixOverrides=",">
  93. <if test="fsUserId != null">fs_user_id = #{fsUserId},</if>
  94. <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
  95. <if test="companyId != null">company_id = #{companyId},</if>
  96. <if test="projectId != null">project_id = #{projectId},</if>
  97. <if test="qwUserId != null">qw_user_id = #{qwUserId},</if>
  98. <if test="qwExternalContactId != null">qw_external_contact_id = #{qwExternalContactId},</if>
  99. <if test="qwCompanyId != null">qw_company_id = #{qwCompanyId},</if>
  100. <if test="courseId != null">course_id = #{courseId},</if>
  101. <if test="videoId != null">video_id = #{videoId},</if>
  102. <if test="watchLogId != null">watch_log_id = #{watchLogId},</if>
  103. <if test="logType != null">log_type = #{logType},</if>
  104. <if test="finishTime != null">finish_time = #{finishTime},</if>
  105. <if test="createTime != null">create_time = #{createTime},</if>
  106. <if test="createBy != null">create_by = #{createBy},</if>
  107. <if test="updateBy != null">update_by = #{updateBy},</if>
  108. <if test="updateTime != null">update_time = #{updateTime},</if>
  109. <if test="remark != null">remark = #{remark},</if>
  110. </trim>
  111. where id = #{id}
  112. </update>
  113. <delete id="deleteFsUserCompanyBindById" parameterType="Long">
  114. delete from fs_user_company_bind where id = #{id}
  115. </delete>
  116. <delete id="deleteFsUserCompanyBindByIds" parameterType="String">
  117. delete from fs_user_company_bind where id in
  118. <foreach item="id" collection="array" open="(" separator="," close=")">
  119. #{id}
  120. </foreach>
  121. </delete>
  122. <!-- 重粉看课:仅展示同一粉丝在同一项目下、由至少两个不同企微员工产生过绑定/看课记录的数据 -->
  123. <select id="getWatchLogList" resultType="com.fs.course.vo.UserWatchLogListVo">
  124. select
  125. a.*,
  126. b.nick_name fsUserName,
  127. c.dict_label projectName,
  128. e.course_name,
  129. g.qw_user_name,
  130. f.title videoName,
  131. qc.corp_name,
  132. qc.corp_id AS corpId
  133. from
  134. fs_user_company_bind a
  135. inner join (
  136. select fs_user_id, project_id
  137. from fs_user_company_bind
  138. where project_id is not null
  139. and qw_user_id is not null
  140. <if test="fsUserId != null">
  141. and fs_user_id = #{fsUserId}
  142. </if>
  143. <if test="fsUserId == null and externalUserId != null">
  144. and fs_user_id in (
  145. select u.fs_user_id from fs_user_company_bind u
  146. where u.qw_external_contact_id = #{externalUserId}
  147. and u.fs_user_id is not null
  148. )
  149. </if>
  150. group by fs_user_id, project_id
  151. having count(distinct qw_user_id) &gt;= 2
  152. ) dup_proj on dup_proj.fs_user_id = a.fs_user_id and dup_proj.project_id = a.project_id
  153. left join fs_user b on a.fs_user_id = b.user_id
  154. left join sys_dict_data c on c.dict_type = 'sys_course_project' and dict_value = a.project_id
  155. left join fs_user_course e on a.course_id = e.course_id
  156. left join fs_user_course_video f on a.video_id = f.video_id
  157. left join qw_user g on g.id = a.qw_user_id
  158. left join qw_company qc on qc.id = a.qw_company_id
  159. <where>
  160. <if test="externalUserId != null">
  161. and a.qw_external_contact_id = #{externalUserId}
  162. </if>
  163. <if test="fsUserId != null">
  164. and a.fs_user_id = #{fsUserId}
  165. </if>
  166. <if test="courseId != null">
  167. and a.course_id = #{courseId}
  168. </if>
  169. <if test="videoId != null">
  170. and a.video_id = #{videoId}
  171. </if>
  172. <if test="equivalentProjectIds != null and equivalentProjectIds.size() > 0">
  173. and a.project_id in
  174. <foreach collection="equivalentProjectIds" item="pid" open="(" separator="," close=")">
  175. #{pid}
  176. </foreach>
  177. </if>
  178. </where>
  179. order by create_time desc
  180. </select>
  181. <delete id="deleteFsUserCompanyBindByCompanyUserIdAndFsUserId" parameterType="Long">
  182. delete from fs_user_company_bind where fs_user_id=#{fsUserId} and company_user_id=#{companyUserId}
  183. </delete>
  184. <!-- 重粉看课历史 - 关联销售信息:通过fsUserId找出该客户添加的所有销售 -->
  185. <!-- 优化:走 qw_external_contact(fs_user_id) 索引 + GROUP BY 去重 + 内连接避免无效数据 -->
  186. <select id="getRelatedSalesByFsUserId" resultType="com.fs.course.vo.RelatedSalesResultVO">
  187. SELECT
  188. CASE
  189. WHEN qu.qw_user_name IS NOT NULL AND qu.qw_user_name LIKE '{%' THEN JSON_UNQUOTE(JSON_EXTRACT(qu.qw_user_name, '$.name'))
  190. ELSE qu.qw_user_name
  191. END AS qwUserName,
  192. qc.corp_name AS corpName,
  193. qu.corp_id AS corpId,
  194. MIN(ec.`status`) AS `status`,
  195. MIN(ec.create_time) AS addTime
  196. FROM qw_external_contact ec
  197. INNER JOIN qw_user qu ON qu.id = ec.qw_user_id
  198. LEFT JOIN qw_company qc ON qc.corp_id = qu.corp_id
  199. WHERE ec.fs_user_id = #{fsUserId}
  200. GROUP BY ec.qw_user_id
  201. ORDER BY addTime DESC
  202. </select>
  203. <!-- 重粉看课历史 - 课程学习进度:通过fsUserId查出看课记录并计算进度 -->
  204. <!-- 优化:使用子查询先缩小课程范围,避免全表扫描;分别统计总节数和完课节数 -->
  205. <select id="getCourseProgressByFsUserId" resultType="com.fs.course.vo.CourseProgressResultVO">
  206. SELECT
  207. uc.course_id AS courseId,
  208. uc.course_name AS courseName,
  209. uc.project AS projectId,
  210. COALESCE(vt.total_count, 0) AS totalCount,
  211. COALESCE(ws.watched_count, 0) AS watchedCount,
  212. latest.video_name AS latestSection,
  213. latest.max_time AS latestTime,
  214. latest.qw_user_name AS qwUserName,
  215. latest.corp_name AS corpName,
  216. latest.corp_id AS corpId
  217. FROM (
  218. SELECT DISTINCT course_id
  219. FROM fs_course_watch_log
  220. WHERE user_id = #{fsUserId}
  221. AND send_type = 2
  222. ) watched
  223. INNER JOIN fs_user_course uc ON uc.course_id = watched.course_id
  224. LEFT JOIN (
  225. SELECT course_id, COUNT(*) AS total_count
  226. FROM fs_user_course_video
  227. WHERE is_del = 0
  228. GROUP BY course_id
  229. ) vt ON vt.course_id = watched.course_id
  230. LEFT JOIN (
  231. SELECT course_id, COUNT(DISTINCT video_id) AS watched_count
  232. FROM fs_course_watch_log
  233. WHERE user_id = #{fsUserId}
  234. AND send_type = 2
  235. GROUP BY course_id
  236. ) ws ON ws.course_id = watched.course_id
  237. LEFT JOIN (
  238. SELECT
  239. wl.course_id,
  240. v.title AS video_name,
  241. wl.create_time AS max_time,
  242. CASE
  243. WHEN qu.qw_user_name IS NOT NULL AND qu.qw_user_name LIKE '{%' THEN JSON_UNQUOTE(JSON_EXTRACT(qu.qw_user_name, '$.name'))
  244. ELSE qu.qw_user_name
  245. END AS qw_user_name,
  246. qc.corp_name,
  247. qu.corp_id
  248. FROM fs_course_watch_log wl
  249. INNER JOIN fs_user_course_video v ON v.video_id = wl.video_id
  250. INNER JOIN (
  251. SELECT course_id, MAX(create_time) AS max_time
  252. FROM fs_course_watch_log
  253. WHERE user_id = #{fsUserId}
  254. AND send_type = 2
  255. GROUP BY course_id
  256. ) lt ON lt.course_id = wl.course_id AND lt.max_time = wl.create_time
  257. LEFT JOIN qw_user qu ON qu.id = wl.qw_user_id
  258. LEFT JOIN qw_company qc ON qc.corp_id = qu.corp_id
  259. WHERE wl.user_id = #{fsUserId}
  260. AND wl.send_type = 2
  261. GROUP BY wl.course_id
  262. ) latest ON latest.course_id = watched.course_id
  263. ORDER BY uc.course_id DESC
  264. </select>
  265. <!-- 重粉看课历史 - 客户企微发课看过的项目(与课程进度口径一致:send_type=2,项目取自课程表) -->
  266. <select id="getUserWatchedProjectsByFsUserId" resultType="com.fs.course.vo.RepeatProjectResultVO">
  267. SELECT
  268. p.project_id AS projectId,
  269. c.dict_label AS projectName
  270. FROM (
  271. SELECT DISTINCT uc.project AS project_id
  272. FROM fs_course_watch_log wl
  273. INNER JOIN fs_user_course uc ON uc.course_id = wl.course_id
  274. WHERE wl.user_id = #{fsUserId}
  275. AND wl.send_type = 2
  276. AND uc.project IS NOT NULL
  277. AND uc.project != 0
  278. ) p
  279. LEFT JOIN sys_dict_data c ON c.dict_type = 'sys_course_project' AND c.dict_value = p.project_id
  280. ORDER BY p.project_id
  281. </select>
  282. </mapper>