<?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.lyms.platform.permission.dao.master.MasterDepartmentsMapper">

    <resultMap id="DepartmentsResultMap" type="com.lyms.platform.permission.model.Departments">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="parent_id" property="parentId" jdbcType="INTEGER"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="org_id" property="orgId" jdbcType="INTEGER"/>
        <result column="description" property="description" jdbcType="VARCHAR"/>
        <result column="publish_id" property="publishId" jdbcType="INTEGER"/>
        <result column="publish_name" property="publishName" jdbcType="VARCHAR"/>
        <result column="yn" property="yn" jdbcType="INTEGER"/>
        <result column="modified" property="modified" jdbcType="TIMESTAMP"/>
        <result column="created" property="created" jdbcType="TIMESTAMP"/>
        <result column="type" property="type" jdbcType="INTEGER"/>
        <result column="shortCode" property="shortCode" jdbcType="VARCHAR"/>
        <result column="foreign_id" property="foreignId" jdbcType="VARCHAR"/>
    </resultMap>


    <insert id="addDepartments" parameterType="com.lyms.platform.permission.model.Departments">
        <selectKey order="AFTER" keyProperty="id" resultType="java.lang.Integer">SELECT LAST_INSERT_ID()</selectKey>
        insert into departments
        (foreign_id,parent_id,name,org_id,description,publish_id,publish_name,yn,modified,created,type,shortCode
        <if test="id != null and id >= 0">
            ,id
        </if>

        ) values
        (#{foreignId},#{parentId},#{name},#{orgId},#{description},#{publishId},#{publishName},#{yn},#{modified},#{created},#{type},#{shortCode}
        <if test="id != null and id >= 0">
            ,#{id}
        </if>
        )
    </insert>


    <update id="updateDepartments" parameterType="com.lyms.platform.permission.model.Departments">
        update departments
        <set>
            <if test="parentId != null and parentId >= 0">
                parent_id = #{parentId,jdbcType=INTEGER},
            </if>
            <if test="name != null and name != ''">
                name = #{name,jdbcType=VARCHAR},
            </if>
            <if test="orgId != null and orgId >= 0">
                org_id = #{orgId,jdbcType=INTEGER},
            </if>
            <if test="foreignId != null and foreignId != ''">
                foreign_id = #{foreignId,jdbcType=INTEGER},
            </if>
            <if test="description != null">
                description = #{description,jdbcType=VARCHAR},
            </if>
            <if test="publishId != null and publishId >= 0">
                publish_id = #{publishId,jdbcType=INTEGER},
            </if>
            <if test="publishName != null and publishName != ''">
                publish_name = #{publishName,jdbcType=VARCHAR},
            </if>
            <if test="yn != null and yn >= 0">
                yn = #{yn,jdbcType=INTEGER},
            </if>
            <if test="modified != null">
                modified = #{modified,jdbcType=TIMESTAMP},
            </if>
            <if test="created != null">
                created = #{created,jdbcType=TIMESTAMP},
            </if>
            <if test="type != null and type >= 0">
                type = #{type,jdbcType=INTEGER},
            </if>
            <if test="shortCode != null">
                shortCode = #{shortCode,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>


    <delete id="deleteDepartments" parameterType="java.lang.Integer">
delete from departments where id = #{id,jdbcType=INTEGER}
</delete>


    <select id="getDepartments" resultMap="DepartmentsResultMap" parameterType="java.lang.Integer">
select id,parent_id,name,org_id,description,publish_id,publish_name,yn,modified,created,type,shortCode,foreign_id
 from departments where id = #{id,jdbcType=INTEGER}
</select>


    <sql id="orderAndLimit">
        <if test="sort != null and sort != '' ">
            order by ${sort}
            <if test="need != null">
                limit #{offset, jdbcType=INTEGER} , #{limit, jdbcType=INTEGER}
            </if>
        </if>
    </sql>


    <sql id="DepartmentsCondition">
        <where>
            1 = 1
            <if test="id != null and id >= 0">
                and id = #{id,jdbcType=INTEGER}
            </if>
            <if test="parentId != null and parentId >= 0">
                and parent_id = #{parentId,jdbcType=INTEGER}
            </if>
            <if test="foreignId != null and foreignId != ''">
                and foreign_id = #{foreignId,jdbcType=VARCHAR}
            </if>
            <if test="name != null and name != ''">
                and name = #{name,jdbcType=VARCHAR}
            </if>
            <if test="orgId != null and orgId >= 0">
                and org_id = #{orgId,jdbcType=INTEGER}
            </if>
            <if test="description != null and description != ''">
                and description = #{description,jdbcType=VARCHAR}
            </if>
            <if test="publishId != null and publishId >= 0">
                and publish_id = #{publishId,jdbcType=INTEGER}
            </if>
            <if test="publishName != null and publishName != ''">
                and publish_name = #{publishName,jdbcType=VARCHAR}
            </if>
            <if test="yn != null and yn >= 0">
                and yn = #{yn,jdbcType=INTEGER}
            </if>
            <if test="modified != null">
                and modified = #{modified,jdbcType=TIMESTAMP}
            </if>
            <if test="created != null">
                and created = #{created,jdbcType=TIMESTAMP}
            </if>
            <if test="type != null and type >= 0">
                and type = #{type,jdbcType=INTEGER}
            </if>
            <if test="shortCode != null and shortCode != ''">
                and shortCode = #{shortCode,jdbcType=VARCHAR}
            </if>
            <if test="keyword != null and keyword != ''">
                and name like CONCAT(#{keyword}, '%')
            </if>
            <if test="orgList != null and orgList.size() > 0">
                and org_id in
                <foreach collection="orgList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </sql>


    <select id="queryDepartments" resultMap="DepartmentsResultMap"
            parameterType="com.lyms.platform.permission.model.DepartmentsQuery">
        select
        id,parent_id,name,org_id,description,publish_id,publish_name,yn,modified,created,type,shortCode,foreign_id
        from departments
        <include refid="DepartmentsCondition"/>
        <include refid="orderAndLimit"/>
    </select>


    <select id="queryDepartmentsCount" resultType="int"
            parameterType="com.lyms.platform.permission.model.DepartmentsQuery">
        select count(1) from departments
        <include refid="DepartmentsCondition"/>
    </select>


</mapper>