<?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="cn.iocoder.yudao.module.knowledge.mapper.TExpertMaintenanceMapper">

    <resultMap type="cn.iocoder.yudao.module.knowledge.entity.TExpertMaintenance" id="tExpertMaintenanceMap">
            <result property="id" column="id"/>
            <result property="tagNum" column="tag_num"/>
            <result property="tagName" column="tag_name"/>
            <result property="expertNum" column="expert_num"/>
            <result property="expertIds" column="expert_ids"/>
            <result property="createTime" column="create_time"/>
            <result property="updateTime" column="update_time"/>
            <result property="deptId" column="dept_id"/>
            <result property="identityId" column="identity_id"/>
            <result property="deleted" column="deleted"/>
        <result property="expertNames" column="expert_names"/>
    </resultMap>

    <!-- 用于select查询公用抽取的列 -->
    <sql id="columns">
        id,tag_num,tag_name,expert_num,expert_ids,create_time,update_time,dept_id,identity_id,deleted
    </sql>
    <update id="deleteByid">
        update t_expert_maintenance set deleted = 1 where id = #{id}
    </update>
    <select id="findList" resultMap="tExpertMaintenanceMap">
        select
        tem.id,tem.tag_num,tem.tag_name,tem.create_time,tem.update_time,tem.dept_id,tem.identity_id,tem.deleted,GROUP_CONCAT(te.username) as expert_names,sum(IF(te.id is null,0, 1)) as expert_num
        from t_expert_maintenance tem
        left join t_expert te on te.domain_expert = tem.id
        <where>
            tem.deleted = 0
                    <if test="params.tagNum != null and params.tagNum != ''">
                          and  tem.tag_num like CONCAT( '%',#{params.tagNum}, '%')
                    </if>
                    <if test="params.tagName != null and params.tagName != ''">
                        and  tem.tag_name  LIKE CONCAT( '%',#{params.tagName},'%')
                    </if>
                    <if test="params.expertNum != null">
                        and tem.expert_num = #{params.expertNum}
                    </if>
                    <if test="params.expertIds != null and params.expertIds != ''">
                        and   tem.expert_ids like CONCAT( '%',#{params.expertIds}, '%')
                    </if>
                    <if test="params.createTime != null">
                        and tem.create_time = #{params.createTime}
                    </if>
                    <if test="params.updateTime != null">
                        and  tem.update_time = #{params.updateTime}
                    </if>
        </where>
        group by tem.id
        ORDER BY
        tem.create_time DESC
    </select>

    <select id="selectList" resultMap="tExpertMaintenanceMap">
        select
        tem.id,tem.tag_num,tem.tag_name,tem.create_time,tem.update_time,tem.dept_id,tem.identity_id,tem.deleted,GROUP_CONCAT(te.username) as expert_names,count(1) as expert_num
        from t_expert_maintenance tem
        left join t_expert te on te.domain_expert = tem.id
        <where>
            tem.deleted = 0
            <if test="params.tagNum != null and params.tagNum != ''">
                and  tem.tag_num like CONCAT( '%',#{params.tagNum}, '%')
            </if>
            <if test="params.tagName != null and params.tagName != ''">
                and  tem.tag_name  LIKE CONCAT( '%',#{params.tagName},'%')
            </if>
            <if test="params.expertNum != null">
                and tem.expert_num = #{params.expertNum}
            </if>
            <if test="params.expertIds != null and params.expertIds != ''">
                and   tem.expert_ids like CONCAT( '%',#{params.expertIds}, '%')
            </if>
            <if test="params.createTime != null">
                and tem.create_time = #{params.createTime}
            </if>
            <if test="params.updateTime != null">
                and  tem.update_time = #{params.updateTime}
            </if>
        </where>
        group by tem.id
        ORDER BY
        tem.create_time DESC
    </select>

    <select id="selectExpertTags" resultType="cn.iocoder.yudao.module.knowledge.controller.responsevo.ExpertTags">
        SELECT
        t.id
        ,t.tag_name
        , substring_index(substring_index(t.expert_ids, ',', b.help_topic_id + 1), ',', - 1) AS expertTagNum
        FROM t_expert_maintenance t
        INNER JOIN mysql.help_topic b
        ON (length(t.expert_ids) - length(REPLACE(t.expert_ids, ',', '')) + 1) > b.help_topic_id  having expertTagNum = #{id}
    </select>

</mapper>
