xkrs_ms/stdiet-custom/src/main/resources/mapper/custom/SysServicesTopicMapper.xml
2021-06-11 14:28:30 +08:00

323 lines
15 KiB
XML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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.stdiet.custom.mapper.SysServicesTopicMapper">
<resultMap type="SysServicesTopic" id="SysServicesTopicResult">
<result column="id" property="id"/>
<result column="topic_id" property="topicId"/>
<result column="topic_type" property="topicType"/>
<result column="content" property="content"/>
<result column="uid" property="uid"/>
<result column="role" property="role"/>
<result column="read" property="read"/>
<result column="img" property="img" typeHandler="com.stdiet.custom.typehandler.ArrayJsonHandler"/>
<result column="create_time" property="createTime"/>
<result column="update_time" property="updateTime"/>
<association column="{uid=uid,role=role}" property="name" select="selectUserInfo"/>
<association property="avatar" column="{uid=uid,role=role}" select="selectUserAvatar"/>
</resultMap>
<select id="selectCustomerListByUserIdAndRole" parameterType="SysServicesTopic" resultMap="SysServicesTopicResult">
SELECT DISTINCT(uid)AS uid, MAX(update_time) AS update_time, MIN(create_time) AS create_time, MIN(`read`) AS
`read`, role FROM (
SELECT * FROM (
SELECT topic_id, `read`, create_time, update_time, 'customer' AS role FROM sys_services_topic_status
<choose>
<when test="role == 'admin' or role == 'manager' or role == 'admin-dev'">
WHERE role = 'dietician'
</when>
<otherwise>
WHERE role = #{role} AND uid = #{uid}
</otherwise>
</choose>
) AS status
LEFT JOIN (SELECT topic_id, uid, del_flag FROM sys_services_topic ) AS topic USING(topic_id)
WHERE del_flag = 0
) AS userList GROUP BY uid
ORDER BY `read` ASC, update_time DESC
</select>
<select id="selectTopicListByUid" parameterType="SysServicesTopic" resultMap="SysServicesTopicResult">
SELECT * FROM(
SELECT topic_id, topic_type, content, uid, img, 'customer' AS role FROM sys_services_topic
WHERE uid = #{fromUid} AND del_flag = 0
) AS topic
LEFT JOIN (SELECT topic_id, create_time, update_time, `read`,
<choose>
<when test="role == 'admin' or role == 'manager' or role == 'admin-dev'">
'0' AS id
</when>
<otherwise>
id
</otherwise>
</choose>
FROM sys_services_topic_status
<choose>
<when test="role == 'admin' or role == 'manager' or role == 'admin-dev'">
WHERE role = 'dietician'
</when>
<otherwise>
WHERE role = #{role} AND uid = #{uid}
</otherwise>
</choose>
) AS state
USING(topic_id)
ORDER BY `read` ASC, update_time DESC
</select>
<!-- 根据userId和角色查询问题列表-->
<select id="selectSysServicesTopicByUserIdAndRole" parameterType="SysServicesTopic"
resultMap="SysServicesTopicResult">
<choose>
<when test="role == 'admin' or role == 'manager' or role == 'admin-dev'">
select topic_id, topic_type, content, uid, create_time, img, 1 as `read`, '0' as id, 'customer' as role
from sys_services_topic where del_flag = 0 order by create_time desc
</when>
<otherwise>
SELECT * FROM (
SELECT topic_id, id, `read`, create_time, update_time, 'customer' AS role FROM sys_services_topic_status
WHERE role = #{role} AND uid = #{uid}
) AS status
LEFT JOIN sys_services_topic USING(topic_id) WHERE del_flag = 0
ORDER BY `read` ASC, update_time DESC
</otherwise>
</choose>
</select>
<!-- 查询主题-->
<resultMap id="SysServicesTopicSessionResult" type="SysServicesTopic">
<result column="id" property="id"/>
<result column="uid" property="uid"/>
<result column="topic_id" property="topicId"/>
<result column="topic_type" property="topicType"/>
<result column="content" property="content"/>
<result column="img" property="img" typeHandler="com.stdiet.custom.typehandler.ArrayJsonHandler"/>
<result column="create_time" property="createTime"/>
<association property="name" column="{uid=uid,role=role}" select="selectUserInfo"/>
<association property="avatar" column="{uid=uid,role=role}" select="selectUserAvatar"/>
<association property="comments" column="topic_id" select="selectServicesTopicCommentByTopicId"/>
</resultMap>
<resultMap id="ServicesTopicCommentResult" type="SysServicesTopic">
<result column="id" property="id"/>
<result column="topic_id" property="topicId"/>
<result column="content" property="content"/>
<result column="from_uid" property="fromUid"/>
<result column="from_role" property="fromRole"/>
<result column="to_uid" property="toUid"/>
<result column="to_role" property="toRole"/>
<result column="img" property="img" typeHandler="com.stdiet.custom.typehandler.ArrayJsonHandler"/>
<result column="create_time" property="createTime"/>
<association property="fromName" column="{uid=from_uid,role=from_role}" select="selectUserInfo"/>
<association property="fromAvatar" column="{uid=from_uid,role=from_role}" select="selectUserAvatar"/>
<association property="toName" column="{uid=to_uid,role=to_role}" select="selectUserInfo"/>
<!-- <association property="toAvatar" column="{uid=to_uid,role=to_role}" select="selectUserAvatar"/>-->
<association property="replys" column="id"
select="selectServicesTopicCommentReplyByCommentId"/>
</resultMap>
<resultMap id="ServicesTopicCommentReplyResult" type="SysServicesTopic">
<result column="id" property="id"/>
<result column="topic_id" property="topicId"/>
<result column="comment_id" property="commentId"/>
<result column="content" property="content"/>
<result column="from_uid" property="fromUid"/>
<result column="from_role" property="fromRole"/>
<result column="to_uid" property="toUid"/>
<result column="to_role" property="toRole"/>
<result column="reply_id" property="replyId"/>
<result column="img" property="img" typeHandler="com.stdiet.custom.typehandler.ArrayJsonHandler"/>
<result column="create_time" property="createTime"/>
<association property="fromName" column="{uid=from_uid,role=from_role}" select="selectUserInfo"/>
<association property="fromAvatar" column="{uid=from_uid,role=from_role}" select="selectUserAvatar"/>
<association property="toName" column="{uid=to_uid,role=to_role}" select="selectUserInfo"/>
<!-- <association property="toAvatar" column="{uid=to_uid,role=to_role}" select="selectUserAvatar"/>-->
</resultMap>
<select id="selectSysServicesTopicSessionByTopicId" resultMap="SysServicesTopicSessionResult"
parameterType="String">
select topic_id, uid, 'customer' as role, content, img, create_time from sys_services_topic where topic_id = #{topicId}
</select>
<select id="selectServicesTopicCommentByTopicId" resultMap="ServicesTopicCommentResult">
select * from sys_services_topic_comment where topic_id = #{topic_id}
order by create_time asc
</select>
<select id="selectServicesTopicCommentReplyByCommentId" resultMap="ServicesTopicCommentReplyResult">
select * from sys_services_topic_reply where comment_id = #{id}
order by create_time asc
</select>
<!-- 查询人名-->
<select id="selectUserInfo" parameterType="java.util.Map" resultType="String">
<choose>
<when test="_parameter.get('role') == 'customer'">
select name from sys_customer where id = #{uid}
</when>
<otherwise>
select nick_name from sys_user where user_id = #{uid}
</otherwise>
</choose>
</select>
<!-- 查询头像-->
<select id="selectUserAvatar" parameterType="java.util.Map" resultType="String">
<choose>
<when test="_parameter.get('role') == 'customer'">
select avatar_url from sys_wx_user_info where cus_id = #{uid}
</when>
<otherwise>
select IF(avatar != '', CONCAT("https://api.stdiet.top/prod-api", avatar), '') as avatar from sys_user
where user_id = #{uid}
</otherwise>
</choose>
</select>
<!-- 插入问题-->
<insert id="insertSysServicesTopic" parameterType="SysServicesTopic" useGeneratedKeys="true"
keyProperty="id" keyColumn="id">
insert into sys_services_topic
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="topicId != null">topic_id,</if>
<if test="uid != null">uid,</if>
<if test="content != null">content,</if>
<if test="topicType != null">topic_type,</if>
<if test="createTime != null">create_time,</if>
<if test="img != null">img,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="topicId != null">#{topicId},</if>
<if test="uid != null">#{uid},</if>
<if test="content != null">#{content},</if>
<if test="topicType != null">#{topicType},</if>
<if test="createTime != null">#{createTime},</if>
<if test="img != null">
#{img, jdbcType=OTHER, typeHandler=com.stdiet.custom.typehandler.ArrayJsonHandler},
</if>
</trim>
</insert>
<!-- 插入问题的四个角色1用户 2营养师 3售后 4助理-->
<insert id="insertSysServicesTopicStatus" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id"
keyColumn="id">
insert into sys_services_topic_status (topic_id, uid, role, `read`, create_time, update_time) values
<foreach collection="list" item="status" index="index" separator=",">
(#{status.topicId}, #{status.uid}, #{status.role}, #{status.read}, now(), now())
</foreach>
</insert>
<!-- 根据状态id更新 role=customer 客户回复这时更新另外三个角色未读role != customer更新客户未读-->
<update id="updateSysServicesTopicStatus" parameterType="SysServicesTopic">
update sys_services_topic_status
<trim prefix="SET" suffixOverrides=",">
<if test="read != null">`read` = #{read},</if>
<if test="read != null">update_time = now(),</if>
</trim>
<where>
<choose>
<when test="id != null">
id = ${id}
</when>
<otherwise>
<if test="role == 'customer'">
role != #{role} and
</if>
<if test="role != 'customer'">
role = 'customer' and
</if>
topic_id = #{topicId}
</otherwise>
</choose>
</where>
</update>
<!-- 插入问题回复-->
<insert id="inserSysServicesTopicComment" parameterType="SysServicesTopic">
insert into sys_services_topic_comment
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="topicId != null">topic_id,</if>
<if test="fromUid != null">from_uid,</if>
<if test="fromRole != null">from_role,</if>
<if test="toUid != null">to_uid,</if>
<if test="toRole != null">to_role,</if>
<if test="content != null">content,</if>
<if test="img != null">img,</if>
<if test="topicId != null">create_time,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id},</if>
<if test="topicId != null">#{topicId},</if>
<if test="fromUid != null">#{fromUid},</if>
<if test="fromRole != null">#{fromRole},</if>
<if test="toUid != null">#{toUid},</if>
<if test="toRole != null">#{toRole},</if>
<if test="content != null">#{content},</if>
<if test="img != null">
#{img, jdbcType=OTHER, typeHandler=com.stdiet.custom.typehandler.ArrayJsonHandler},
</if>
<if test="topicId != null">now(),</if>
</trim>
</insert>
<!-- 插入问题回复-->
<insert id="inserSysServicesTopicReply" parameterType="SysServicesTopic" useGeneratedKeys="true" keyColumn="id"
keyProperty="id">
insert into sys_services_topic_reply
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="commentId != null">comment_id,</if>
<if test="replyId != null">reply_id,</if>
<if test="replyType != null">reply_type,</if>
<if test="content != null">content,</if>
<if test="fromUid != null">from_uid,</if>
<if test="toUid != null">to_uid,</if>
<if test="img != null">img,</if>
<if test="fromRole != null">from_role,</if>
<if test="toRole != null">to_role,</if>
<if test="content != null">create_time,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id},</if>
<if test="commentId != null">#{commentId},</if>
<if test="replyId != null">#{replyId},</if>
<if test="replyType != null">#{replyType},</if>
<if test="content != null">#{content},</if>
<if test="fromUid != null">#{fromUid},</if>
<if test="toUid != null">#{toUid},</if>
<if test="img != null">
#{img, jdbcType=OTHER, typeHandler=com.stdiet.custom.typehandler.ArrayJsonHandler},
</if>
<if test="fromRole != null">#{fromRole},</if>
<if test="toRole != null">#{toRole},</if>
<if test="content != null">now(),</if>
</trim>
</insert>
<resultMap type="SysServicesTopic" id="SysServicesCountResult">
<result column="uid" property="uid"/>
<result column="count" property="count"/>
</resultMap>
<select id="selectUnreadTopicCount" parameterType="java.util.List" resultMap="SysServicesCountResult">
<foreach collection="list" item="status" index="index">
<choose>
<when test="index == 0">
SELECT COUNT(*) AS count, uid FROM sys_services_topic_status WHERE `read` = 0 AND uid =
#{status.uid}
</when>
<otherwise>
UNION ALL SELECT COUNT(*) AS count, uid FROM sys_services_topic_status WHERE `read` = 0 AND uid =
#{status.uid}
</otherwise>
</choose>
</foreach>
</select>
</mapper>