<?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.cf.imes.module.system.dal.mysql.machinetemplate.MachineTemplateMapper">

    <resultMap id="map" type="com.cf.imes.module.system.controller.admin.machine.vo.AuthTemplateResp">
        <result property="nickname" column="nickname"/>
        <result property="organId" column="organ_id"/>
        <result property="userId" column="userId"/>
        <result property="organName" column="organName"/>
        <result property="updateTime" column="updateTime"/>
        <collection property="authTemplateList" javaType="java.util.List" ofType="com.cf.imes.module.system.controller.admin.machine.vo.AutoTemplateVO"
                    select="selectAuthTemplateList" column="{userId=userId, templateName = templateName}" >
        </collection>
    </resultMap>

    <resultMap id="orgMap" type="com.cf.imes.module.system.controller.admin.machine.vo.OrganTemplateResp">
        <result property="organId" column="organId"/>
        <result property="organName" column="organName"/>
        <result property="updateTime" column="updateTime"/>
        <collection property="authTemplateList" javaType="java.util.List" ofType="com.cf.imes.module.system.controller.admin.machine.vo.OrganTemplateVO"
                    select="selectOrganTemplateList" column="{organId=organId}"  >
        </collection>
    </resultMap>

    <resultMap id="xxMap" type="com.cf.imes.module.system.controller.admin.machine.vo.OrganTemplateResp">
        <result property="organId" column="organId"/>
        <result property="organName" column="organName"/>
        <result property="updateTime" column="updateTime"/>
        <collection property="authTemplateList" javaType="java.util.List" ofType="com.cf.imes.module.system.controller.admin.machine.vo.OrganTemplateVO"
                    select="selectOrganTemplateList" column="{organId=organId, templateName = templateName}" notNullColumn="templateId" >
        </collection>
        <!--<collection property="authTemplateList" javaType="java.util.List" ofType="com.cf.imes.module.system.controller.admin.machine.vo.OrganTemplateVO">
            <result property="templateName" column="templateName"/>
            <result property="templateId" column="templateId"/>
        </collection>-->
    </resultMap>

    <select id="selectAuthTemplatePage" resultMap="map" parameterType="com.cf.imes.module.system.controller.admin.machine.vo.AuthTemplatePage">
        select a.id AS userId, a.nickname, a.organ_id, max(b.create_time) as updateTime, f.name as organName,
        case when ('${vo.templateName}' != '') then '${vo.templateName}' else '' end as templateName
        from system_users a, user_machine b, system_organization f
        <where>
            a.id = b.user_id
            and a.organ_id = f.id
            and a.deleted = 0
            <if test="vo.organId != null">
               and a.organ_id = #{vo.organId}
            </if>
            <if test="vo.nickname != null and vo.nickname != '' ">
               and a.nickname like concat('%',#{vo.nickname},'%')
            </if>
        </where>
        group by a.id
    </select>

    <select id="selectAuthTemplateList"
            resultType="com.cf.imes.module.system.controller.admin.machine.vo.AutoTemplateVO" parameterType="java.util.Map">
        select b.user_id, c.name AS templateName, c.id AS templateId
        from user_machine b
        inner join system_machine_template c on b.machine_id = c.id and c.deleted = 0
        <where>
            b.user_id = #{userId}
            <if test="templateName != null and templateName != ''">
               and c.name like concat('%',#{templateName},'%')
            </if>
        </where>
    </select>

    <!--<select id="selectOrganTemplatePage" resultMap="orgMap">
        select a.id as organId, a.name as organName, max(b.create_time) as updateTime,
        case when ('${vo.templateName}' != '') then '${vo.templateName}' else '' end as templateName
        from system_organization a, system_machine_template_limit b
        <where>
            a.id = b.organ_id
            <if test="vo.organId != null">
                and a.id = #{vo.organId}
            </if>
        </where>
        GROUP BY organId
        order by updateTime desc

    </select>-->

    <select id="selectOrganTemplateList"
            resultType="com.cf.imes.module.system.controller.admin.machine.vo.OrganTemplateVO">
        select b.organ_id organId, b.show_prior_facing, b.show_dual_workstation, b.show_auto_note_printer,
               c.name AS templateName, c.id AS templateId, c.machine_type
        from system_machine_template_limit b
        inner join system_machine_template c on b.machine_id = c.id and c.deleted = 0
        <where>
            b.organ_id = #{organId}
            <if test="templateName != null and templateName != ''">
                and c.name like concat('%',#{templateName},'%')
            </if>
        </where>
    </select>

    <select id="selectOrganTemplatePage" resultMap="xxMap">
        select *  from (
            select a.id as organId, a.name as organName, max(b.create_time) as updateTime,
            case when ('${vo.templateName}' != '') then '${vo.templateName}' else '' end as templateName
            from system_organization a, system_machine_template_limit b
            <where>
                a.id = b.organ_id
                <if test="vo.organId !=null">
                    and a.id = #{vo.organId}
                </if>
            </where>
          GROUP BY organId
        ) t1
            LEFT JOIN (
            select b.organ_id t2_organId
            from system_machine_template_limit b inner join system_machine_template c on b.machine_id = c.id and c.deleted = 0
            <where>
                <if test="vo.templateName !=null and vo.templateName !='' " >
                    c.name like concat('%',#{vo.templateName},'%')
                </if>
            </where>
        ) t2 on t1.organId = t2.t2_organId
        where t2.t2_organId is not null
        GROUP BY organId
        order by updateTime desc

    </select>

    <select id="selectOrganTemplate" resultMap="orgMap">
        select a.id as organId, a.name as organName, max(b.create_time) as updateTime
        from system_organization a, system_machine_template_limit b
        <where>
            a.id = b.organ_id
            and a.id = #{vo.organId}
        </where>
        GROUP BY organId

    </select>
</mapper>