<?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.MasterOrganizationMapper">

    <resultMap id="OrganizationResultMap" type="com.lyms.platform.permission.model.Organization">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="type" property="type" jdbcType="INTEGER"/>
        <result column="level" property="level" jdbcType="INTEGER"/>
        <result column="area_manage" property="areaManage" jdbcType="INTEGER"/>
        <result column="province_id" property="provinceId" jdbcType="VARCHAR"/>
        <result column="city_id" property="cityId" jdbcType="VARCHAR"/>
        <result column="street_id" property="streetId" jdbcType="VARCHAR"/>
        <result column="area_id" property="areaId" jdbcType="VARCHAR"/>
        <result column="address" property="address" jdbcType="VARCHAR"/>
        <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="shortCode" property="shortCode" jdbcType="VARCHAR"/>
        <result column="foreign_id" property="foreignId" jdbcType="VARCHAR"/>
        <result column="bStatus" property="bStatus" jdbcType="INTEGER"/>

    </resultMap>


    <insert id="addOrganization" parameterType="com.lyms.platform.permission.model.Organization">
        <selectKey order="AFTER" keyProperty="id" resultType="java.lang.Integer">SELECT LAST_INSERT_ID()</selectKey>
        insert into organization
        (foreign_id,name,type,level,area_manage,province_id,city_id,area_id,address,description,publish_id,publish_name,yn,modified,created,shortCode,street_id,bStatus
        <if test="id != null and id >= 0">
            ,id
        </if>
        )
        values
        (#{foreignId},#{name},#{type},#{level},#{areaManage},#{provinceId},#{cityId},#{areaId},#{address},#{description},#{publishId},#{publishName},#{yn},#{modified},#{created},#{shortCode},#{streetId},#{bStatus}
        <if test="id != null and id >= 0">
            ,#{id}
        </if>
        )
    </insert>


    <update id="updateOrganization" parameterType="com.lyms.platform.permission.model.Organization">
        update organization
        <set>
            <if test="foreignId != null and foreignId != ''">
                foreign_id = #{foreignId,jdbcType=VARCHAR},
            </if>
            <if test="name != null and name != ''">
                name = #{name,jdbcType=VARCHAR},
            </if>
            <if test="type != null and type >= 0">
                type = #{type,jdbcType=INTEGER},
            </if>
            <if test="level != null and level >= 0">
                level = #{level,jdbcType=INTEGER},
            </if>
            <if test="areaManage != null and areaManage >= 0">
                area_manage = #{areaManage,jdbcType=INTEGER},
            </if>
            <if test="provinceId != null">
                province_id = #{provinceId,jdbcType=INTEGER},
            </if>
            <if test="bStatus !=null">
                bStatus=#{bStatus,jdbcType=INTEGER},
            </if>
            <if test="cityId != null">
                city_id = #{cityId,jdbcType=INTEGER},
            </if>
            <if test="areaId != null">
                area_id = #{areaId,jdbcType=INTEGER},
            </if>
            <if test="streetId != null">
                street_id = #{streetId,jdbcType=INTEGER},
            </if>
            <if test="address != null and address != ''">
                address = #{address,jdbcType=VARCHAR},
            </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="shortCode != null">
                shortCode = #{shortCode,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                status = #{status,jdbcType=INTEGER},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>


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


    <select id="getOrganization" resultMap="OrganizationResultMap" parameterType="java.lang.Integer">
select id,name,type,level,area_manage,province_id,city_id,area_id,address,description,publish_id,publish_name,yn,modified,created,shortCode,foreign_id,street_id,status,bStatus
 from organization 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="OrganizationCondition">
        <where>
            1 = 1
            <if test="id != null and id >= 0">
                and id = #{id,jdbcType=INTEGER}
            </if>
            <if test="idList !=null  and idList.size() >0">
                and id in
                <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </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="type != null and type >= 0">
                and type = #{type,jdbcType=INTEGER}
            </if>
            <if test="level != null and level >= 0">
                and level = #{level,jdbcType=INTEGER}
            </if>
            <if test="areaManage != null and areaManage >= 0">
                and area_manage = #{areaManage,jdbcType=INTEGER}
            </if>
            <if test="provinceId != null">
                and province_id = #{provinceId,jdbcType=INTEGER}
            </if>
            <if test="cityId != null">
                and city_id = #{cityId,jdbcType=INTEGER}
            </if>
            <if test="areaId != null">
                and area_id = #{areaId,jdbcType=INTEGER}
            </if>
            <if test="streetId != null">
                and street_id = #{streetId,jdbcType=INTEGER}
            </if>
            <if test="address != null and address != ''">
                and address = #{address,jdbcType=VARCHAR}
            </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="shortCode != null and shortCode != ''">
                and shortCode = #{shortCode,jdbcType=VARCHAR}
            </if>
            <if test="keyword != null and keyword != ''">
                and name like CONCAT(#{keyword}, '%')
            </if>
            <if test="typeList != null and typeList.size() > 0">
                and type in
                <foreach collection="typeList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="provinceIds != null and provinceIds.size() > 0">
                and province_id in
                <foreach collection="provinceIds" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>

            <if test="status != null">
                and status = #{status,jdbcType=INTEGER}
            </if>
        </where>
    </sql>


    <select id="queryOrganization" resultMap="OrganizationResultMap"
            parameterType="com.lyms.platform.permission.model.OrganizationQuery">
        select
        id,name,type,level,area_manage,province_id,city_id,area_id,address,description,publish_id,publish_name,yn,modified,created,shortCode,foreign_id,street_id,status,bStatus
        from organization
        <include refid="OrganizationCondition"/>
        <include refid="orderAndLimit"/>
    </select>


    <select id="queryOrganizationCount" resultType="int"
            parameterType="com.lyms.platform.permission.model.OrganizationQuery">
        select count(1) from organization
        <include refid="OrganizationCondition"/>
    </select>

    <select id="queryProvinceIdByOrgId" parameterType="java.util.ArrayList" resultType="java.lang.String">

        SELECT DISTINCT province_id from organization where yn=1 and province_id is NOT null

        <if test="list != null and list.size() > 0">
            and id in
            <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>
    <select id="queryHospital" resultMap="OrganizationResultMap">
        select * from organization where yn=1 and type in(2,3,4,5,6,7)
            </select>
</mapper>