<?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.yhy.module.core.dal.mysql.resource.ResourceMergedMapper">

    <!-- ResultMap for ResourceMergedRespVO -->
    <resultMap id="ResourceMergedRespVOMap" type="com.yhy.module.core.controller.admin.resource.vo.ResourceMergedRespVO">
        <id property="id" column="id"/>
        <result property="mergedId" column="merged_id"/>
        <result property="sourceId" column="source_id"/>
        <result property="regionCode" column="region_code"/>
        <result property="quotaConsumption" column="quota_consumption"/>
        <result property="taxExclMarketPrice" column="tax_excl_market_price"/>
        <result property="createTime" column="create_time"/>
        <result property="code" column="code"/>
        <result property="name" column="name"/>
        <result property="catalogItemId" column="catalog_item_id"/>
        <result property="spec" column="spec"/>
        <result property="categoryId" column="category_id"/>
        <result property="categoryName" column="category_name"/>
        <result property="unit" column="unit"/>
        <result property="taxRate" column="tax_rate"/>
        <result property="taxExclBasePrice" column="tax_excl_base_price"/>
        <result property="taxInclBasePrice" column="tax_incl_base_price"/>
        <result property="taxExclCompilePrice" column="tax_excl_compile_price"/>
        <result property="taxInclCompilePrice" column="tax_incl_compile_price"/>
        <result property="calcBase" column="calc_base" typeHandler="com.yhy.module.core.dal.typehandler.PostgreSQLJsonbTypeHandler"/>
        <result property="taxExclBaseTotal" column="tax_excl_base_total"/>
        <result property="taxInclBaseTotal" column="tax_incl_base_total"/>
        <result property="taxExclCompileTotal" column="tax_excl_compile_total"/>
        <result property="taxInclCompileTotal" column="tax_incl_compile_total"/>
    </resultMap>

    <!-- 查询复合工料机分页（JOIN 第四层数据） -->
    <select id="selectPageWithDetails" resultMap="ResourceMergedRespVOMap">
        SELECT
            rm.id, rm.tenant_id, rm.merged_id, rm.source_id, rm.region_code,
            rm.quota_consumption, rm.tax_excl_market_price,
            rm.creator, rm.create_time, rm.updater, rm.update_time,
            ri.code, ri.name, ri.unit, ri.spec,
            ri.catalog_item_id, ri.category_id, ri.tax_rate,
            ri.tax_excl_base_price, ri.tax_incl_base_price,
            ri.tax_excl_compile_price, ri.tax_incl_compile_price,
            ri.calc_base,
            rc.name AS category_name,
            ROUND(COALESCE(ri.tax_excl_base_price, 0) * COALESCE(rm.quota_consumption, 0), 4) AS tax_excl_base_total,
            ROUND(COALESCE(ri.tax_incl_base_price, 0) * COALESCE(rm.quota_consumption, 0), 4) AS tax_incl_base_total,
            ROUND(COALESCE(ri.tax_excl_compile_price, 0) * COALESCE(rm.quota_consumption, 0), 4) AS tax_excl_compile_total,
            ROUND(COALESCE(ri.tax_incl_compile_price, 0) * COALESCE(rm.quota_consumption, 0), 4) AS tax_incl_compile_total
        FROM yhy_resource_merged rm
        LEFT JOIN yhy_resource_item ri ON rm.source_id = ri.id AND ri.deleted = 0
        LEFT JOIN yhy_resource_category rc ON ri.category_id = rc.id AND rc.deleted = 0
        WHERE rm.deleted = 0
        <if test="pageReqVO.mergedId != null">
            AND rm.merged_id = #{pageReqVO.mergedId}
        </if>
        <if test="pageReqVO.sourceId != null">
            AND rm.source_id = #{pageReqVO.sourceId}
        </if>
        <if test="pageReqVO.regionCode != null and pageReqVO.regionCode != ''">
            AND rm.region_code = #{pageReqVO.regionCode}
        </if>
        ORDER BY rm.merged_id ASC, rm.source_id ASC, rm.region_code ASC
        LIMIT #{pageReqVO.pageSize} OFFSET #{offset}
    </select>

</mapper>
