<?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.cnbm.basic.mapper.ProductFeaturesMapper">

    <select id="getProductFeaturesByProductId" resultType="com.cnbm.basic.dto.ProductFeaturesDTO">
        select p.*,m.name as measureToolName,u.name as unitName,c.name as controlGraphName
        from product_features p
        LEFT JOIN measure_tool m ON p.measure_tool_id=m.id
        LEFT JOIN unit u ON p.unit_id=u.id
        LEFT JOIN control_graph c ON p.control_graph_id=c.id
        <where>
            p.valid = 1 AND p.product_id = #{id}
        </where>
        order by p.id asc
    </select>

    <select id="getFeaturesByStageProcedure" resultType="com.cnbm.basic.dto.FeaturesProcedureDTO">
        select pf.id as productFeaturesId, fspr.working_procedure_id as workingProcedureId
        from features_stage_procedure_relation fspr
        LEFT JOIN product_features pf ON pf.id=fspr.product_features_id
        <where>
            fspr.valid = 1
            <if test="productId != null">
                and pf.product_id = #{productId}
            </if>
            <if test="inspectionStage != null">
                and fspr.inspection_stage = #{inspectionStage}
            </if>
        </where>
        order by pf.id asc
    </select>

    <!--
    <select id="getFeaturesByStageProcedure" resultType="com.cnbm.basic.dto.ProductFeaturesDTO">
        select *
        from product_features pf
        LEFT JOIN features_stage_procedure_relation fspr ON pf.id=fspr.product_features_id
        LEFT JOIN product_workingprocedure_relation pwr ON pwr.working_procedure_id=fspr.working_procedure_id
        <where>
            fspr.valid = 1
            <if test="productId != null">
                and pwr.product_id = #{productId}
            </if>
            <if test="inspectionStage != null">
                and fspr.inspection_stage = #{inspectionStage}
            </if>
            <if test="workingProcedureId != null">
                and fspr.working_procedure_id = #{workingProcedureId}
            </if>
        </where>
        order by pf.id asc
    </select>
    -->
    <select id="getFeaturesList" resultType="com.cnbm.basic.dto.ProductFeaturesDTO">
        select pf.*,m.name as measureToolName,u.name as unitName,c.name as controlGraphName
        from product_features pf
        left join features_stage_procedure_relation fspr ON pf.id=fspr.product_features_id
        LEFT JOIN measure_tool m ON pf.measure_tool_id=m.id
        LEFT JOIN unit u ON pf.unit_id=u.id
        LEFT JOIN control_graph c ON pf.control_graph_id=c.id
        <where>
            fspr.valid = 1
            <if test="productId != null">
                and pf.product_id = #{productId}
            </if>
            <if test="inspectionStage != null">
                and fspr.inspection_stage = #{inspectionStage}
            </if>
        </where>
        order by pf.id asc
    </select>

    <select id="getControlGraphNameById" resultType="String">
        select name from control_graph where id = #{id}
    </select>
</mapper>