<?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.inventory.mapper.WarehousesMapper">

    <resultMap type="Warehouses" id="WarehousesResult">
        <result property="id"    column="id"    />
        <result property="warehousesCode"    column="warehouses_code"    />
        <result property="warehousesName"    column="warehouses_name"    />
        <result property="warehouseType"    column="warehouse_type"    />
        <result property="address"    column="address"    />
        <result property="area"    column="area"    />
        <result property="capacity"    column="capacity"    />
        <result property="manager"    column="manager"    />
        <result property="contactPhone"    column="contact_phone"    />
        <result property="isEnabled"    column="is_enabled"    />
        <result property="isUsed"    column="is_used"    />
        <result property="sortNo"    column="sort_no"    />
        <result property="createTime"    column="create_time"    />
        <result property="createUserCode"    column="create_user_code"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateUserCode"    column="update_user_code"    />
    </resultMap>

    <sql id="selectWarehousesVo">
        select id, warehouses_code, warehouses_name, warehouse_type, address, area, capacity, manager, contact_phone, is_enabled, is_used, sort_no, create_time, create_user_code, update_time, update_user_code from warehouses
    </sql>

    <select id="selectWarehousesList" parameterType="Warehouses" resultMap="WarehousesResult">
        <include refid="selectWarehousesVo"/>
            where is_used=1 and id != #{localId}
        <if test="warehousesCode != null  and warehousesCode != ''"> and warehouses_code like concat('%', #{warehousesCode}, '%')</if>
            <if test="warehousesName != null  and warehousesName != ''"> and warehouses_name like concat('%', #{warehousesName}, '%')</if>
            <if test="warehouseType != null "> and warehouse_type = #{warehouseType}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="area != null "> and area = #{area}</if>
            <if test="capacity != null "> and capacity = #{capacity}</if>
        <if test="manager != null  and manager != ''"> and manager like concat('%', #{manager}, '%')</if>
            <if test="contactPhone != null  and contactPhone != ''"> and contact_phone = #{contactPhone}</if>
            <if test="isEnabled != null "> and is_enabled = #{isEnabled}</if>
            <if test="sortNo != null "> and sort_no = #{sortNo}</if>
            <if test="createUserCode != null  and createUserCode != ''"> and create_user_code = #{createUserCode}</if>
            <if test="updateUserCode != null  and updateUserCode != ''"> and update_user_code = #{updateUserCode}</if>

    </select>

    <select id="selectWarehousesById" parameterType="String" resultMap="WarehousesResult">
        <include refid="selectWarehousesVo"/>
        where id = #{id}
    </select>

    <select id="selectWarehousesByName" parameterType="String" resultMap="WarehousesResult">
        <include refid="selectWarehousesVo"/>
        where warehouses_name = #{name} and is_used = 1
        limit 1
    </select>
<!--    获取仓库 的 warehouses_code 仓库编码 warehouses_name 做成字典-->
    <select id="getMapList" resultType="java.util.Map">
        select id, IFNULL(warehouses_name, '') as warehouses_name from warehouses where is_used = 1 and id != 'local';
    </select>

    <insert id="insertWarehouses" parameterType="Warehouses">
        insert into warehouses
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="warehousesCode != null">warehouses_code,</if>
            <if test="warehousesName != null">warehouses_name,</if>
            <if test="warehouseType != null">warehouse_type,</if>
            <if test="address != null">address,</if>
            <if test="area != null">area,</if>
            <if test="capacity != null">capacity,</if>
            <if test="manager != null">manager,</if>
            <if test="contactPhone != null">contact_phone,</if>
            <if test="isEnabled != null">is_enabled,</if>
            <if test="isUsed != null">is_used,</if>
            <if test="sortNo != null">sort_no,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createUserCode != null">create_user_code,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateUserCode != null">update_user_code,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="warehousesCode != null">#{warehousesCode},</if>
            <if test="warehousesName != null">#{warehousesName},</if>
            <if test="warehouseType != null">#{warehouseType},</if>
            <if test="address != null">#{address},</if>
            <if test="area != null">#{area},</if>
            <if test="capacity != null">#{capacity},</if>
            <if test="manager != null">#{manager},</if>
            <if test="contactPhone != null">#{contactPhone},</if>
            <if test="isEnabled != null">#{isEnabled},</if>
            <if test="isUsed != null">#{isUsed},</if>
            <if test="sortNo != null">#{sortNo},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createUserCode != null">#{createUserCode},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateUserCode != null">#{updateUserCode},</if>
         </trim>
    </insert>

    <update id="updateWarehouses" parameterType="Warehouses">
        update warehouses
        <trim prefix="SET" suffixOverrides=",">
            <if test="warehousesCode != null">warehouses_code = #{warehousesCode},</if>
            <if test="warehousesName != null">warehouses_name = #{warehousesName},</if>
            <if test="warehouseType != null">warehouse_type = #{warehouseType},</if>
            <if test="address != null">address = #{address},</if>
            <if test="area != null">area = #{area},</if>
            <if test="capacity != null">capacity = #{capacity},</if>
            <if test="manager != null">manager = #{manager},</if>
            <if test="contactPhone != null">contact_phone = #{contactPhone},</if>
            <if test="isEnabled != null">is_enabled = #{isEnabled},</if>
            <if test="isUsed != null">is_used = #{isUsed},</if>
            <if test="sortNo != null">sort_no = #{sortNo},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createUserCode != null">create_user_code = #{createUserCode},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateUserCode != null">update_user_code = #{updateUserCode},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteWarehousesById" parameterType="String">
        delete from warehouses where id = #{id}
    </delete>

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

    <insert id="batchInsertWarehouses" parameterType="java.util.List">
        insert into warehouses
        (
        id, warehouses_code, warehouses_name, warehouse_type, address, area,
        capacity, manager, contact_phone, is_enabled, is_used, sort_no,
        create_time, create_user_code, update_time, update_user_code
        )
        values
        <foreach collection="list" item="item" separator=",">
            (
            #{item.id}, #{item.warehousesCode}, #{item.warehousesName},
            #{item.warehouseType}, #{item.address}, #{item.area},
            #{item.capacity}, #{item.manager}, #{item.contactPhone},
            #{item.isEnabled}, #{item.isUsed}, #{item.sortNo},
            #{item.createTime}, #{item.createUserCode}, #{item.updateTime},
            #{item.updateUserCode}
            )
        </foreach>
    </insert>
</mapper>