<?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.xinyilu.system.mapper.organize.OrganizeInfoMapper">

    <resultMap type="OrganizeInfo" id="OrganizeInfoResult">
        <result property="id" column="id"/>
        <result property="parentId" column="parent_id"/>
        <result property="areaId" column="area_id"/>
        <result property="organizeName" column="organize_name"/>
        <result property="organizeAddress" column="organize_address"/>
        <result property="phone" column="phone"/>
        <result property="contacts" column="contacts"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectOrganizeInfoVo">
        select id, parent_id, area_id, organize_name, organize_address, phone, contacts, del_flag, create_by, create_time, update_by, update_time from organize_info
    </sql>

    <select id="selectOrganizeInfoList" parameterType="OrganizeInfoVO" resultType="organizeInfoDTO">
        SELECT
        oi.id,
        oi.parent_id,
        oi.area_id,
        sa.`name` as areaName,
        oi.organize_name,
        oi.organize_address,
        oi.phone,
        oi.contacts,
        oi.del_flag,
        oi.create_by,
        oi.create_time,
        oi.update_by,
        oi.update_time
        FROM
        organize_info oi LEFT JOIN sys_area sa ON oi.area_id = sa.id
        <where>
            <if test="id != null ">
                and oi.id = #{id}
            </if>
            <if test="areaId != null ">
                and oi.area_id = #{areaId}
            </if>
            <if test="organizeName != null  and organizeName != ''">
                and oi.organize_name like concat('%', #{organizeName}, '%')
            </if>
            <if test="organizeAddress != null  and organizeAddress != ''">
                and oi.organize_address = #{organizeAddress}
            </if>
            <if test="phone != null  and phone != ''">
                and oi.phone = #{phone}
            </if>
            <if test="contacts != null  and contacts != ''">
                and oi.contacts = #{contacts}
            </if>
            <if test="level != null  and level != ''">
                and sa.level = #{level}
            </if>
            and oi.del_flag = 0
        </where>
        order by oi.id asc
    </select>

    <select id="selectOrganizeInfoById" parameterType="Long" resultType="organizeInfoDTO">
        SELECT
            oi.id,
            oi.parent_id,
            oi.area_id,
            sa.`name` as areaName,
            oi.organize_name,
            oi.organize_address,
            oi.phone,
            oi.contacts,
            oi.del_flag,
            oi.create_by,
            oi.create_time,
            oi.update_by,
            oi.update_time
        FROM
            organize_info oi LEFT JOIN sys_area sa ON oi.area_id = sa.id
        where oi.id = #{id} and oi.del_flag = 0
    </select>

    <insert id="insertOrganizeInfo" parameterType="OrganizeInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into organize_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,
            </if>
            <if test="areaId != null">area_id,
            </if>
            <if test="organizeName != null">organize_name,
            </if>
            <if test="organizeAddress != null">organize_address,
            </if>
            <if test="phone != null">phone,
            </if>
            <if test="contacts != null">contacts,
            </if>
            <if test="delFlag != null">del_flag,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},
            </if>
            <if test="areaId != null">#{areaId},
            </if>
            <if test="organizeName != null">#{organizeName},
            </if>
            <if test="organizeAddress != null">#{organizeAddress},
            </if>
            <if test="phone != null">#{phone},
            </if>
            <if test="contacts != null">#{contacts},
            </if>
            <if test="delFlag != null">#{delFlag},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <update id="updateOrganizeInfo" parameterType="OrganizeInfo">
        update organize_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id =
                #{parentId},
            </if>
            <if test="areaId != null">area_id =
                #{areaId},
            </if>
            <if test="organizeName != null">organize_name =
                #{organizeName},
            </if>
            <if test="organizeAddress != null">organize_address =
                #{organizeAddress},
            </if>
            <if test="phone != null">phone =
                #{phone},
            </if>
            <if test="contacts != null">contacts =
                #{contacts},
            </if>
            <if test="delFlag != null">del_flag =
                #{delFlag},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteOrganizeInfoById" parameterType="Long">
        delete from organize_info where id = #{id}
    </delete>

    <delete id="deleteOrganizeInfoByIds" parameterType="String">
        delete from organize_info where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="getOrganizeInfoByName" parameterType="java.lang.String" resultType="int">
        select count(1) as organizeCount from organize_info where organize_name = #{name} and del_flag = 0
    </select>

    <select id="selectOrganizeByKindergartenId"
            resultType="com.xinyilu.system.domain.applogkindergarteninfo.AppLogKindergartenInfo">
        select ki.id kindergartenId,
        ki.organize_id,
        oi.area_id,
        sa.code areaCode
        from kindergarten_info ki
        LEFT JOIN organize_info oi ON ki.organize_id = oi.id
        LEFT JOIN sys_area sa ON sa.id = oi.area_id
        <where>
            ki.id in
            <foreach item="kindergartenIds" collection="list" open="(" separator="," close=")">
                #{kindergartenIds}
            </foreach>
        </where>
    </select>
</mapper>
