<?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.machine.MachineMapper">

    <resultMap id="xxMap" type="com.cf.imes.module.system.controller.admin.machine.vo.OrganMachineResp">
        <result property="organId" column="organId"/>
        <result property="organName" column="organName"/>
        <result property="updateTime" column="updateTime"/>
        <collection property="authMachineList" javaType="java.util.List" ofType="com.cf.imes.module.system.controller.admin.machine.vo.OrganTemplateVO"
                    select="selectOrganMachineList" column="{organId=organId, machineName = machineName}" notNullColumn="machineName" >
        </collection>
    </resultMap>

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

    <select id="selectOrganMachinePage" resultMap="xxMap">
        select *  from (
        select a.id as organId, a.name as organName, max(b.create_time) as updateTime,
        case when ('${vo.machineName}' != '') then '${vo.machineName}' else '' end as machineName
        from system_organization a, system_machine_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_limit b inner join system_machine c on b.machine_id = c.id and c.deleted = 0
        <where>
            <if test="vo.machineName !=null and vo.machineName !='' " >
                c.name like concat('%',#{vo.machineName},'%')
            </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="selectOrganMachineList"
            resultType="com.cf.imes.module.system.controller.admin.machine.vo.OrganMachineVO">
        select b.organ_id organId, b.show_prior_facing, b.show_dual_workstation, b.show_auto_note_printer,
        c.name AS machineName, c.id AS machineId, c.machine_type
        from system_machine_limit b
        inner join system_machine c on b.machine_id = c.id and c.deleted = 0
        <where>
            b.organ_id = #{organId}
            <if test="machineName != null and machineName != ''">
                and c.name like concat('%',#{machineName},'%')
            </if>
        </where>
    </select>

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

    </select>


</mapper>