<?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.com.ctop.alarm.modules.mapper.RuleAccountTemplateMapper">

    <select id="getTemplateApplied" parameterType="Map"
            resultType="cn.com.ctop.alarm.modules.entity.vo.TemplateAppliedVo">
        select
        t1.id id,
        t1.template_status templateStatus,
        t2.account_status accountStatus,
        t1.account_id accountId,
        t2.auth_name authName,
        t1.template_id templateId,
        (
        SELECT
        template_name
        FROM
        ctop_rule_template
        WHERE
        id = t1.template_id
        ) as templateName,
        t3.id projectId,
        t3.project_name projectName,
        t2.user_id userId,
        t4.realname userName
        FROM
        ctop_rule_account_template t1
        LEFT JOIN ctop_user_allocation t2 ON t1.account_id = t2.account_id
        LEFT JOIN ctop_project t3 ON t2.project_id = t3.id
        left join sys_user t4 on t2.user_id = t4.id
        where 1 = 1
        <if test="params.userId !=null and params.userId !='' ">
            and t2.user_id = #{params.userId}
        </if>

        <if test="params.accountId !=null and params.accountId !='' ">
            and t2.account_id = #{params.accountId}
        </if>

        <if test="params.accountId !=null and params.accountId !='' ">
            and t2.account_id = #{params.accountId}
        </if>
        <if test="params.projectName !=null and params.projectName !='' ">
            and t3.project_name like concat(concat('%',#{params.projectName}),'%')
        </if>
        <if test="params.userName !=null and params.userName !='' ">
            and t4.realname like concat(concat('%',#{params.userName}),'%')
        </if>
        order by t1.update_time desc
    </select>

</mapper>