<?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.zg.project.information.mapper.WarehouseInfoMapper">

    <!-- ========= 映射 ========= -->
    <resultMap id="WarehouseInfoResult" type="WarehouseInfo">
        <result property="id"                   column="id"/>
        <result property="warehouseCode"        column="warehouse_code"/>
        <result property="warehouseName"        column="warehouse_name"/>
        <result property="address"              column="address"/>

        <!-- 新增：所属大仓 -->
        <result property="parentWarehouseCode"  column="parent_warehouse_code"/>
        <result property="parentWarehouseName"  column="parent_warehouse_name"/>

        <result property="status"               column="status"/>
        <result property="sort"                 column="sort"/>
        <result property="remark"               column="remark"/>
        <result property="createdAt"            column="created_at"/>
        <result property="updatedAt"            column="updated_at"/>
    </resultMap>

    <!-- ========= 通用列 ========= -->
    <sql id="selectWarehouseInfoVo">
        SELECT
            id,
            warehouse_code,
            warehouse_name,
            address,
            parent_warehouse_code,
            parent_warehouse_name,
            status,
            sort,
            remark,
            created_at,
            updated_at
        FROM warehouse_info
    </sql>

    <!-- ========= 列表查询 ========= -->
    <select id="selectWarehouseInfoList" parameterType="WarehouseInfo" resultMap="WarehouseInfoResult">
        <include refid="selectWarehouseInfoVo"/>
        <where>
            <if test="warehouseCode != null and warehouseCode != ''">
                AND warehouse_code = #{warehouseCode}
            </if>
            <if test="warehouseName != null and warehouseName != ''">
                AND warehouse_name LIKE concat('%', #{warehouseName}, '%')
            </if>
            <if test="address != null and address != ''">
                AND address = #{address}
            </if>

            <!-- 新增：按所属大仓筛选 -->
            <if test="parentWarehouseCode != null and parentWarehouseCode != ''">
                AND parent_warehouse_code = #{parentWarehouseCode}
            </if>
            <if test="parentWarehouseName != null and parentWarehouseName != ''">
                AND parent_warehouse_name LIKE concat('%', #{parentWarehouseName}, '%')
            </if>

            <if test="status != null">
                AND status = #{status}
            </if>
            <if test="sort != null">
                AND sort = #{sort}
            </if>
            <if test="createdAt != null">
                AND created_at = #{createdAt}
            </if>
            <if test="updatedAt != null">
                AND updated_at = #{updatedAt}
            </if>
        </where>
    </select>

    <!-- ========= 主键查询 ========= -->
    <select id="selectWarehouseInfoById" parameterType="Long" resultMap="WarehouseInfoResult">
        <include refid="selectWarehouseInfoVo"/>
        WHERE id = #{id}
    </select>

    <!-- ========= 新增 ========= -->
    <insert id="insertWarehouseInfo" parameterType="WarehouseInfo" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO warehouse_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="warehouseCode != null and warehouseCode != ''">warehouse_code,</if>
            <if test="warehouseName != null and warehouseName != ''">warehouse_name,</if>
            <if test="address != null">address,</if>

            <!-- 新增：所属大仓 -->
            <if test="parentWarehouseCode != null and parentWarehouseCode != ''">parent_warehouse_code,</if>
            <if test="parentWarehouseName != null and parentWarehouseName != ''">parent_warehouse_name,</if>

            <if test="status != null">status,</if>
            <if test="sort != null">sort,</if>
            <if test="remark != null">remark,</if>
            <if test="createdAt != null">created_at,</if>
            <if test="updatedAt != null">updated_at,</if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="warehouseCode != null and warehouseCode != ''">#{warehouseCode},</if>
            <if test="warehouseName != null and warehouseName != ''">#{warehouseName},</if>
            <if test="address != null">#{address},</if>

            <!-- 新增：所属大仓 -->
            <if test="parentWarehouseCode != null and parentWarehouseCode != ''">#{parentWarehouseCode},</if>
            <if test="parentWarehouseName != null and parentWarehouseName != ''">#{parentWarehouseName},</if>

            <if test="status != null">#{status},</if>
            <if test="sort != null">#{sort},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createdAt != null">#{createdAt},</if>
            <if test="updatedAt != null">#{updatedAt},</if>
        </trim>
    </insert>

    <!-- ========= 更新 ========= -->
    <update id="updateWarehouseInfo" parameterType="WarehouseInfo">
        UPDATE warehouse_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="warehouseCode != null and warehouseCode != ''">warehouse_code = #{warehouseCode},</if>
            <if test="warehouseName != null and warehouseName != ''">warehouse_name = #{warehouseName},</if>
            <if test="address != null">address = #{address},</if>

            <!-- 新增：所属大仓 -->
            <if test="parentWarehouseCode != null and parentWarehouseCode != ''">parent_warehouse_code = #{parentWarehouseCode},</if>
            <if test="parentWarehouseName != null and parentWarehouseName != ''">parent_warehouse_name = #{parentWarehouseName},</if>

            <if test="status != null">status = #{status},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createdAt != null">created_at = #{createdAt},</if>
            <if test="updatedAt != null">updated_at = #{updatedAt},</if>
        </trim>
        WHERE id = #{id}
    </update>

    <!-- ========= 删除 ========= -->
    <delete id="deleteWarehouseInfoById" parameterType="Long">
        DELETE FROM warehouse_info WHERE id = #{id}
    </delete>

    <delete id="deleteWarehouseInfoByIds" parameterType="String">
        DELETE FROM warehouse_info WHERE id IN
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
