<?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.SceneMappingMapper">

    <!-- ====== 实体映射 ====== -->
    <resultMap id="SceneMappingResult" type="SceneMapping">
        <result property="id"             column="id"/>
        <result property="sceneCode"      column="scene_code"/>
        <result property="sceneName"      column="scene_name"/>
        <result property="warehouseCode"  column="warehouse_code"/>
        <result property="warehouseName"  column="warehouse_name"/>
    </resultMap>

    <!-- ====== 通用查询列（含连表字段） ====== -->
    <sql id="selectSceneMappingVo">
        SELECT
            sm.id,
            sm.scene_code,
            sm.scene_name,
            sm.warehouse_code,
            wi.warehouse_name AS warehouse_name
        FROM scene_mapping sm
                 LEFT JOIN warehouse_info wi
                           ON wi.warehouse_code = sm.warehouse_code
    </sql>

    <!-- ====== 列表查询 ====== -->
    <select id="selectSceneMappingList" parameterType="SceneMapping" resultMap="SceneMappingResult">
        <include refid="selectSceneMappingVo"/>
        <where>
            <if test="sceneCode != null and sceneCode != ''">
                AND sm.scene_code = #{sceneCode}
            </if>
            <if test="sceneName != null and sceneName != ''">
                AND sm.scene_name LIKE CONCAT('%', #{sceneName}, '%')
            </if>
            <if test="warehouseCode != null and warehouseCode != ''">
                AND sm.warehouse_code = #{warehouseCode}
            </if>
            <if test="warehouseName != null and warehouseName != ''">
                AND wi.warehouse_name LIKE CONCAT('%', #{warehouseName}, '%')
            </if>
        </where>
    </select>

    <!-- ====== 主键查询 ====== -->
    <select id="selectSceneMappingById" parameterType="Long" resultMap="SceneMappingResult">
        <include refid="selectSceneMappingVo"/>
        WHERE sm.id = #{id}
    </select>

    <!-- ====== 新增 ====== -->
    <insert id="insertSceneMapping" parameterType="SceneMapping" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO scene_mapping
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sceneCode != null and sceneCode != ''">scene_code,</if>
            <if test="sceneName != null and sceneName != ''">scene_name,</if>
            <if test="warehouseCode != null and warehouseCode != ''">warehouse_code,</if>
            <if test="warehouseName != null and warehouseName != ''">warehouse_name,</if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="sceneCode != null and sceneCode != ''">#{sceneCode},</if>
            <if test="sceneName != null and sceneName != ''">#{sceneName},</if>
            <if test="warehouseCode != null and warehouseCode != ''">#{warehouseCode},</if>
            <if test="warehouseName != null and warehouseName != ''">#{warehouseName},</if>
        </trim>
    </insert>

    <!-- ====== 更新（可修改所属仓库编码 + 名称） ====== -->
    <update id="updateSceneMapping" parameterType="SceneMapping">
        UPDATE scene_mapping
        <trim prefix="SET" suffixOverrides=",">
            <if test="sceneCode != null and sceneCode != ''">
                scene_code = #{sceneCode},
            </if>
            <if test="sceneName != null and sceneName != ''">
                scene_name = #{sceneName},
            </if>
            <if test="warehouseCode != null and warehouseCode != ''">
                warehouse_code = #{warehouseCode},
            </if>
            <if test="warehouseName != null and warehouseName != ''">
                warehouse_name = #{warehouseName},
            </if>
        </trim>
        WHERE id = #{id}
    </update>

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

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

</mapper>
