<?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.tashow.cloud.product.mapper.ProdMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <!-- 结果映射：将数据库查询结果映射到 ProdServiceVO -->
    <resultMap id="ProdServiceResultMap" type="com.tashow.cloud.productapi.api.product.vo.prod.ProdServiceVO"> <!-- 确保这是 ProdServiceVO 的正确包名 -->
        <!-- 商品基本信息映射 -->
        <id property="prodId" column="prod_id"/>
        <result property="regionSwitch" column="region_switch"/>
        <result property="additionalSwitch" column="additional_switch"/>
        <result property="additionalFeeSwitch" column="additional_fee_switch"/>
        <result property="emergencySwitch" column="emergency_switch"/>
        <result property="reservationSwitch" column="reservation_switch"/>
        <result property="orderLimitSwitch" column="order_limit_switch"/>
        <result property="weightSwitch" column="weight_switch"/>

        <!-- 关联紧急响应配置 -->
        <association property="prodEmergencyInfoVO" javaType="com.tashow.cloud.productapi.api.product.vo.prodemergencyresponse.ProdEmergencyInfoVO">
            <id property="id" column="erc_record_id"/>
            <result property="prodId" column="erc_prod_id"/>
            <result property="responseTimeSlots" column="erc_response_time_slots"/>
            <!-- 嵌套集合：紧急响应时间区间 -->
            <collection property="prodEmergencyResponseBlackList" ofType="com.tashow.cloud.productapi.api.product.vo.prodadditionalfeedates.ProdAdditionalFeeBlackVO">
                <id property="id" column="eblack_record_id"/>
                <result property="dateType" column="eblack_date_type"/>
                <result property="customTimeSlots" column="eblack_custom_time_slots"/>
                <result property="type" column="eblack_type"/>
                <result property="isEnabled" column="eblack_is_enabled"/>
            </collection>
            <!-- 嵌套集合：紧急响应时间区间 -->
            <collection property="prodEmergencyResponseIntervalsList" ofType="com.tashow.cloud.productapi.api.product.dto.ProdEmergencyResponseIntervalsDO">
                <id property="id" column="eri_record_id"/>
                <result property="configId" column="eri_config_id"/>
                <result property="name" column="eri_name"/>
                <result property="responseHours" column="response_hours"/>
                <result property="chargeMode" column="charge_mode"/>
                <result property="floatingPercentage" column="floating_percentage"/>
                <result property="price" column="price"/>
            </collection>
        </association>

        <association property="prodReservationConfig" javaType="com.tashow.cloud.productapi.api.product.vo.prodreservationconfig.ProdReservationInfoVO">
            <id property="id" column="rc_record_id"/>
            <result property="prodId" column="rc_prod_id"/>
            <result property="reservationTimeSlots" column="rc_reservation_time_slots"/>
            <result property="advanceHours" column="rc_advance_hours"/>
            <result property="reservationDateRange" column="rc_reservation_date_range"/>
            <result property="allowChange" column="rc_allow_change"/>
            <result property="changeTimeRule" column="rc_change_time_rule"/>
            <result property="timeSlot" column="rc_time_slot"/>
            <result property="maxChangeTimes" column="rc_max_change_times"/>

            <collection property="prodReservationBlackList"
                        ofType="com.tashow.cloud.productapi.api.product.vo.prodadditionalfeedates.ProdAdditionalFeeBlackVO">  <!-- 所有 blacklist 字段加 black_ 前缀 -->
                <id property="id" column="black_id"/>
                <result property="dateType" column="black_date_type"/>
                <result property="customTimeSlots" column="black_custom_time_slots"/>
                <result property="type" column="black_type"/>
                <result property="isEnabled" column="black_is_enabled"/>
            </collection>
        </association>

        <!-- 映射接单上限配置 (productOrderLimitVO) -->
        <association property="productOrderLimitVO" javaType="com.tashow.cloud.productapi.api.product.dto.ProductOrderLimitDO">
            <id property="id" column="ol_record_id"/>
            <result property="prodId" column="ol_prod_id"/>
            <result property="limitUnit" column="ol_limit_unit"/>
            <result property="maxOrders" column="ol_max_orders"/>
        </association>

        <!-- 2. 所有 association 放在前面 -->
        <!-- 映射服务区域配置 (prodServiceAreasInfo) -->
        <association property="prodServiceAreasInfo" javaType="com.tashow.cloud.productapi.api.product.vo.prodserviceareas.ProdServiceAreasInfoVO">
            <id property="id" column="sa_rule_id"/>
            <result property="prodId" column="sa_prod_id"/>
            <result property="ruleType" column="sa_rule_type"/>
            <result property="fee" column="sa_fee"/>
            <!-- 使用 columnPrefix 区分多个 area_name -->
            <collection property="areaNameList" ofType="string">
                <result column="sa_area_name"/> <!-- 每行一个 area_name -->
            </collection>
        </association>


        <!--  新增：映射体重区间价格配置列表 (prodWeightConfigList) -->
        <association property="prodWeightConfig" javaType="com.tashow.cloud.productapi.api.product.vo.prodweightrangeprices.ProdWeightRangePricesSaveInfoVO">
            <result property="isWeightCharge" column="tpe_is_weight_charge"/>
            <!--  新增：映射体重区间价格配置列表 (prodWeightConfigList) -->
            <collection property="prodWeightConfigList" ofType="com.tashow.cloud.productapi.api.product.dto.ProdWeightRangePricesDO">
                <id property="id" column="wrp_record_id"/>
                <result property="prodId" column="wrp_prod_id"/>
                <result property="weightRange" column="wrp_weight_range"/> <!-- 注意：数据库列是 weight_range -->
                <result property="price" column="wrp_price"/>
                <result property="isEnabled" column="wrp_is_enabled"/>
                <!-- <result property="createTime" column="wrp_create_time"/> -->
                <!-- <result property="updateTime" column="wrp_update_time"/> -->
                <!-- <result property="creator" column="wrp_creator"/> -->
                <!-- <result property="updater" column="wrp_updater"/> -->
            </collection>
        </association>


        <!-- 新增：映射特殊日期规则配置列表 (prodAdditionalFeePeriodsList) -->
        <collection property="prodAdditionalFeePeriodsList" ofType="com.tashow.cloud.productapi.api.product.dto.ProdAdditionalFeePeriodsDO">
            <id property="id" column="afp_record_id"/>
            <result property="prodId" column="afp_prod_id"/>
            <result property="name" column="afp_name"/>
            <result property="specialTimeSlots" column="afp_special_time_slots"/>
            <result property="chargeMode" column="afp_charge_mode"/>
            <result property="price" column="afp_price"/>
            <result property="floatingPercentage" column="afp_floating_percentage"/>
        </collection>

        <!-- 新增：映射特殊时段规则配置列表 (ProdAdditionalFeeDatesList) -->
        <collection property="prodAdditionalFeeDatesList" ofType="com.tashow.cloud.productapi.api.product.dto.ProdAdditionalFeeDatesDO">
            <id property="id" column="afd_record_id"/>
            <result property="prodId" column="afd_prod_id"/>
            <result property="name" column="afd_name"/>
            <result property="dateType" column="afd_date_type"/>
            <result property="customTimeSlots" column="afd_custom_time_slots"/>
           <!-- <result property="specificDates" column="afd_specific_dates"/>-->
            <result property="chargeMode" column="afd_charge_mode"/>
            <result property="price" column="afd_price"/>
            <result property="isEnabled" column="afd_is_enabled"/>
        </collection>
        <!-- 映射服务区域地址名称 (areaNameList) -->
        <!--<collection property="prodServiceAreasInfo.areaNameList" ofType="string">
            <result column="sa_area_name"/>
        </collection>-->
    </resultMap>




    <!-- 结果映射：将数据库查询结果映射到 ProdServiceVO -->
    <resultMap id="ProdServiceResultInfoMap" type="com.tashow.cloud.productapi.api.product.vo.prod.ProdServiceVO"> <!-- 确保这是 ProdServiceVO 的正确包名 -->
        <!-- 商品基本信息映射 -->
        <id property="prodId" column="prod_id"/>
        <result property="regionSwitch" column="region_switch"/>
        <result property="additionalSwitch" column="additional_switch"/>
        <result property="additionalFeeSwitch" column="additional_fee_switch"/>
        <result property="emergencySwitch" column="emergency_switch"/>
        <result property="reservationSwitch" column="reservation_switch"/>
        <result property="orderLimitSwitch" column="order_limit_switch"/>
        <result property="weightSwitch" column="weight_switch"/>

        <!-- 关联紧急响应配置 -->
        <association property="prodEmergencyInfoVO" javaType="com.tashow.cloud.productapi.api.product.vo.prodemergencyresponse.ProdEmergencyInfoVO">
            <id property="id" column="erc_record_id"/>
            <result property="prodId" column="erc_prod_id"/>
            <result property="responseTimeSlots" column="erc_response_time_slots"/>
            <!-- 嵌套集合：紧急响应时间区间 -->
            <collection property="prodEmergencyResponseBlackList" ofType="com.tashow.cloud.productapi.api.product.vo.prodadditionalfeedates.ProdAdditionalFeeBlackVO">
                <id property="id" column="eblack_record_id"/>
                <result property="dateType" column="eblack_date_type"/>
                <result property="customTimeSlots" column="eblack_custom_time_slots"/>
                <result property="type" column="eblack_type"/>
                <result property="isEnabled" column="eblack_is_enabled"/>
            </collection>
            <!-- 嵌套集合：紧急响应时间区间 -->
            <collection property="prodEmergencyResponseIntervalsList" ofType="com.tashow.cloud.productapi.api.product.dto.ProdEmergencyResponseIntervalsDO">
                <id property="id" column="eri_record_id"/>
                <result property="configId" column="eri_config_id"/>
                <result property="name" column="eri_name"/>
                <result property="responseHours" column="response_hours"/>
                <result property="chargeMode" column="charge_mode"/>
                <result property="floatingPercentage" column="floating_percentage"/>
                <result property="price" column="price"/>
            </collection>
        </association>

        <association property="prodReservationConfig" javaType="com.tashow.cloud.productapi.api.product.vo.prodreservationconfig.ProdReservationInfoVO">
            <id property="id" column="rc_record_id"/>
            <result property="prodId" column="rc_prod_id"/>
            <result property="reservationTimeSlots" column="rc_reservation_time_slots"/>
            <result property="advanceHours" column="rc_advance_hours"/>
            <result property="reservationDateRange" column="rc_reservation_date_range"/>
            <result property="allowChange" column="rc_allow_change"/>
            <result property="changeTimeRule" column="rc_change_time_rule"/>
            <result property="timeSlot" column="rc_time_slot"/>
            <result property="maxChangeTimes" column="rc_max_change_times"/>
            <collection property="prodReservationBlackList"
                        ofType="com.tashow.cloud.productapi.api.product.vo.prodadditionalfeedates.ProdAdditionalFeeBlackVO">  <!-- 所有 blacklist 字段加 black_ 前缀 -->
                <id property="id" column="black_id"/>
                <result property="dateType" column="black_date_type"/>
                <result property="customTimeSlots" column="black_custom_time_slots"/>
                <result property="type" column="black_type"/>
                <result property="isEnabled" column="black_is_enabled"/>
            </collection>
        </association>

        <!-- 映射接单上限配置 (productOrderLimitVO) -->
        <association property="productOrderLimitVO" javaType="com.tashow.cloud.productapi.api.product.dto.ProductOrderLimitDO">
            <id property="id" column="ol_record_id"/>
            <result property="prodId" column="ol_prod_id"/>
            <result property="limitUnit" column="ol_limit_unit"/>
            <result property="maxOrders" column="ol_max_orders"/>
        </association>

        <association property="prodWeightConfig" javaType="com.tashow.cloud.productapi.api.product.vo.prodweightrangeprices.ProdWeightRangePricesSaveInfoVO">
            <result property="isWeightCharge" column="tpe_is_weight_charge"/>
            <!--  新增：映射体重区间价格配置列表 (prodWeightConfigList) -->
            <collection property="prodWeightConfigList" ofType="com.tashow.cloud.productapi.api.product.dto.ProdWeightRangePricesDO">
                <id property="id" column="wrp_record_id"/>
                <result property="prodId" column="wrp_prod_id"/>
                <result property="weightRange" column="wrp_weight_range"/> <!-- 注意：数据库列是 weight_range -->
                <result property="price" column="wrp_price"/>
                <result property="isEnabled" column="wrp_is_enabled"/>
                <!-- <result property="createTime" column="wrp_create_time"/> -->
                <!-- <result property="updateTime" column="wrp_update_time"/> -->
                <!-- <result property="creator" column="wrp_creator"/> -->
                <!-- <result property="updater" column="wrp_updater"/> -->
            </collection>
        </association>



        <!-- 新增：映射特殊日期规则配置列表 (prodAdditionalFeePeriodsList) -->
        <collection property="prodAdditionalFeePeriodsList" ofType="com.tashow.cloud.productapi.api.product.dto.ProdAdditionalFeePeriodsDO">
            <id property="id" column="afp_record_id"/>
            <result property="prodId" column="afp_prod_id"/>
            <result property="name" column="afp_name"/>
            <result property="specialTimeSlots" column="afp_special_time_slots"/>
            <result property="chargeMode" column="afp_charge_mode"/>
            <result property="price" column="afp_price"/>
            <result property="floatingPercentage" column="afp_floating_percentage"/>
        </collection>

        <!-- 新增：映射特殊时段规则配置列表 (ProdAdditionalFeeDatesList) -->
        <collection property="prodAdditionalFeeDatesList" ofType="com.tashow.cloud.productapi.api.product.dto.ProdAdditionalFeeDatesDO">
            <id property="id" column="afd_record_id"/>
            <result property="prodId" column="afd_prod_id"/>
            <result property="name" column="afd_name"/>
            <result property="dateType" column="afd_date_type"/>
            <result property="customTimeSlots" column="afd_custom_time_slots"/>
           <!-- <result property="specificDates" column="afd_specific_dates"/>-->
            <result property="chargeMode" column="afd_charge_mode"/>
            <result property="price" column="afd_price"/>
            <result property="isEnabled" column="afd_is_enabled"/>
        </collection>

    </resultMap>







    <!-- 动态SQL查询商品列表及其服务配置 (返回 List<ProdServiceVO>) -->
    <!-- 用于列表页，可根据开关动态决定是否关联紧急响应信息以优化性能 -->
    <select id="selectProdService" parameterType="map" resultMap="ProdServiceResultMap">
        SELECT p.prod_id,
               p.emergency_switch,
               p.additional_switch,
               p.additional_fee_switch,
               p.reservation_switch,
               p.order_limit_switch,
               p.weight_switch,
               p.region_switch,
               p.create_time,
               p.update_time,
               p.creator,
               p.updater,
               p.category_name,

               erc.id                    AS erc_record_id,
               erc.response_time_slots   as erc_response_time_slots,

               epafd.id                  AS eblack_record_id,
               epafd.date_type           as eblack_date_type,
               epafd.custom_time_slots   as eblack_custom_time_slots,
               epafd.type                as eblack_type,
               epafd.is_enabled          as eblack_is_enabled,

               eri.id                    AS eri_record_id,
               eri.config_id             AS eri_config_id,
               eri.name                  as eri_name,
               eri.response_hours,
               eri.charge_mode,
               eri.floating_percentage,
               eri.price,
               eri.create_time           AS eri_create_time,
               eri.update_time           AS eri_update_time,
               eri.creator               AS eri_creator,
               eri.updater               AS eri_updater,
               eri.deleted               AS eri_deleted,

               afp.id                    AS afp_record_id,
               afp.prod_id               AS afp_prod_id,
               afp.name                  AS afp_name,
               afp.special_time_slots    AS afp_special_time_slots,
               afp.charge_mode           AS afp_charge_mode,
               afp.price                 AS afp_price,
               afp.floating_percentage   AS afp_floating_percentage,

               afd.id                    AS afd_record_id,
               afd.prod_id               AS afd_prod_id,
               afd.name                  AS afd_name,
               afd.date_type             AS afd_date_type,
               afd.custom_time_slots     AS afd_custom_time_slots,
               afd.specific_dates        AS afd_specific_dates,
               afd.charge_mode           AS afd_charge_mode,
               afd.price                 AS afd_price,
               afd.is_enabled            AS afd_is_enabled,
               afd.create_time           AS afd_create_time,
               afd.update_time           AS afd_update_time,
               afd.creator               AS afd_creator,
               afd.updater               AS afd_updater,
               afd.deleted               AS afd_deleted,

               rc.id                     AS rc_record_id,
               rc.prod_id                AS rc_prod_id,
               rc.reservation_time_slots AS rc_reservation_time_slots,
               rc.advance_hours          AS rc_advance_hours,
               rc.reservation_date_range AS rc_reservation_date_range,
               rc.allow_change           AS rc_allow_change,
               rc.change_time_rule       AS rc_change_time_rule,
               rc.time_slot              AS rc_time_slot,
               rc.max_change_times       AS rc_max_change_times,

               rpafd.id                  AS black_id,
               rpafd.date_type           as black_date_type,
               rpafd.custom_time_slots   as black_custom_time_slots,
               rpafd.type                as black_type,
               rpafd.is_enabled          as black_is_enabled,


               ol.id                     AS ol_record_id,
               ol.prod_id                AS ol_prod_id,
               ol.limit_unit             AS ol_limit_unit,
               ol.max_orders             AS ol_max_orders,


               tpe.is_weight_charge      as tpe_is_weight_charge,
               wrp.id                    AS wrp_record_id,
               wrp.prod_id               AS wrp_prod_id,
               wrp.weight_range          AS wrp_weight_range,
               wrp.price                 AS wrp_price,
               wrp.is_enabled            AS wrp_is_enabled,


               sa.id                     AS sa_rule_id,
               sa.prod_id                AS sa_prod_id,
               sa.rule_type              AS sa_rule_type,
               sa.fee                    AS sa_fee,
               saa.area_name             AS sa_area_name,

               p.prod_name
        FROM tz_prod p

                 LEFT JOIN tz_prod_emergency_response erc ON p.prod_id = erc.prod_id AND erc.deleted = 0
                 LEFT JOIN tz_prod_additional_fee_dates epafd
                           ON p.prod_id = epafd.prod_id AND epafd.deleted = 0 AND epafd.type = 3
                 LEFT JOIN tz_prod_emergency_response_intervals eri ON erc.id = eri.config_id AND eri.deleted = 0

                 LEFT JOIN tz_prod_additional_fee_periods afp ON p.prod_id = afp.prod_id AND afp.deleted = 0

                 LEFT JOIN tz_prod_additional_fee_dates afd
                           ON p.prod_id = afd.prod_id AND afd.deleted = 0 and afd.type = 1


                 LEFT JOIN tz_prod_reservation_config rc ON p.prod_id = rc.prod_id AND rc.deleted = 0
                 LEFT JOIN tz_prod_additional_fee_dates rpafd
                           ON p.prod_id = rpafd.prod_id AND rpafd.deleted = 0 AND rpafd.type = 2


                 LEFT JOIN tz_product_order_limit ol ON p.prod_id = ol.prod_id AND ol.deleted = 0


                 LEFT JOIN tz_prod_extend tpe ON p.prod_id = tpe.prod_id
                 LEFT JOIN tz_prod_weight_range_prices wrp ON p.prod_id = wrp.prod_id AND wrp.deleted = 0


                 LEFT JOIN tz_prod_service_over_area_rules sa ON p.prod_id = sa.prod_id AND sa.deleted = 0
                 LEFT JOIN tz_prod_service_area_relevance sar ON p.prod_id = sar.prod_id
                 LEFT JOIN tz_prod_service_areas saa ON sar.area_id = saa.id AND saa.deleted = 0

        where p.deleted = 0
          AND p.prod_id = #{prodId}
        ORDER BY p.prod_id ,rpafd.date_type ,epafd.date_type,afd.date_type ASC
    </select>




    <!-- 动态SQL查询商品列表及其服务配置 (返回 List<ProdServiceVO>) -->
    <!-- 用于列表页，可根据开关动态决定是否关联紧急响应信息以优化性能 -->
    <select id="selectProdServiceInfo" parameterType="map" resultMap="ProdServiceResultInfoMap">
        SELECT
        p.prod_id,
        p.emergency_switch,
        p.additional_switch,
        p.additional_fee_switch,
        p.reservation_switch,
        p.order_limit_switch,
        p.weight_switch,
        p.region_switch,

            erc.id AS erc_record_id,
            erc.response_time_slots as erc_response_time_slots,

            epafd.id AS eblack_record_id,
            epafd.date_type as eblack_date_type,
            epafd.custom_time_slots as eblack_custom_time_slots,
            epafd.type as eblack_type,
            epafd.is_enabled as eblack_is_enabled,

            eri.id AS eri_record_id,
            eri.config_id AS eri_config_id,
            eri.name as eri_name,
            eri.response_hours,
            eri.charge_mode,
            eri.floating_percentage,
            eri.price,
            eri.create_time AS eri_create_time,
            eri.update_time AS eri_update_time,
            eri.creator AS eri_creator,
            eri.updater AS eri_updater,
            eri.deleted AS eri_deleted,


            afp.id AS afp_record_id,
            afp.prod_id AS afp_prod_id,
            afp.name AS afp_name,
            afp.special_time_slots AS afp_special_time_slots,
            afp.charge_mode AS afp_charge_mode,
            afp.price AS afp_price,
            afp.floating_percentage AS afp_floating_percentage,


            afd.id AS afd_record_id,
            afd.prod_id AS afd_prod_id,
            afd.name AS afd_name,
            afd.date_type AS afd_date_type,
            afd.custom_time_slots AS afd_custom_time_slots,
            afd.specific_dates AS afd_specific_dates,
            afd.charge_mode AS afd_charge_mode,
            afd.price AS afd_price,
            afd.is_enabled AS afd_is_enabled,
            afd.create_time AS afd_create_time,
            afd.update_time AS afd_update_time,
            afd.creator AS afd_creator,
            afd.updater AS afd_updater,
            afd.deleted AS afd_deleted,


            rc.id AS rc_record_id,
            rc.prod_id AS rc_prod_id,
            rc.reservation_time_slots AS rc_reservation_time_slots,
            rc.advance_hours AS rc_advance_hours,
            rc.reservation_date_range AS rc_reservation_date_range,
            rc.allow_change AS rc_allow_change,
            rc.change_time_rule AS rc_change_time_rule,
            rc.time_slot AS rc_time_slot,
            rc.max_change_times AS rc_max_change_times,

            rpafd.id AS black_id,
            rpafd.date_type as black_date_type,
            rpafd.custom_time_slots as black_custom_time_slots,
            rpafd.type as black_type,
            rpafd.is_enabled as black_is_enabled,



            ol.id AS ol_record_id,
            ol.prod_id AS ol_prod_id,
            ol.limit_unit AS ol_limit_unit,
            ol.max_orders AS ol_max_orders,


            tpe.is_weight_charge as tpe_is_weight_charge,
            wrp.id AS wrp_record_id,
            wrp.prod_id AS wrp_prod_id,
            wrp.weight_range AS wrp_weight_range,
            wrp.price AS wrp_price,
            wrp.is_enabled AS wrp_is_enabled,

        p.prod_name
        FROM
        tz_prod p
        <!-- 只有当 emergencySwitch 开启时才进行 LEFT JOIN -->

            LEFT JOIN tz_prod_emergency_response erc ON p.prod_id = erc.prod_id AND erc.deleted = 0
            LEFT JOIN tz_prod_additional_fee_dates epafd ON p.prod_id = epafd.prod_id AND epafd.deleted = 0 AND epafd.type = 3
            LEFT JOIN tz_prod_emergency_response_intervals eri ON erc.id = eri.config_id AND eri.deleted = 0


            LEFT JOIN tz_prod_additional_fee_periods afp ON p.prod_id = afp.prod_id AND afp.deleted = 0


            LEFT JOIN tz_prod_additional_fee_dates afd ON p.prod_id = afd.prod_id AND afd.deleted = 0 and afd.type = 1


            LEFT JOIN tz_prod_reservation_config rc ON p.prod_id = rc.prod_id AND rc.deleted = 0
            LEFT JOIN tz_prod_additional_fee_dates rpafd ON p.prod_id = rpafd.prod_id AND rpafd.deleted = 0 AND rpafd.type = 2


            LEFT JOIN tz_product_order_limit ol ON p.prod_id = ol.prod_id AND ol.deleted = 0


            LEFT JOIN tz_prod_extend tpe ON p.prod_id = tpe.prod_id
            LEFT JOIN tz_prod_weight_range_prices wrp ON p.prod_id = wrp.prod_id AND wrp.deleted = 0

        where  p.deleted = 0
        AND p.prod_id = #{prodId}
        ORDER BY p.prod_id
    </select>




    <!-- 结果映射：将数据库查询结果映射到 ProdServiceVO -->
    <resultMap id="ProdRecycleBinMap" type="com.tashow.cloud.productapi.api.product.vo.prod.ProdRestoreListVO"> <!-- 确保这是 ProdServiceVO 的正确包名 -->
        <!-- 商品基本信息映射 -->
        <id property="prodId" column="prod_id"/>
        <result property="prodName" column="prod_name"/>
        <result property="categoryName" column="category_name"/>
        <result property="shopId" column="shop_id"/>
        <result property="shopName" column="shop_name"/>
        <result property="responseHours" column="response_hours"/>
        <result property="reservationTimeSlots" column="reservation_time_slots"/>
        <result property="deleteTime" column="delete_time"/>
        <result property="createTime" column="create_time"/>
        <collection property="areaNameList" ofType="string">
            <result column="area_name"/>
        </collection>
    </resultMap>


    <resultMap id="ProdListMap" type="com.tashow.cloud.productapi.api.product.vo.prod.ProdListVO"> <!-- 确保这是 ProdServiceVO 的正确包名 -->
        <!-- 商品基本信息映射 -->
        <id property="prodId" column="prod_id"/>
        <result property="prodName" column="prod_name"/>
        <result property="categoryName" column="category_name"/>
        <result property="shopId" column="shop_id"/>
        <result property="shopName" column="shop_name"/>
        <result property="status" column="status"/>
        <result property="shopName" column="shop_name"/>
        <result property="isProhibit" column="is_prohibit"/>
        <result property="responseHours" column="response_hours"/>
        <result property="processNotes" column="processNotes"/>
        <result property="reservationTimeSlots" column="reservation_time_slots"/>
        <result property="deleteTime" column="delete_time"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="pic" column="pic"/>
        <collection property="areaNameList" ofType="string">
            <result column="area_name"/>
        </collection>
    </resultMap>


    <select id="getProdRecycleBinPageList" resultMap="ProdRecycleBinMap" >
        select tp.prod_id,tp.prod_name,tp.category_name,tp.shop_id,tsd.shop_name,tpsa.area_name,min_eri.response_hours,
        tprc.reservation_time_slots,tp.delete_time,tp.create_time
        from tz_prod tp
                left join tz_shop_detail tsd on tp.shop_id = tsd.shop_id
        left join tz_prod_service_area_relevance tpsar on tp.prod_id = tpsar.prod_id
        left join tz_prod_service_areas tpsa on tpsar.area_id = tpsa.id
        left join tz_prod_reservation_config  tprc on tp.prod_id = tprc.prod_id
        LEFT JOIN (
        SELECT
        prod_id,
        MIN(response_hours) AS response_hours
        FROM tz_prod_emergency_response_intervals
        WHERE deleted = 0  -- 排除已删除的配置
        GROUP BY prod_id
        ) min_eri ON tp.prod_id = min_eri.prod_id
        where  tp.deleted = 1
        <if test="prodName != null and prodName != ''">
            and tp.prod_name like concat('%', #{prodName}, '%')
        </if>
        <if test="deleteTime != null and deleteTime.length == 2">
            AND tp.delete_time BETWEEN #{deleteTime[0]} AND #{deleteTime[1]}
        </if>
    </select>


    <select id="getProdPageList" resultMap="ProdListMap" >
        select tp.prod_id,tp.prod_name,tp.category_name,tp.shop_id,tsd.shop_name,tpsa.area_name,min_eri.response_hours,
        tprc.reservation_time_slots,tp.delete_time,tp.create_time,tp.update_time,tp.status,tp.is_prohibit,tp.pic,
        tp.process_notes processNotes
        from tz_prod tp
        left join tz_shop_detail tsd on tp.shop_id = tsd.shop_id
        left join tz_prod_service_area_relevance tpsar on tp.prod_id = tpsar.prod_id
        left join tz_prod_service_areas tpsa on tpsar.area_id = tpsa.id
        left join tz_prod_reservation_config  tprc on tp.prod_id = tprc.prod_id
        LEFT JOIN (
        SELECT
        prod_id,
        MIN(response_hours) AS response_hours
        FROM tz_prod_emergency_response_intervals
        WHERE deleted = 0  -- 排除已删除的配置
        GROUP BY prod_id
        ) min_eri ON tp.prod_id = min_eri.prod_id
        where  tp.deleted = 0
        <if test="createTime != null and createTime.length == 2">
            AND tp.create_time BETWEEN #{createTime[0]} AND #{createTime[1]}
        </if>
        <if test="prodName != null and prodName != ''">
            AND tp.prod_name = #{prodName}
        </if>
        <if test="shopId != null">
            AND tp.shop_id = #{shopId}
        </if>
        <if test="status != null">
            AND tp.status = #{status}
        </if>
        <if test="categoryId != null">
            AND tp.category_id = #{categoryId}
        </if>
        ORDER  by tp.create_time DESC
    </select>

    <update id="batchRestoreProd">
        UPDATE tz_prod
        SET  deleted = 0
        WHERE prod_id IN
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>




</mapper>