<?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.huatek.busi.mapper.BusiParamAppendixMapper">

    <sql id="Base_Column_List">
            id as id,
            site_id as siteId,
            main_equipment_id as mainEquipmentId,
            weight_level as weightLevel,
            name as name,
            pname as pname,
            description as description,
            value as value,
            ab_value_flag as abValueFlag,
            a_value as aValue,
            b_value as bValue,
            warning_value as warningValue,
            model as model,
            unit as unit,
            is_attend as isAttend,
            create_time as createTime,
            create_user as createUser,
            sort_num as sortNum,
            calculated_value as calculatedValue,
            scope as scope,
            number as number,
            concat(ifnull(description,''),IFNULL(scope,'')) as scopeDescription
    </sql>

    <sql id="Base_Column_List_NotValue">
            id as id,
            site_id as siteId,
            main_equipment_id as mainEquipmentId,
            weight_level as weightLevel,
            name as name,
            pname as pname,
            description as description,
            ab_value_flag as abValueFlag,
            warning_value as warningValue,
            model as model,
            is_attend as isAttend,
            create_time as createTime,
            create_user as createUser,
            sort_num as sortNum,
            calculated_value as calculatedValue,
		    scope as scope,
		    number as number,
            concat(ifnull(description,''),IFNULL(scope,'')) as scopeDescription
    </sql>


    <select id="findBusiParamAppendixWeights" resultType="com.huatek.busi.vo.BusiParamAppendixVo">
		SELECT
            id AS id,
            site_id AS siteId,
            main_equipment_id AS mainEquipmentId,
            weight_level AS weightLevel,
            NAME AS NAME,
            pname AS pname,
            description AS description,
            VALUE AS VALUE,
            ab_value_flag AS abValueFlag,
            a_value AS aValue,
            b_value AS bValue,
            warning_value AS warningValue,
            model AS model,
            is_attend AS isAttend,
            create_time AS createTime,
            create_user AS createUser,
            sort_num AS sortNum,
            calculated_value AS calculatedValue,
            number
        FROM
        busi_param_appendix t
        WHERE t.site_id = #{siteId} and t.main_equipment_id = #{mainEquipmentId}
          AND t.is_attend = '1'  and t.number = #{number} and pname=#{pname}
        ORDER BY  t.sort_num ASC, t.name ASC
	</select>
	<select id="findFirstLevelParams" resultType="com.huatek.busi.vo.BusiParamAppendixVo">
		SELECT
        <include refid="Base_Column_List"/>
        FROM
        busi_param_appendix t
        WHERE t.site_id = #{siteId} and t.main_equipment_id = #{mainEquipmentId}
        AND t.number = #{number}
        	AND t.weight_level = '1'
        GROUP BY name
        ORDER BY t.sort_num ASC, t.name ASC
	</select>

    <select id="findNotValueFirstLevelParams" resultType="com.huatek.busi.vo.BusiParamAppendixVo">
        SELECT
        <include refid="Base_Column_List_NotValue"/>
        FROM
        busi_param_appendix t
        WHERE t.site_id = #{siteId} and t.main_equipment_id = #{mainEquipmentId}
        AND t.number = #{number}
        AND t.weight_level = '1'
        GROUP BY name
        ORDER BY t.sort_num ASC, t.name ASC
    </select>

	<select id="findSubParams" resultType="com.huatek.busi.vo.BusiParamAppendixVo">
		SELECT
        <include refid="Base_Column_List"/>
        FROM
        busi_param_appendix t
        WHERE t.site_id = #{siteId} and t.main_equipment_id = #{mainEquipmentId}
        	AND t.pname = #{pname}
        	AND t.number = #{number}
        GROUP BY name
        ORDER BY t.sort_num ASC, t.name ASC
	</select>
    <select id="findParams" resultType="com.huatek.busi.vo.BusiParamAppendixVo">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        busi_param_appendix t
        WHERE t.site_id = #{siteId} and t.main_equipment_id = #{mainEquipmentId}
        AND t.number = #{number}
        GROUP BY name
        ORDER BY t.sort_num ASC, t.name ASC
    </select>

    <select id="findFirstLevelNumber" resultType="com.huatek.busi.vo.BusiParamAppendixVo">
        SELECT
        max(number) as number
        FROM
        busi_param_appendix
        WHERE main_equipment_id = #{mainEquipmentId}
    </select>

    <select id="findFirstLevelNumberVoluntarily" resultType="com.huatek.busi.vo.BusiParamAppendixVo">
        SELECT
        max(number) as number
        FROM
        busi_param_appendix_voluntarily
        WHERE main_equipment_id = #{mainEquipmentId}
    </select>
    <select id="findExperimentData" resultType="String">
        select  c.current_value AS value from x_lab_report_param_data c
        LEFT JOIN x_lab_report_basis_data A  ON A.id = C.r_id
        LEFT JOIN x_r_master_data B ON A.m_id = B.id
        where b.deleted !='1'
        and c.deleted !='1'
        and B.equipment_type = '1'
        <if test="equLittleId != null and equLittleId != ''">
            AND B.equ_little_id = #{equLittleId}
        </if>
        <if test="testItem != null and testItem != ''">
            AND A.test_item like CONCAT('%',#{testItem},'%')
        </if>
        <if test="offlineParam != null and offlineParam != ''">
            AND C.offline_param like CONCAT('%',#{offlineParam},'%')
        </if>
        order  by a.create_time desc,c.current_value desc;
    </select>
</mapper>
