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

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

    <!-- 结果映射：将多表查询结果映射到 SkuServiceExtendVO -->
    <resultMap id="SkuServiceExtendResultMap" type="com.tashow.cloud.productapi.api.product.vo.sku.SkuServiceExtendVO">
        <!-- 主表字段 -->
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="serviceName" column="service_name"/>
        <result property="isEnabled" column="is_enabled"/>

        <!-- 嵌套集合：服务详情 -->
        <collection property="skuServiceDetailsDOList" ofType="com.tashow.cloud.productapi.api.product.dto.SkuServiceDetailsDO"
                    column="id" select="selectDetailsByServiceId" />

        <!-- 嵌套集合：服务物料 -->
        <collection property="skuServiceMaterialDOList" ofType="com.tashow.cloud.productapi.api.product.dto.SkuServiceMaterialDO"
                    column="id" select="selectMaterialByServiceId" />

        <!-- 嵌套集合：服务遗体运输 -->
        <collection property="skuServiceTransportDOList" ofType="com.tashow.cloud.productapi.api.product.dto.SkuServiceTransportDO"
                    column="id" select="selectTransportByServiceId" />

        <!-- 嵌套集合：服务交付方式 -->
        <collection property="skuServiceDeliverDOList" ofType="com.tashow.cloud.productapi.api.product.dto.SkuServiceDeliverDO"
                    column="id" select="selectDeliverByServiceId" />
    </resultMap>

    <!-- 查询主表及关联数据 -->
    <select id="selectSkuServiceExtendWithDetails" resultMap="SkuServiceExtendResultMap">
        SELECT DISTINCT tssf.*
        FROM `tz_sku_services_form` tssf
        LEFT JOIN tz_sku_service_details tssd ON tssf.id = tssd.service_id
        LEFT JOIN tz_sku_service_transport tstr ON tssf.id = tstr.service_id
        LEFT JOIN tz_sku_service_material tsm ON tssf.id = tsm.service_id
        WHERE  form_id = #{formId}
        <!-- 可加 WHERE 条件 -->
    </select>

    <!-- 子查询：根据 service_id 查询服务详情 -->
    <select id="selectDetailsByServiceId" resultType="com.tashow.cloud.productapi.api.product.dto.SkuServiceDetailsDO">
        SELECT id, service_id, pic, name, price, is_charge, is_default, type, adress,
               trigger_name, trigger_id, is_parallel, describe_content,
               create_time, update_time, creator, updater, deleted
        FROM tz_sku_service_details
        WHERE service_id = #{id} AND deleted = 0
    </select>

    <!-- 子查询：根据 service_id 查询服务物料 -->
    <select id="selectMaterialByServiceId" resultType="com.tashow.cloud.productapi.api.product.dto.SkuServiceMaterialDO">
        SELECT id, service_id, name, describe_content,
               create_time, update_time, creator, updater, deleted
        FROM tz_sku_service_material
        WHERE service_id = #{id} AND deleted = 0
    </select>

    <!-- 子查询：根据 service_id 查询服务遗体运输 -->
    <select id="selectTransportByServiceId" resultType="com.tashow.cloud.productapi.api.product.dto.SkuServiceTransportDO">
        SELECT id, service_id, contacts, address, province, city, area, tel,
               create_time, update_time, creator, updater, deleted
        FROM tz_sku_service_transport
        WHERE service_id = #{id} AND deleted = 0
    </select>

    <!-- 子查询：根据 service_id 查询服务交付方式 -->
    <select id="selectDeliverByServiceId" resultType="com.tashow.cloud.productapi.api.product.dto.SkuServiceDeliverDO">
        SELECT id, service_id, type, price, is_charge, address, province, city, area, tel,
               create_time, update_time, creator, updater, deleted
        FROM tz_sku_service_deliver
        WHERE service_id = #{id} AND deleted = 0
    </select>
</mapper>