<?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.ruoyi.system.mapper.TWarehouseUserMapper">

    <resultMap type="TWarehouseUser" id="TWarehouseUserResult">
        <result property="userId"    column="user_id"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="warehouseName"    column="warehouse_name"    />
    </resultMap>

    <sql id="selectTWarehouseUserVo">
        select user_id, warehouse_id, warehouse_name from t_warehouse_user
    </sql>

    <select id="selectTWarehouseUserList" parameterType="TWarehouseUser" resultMap="TWarehouseUserResult">
        <include refid="selectTWarehouseUserVo"/>
        <where>
            <if test="warehouseName != null  and warehouseName != ''"> and warehouse_name like concat('%', #{warehouseName}, '%')</if>
        </where>
    </select>

    <select id="selectWarehouseListByUserId" parameterType="Long" resultType="Long">
        select warehouse_id
        from t_warehouse_user
        where user_id = #{userId}
    </select>

    <select id="selectTWarehouseUserById" parameterType="Long" resultMap="TWarehouseUserResult">
        <include refid="selectTWarehouseUserVo"/>
        where user_id = #{userId}
    </select>

    <insert id="insertTWarehouseUser" parameterType="TWarehouseUser">
        insert into t_warehouse_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="warehouseName != null and warehouseName != ''">warehouse_name,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="warehouseName != null and warehouseName != ''">#{warehouseName},</if>
         </trim>
    </insert>

    <update id="updateTWarehouseUser" parameterType="TWarehouseUser">
        update t_warehouse_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="warehouseName != null and warehouseName != ''">warehouse_name = #{warehouseName},</if>
        </trim>
        where user_id = #{userId}
    </update>

    <delete id="deleteTWarehouseUserById" parameterType="Long">
        delete from t_warehouse_user where user_id = #{userId}
    </delete>

    <delete id="deleteTWarehouseUserByIds" parameterType="String">
        delete from t_warehouse_user where user_id in
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>

    <insert id="batchWarehouseUser">
        insert into t_warehouse_user(user_id, warehouse_id, warehouse_name) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.userId}, #{item.warehouseId}, #{item.warehouseName})
        </foreach>
    </insert>

    <select id="getWarehouseName" parameterType="Long" resultType="map">
        select f_name as warehouseName
        from t_warehouse
        where f_id = #{warehouseId}
    </select>

    <select id="biWarehouseList" parameterType="Long" resultType="map">
        select
            warehouse_id as warehouseId,
            warehouse_name as warehouseName
        from t_warehouse_user
        where user_id = #{userId}
        order by convert(warehouse_name using gbk)
    </select>

</mapper>
