<?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.bonus.material.ma.mapper.TypeKeeperMapper">
    <resultMap type="com.bonus.material.ma.domain.TypeKeeper" id="TypeKeeperResult">
        <result property="ID"    column="ID"    />
        <result property="typeId"    column="type_id"    />
        <result property="userId"    column="user_id"    />
        <result property="userName"    column="user_name"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="companyId"    column="company_id"    />
    </resultMap>

    <sql id="selectTypeKeeperVo">
        select ID, type_id, user_id, create_time, update_time, company_id from ma_type_keeper
    </sql>

    <select id="selectTypeKeeperList" parameterType="com.bonus.material.ma.domain.TypeKeeper" resultMap="TypeKeeperResult">
        <include refid="selectTypeKeeperVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="companyId != null  and companyId != ''"> and company_id = #{companyId}</if>
        </where>
    </select>
    
    <select id="selectTypeKeeperByID" parameterType="Long" resultMap="TypeKeeperResult">
        <include refid="selectTypeKeeperVo"/>
        where ID = #{ID}
    </select>

    <select id="selectTypeKeeperByUserIdAndTypeId" parameterType="Long" resultMap="TypeKeeperResult">
        <include refid="selectTypeKeeperVo"/>
        where type_id = #{typeId} and user_id = #{userId}
        limit 1
    </select>
        
    <insert id="insertTypeKeeper" parameterType="com.bonus.material.ma.domain.TypeKeeper" useGeneratedKeys="true" keyProperty="ID">
        insert into ma_type_keeper (type_id,user_id,create_time,update_time,company_id)
        values
        <foreach collection="list" item="item" separator=",">
            (
                #{item.typeId},
                #{item.userId},
                #{item.createTime},
                #{item.updateTime},
                #{item.companyId}
            )
        </foreach>
    </insert>

    <update id="updateTypeKeeper" parameterType="com.bonus.material.ma.domain.TypeKeeper">
        update ma_type_keeper
        <trim prefix="SET" suffixOverrides=",">
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            update_time = #{updateTime},
            <if test="companyId != null">company_id = #{companyId},</if>
        </trim>
        where ID = #{ID}
    </update>

    <delete id="deleteTypeKeeperByID" parameterType="Long">
        delete from ma_type_keeper where ID = #{ID}
    </delete>

    <delete id="deleteTypeKeeperByIDs" parameterType="String">
        delete from ma_type_keeper where ID in 
        <foreach item="ID" collection="array" open="(" separator="," close=")">
            #{ID}
        </foreach>
    </delete>

    <select id="selectTypeKeeperListAndUserName" resultMap="TypeKeeperResult">
        select
            tk.ID, tk.type_id, tk.user_id, tk.create_time, tk.update_time, tk.company_id, u.user_name
        from
            ma_type_keeper tk
        left join
            sys_user u on tk.user_id = u.user_id
        <where>
            <if test="userId != null "> and tk.user_id = #{userId}</if>
            <if test="typeId != null "> and tk.type_id = #{typeId}</if>
            <if test="companyId != null "> and tk.company_id = #{companyId}</if>
        </where>
    </select>

    <delete id="deleteTypeKeeperByUserIdAndTypeId">
        delete from ma_type_keeper where
        type_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.typeId}
        </foreach>
        and
        user_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.userId}
        </foreach>
    </delete>
</mapper>