<?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.organizationmanagement.dao.organization.IOrganizationDao">

    <resultMap id="organizationDTO" type="cn.com.tenlion.organizationmanagement.pojo.dtos.organization.OrganizationDTO">
        <result column="organization_id" property="organizationId"/>
        <result column="organization_name" property="organizationName"/>
        <result column="organization_user_num" property="organizationUserNum"/>
        <result column="organization_principal" property="organizationPrincipal"/>
        <result column="organization_desc" property="organizationDesc"/>
        <result column="organization_image" property="organizationImage"/>
        <result column="organization_type" property="organizationType"/>
        <result column="creator" property="creator"/>
        <result column="gmt_create" property="gmtCreate"/>
    </resultMap>

    <resultMap id="organizationBO" type="cn.com.tenlion.organizationmanagement.pojo.bos.organization.OrganizationBO">
        <result column="organization_id" property="organizationId"/>
        <result column="organization_name" property="organizationName"/>
        <result column="organization_user_num" property="organizationUserNum"/>
        <result column="organization_principal" property="organizationPrincipal"/>
        <result column="organization_desc" property="organizationDesc"/>
        <result column="organization_image" property="organizationImage"/>
        <result column="organization_type" property="organizationType"/>
        <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="organizationPO" type="cn.com.tenlion.organizationmanagement.pojo.pos.organization.OrganizationPO">
        <result column="organization_id" property="organizationId"/>
        <result column="organization_name" property="organizationName"/>
        <result column="organization_user_num" property="organizationUserNum"/>
        <result column="organization_principal" property="organizationPrincipal"/>
        <result column="organization_desc" property="organizationDesc"/>
        <result column="organization_image" property="organizationImage"/>
        <result column="organization_type" property="organizationType"/>
        <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 m_organization(
            organization_id,
            organization_name,
            organization_user_num,
            organization_principal,
            organization_desc,
            organization_image,
            organization_type,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{organizationId},
            #{organizationName},
            #{organizationUserNum},
            #{organizationPrincipal},
            #{organizationDesc},
            #{organizationImage},
            #{organizationType},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除组织管理 -->
    <update id="remove" parameterType="map">
        UPDATE
            m_organization
        SET
            gmt_modified = #{gmtModified},
            modifier = #{modifier},
            is_delete = 1
        WHERE
            organization_id IN
        <foreach collection="organizationIds" index="index" open="(" separator="," close=")">
            #{organizationIds[${index}]}
        </foreach>
    </update>

    <!-- 删除组织管理（物理） -->
    <update id="delete" parameterType="map">
        DELETE FROM
            m_organization
        WHERE
            organization_id IN
        <foreach collection="organizationIds" index="index" open="(" separator="," close=")">
            #{organizationIds[${index}]}
        </foreach>
    </update>

    <!-- 修改组织管理 -->
    <update id="update" parameterType="map">
        UPDATE
            m_organization
        SET
            organization_name = #{organizationName},
            organization_user_num = #{organizationUserNum},
            organization_principal = #{organizationPrincipal},
            organization_desc = #{organizationDesc},
            organization_image = #{organizationImage},
            <if test="organizationType != null and organizationType != ''">
                organization_type = #{organizationType},
            </if>
            gmt_modified = #{gmtModified},
            modifier = #{modifier}
        WHERE
            organization_id = #{organizationId}
    </update>

    <!-- 组织管理详情 -->
    <select id="get" parameterType="map" resultMap="organizationDTO">
        SELECT
            t1.organization_name,
            t1.organization_user_num,
            t1.organization_principal,
            t1.organization_desc,
            t1.organization_image,
            t1.organization_type,
            t1.organization_id
        FROM
            m_organization t1
        WHERE
            t1.is_delete = 0
        <if test="organizationId != null and organizationId != ''">
            AND
            t1.organization_id = #{organizationId}
        </if>
    </select>

    <!-- 组织管理详情 -->
    <select id="getBO" parameterType="map" resultMap="organizationBO">
        SELECT
            t1.organization_id,
            t1.organization_name,
            t1.organization_user_num,
            t1.organization_principal,
            t1.organization_desc,
            t1.organization_image,
            t1.organization_type,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            m_organization t1
        WHERE
            t1.is_delete = 0
        <if test="organizationId != null and organizationId != ''">
            AND
            t1.organization_id = #{organizationId}
        </if>
    </select>

    <!-- 组织管理详情 -->
    <select id="getPO" parameterType="map" resultMap="organizationPO">
        SELECT
            t1.organization_id,
            t1.organization_name,
            t1.organization_user_num,
            t1.organization_principal,
            t1.organization_desc,
            t1.organization_image,
            t1.organization_type,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            m_organization t1
        WHERE
            t1.is_delete = 0
        <if test="organizationId != null and organizationId != ''">
            AND
            t1.organization_id = #{organizationId}
        </if>
    </select>

    <!-- 组织管理列表 -->
    <select id="list" parameterType="map" resultMap="organizationDTO">
        SELECT
            t1.organization_id,
            t1.organization_name,
            t1.organization_user_num,
            t1.organization_principal,
            t1.organization_desc,
            t1.organization_image,
            t1.organization_type,
            t1.creator,
            t1.gmt_create
        FROM
            m_organization t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            <!-- 这里添加其他条件 -->
            t1.organization_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="organizationIds != null and organizationIds.size > 0">
        AND
            t1.organization_id IN
            <foreach collection="organizationIds" index="index" open="(" separator="," close=")">
                #{organizationIds[${index}]}
            </foreach>
        </if>
         <if test="organizationType != null and organizationType != ''">
             AND
             t1.organization_type = #{organizationType}
        </if>
        <if test="organizationTypes != null and organizationTypes.size > 0">
            AND
            t1.organization_type IN
            <foreach collection="organizationTypes" index="index" open="(" separator="," close=")">
                #{organizationTypes[${index}]}
            </foreach>
        </if>
        ORDER BY t1.gmt_create DESC
    </select>

    <!-- 组织管理列表 -->
    <select id="listBO" parameterType="map" resultMap="organizationBO">
        SELECT
            t1.organization_id,
            t1.organization_name,
            t1.organization_user_num,
            t1.organization_principal,
            t1.organization_desc,
            t1.organization_image,
            t1.organization_type,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            m_organization t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            <!-- 这里添加其他条件 -->
            t1.organization_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="organizationIds != null and organizationIds.size > 0">
        AND
            t1.organization_id IN
            <foreach collection="organizationIds" index="index" open="(" separator="," close=")">
                #{organizationIds[${index}]}
            </foreach>
        </if>
        <if test="organizationType != null and organizationType != ''">
            AND
            t1.organization_type = #{organizationType}
        </if>
        ORDER BY t1.gmt_create DESC
    </select>

    <!-- 组织管理列表 -->
    <select id="listPO" parameterType="map" resultMap="organizationPO">
        SELECT
            t1.organization_id,
            t1.organization_name,
            t1.organization_user_num,
            t1.organization_principal,
            t1.organization_desc,
            t1.organization_image,
            t1.organization_type,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            m_organization t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            <!-- 这里添加其他条件 -->
            t1.organization_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="organizationIds != null and organizationIds.size > 0">
        AND
            t1.organization_id IN
            <foreach collection="organizationIds" index="index" open="(" separator="," close=")">
                #{organizationIds[${index}]}
            </foreach>
        </if>
        <if test="organizationType != null and organizationType != ''">
            AND
            t1.organization_type = #{organizationType}
        </if>
        ORDER BY t1.gmt_create DESC
    </select>

    <!-- 组织管理统计  -->
    <select id="count" parameterType="map" resultType="Integer">
        SELECT
            COUNT(*)
        FROM
            m_organization t1
        WHERE
            t1.is_delete = 0
    </select>

</mapper>