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

    <!-- ==================== 通用结果映射 ==================== -->
    <resultMap type="PcdeDetail" id="PcdeDetailResult">
        <result property="id"                    column="id"/>
        <result property="pcode"                 column="pcode"/>
        <result property="scene"                 column="scene"/>
        <result property="sceneName"             column="scene_name"/>

        <result property="parentWarehouseCode"   column="parent_warehouse_code"/>
        <result property="parentWarehouseName"   column="parent_warehouse_name"/>
        <result property="warehouseCode"         column="warehouse_code"/>
        <result property="warehouseName"         column="warehouse_name"/>

        <result property="encodedId"             column="encoded_id"/>
        <result property="tag"                   column="tag"/>
        <result property="remark"                column="remark"/>
        <result property="isDelete"              column="is_delete"/>
        <result property="createdBy"             column="created_by"/>
        <result property="createdAt"             column="created_at"/>
        <result property="updatedBy"             column="updated_by"/>
        <result property="updatedAt"             column="updated_at"/>
    </resultMap>

    <!-- 公共 SELECT 片段：带场景名称 -->
    <sql id="selectPcdeDetailVo">
        SELECT
            d.id,
            d.pcode,
            d.scene,
            m.scene_name,
            d.parent_warehouse_code,
            d.parent_warehouse_name,
            d.warehouse_code,
            d.warehouse_name,
            d.encoded_id,
            d.tag,
            d.remark,
            d.is_delete,
            d.created_by,
            d.created_at,
            d.updated_by,
            d.updated_at
        FROM pcde_detail d
                 LEFT JOIN scene_mapping m ON d.scene = m.scene_code
    </sql>

    <!-- 列表查询 -->
    <select id="selectPcdeDetailList"
            parameterType="PcdeDetail"
            resultMap="PcdeDetailResult">
        <include refid="selectPcdeDetailVo"/>
        <where>
            <if test="pcode != null and pcode != ''">
                AND d.pcode = #{pcode}
            </if>
            <if test="scene != null and scene != ''">
                AND d.scene = #{scene}
            </if>

            <!-- 按大仓 / 小仓过滤（可选） -->
            <if test="parentWarehouseCode != null and parentWarehouseCode != ''">
                AND d.parent_warehouse_code = #{parentWarehouseCode}
            </if>
            <if test="parentWarehouseName != null and parentWarehouseName != ''">
                AND d.parent_warehouse_name LIKE concat('%', #{parentWarehouseName}, '%')
            </if>

            <if test="warehouseCode != null and warehouseCode != ''">
                AND d.warehouse_code = #{warehouseCode}
            </if>

            <if test="warehouseName != null and warehouseName != ''">
                AND d.warehouse_name LIKE concat('%', #{warehouseName}, '%')
            </if>

            <if test="encodedId != null and encodedId != ''">
                AND d.encoded_id = #{encodedId}
            </if>
            <if test="tag != null and tag != ''">
                AND d.tag = #{tag}
            </if>
            <if test="isDelete != null">
                AND d.is_delete = #{isDelete}
            </if>
        </where>
    </select>

    <!-- 根据 ID 列表查询 -->
    <select id="selectPcdeDetailListIds"
            parameterType="PcdeDetail"
            resultMap="PcdeDetailResult">
        <include refid="selectPcdeDetailVo"/>
        <where>
            <choose>
                <when test="ids != null and ids.size > 0">
                    AND d.id IN
                    <foreach collection="ids" item="val" open="(" separator="," close=")">
                        #{val}
                    </foreach>
                </when>
            </choose>
        </where>
    </select>

    <!-- 根据主键查询 -->
    <select id="selectPcdeDetailById"
            parameterType="Long"
            resultMap="PcdeDetailResult">
        <include refid="selectPcdeDetailVo"/>
        WHERE d.id = #{id}
    </select>

    <!-- 根据库位编码查询（不联表，直接取表字段） -->
    <select id="selectByPcode"
            resultType="com.zg.project.information.domain.PcdeDetail"
            parameterType="java.lang.String">
        SELECT
            id,
            pcode,
            scene,
            parent_warehouse_code   AS parentWarehouseCode,
            parent_warehouse_name   AS parentWarehouseName,
            warehouse_code          AS warehouseCode,
            warehouse_name          AS warehouseName,
            encoded_id              AS encodedId,
            tag,
            is_delete               AS isDelete,
            created_by              AS createdBy,
            created_at              AS createdAt,
            updated_by              AS UpdatedBy,
            updated_at              AS updatedAt,
            remark
        FROM pcde_detail
        WHERE pcode = #{pcode}
          AND is_delete = '0'
    </select>

    <!-- 根据库位编码查编码后ID -->
    <select id="selectEncodedIdByPcode"
            resultType="java.lang.String"
            parameterType="java.lang.String">
        SELECT encoded_id
        FROM pcde_detail
        WHERE pcode = #{pcode}
          AND is_delete = '0'
    </select>

    <select id="selectByWarehouseCode" parameterType="String" resultMap="PcdeDetailResult">
        SELECT
            id,
            pcode,
            scene,
            parent_warehouse_code,
            parent_warehouse_name,
            warehouse_code,
            warehouse_name,
            encoded_id,
            tag,
            remark,
            is_delete,
            created_by,
            created_at,
            updated_by,
            updated_at
        FROM pcde_detail
        WHERE warehouse_code = #{warehouseCode}
          AND is_delete = '0'
        ORDER BY id ASC
    </select>

    <!-- 根据库位编码查所属小仓编码（小仓 warehouse_code） -->
    <select id="selectWarehouseCodeByPcode" parameterType="java.lang.String" resultType="java.lang.String">
        SELECT warehouse_code
        FROM pcde_detail
        WHERE pcode = #{pcode}
          AND is_delete = '0'
            LIMIT 1
    </select>

    <select id="selectEncodedIdByPcodeAndWarehouse" resultType="java.lang.String">
        SELECT encoded_id
        FROM pcde_detail
        WHERE pcode = #{pcode}
          AND warehouse_code = #{warehouseCode}
          AND is_delete = '0'
            LIMIT 1
    </select>

    <!-- 单条插入 -->
    <insert id="insertPcdeDetail"
            parameterType="PcdeDetail"
            useGeneratedKeys="true"
            keyProperty="id">
        INSERT INTO pcde_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="pcode != null and pcode != ''">pcode,</if>
            <if test="scene != null and scene != ''">scene,</if>

            <if test="parentWarehouseCode != null and parentWarehouseCode != ''">
                parent_warehouse_code,
            </if>
            <if test="parentWarehouseName != null and parentWarehouseName != ''">
                parent_warehouse_name,
            </if>
            <if test="warehouseCode != null and warehouseCode != ''">
                warehouse_code,
            </if>
            <if test="warehouseName != null and warehouseName != ''">
                warehouse_name,
            </if>

            <if test="encodedId != null">encoded_id,</if>
            <if test="tag != null">tag,</if>
            <if test="remark != null and remark != ''">remark,</if>
            <if test="isDelete != null">is_delete,</if>
            <if test="createdBy != null">created_by,</if>
            <if test="createdAt != null">created_at,</if>
            <if test="updatedBy != null">updated_by,</if>
            <if test="updatedAt != null">updated_at,</if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="pcode != null and pcode != ''">#{pcode},</if>
            <if test="scene != null and scene != ''">#{scene},</if>

            <if test="parentWarehouseCode != null and parentWarehouseCode != ''">
                #{parentWarehouseCode},
            </if>
            <if test="parentWarehouseName != null and parentWarehouseName != ''">
                #{parentWarehouseName},
            </if>
            <if test="warehouseCode != null and warehouseCode != ''">
                #{warehouseCode},
            </if>
            <if test="warehouseName != null and warehouseName != ''">
                #{warehouseName},
            </if>

            <if test="encodedId != null">#{encodedId},</if>
            <if test="tag != null">#{tag},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            <if test="isDelete != null">#{isDelete},</if>
            <if test="createdBy != null">#{createdBy},</if>
            <if test="createdAt != null">#{createdAt},</if>
            <if test="updatedBy != null">#{updatedBy},</if>
            <if test="updatedAt != null">#{updatedAt},</if>
        </trim>
    </insert>

    <!-- 更新 -->
    <update id="updatePcdeDetail" parameterType="PcdeDetail">
        UPDATE pcde_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="pcode != null and pcode != ''">
                pcode = #{pcode},
            </if>
            <if test="scene != null and scene != ''">
                scene = #{scene},
            </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="warehouseCode != null and warehouseCode != ''">
                warehouse_code = #{warehouseCode},
            </if>
            <if test="warehouseName != null and warehouseName != ''">
                warehouse_name = #{warehouseName},
            </if>

            <if test="encodedId != null">
                encoded_id = #{encodedId},
            </if>
            <if test="tag != null">
                tag = #{tag},
            </if>
            <if test="remark != null and remark != ''">
                remark = #{remark},
            </if>
            <if test="isDelete != null">
                is_delete = #{isDelete},
            </if>
            <if test="createdBy != null">
                created_by = #{createdBy},
            </if>
            <if test="createdAt != null">
                created_at = #{createdAt},
            </if>
            <if test="updatedBy != null">
                updated_by = #{updatedBy},
            </if>
            <if test="updatedAt != null">
                updated_at = #{updatedAt},
            </if>
        </trim>
        WHERE id = #{id}
    </update>

    <!-- 批量插入（存在则忽略），用于导入 -->
    <insert id="batchInsertIgnore">
        INSERT IGNORE INTO pcde_detail
        (pcode,
        scene,
        scene_name,
        parent_warehouse_code,
        parent_warehouse_name,
        warehouse_code,
        warehouse_name,
        encoded_id,
        tag,
        remark,
        is_delete,
        created_by,
        created_at,
        updated_by,
        updated_at)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.pcode},
            #{item.scene},
            #{item.sceneName},
            #{item.parentWarehouseCode},
            #{item.parentWarehouseName},
            #{item.warehouseCode},
            #{item.warehouseName},
            #{item.encodedId},
            #{item.tag},
            #{item.remark},
            #{item.isDelete},
            #{item.createdBy},
            #{item.createdAt},
            #{item.updatedBy},
            #{item.updatedAt}
            )
        </foreach>
    </insert>

    <!-- 单条删除 -->
    <delete id="deletePcdeDetailById" parameterType="Long">
        DELETE FROM pcde_detail
        WHERE id = #{id}
    </delete>

    <!-- 批量删除 -->
    <delete id="deletePcdeDetailByIds" parameterType="String">
        DELETE FROM pcde_detail
        WHERE id IN
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>
