<?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.teamuser.ITeamUserDao">

    <resultMap id="teamUserDTO" type="cn.com.tenlion.systembase.pojo.dtos.teamuser.TeamUserDTO">
        <result column="institution_id" property="institutionId"/>
        <result column="institution_name" property="institutionName"/>
        <result column="team_user_id" property="teamUserId"/>
        <result column="team_user_name" property="teamUserName"/>
        <result column="team_user_sex_id" property="teamUserSexId"/>
        <result column="team_user_sex_text" property="teamUserSexText"/>
        <result column="team_user_nation_id" property="teamUserNationId"/>
        <result column="team_user_nation_text" property="teamUserNationText"/>
        <result column="team_user_political_id" property="teamUserPoliticalId"/>
        <result column="team_user_political_text" property="teamUserPoliticalText"/>
        <result column="team_user_card" property="teamUserCard"/>
        <result column="team_user_birthday" property="teamUserBirthday"/>
        <result column="team_user_level_id" property="teamUserLevelId"/>
        <result column="team_user_level_text" property="teamUserLevelText"/>
        <result column="team_user_position_id" property="teamUserPositionId"/>
        <result column="team_user_position_text" property="teamUserPositionText"/>
        <result column="team_user_specialty_id" property="teamUserSpecialtyId"/>
        <result column="team_user_specialty_text" property="teamUserSpecialtyText"/>
        <result column="team_user_eb_id" property="teamUserEbId"/>
        <result column="team_user_eb_text" property="teamUserEbText"/>
        <result column="team_user_phone1" property="teamUserPhone1"/>
        <result column="team_user_phone2" property="teamUserPhone2"/>
        <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="teamUserBO" type="cn.com.tenlion.systembase.pojo.bos.teamuser.TeamUserBO">
        <result column="institution_id" property="institutionId"/>
        <result column="team_user_id" property="teamUserId"/>
        <result column="team_user_name" property="teamUserName"/>
        <result column="team_user_sex_id" property="teamUserSexId"/>
        <result column="team_user_sex_text" property="teamUserSexText"/>
        <result column="team_user_nation_id" property="teamUserNationId"/>
        <result column="team_user_nation_text" property="teamUserNationText"/>
        <result column="team_user_political_id" property="teamUserPoliticalId"/>
        <result column="team_user_political_text" property="teamUserPoliticalText"/>
        <result column="team_user_card" property="teamUserCard"/>
        <result column="team_user_birthday" property="teamUserBirthday"/>
        <result column="team_user_level_id" property="teamUserLevelId"/>
        <result column="team_user_level_text" property="teamUserLevelText"/>
        <result column="team_user_position_id" property="teamUserPositionId"/>
        <result column="team_user_position_text" property="teamUserPositionText"/>
        <result column="team_user_specialty_id" property="teamUserSpecialtyId"/>
        <result column="team_user_specialty_text" property="teamUserSpecialtyText"/>
        <result column="team_user_eb_id" property="teamUserEbId"/>
        <result column="team_user_eb_text" property="teamUserEbText"/>
        <result column="team_user_phone1" property="teamUserPhone1"/>
        <result column="team_user_phone2" property="teamUserPhone2"/>
        <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="teamUserPO" type="cn.com.tenlion.systembase.pojo.pos.teamuser.TeamUserPO">
        <result column="institution_id" property="institutionId"/>
        <result column="team_user_id" property="teamUserId"/>
        <result column="team_user_name" property="teamUserName"/>
        <result column="team_user_sex_id" property="teamUserSexId"/>
        <result column="team_user_sex_text" property="teamUserSexText"/>
        <result column="team_user_nation_id" property="teamUserNationId"/>
        <result column="team_user_nation_text" property="teamUserNationText"/>
        <result column="team_user_political_id" property="teamUserPoliticalId"/>
        <result column="team_user_political_text" property="teamUserPoliticalText"/>
        <result column="team_user_card" property="teamUserCard"/>
        <result column="team_user_birthday" property="teamUserBirthday"/>
        <result column="team_user_level_id" property="teamUserLevelId"/>
        <result column="team_user_level_text" property="teamUserLevelText"/>
        <result column="team_user_position_id" property="teamUserPositionId"/>
        <result column="team_user_position_text" property="teamUserPositionText"/>
        <result column="team_user_specialty_id" property="teamUserSpecialtyId"/>
        <result column="team_user_specialty_text" property="teamUserSpecialtyText"/>
        <result column="team_user_eb_id" property="teamUserEbId"/>
        <result column="team_user_eb_text" property="teamUserEbText"/>
        <result column="team_user_phone1" property="teamUserPhone1"/>
        <result column="team_user_phone2" property="teamUserPhone2"/>
        <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_team_user(
            institution_id,
            team_user_id,
            team_user_name,
            team_user_sex_id,
            team_user_sex_text,
            team_user_nation_id,
            team_user_nation_text,
            team_user_political_id,
            team_user_political_text,
            team_user_card,
            team_user_birthday,
            team_user_level_id,
            team_user_level_text,
            team_user_position_id,
            team_user_position_text,
            team_user_specialty_id,
            team_user_specialty_text,
            team_user_eb_id,
            team_user_eb_text,
            team_user_phone1,
            team_user_phone2,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{institutionId},
            #{teamUserId},
            #{teamUserName},
            #{teamUserSexId},
            #{teamUserSexText},
            #{teamUserNationId},
            #{teamUserNationText},
            #{teamUserPoliticalId},
            #{teamUserPoliticalText},
            #{teamUserCard},
            #{teamUserBirthday},
            #{teamUserLevelId},
            #{teamUserLevelText},
            #{teamUserPositionId},
            #{teamUserPositionText},
            #{teamUserSpecialtyId},
            #{teamUserSpecialtyText},
            #{teamUserEbId},
            #{teamUserEbText},
            #{teamUserPhone1},
            #{teamUserPhone2},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

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

    <!-- 删除综治队伍（物理） -->
    <update id="delete" parameterType="map">
        DELETE FROM
            cg_team_user
        WHERE
            team_user_id IN
        <foreach collection="teamUserIds" index="index" open="(" separator="," close=")">
            #{teamUserIds[${index}]}
        </foreach>
    </update>

    <!-- 修改综治队伍 -->
    <update id="update" parameterType="map">
        UPDATE
            cg_team_user
        SET
        <if test="institutionId != null and institutionId != ''">
            institution_id = #{institutionId},
        </if>
        <if test="teamUserName != null and teamUserName != ''">
            team_user_name = #{teamUserName},
        </if>
        <if test="teamUserSexId != null">
            team_user_sex_id = #{teamUserSexId},
        </if>
        <if test="teamUserSexText != null and teamUserSexText != ''">
            team_user_sex_text = #{teamUserSexText},
        </if>
        <if test="teamUserNationId != null">
            team_user_nation_id = #{teamUserNationId},
        </if>
        <if test="teamUserNationText != null and teamUserNationText != ''">
            team_user_nation_text = #{teamUserNationText},
        </if>
        <if test="teamUserPoliticalId != null">
            team_user_political_id = #{teamUserPoliticalId},
        </if>
        <if test="teamUserPoliticalText != null and teamUserPoliticalText != ''">
            team_user_political_text = #{teamUserPoliticalText},
        </if>
        <if test="teamUserCard != null and teamUserCard != ''">
            team_user_card = #{teamUserCard},
        </if>
        <if test="teamUserBirthday != null and teamUserBirthday != ''">
            team_user_birthday = #{teamUserBirthday},
        </if>
        <if test="teamUserLevelId != null">
            team_user_level_id = #{teamUserLevelId},
        </if>
        <if test="teamUserLevelText != null and teamUserLevelText != ''">
            team_user_level_text = #{teamUserLevelText},
        </if>
        <if test="teamUserPositionId != null">
            team_user_position_id = #{teamUserPositionId},
        </if>
        <if test="teamUserPositionText != null and teamUserPositionText != ''">
            team_user_position_text = #{teamUserPositionText},
        </if>
        <if test="teamUserSpecialtyId != null and teamUserSpecialtyId != ''">
            team_user_specialty_id = #{teamUserSpecialtyId},
        </if>
        <if test="teamUserSpecialtyText != null and teamUserSpecialtyText != ''">
            team_user_specialty_text = #{teamUserSpecialtyText},
        </if>
        <if test="teamUserEbId != null">
            team_user_eb_id = #{teamUserEbId},
        </if>
        <if test="teamUserEbText != null and teamUserEbText != ''">
            team_user_eb_text = #{teamUserEbText},
        </if>
        <if test="teamUserPhone1 != null and teamUserPhone1 != ''">
            team_user_phone1 = #{teamUserPhone1},
        </if>
        <if test="teamUserPhone2 != null and teamUserPhone2 != ''">
            team_user_phone2 = #{teamUserPhone2},
        </if>
            gmt_modified = #{gmtModified},
            modifier = #{modifier}
        WHERE
            team_user_id = #{teamUserId}
    </update>

    <!-- 综治队伍详情 -->
    <select id="get" parameterType="map" resultMap="teamUserDTO">
        SELECT
            t1.institution_id,
            t1.team_user_name,
            t1.team_user_sex_id,
            t1.team_user_sex_text,
            t1.team_user_nation_id,
            t1.team_user_nation_text,
            t1.team_user_political_id,
            t1.team_user_political_text,
            t1.team_user_card,
            t1.team_user_birthday,
            t1.team_user_level_id,
            t1.team_user_level_text,
            t1.team_user_position_id,
            t1.team_user_position_text,
            t1.team_user_specialty_id,
            t1.team_user_specialty_text,
            t1.team_user_eb_id,
            t1.team_user_eb_text,
            t1.team_user_phone1,
            t1.team_user_phone2,
            t1.team_user_id
        FROM
            cg_team_user t1
        WHERE
            t1.is_delete = 0
            <if test="teamUserId != null and teamUserId != ''">
        AND
            t1.team_user_id = #{teamUserId}
            </if>
    </select>

    <!-- 综治队伍详情 -->
    <select id="getBO" parameterType="map" resultMap="teamUserBO">
        SELECT
            t1.institution_id,
            t1.team_user_id,
            t1.team_user_name,
            t1.team_user_sex_id,
            t1.team_user_sex_text,
            t1.team_user_nation_id,
            t1.team_user_nation_text,
            t1.team_user_political_id,
            t1.team_user_political_text,
            t1.team_user_card,
            t1.team_user_birthday,
            t1.team_user_level_id,
            t1.team_user_level_text,
            t1.team_user_position_id,
            t1.team_user_position_text,
            t1.team_user_specialty_id,
            t1.team_user_specialty_text,
            t1.team_user_eb_id,
            t1.team_user_eb_text,
            t1.team_user_phone1,
            t1.team_user_phone2,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            cg_team_user t1
        WHERE
            t1.is_delete = 0
            <if test="teamUserId != null and teamUserId != ''">
        AND
            t1.team_user_id = #{teamUserId}
            </if>
    </select>

    <!-- 综治队伍详情 -->
    <select id="getPO" parameterType="map" resultMap="teamUserPO">
        SELECT
            t1.institution_id,
            t1.team_user_id,
            t1.team_user_name,
            t1.team_user_sex_id,
            t1.team_user_sex_text,
            t1.team_user_nation_id,
            t1.team_user_nation_text,
            t1.team_user_political_id,
            t1.team_user_political_text,
            t1.team_user_card,
            t1.team_user_birthday,
            t1.team_user_level_id,
            t1.team_user_level_text,
            t1.team_user_position_id,
            t1.team_user_position_text,
            t1.team_user_specialty_id,
            t1.team_user_specialty_text,
            t1.team_user_eb_id,
            t1.team_user_eb_text,
            t1.team_user_phone1,
            t1.team_user_phone2,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            cg_team_user t1
        WHERE
            t1.is_delete = 0
            <if test="teamUserId != null and teamUserId != ''">
        AND
            t1.team_user_id = #{teamUserId}
            </if>
    </select>

    <!-- 综治队伍列表 -->
    <select id="list" parameterType="map" resultMap="teamUserDTO">
        SELECT
            t1.institution_id,
            t1.team_user_id,
            t1.team_user_name,
            t1.team_user_sex_id,
            t1.team_user_sex_text,
            t1.team_user_nation_id,
            t1.team_user_nation_text,
            t1.team_user_political_id,
            t1.team_user_political_text,
            t1.team_user_card,
            t1.team_user_birthday,
            t1.team_user_level_id,
            t1.team_user_level_text,
            t1.team_user_position_id,
            t1.team_user_position_text,
            t1.team_user_specialty_id,
            t1.team_user_specialty_text,
            t1.team_user_eb_id,
            t1.team_user_eb_text,
            t1.team_user_phone1,
            t1.team_user_phone2,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t2.institution_name
        FROM
            cg_team_user t1
        LEFT JOIN cg_institution t2
        ON t1.institution_id = t2.institution_id
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            t1.team_user_name LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.team_user_phone1 LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.team_user_phone2 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="institutionId != null and institutionId != ''">
            AND t1.institution_id = #{institutionId}
        </if>
        <if test="teamUserName != null and teamUserName != ''">
            AND t1.team_user_name = #{teamUserName}
        </if>
        <if test="teamUserSexId != null and teamUserSexId != ''">
            AND t1.team_user_sex_id = #{teamUserSexId}
        </if>
        <if test="teamUserNationId != null and teamUserNationId != ''">
            AND t1.team_user_nation_id = #{teamUserNationId}
        </if>
        <if test="teamUserPoliticalId != null and teamUserPoliticalId != ''">
            AND t1.team_user_political_id = #{teamUserPoliticalId}
        </if>
        <if test="teamUserCard != null and teamUserCard != ''">
            AND t1.team_user_card = #{teamUserCard}
        </if>
        <if test="teamUserLevelId != null and teamUserLevelId != ''">
            AND t1.team_user_level_id = #{teamUserLevelId}
        </if>
        <if test="teamUserPositionId != null and teamUserPositionId != ''">
            AND t1.team_user_position_id = #{teamUserPositionId}
        </if>
        <if test="teamUserEbId != null and teamUserEbId != ''">
            AND t1.team_user_eb_id = #{teamUserEbId}
        </if>
        <if test="teamUserPhone1 != null and teamUserPhone1 != ''">
            AND t1.team_user_phone1 = #{teamUserPhone1}
        </if>
        <if test="teamUserPhone2 != null and teamUserPhone2 != ''">
            AND t1.team_user_phone2 = #{teamUserPhone2}
        </if>
        <if test="teamUserBirthday != null and teamUserBirthday != ''">
            AND t1.team_user_birthday = #{teamUserBirthday}
        </if>
        <if test="teamUserSpecialtyIds != null and teamUserSpecialtyIds.size > 0">
        AND
            <foreach  item="teamUserSpecialtyIds" collection="teamUserSpecialtyIds" index="index" open="(" separator="AND" close=")">
                t1.team_user_specialty_id LIKE CONCAT('%', #{teamUserSpecialtyIds}, '%')
            </foreach>
        </if>
        ORDER BY t1.gmt_create DESC
    </select>

    <!-- 综治队伍列表 -->
    <select id="listBO" parameterType="map" resultMap="teamUserBO">
        SELECT
            t1.institution_id,
            t1.team_user_id,
            t1.team_user_name,
            t1.team_user_sex_id,
            t1.team_user_sex_text,
            t1.team_user_nation_id,
            t1.team_user_nation_text,
            t1.team_user_political_id,
            t1.team_user_political_text,
            t1.team_user_card,
            t1.team_user_birthday,
            t1.team_user_level_id,
            t1.team_user_level_text,
            t1.team_user_position_id,
            t1.team_user_position_text,
            t1.team_user_specialty_id,
            t1.team_user_specialty_text,
            t1.team_user_eb_id,
            t1.team_user_eb_text,
            t1.team_user_phone1,
            t1.team_user_phone2,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            cg_team_user 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>
        <if test="teamUserIds != null and teamUserIds.size > 0">
        AND
            t1.team_user_id IN
            <foreach collection="teamUserIds" index="index" open="(" separator="," close=")">
                #{teamUserIds[${index}]}
            </foreach>
        </if>
        ORDER BY t1.gmt_create DESC
    </select>

    <!-- 综治队伍列表 -->
    <select id="listPO" parameterType="map" resultMap="teamUserPO">
        SELECT
            t1.institution_id,
            t1.team_user_id,
            t1.team_user_name,
            t1.team_user_sex_id,
            t1.team_user_sex_text,
            t1.team_user_nation_id,
            t1.team_user_nation_text,
            t1.team_user_political_id,
            t1.team_user_political_text,
            t1.team_user_card,
            t1.team_user_birthday,
            t1.team_user_level_id,
            t1.team_user_level_text,
            t1.team_user_position_id,
            t1.team_user_position_text,
            t1.team_user_specialty_id,
            t1.team_user_specialty_text,
            t1.team_user_eb_id,
            t1.team_user_eb_text,
            t1.team_user_phone1,
            t1.team_user_phone2,
            t1.creator,
            t1.gmt_create,
            t1.modifier,
            t1.gmt_modified,
            t1.is_delete
        FROM
            cg_team_user 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>
        <if test="teamUserIds != null and teamUserIds.size > 0">
        AND
            t1.team_user_id IN
            <foreach collection="teamUserIds" index="index" open="(" separator="," close=")">
                #{teamUserIds[${index}]}
            </foreach>
        </if>
        ORDER BY t1.gmt_create DESC
    </select>

    <!-- 综治队伍统计  -->
    <select id="count" parameterType="map" resultType="Integer">
        SELECT
            COUNT(*)
        FROM
            cg_team_user t1
        WHERE
            t1.is_delete = 0
        <if test="institutionId != null and institutionId != ''">
            AND t1.institution_id = #{institutionId}
        </if>
    </select>

</mapper>