<?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.aucma.base.mapper.BaseTeamMembersMapper">

    <resultMap type="BaseTeamMembers" id="BaseTeamMembersResult">
            <result property="objId"    column="obj_id"    />
            <result property="teamCode"    column="team_code"    />
            <result property="teamName"    column="team_name"    />
            <result property="teamHead"    column="team_head"    />
            <result property="productLineCode"    column="product_line_code"    />
            <result property="productLineName"    column="productLineName"    />
            <result property="isFlag"    column="is_flag"    />
            <result property="createdBy"    column="created_by"    />
            <result property="createdTime"    column="created_time"    />
            <result property="updatedBy"    column="updated_by"    />
            <result property="updatedTime"    column="updated_time"    />
            <result property="startTime"    column="start_time"    />
            <result property="endTime"    column="end_time"    />
            <result property="dineTime"    column="dine_time"    />
            <result property="classes"    column="classes"    />
    </resultMap>

    <sql id="selectBaseTeamMembersVo">
        select tm.obj_id,
               tm.team_code,
               tm.team_name,
               tm.team_head,
               tm.product_line_code,
               bpl.PRODUCT_LINE_NAME productLineName,
               tm.is_flag,
               tm.created_by,
               tm.created_time,
               tm.updated_by,
               tm.updated_time,
               tm.start_time,
               tm.end_time,
               tm.dine_time,
               tm.classes
        from base_teammembers tm
                 left join base_productline bpl on bpl.PRODUCT_LINE_CODE = tm.PRODUCT_LINE_CODE
    </sql>

    <select id="selectBaseTeamMembersList" parameterType="BaseTeamMembers" resultMap="BaseTeamMembersResult">
        <include refid="selectBaseTeamMembersVo"/>
        <where>
                        <if test="teamCode != null  and teamCode != ''"> and tm.team_code = #{teamCode}</if>
                        <if test="classes != null  and classes != ''"> and tm.classes = #{classes}</if>
                        <if test="teamName != null  and teamName != ''"> and tm.team_name like concat(concat('%', #{teamName}), '%')</if>
                        <if test="teamHead != null  and teamHead != ''"> and tm.team_head = #{teamHead}</if>
                        <if test="productLineCode != null  and productLineCode != ''"> and tm.product_line_code = #{productLineCode}</if>
                        <if test="isFlag != null "> and tm.is_flag = #{isFlag}</if>
                        <if test="createdBy != null  and createdBy != ''"> and tm.created_by = #{createdBy}</if>
                        <if test="createdTime != null "> and tm.created_time = #{createdTime}</if>
                        <if test="updatedBy != null  and updatedBy != ''"> and tm.updated_by = #{updatedBy}</if>
                        <if test="updatedTime != null "> and tm.updated_time = #{updatedTime}</if>
        </where>
        ORDER BY tm.team_code
    </select>

    <select id="selectBaseTeamMembersByObjId" parameterType="Long" resultMap="BaseTeamMembersResult">
            <include refid="selectBaseTeamMembersVo"/>
            where tm.obj_id = #{objId}
    </select>

    <select id="teamUserList" parameterType="BaseTeamMembers" resultMap="BaseTeamMembersResult">
        select tm.obj_id,
               tm.team_code,
               tm.team_name,
               tm.team_head,
               tm.product_line_code,
               bpl.PRODUCT_LINE_NAME productLineName,
               tm.is_flag,
               tm.created_by,
               tm.created_time,
               tm.updated_by,
               tm.updated_time,
               tm.start_time,
               tm.end_time,
               tm.dine_time,
               tm.classes,
               u.nick_name nickName,
               u.user_name userName
        from base_teammembers tm
                 left join base_productline bpl on bpl.PRODUCT_LINE_CODE = tm.PRODUCT_LINE_CODE
                 left join sys_user u on tm.TEAM_CODE = u.TEAM_CODE
        <where>
            u.user_name is not null
            <if test="teamCode != null  and teamCode != ''"> and tm.team_code = #{teamCode}</if>
            <if test="classes != null  and classes != ''"> and tm.classes = #{classes}</if>
            <if test="teamName != null  and teamName != ''"> and tm.team_name like concat(concat('%', #{teamName}), '%')</if>
            <if test="teamHead != null  and teamHead != ''"> and tm.team_head = #{teamHead}</if>
            <if test="productLineCode != null  and productLineCode != ''"> and tm.product_line_code = #{productLineCode}</if>
        </where>
    </select>

    <insert id="insertBaseTeamMembers" parameterType="BaseTeamMembers">
            <selectKey keyProperty="objId" resultType="long" order="BEFORE">
                SELECT seq_base_teammembers.NEXTVAL as objId FROM DUAL
            </selectKey>
        insert into base_teammembers
        <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="objId != null">obj_id,</if>
                <if test="teamCode != null">team_code,</if>
                <if test="teamName != null">team_name,</if>
                <if test="teamHead != null">team_head,</if>
                <if test="productLineCode != null">product_line_code,</if>
                <if test="isFlag != null">is_flag,</if>
                <if test="createdBy != null">created_by,</if>
                <if test="createdTime != null">created_time,</if>
                <if test="updatedBy != null">updated_by,</if>
                <if test="updatedTime != null">updated_time,</if>
                <if test="startTime != null">start_time,</if>
                <if test="endTime != null">end_time,</if>
                <if test="dineTime != null">dine_time,</if>
                <if test="classes != null">classes,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="objId != null">#{objId},</if>
                <if test="teamCode != null">#{teamCode},</if>
                <if test="teamName != null">#{teamName},</if>
                <if test="teamHead != null">#{teamHead},</if>
                <if test="productLineCode != null">#{productLineCode},</if>
                <if test="isFlag != null">#{isFlag},</if>
                <if test="createdBy != null">#{createdBy},</if>
                <if test="createdTime != null">#{createdTime},</if>
                <if test="updatedBy != null">#{updatedBy},</if>
                <if test="updatedTime != null">#{updatedTime},</if>
                <if test="startTime != null">#{startTime},</if>
                <if test="endTime != null">#{endTime},</if>
                <if test="dineTime != null">#{dineTime},</if>
                <if test="classes != null">#{classes},</if>
        </trim>
    </insert>

    <update id="updateBaseTeamMembers" parameterType="BaseTeamMembers">
        update base_teammembers
        <trim prefix="SET" suffixOverrides=",">
                    <if test="teamCode != null">team_code = #{teamCode},</if>
                    <if test="teamName != null">team_name = #{teamName},</if>
                    <if test="teamHead != null">team_head = #{teamHead},</if>
                    <if test="productLineCode != null">product_line_code = #{productLineCode},</if>
                    <if test="isFlag != null">is_flag = #{isFlag},</if>
                    <if test="createdBy != null">created_by = #{createdBy},</if>
                    <if test="createdTime != null">created_time = #{createdTime},</if>
                    <if test="updatedBy != null">updated_by = #{updatedBy},</if>
                    <if test="updatedTime != null">updated_time = #{updatedTime},</if>
                    <if test="startTime != null">start_time = #{startTime},</if>
                    <if test="endTime != null">end_time = #{endTime},</if>
                    <if test="dineTime != null">dine_time = #{dineTime},</if>
                    <if test="classes != null">classes = #{classes},</if>
        </trim>
        where obj_id = #{objId}
    </update>

    <delete id="deleteBaseTeamMembersByObjId" parameterType="Long">
        delete from base_teammembers where obj_id = #{objId}
    </delete>

    <delete id="deleteBaseTeamMembersByObjIds" parameterType="String">
        delete from base_teammembers where obj_id in
        <foreach item="objId" collection="array" open="(" separator="," close=")">
            #{objId}
        </foreach>
    </delete>
</mapper>