<?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="cn.com.tenlion.systembase.dao.institution.IInstitutionDao">

    <resultMap id="institutionDTO" type="cn.com.tenlion.systembase.pojo.dtos.institution.InstitutionDTO">
        <result column="institution_id" property="institutionId"/>
        <result column="institution_name" property="institutionName"/>
        <result column="institution_type_id" property="institutionTypeId"/>
        <result column="institution_type_text" property="institutionTypeText"/>
        <result column="institution_level_id" property="institutionLevelId"/>
        <result column="institution_level_text" property="institutionLevelText"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="location" property="location"/>
        <result column="location_code" property="locationCode"/>
        <result column="grid" property="grid"/>
    </resultMap>

    <resultMap id="institutionBO" type="cn.com.tenlion.systembase.pojo.bos.institution.InstitutionBO">
        <result column="institution_id" property="institutionId"/>
        <result column="institution_name" property="institutionName"/>
        <result column="institution_type_id" property="institutionTypeId"/>
        <result column="institution_type_text" property="institutionTypeText"/>
        <result column="institution_level_id" property="institutionLevelId"/>
        <result column="institution_level_text" property="institutionLevelText"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <resultMap id="institutionPO" type="cn.com.tenlion.systembase.pojo.pos.institution.InstitutionPO">
        <result column="institution_id" property="institutionId"/>
        <result column="institution_name" property="institutionName"/>
        <result column="institution_type_id" property="institutionTypeId"/>
        <result column="institution_type_text" property="institutionTypeText"/>
        <result column="institution_level_id" property="institutionLevelId"/>
        <result column="institution_level_text" property="institutionLevelText"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="modifier" property="modifier"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="is_delete" property="isDelete"/>
    </resultMap>

    <!-- 新增综治机构 -->
    <insert id="save" parameterType="map">
        INSERT INTO cg_institution(
            institution_id,
            institution_name,
            institution_type_id,
            institution_type_text,
            institution_level_id,
            institution_level_text,
            location,
            location_code,
            grid,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{institutionId},
            #{institutionName},
            #{institutionTypeId},
            #{institutionTypeText},
            #{institutionLevelId},
            #{institutionLevelText},
            #{location},
            #{locationCode},
            #{grid},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除综治机构 -->
    <update id="remove" parameterType="map">
        UPDATE
            cg_institution
        SET
            gmt_modified = #{gmtModified},
            modifier = #{modifier},
            is_delete = 1
        WHERE
        institution_id IN
        <foreach collection="institutionIds" index="index" open="(" separator="," close=")">
            #{institutionIds[${index}]}
        </foreach>
    </update>

    <!-- 删除综治机构（物理） -->
    <update id="delete" parameterType="map">
        DELETE FROM
            cg_institution
        WHERE
        institution_id IN
        <foreach collection="institutionIds" index="index" open="(" separator="," close=")">
            #{institutionIds[${index}]}
        </foreach>
    </update>

    <!-- 修改综治机构 -->
    <update id="update" parameterType="map">
        UPDATE
            cg_institution
        SET
        <if test="institutionName != null and institutionName != ''">
            institution_name = #{institutionName},
        </if>
        <if test="institutionTypeId != null">
            institution_type_id = #{institutionTypeId},
        </if>
        <if test="institutionTypeText != null and institutionTypeText != ''">
            institution_type_text = #{institutionTypeText},
        </if>
        <if test="institutionLevelId != null">
            institution_level_id = #{institutionLevelId},
        </if>
        <if test="institutionLevelText != null and institutionLevelText != ''">
            institution_level_text = #{institutionLevelText},
        </if>
        <if test="location != null and location != ''">
            location = #{location},
        </if>
        <if test="locationCode != null and locationCode != ''">
            location_code = #{locationCode},
        </if>
        <if test="grid != null and grid != ''">
            grid = #{grid},
        </if>
            gmt_modified = #{gmtModified},
            modifier = #{modifier}
        WHERE
            institution_id = #{institutionId}
    </update>

    <!-- 综治机构详情 -->
    <select id="get" parameterType="map" resultMap="institutionDTO">
        SELECT
            t1.institution_id,
            t1.institution_name,
            t1.institution_type_id,
            t1.institution_type_text,
            t1.institution_level_id,
            t1.location,
            t1.location_code,
            t1.grid,
            t1.institution_level_text
        FROM
            cg_institution t1
        WHERE
            t1.is_delete = 0
        AND
            t1.institution_id = #{institutionId}
    </select>

    <!-- 综治机构详情 -->
    <select id="getBO" parameterType="map" resultMap="institutionBO">
        SELECT
            t1.institution_id,
            t1.institution_name,
            t1.institution_type_id,
            t1.institution_type_text,
            t1.institution_level_id,
            t1.institution_level_text,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            cg_institution t1
        WHERE
            t1.is_delete = 0
        AND
            t1.institution_id = #{institutionId}
    </select>

    <!-- 综治机构详情 -->
    <select id="getPO" parameterType="map" resultMap="institutionPO">
        SELECT
            t1.institution_id,
            t1.institution_name,
            t1.institution_type_id,
            t1.institution_type_text,
            t1.institution_level_id,
            t1.institution_level_text,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            cg_institution t1
        WHERE
            t1.is_delete = 0
        AND
            t1.institution_id = #{institutionId}
    </select>

    <!-- 综治机构列表 -->
    <select id="list" parameterType="map" resultMap="institutionDTO">
        SELECT
            t1.institution_id,
            t1.institution_name,
            t1.institution_type_id,
            t1.institution_type_text,
            t1.institution_level_id,
            t1.institution_level_text,
            t1.location,
            t1.location_code,
            t1.grid,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified
        FROM
            cg_institution t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            t1.institution_name LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="institutionTypeId != null and institutionTypeId != ''">
            AND t1.institution_type_id = #{institutionTypeId}
        </if>
        <if test="institutionLevelId != null and institutionLevelId != ''">
            AND t1.institution_level_id = #{institutionLevelId}
        </if>
    </select>

    <!-- 综治机构列表 -->
    <select id="listBO" parameterType="map" resultMap="institutionBO">
        SELECT
            t1.institution_id,
            t1.institution_name,
            t1.institution_type_id,
            t1.institution_type_text,
            t1.institution_level_id,
            t1.institution_level_text,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            cg_institution t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            <!-- 这里添加其他条件 -->
            t1.id LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
    </select>

    <!-- 综治机构列表 -->
    <select id="listPO" parameterType="map" resultMap="institutionPO">
        SELECT
            t1.institution_id,
            t1.institution_name,
            t1.institution_type_id,
            t1.institution_type_text,
            t1.institution_level_id,
            t1.institution_level_text,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            cg_institution t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            <!-- 这里添加其他条件 -->
            t1.id LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
    </select>

    <!-- 综治机构统计  -->
    <select id="count" parameterType="map" resultType="Integer">
        SELECT
            COUNT(*)
        FROM
            cg_institution t1
        WHERE
            t1.is_delete = 0
    </select>

</mapper>