<?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.casic.missiles.mapper.system.SystemTemplateMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.casic.missiles.model.system.SystemTemplate">
        <id column="id" property="id" />
        <result column="template_no" property="templateNo" />
        <result column="template_name" property="templateName" />
        <result column="template_type" property="templateType" />
        <result column="certification_type" property="certificationType" />
        <result column="template_desc" property="templateDesc" />
        <result column="template_creator" property="templateCreator" />
        <result column="minio_file_name" property="minioFileName" />
        <result column="is_del" property="isDel" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, template_no, template_name, template_type, certification_type, template_desc, template_creator, minio_file_name, is_del, create_time, update_time
    </sql>

    <resultMap id="TemplateDetailResultMap" type="com.casic.missiles.dto.system.TemplateDetailResponse">
        <id column="id" property="id" />
        <result column="template_no" property="templateNo" />
        <result column="template_name" property="templateName" />
        <result column="template_desc" property="templateDesc" />
        <result column="template_creator" property="templateCreator" />
        <result column="template_type" property="templateType" />
        <result column="minio_file_name" property="minioFileName" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />

        <collection property="userList" javaType="list" ofType="com.casic.missiles.dto.system.SysUserDTO">
            <result column="USER_ID" property="id"/>
            <result column="ACCOUNT" property="account"/>
            <result column="NAME" property="name"/>
            <result column="DEPT_ID" property="deptId"/>
        </collection>
    </resultMap>

    <select id="selectMaxTemplateNo" resultType="java.lang.Long">
        SELECT IFNULL(max(RIGHT(template_no, 12)), 0) from system_template
    </select>

    <select id="selectTemplateDetailById" resultMap="TemplateDetailResultMap">
        SELECT st.id, st.template_no, st.template_name, st.template_desc, st.template_creator, st.template_type,
        st.minio_file_name, st.create_time, st.update_time, su.ID AS USER_ID, su.ACCOUNT AS ACCOUNT,
        su.NAME AS NAME, su.DEPT_ID AS DEPT_ID
        FROM system_template st
        LEFT JOIN system_template_user_relation stur ON st.id = stur.template_id
        LEFT JOIN sys_user su ON su.id = stur.user_id
        WHERE st.id = #{id} AND st.is_del = 0
    </select>
</mapper>
