| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.fs.course.mapper.FsUserCompanyBindMapper">
- <resultMap type="FsUserCompanyBind" id="FsUserCompanyBindResult">
- <result property="id" column="id" />
- <result property="fsUserId" column="fs_user_id" />
- <result property="companyUserId" column="company_user_id" />
- <result property="companyId" column="company_id" />
- <result property="projectId" column="project_id" />
- <result property="qwUserId" column="qw_user_id" />
- <result property="qwExternalContactId" column="qw_external_contact_id" />
- <result property="qwCompanyId" column="qw_company_id" />
- <result property="courseId" column="course_id" />
- <result property="videoId" column="video_id" />
- <result property="watchLogId" column="watch_log_id" />
- <result property="logType" column="log_type" />
- <result property="finishTime" column="finish_time" />
- <result property="createTime" column="create_time" />
- <result property="createBy" column="create_by" />
- <result property="updateBy" column="update_by" />
- <result property="updateTime" column="update_time" />
- <result property="remark" column="remark" />
- </resultMap>
- <sql id="selectFsUserCompanyBindVo">
- 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
- </sql>
- <select id="selectFsUserCompanyBindList" parameterType="FsUserCompanyBind" resultMap="FsUserCompanyBindResult">
- <include refid="selectFsUserCompanyBindVo"/>
- <where>
- <if test="fsUserId != null "> and fs_user_id = #{fsUserId}</if>
- <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
- <if test="companyId != null "> and company_id = #{companyId}</if>
- <if test="projectId != null "> and project_id = #{projectId}</if>
- <if test="qwUserId != null "> and qw_user_id = #{qwUserId}</if>
- <if test="qwExternalContactId != null "> and qw_external_contact_id = #{qwExternalContactId}</if>
- <if test="qwCompanyId != null "> and qw_company_id = #{qwCompanyId}</if>
- <if test="courseId != null "> and course_id = #{courseId}</if>
- <if test="videoId != null "> and video_id = #{videoId}</if>
- <if test="logType != null "> and log_type = #{logType}</if>
- <if test="finishTime != null "> and finish_time = #{finishTime}</if>
- </where>
- </select>
- <select id="selectFsUserCompanyBindById" parameterType="Long" resultMap="FsUserCompanyBindResult">
- <include refid="selectFsUserCompanyBindVo"/>
- where id = #{id}
- </select>
- <insert id="insertFsUserCompanyBind" parameterType="FsUserCompanyBind" useGeneratedKeys="true" keyProperty="id">
- insert into fs_user_company_bind
- <trim prefix="(" suffix=")" suffixOverrides=",">
- <if test="fsUserId != null">fs_user_id,</if>
- <if test="companyUserId != null">company_user_id,</if>
- <if test="companyId != null">company_id,</if>
- <if test="projectId != null">project_id,</if>
- <if test="qwUserId != null">qw_user_id,</if>
- <if test="qwExternalContactId != null">qw_external_contact_id,</if>
- <if test="qwCompanyId != null">qw_company_id,</if>
- <if test="courseId != null">course_id,</if>
- <if test="videoId != null">video_id,</if>
- <if test="watchLogId != null">watch_log_id,</if>
- <if test="logType != null">log_type,</if>
- <if test="finishTime != null">finish_time,</if>
- <if test="createTime != null">create_time,</if>
- <if test="createBy != null">create_by,</if>
- <if test="updateBy != null">update_by,</if>
- <if test="updateTime != null">update_time,</if>
- <if test="remark != null">remark,</if>
- </trim>
- <trim prefix="values (" suffix=")" suffixOverrides=",">
- <if test="fsUserId != null">#{fsUserId},</if>
- <if test="companyUserId != null">#{companyUserId},</if>
- <if test="companyId != null">#{companyId},</if>
- <if test="projectId != null">#{projectId},</if>
- <if test="qwUserId != null">#{qwUserId},</if>
- <if test="qwExternalContactId != null">#{qwExternalContactId},</if>
- <if test="qwCompanyId != null">#{qwCompanyId},</if>
- <if test="courseId != null">#{courseId},</if>
- <if test="videoId != null">#{videoId},</if>
- <if test="watchLogId != null">#{watchLogId},</if>
- <if test="logType != null">#{logType},</if>
- <if test="finishTime != null">#{finishTime},</if>
- <if test="createTime != null">#{createTime},</if>
- <if test="createBy != null">#{createBy},</if>
- <if test="updateBy != null">#{updateBy},</if>
- <if test="updateTime != null">#{updateTime},</if>
- <if test="remark != null">#{remark},</if>
- </trim>
- </insert>
- <update id="updateFsUserCompanyBind" parameterType="FsUserCompanyBind">
- update fs_user_company_bind
- <trim prefix="SET" suffixOverrides=",">
- <if test="fsUserId != null">fs_user_id = #{fsUserId},</if>
- <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
- <if test="companyId != null">company_id = #{companyId},</if>
- <if test="projectId != null">project_id = #{projectId},</if>
- <if test="qwUserId != null">qw_user_id = #{qwUserId},</if>
- <if test="qwExternalContactId != null">qw_external_contact_id = #{qwExternalContactId},</if>
- <if test="qwCompanyId != null">qw_company_id = #{qwCompanyId},</if>
- <if test="courseId != null">course_id = #{courseId},</if>
- <if test="videoId != null">video_id = #{videoId},</if>
- <if test="watchLogId != null">watch_log_id = #{watchLogId},</if>
- <if test="logType != null">log_type = #{logType},</if>
- <if test="finishTime != null">finish_time = #{finishTime},</if>
- <if test="createTime != null">create_time = #{createTime},</if>
- <if test="createBy != null">create_by = #{createBy},</if>
- <if test="updateBy != null">update_by = #{updateBy},</if>
- <if test="updateTime != null">update_time = #{updateTime},</if>
- <if test="remark != null">remark = #{remark},</if>
- </trim>
- where id = #{id}
- </update>
- <delete id="deleteFsUserCompanyBindById" parameterType="Long">
- delete from fs_user_company_bind where id = #{id}
- </delete>
- <delete id="deleteFsUserCompanyBindByIds" parameterType="String">
- delete from fs_user_company_bind where id in
- <foreach item="id" collection="array" open="(" separator="," close=")">
- #{id}
- </foreach>
- </delete>
- <!-- 重粉看课:仅展示同一粉丝在同一项目下、由至少两个不同企微员工产生过绑定/看课记录的数据 -->
- <select id="getWatchLogList" resultType="com.fs.course.vo.UserWatchLogListVo">
- select
- a.*,
- b.nick_name fsUserName,
- c.dict_label projectName,
- e.course_name,
- g.qw_user_name,
- f.title videoName,
- qc.corp_name,
- qc.corp_id AS corpId
- from
- fs_user_company_bind a
- inner join (
- select fs_user_id, project_id
- from fs_user_company_bind
- where project_id is not null
- and qw_user_id is not null
- <if test="fsUserId != null">
- and fs_user_id = #{fsUserId}
- </if>
- <if test="fsUserId == null and externalUserId != null">
- and fs_user_id in (
- select u.fs_user_id from fs_user_company_bind u
- where u.qw_external_contact_id = #{externalUserId}
- and u.fs_user_id is not null
- )
- </if>
- group by fs_user_id, project_id
- having count(distinct qw_user_id) >= 2
- ) dup_proj on dup_proj.fs_user_id = a.fs_user_id and dup_proj.project_id = a.project_id
- left join fs_user b on a.fs_user_id = b.user_id
- left join sys_dict_data c on c.dict_type = 'sys_course_project' and dict_value = a.project_id
- left join fs_user_course e on a.course_id = e.course_id
- left join fs_user_course_video f on a.video_id = f.video_id
- left join qw_user g on g.id = a.qw_user_id
- left join qw_company qc on qc.id = a.qw_company_id
- <where>
- <if test="externalUserId != null">
- and a.qw_external_contact_id = #{externalUserId}
- </if>
- <if test="fsUserId != null">
- and a.fs_user_id = #{fsUserId}
- </if>
- <if test="courseId != null">
- and a.course_id = #{courseId}
- </if>
- <if test="videoId != null">
- and a.video_id = #{videoId}
- </if>
- <if test="equivalentProjectIds != null and equivalentProjectIds.size() > 0">
- and a.project_id in
- <foreach collection="equivalentProjectIds" item="pid" open="(" separator="," close=")">
- #{pid}
- </foreach>
- </if>
- </where>
- order by create_time desc
- </select>
- <delete id="deleteFsUserCompanyBindByCompanyUserIdAndFsUserId" parameterType="Long">
- delete from fs_user_company_bind where fs_user_id=#{fsUserId} and company_user_id=#{companyUserId}
- </delete>
- <!-- 重粉看课历史 - 关联销售信息:通过fsUserId找出该客户添加的所有销售 -->
- <!-- 优化:走 qw_external_contact(fs_user_id) 索引 + GROUP BY 去重 + 内连接避免无效数据 -->
- <select id="getRelatedSalesByFsUserId" resultType="com.fs.course.vo.RelatedSalesResultVO">
- SELECT
- CASE
- WHEN qu.qw_user_name IS NOT NULL AND qu.qw_user_name LIKE '{%' THEN JSON_UNQUOTE(JSON_EXTRACT(qu.qw_user_name, '$.name'))
- ELSE qu.qw_user_name
- END AS qwUserName,
- qc.corp_name AS corpName,
- qu.corp_id AS corpId,
- MIN(ec.`status`) AS `status`,
- MIN(ec.create_time) AS addTime
- FROM qw_external_contact ec
- INNER JOIN qw_user qu ON qu.id = ec.qw_user_id
- LEFT JOIN qw_company qc ON qc.corp_id = qu.corp_id
- WHERE ec.fs_user_id = #{fsUserId}
- GROUP BY ec.qw_user_id
- ORDER BY addTime DESC
- </select>
- <!-- 重粉看课历史 - 课程学习进度:通过fsUserId查出看课记录并计算进度 -->
- <!-- 优化:使用子查询先缩小课程范围,避免全表扫描;分别统计总节数和完课节数 -->
- <select id="getCourseProgressByFsUserId" resultType="com.fs.course.vo.CourseProgressResultVO">
- SELECT
- uc.course_id AS courseId,
- uc.course_name AS courseName,
- uc.project AS projectId,
- COALESCE(vt.total_count, 0) AS totalCount,
- COALESCE(ws.watched_count, 0) AS watchedCount,
- latest.video_name AS latestSection,
- latest.max_time AS latestTime,
- latest.qw_user_name AS qwUserName,
- latest.corp_name AS corpName,
- latest.corp_id AS corpId
- FROM (
- SELECT DISTINCT course_id
- FROM fs_course_watch_log
- WHERE user_id = #{fsUserId}
- AND send_type = 2
- ) watched
- INNER JOIN fs_user_course uc ON uc.course_id = watched.course_id
- LEFT JOIN (
- SELECT course_id, COUNT(*) AS total_count
- FROM fs_user_course_video
- WHERE is_del = 0
- GROUP BY course_id
- ) vt ON vt.course_id = watched.course_id
- LEFT JOIN (
- SELECT course_id, COUNT(DISTINCT video_id) AS watched_count
- FROM fs_course_watch_log
- WHERE user_id = #{fsUserId}
- AND send_type = 2
- GROUP BY course_id
- ) ws ON ws.course_id = watched.course_id
- LEFT JOIN (
- SELECT
- wl.course_id,
- v.title AS video_name,
- wl.create_time AS max_time,
- CASE
- WHEN qu.qw_user_name IS NOT NULL AND qu.qw_user_name LIKE '{%' THEN JSON_UNQUOTE(JSON_EXTRACT(qu.qw_user_name, '$.name'))
- ELSE qu.qw_user_name
- END AS qw_user_name,
- qc.corp_name,
- qu.corp_id
- FROM fs_course_watch_log wl
- INNER JOIN fs_user_course_video v ON v.video_id = wl.video_id
- INNER JOIN (
- SELECT course_id, MAX(create_time) AS max_time
- FROM fs_course_watch_log
- WHERE user_id = #{fsUserId}
- AND send_type = 2
- GROUP BY course_id
- ) lt ON lt.course_id = wl.course_id AND lt.max_time = wl.create_time
- LEFT JOIN qw_user qu ON qu.id = wl.qw_user_id
- LEFT JOIN qw_company qc ON qc.corp_id = qu.corp_id
- WHERE wl.user_id = #{fsUserId}
- AND wl.send_type = 2
- GROUP BY wl.course_id
- ) latest ON latest.course_id = watched.course_id
- ORDER BY uc.course_id DESC
- </select>
- <!-- 重粉看课历史 - 客户企微发课看过的项目(与课程进度口径一致:send_type=2,项目取自课程表) -->
- <select id="getUserWatchedProjectsByFsUserId" resultType="com.fs.course.vo.RepeatProjectResultVO">
- SELECT
- p.project_id AS projectId,
- c.dict_label AS projectName
- FROM (
- SELECT DISTINCT uc.project AS project_id
- FROM fs_course_watch_log wl
- INNER JOIN fs_user_course uc ON uc.course_id = wl.course_id
- WHERE wl.user_id = #{fsUserId}
- AND wl.send_type = 2
- AND uc.project IS NOT NULL
- AND uc.project != 0
- ) p
- LEFT JOIN sys_dict_data c ON c.dict_type = 'sys_course_project' AND c.dict_value = p.project_id
- ORDER BY p.project_id
- </select>
- </mapper>
|