<?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.LabReportMarkStatisticMapper">
    <sql id="Base_Column_List">
        id,
        ref_id AS refId,
        report_id AS reportId,
        mark,
        valid_value_range AS validValueRange,
        current_value AS currentValue,
        unit,
        test_result AS testResult,
        equ_type_rule AS equTypeRule,
        e_rule AS eRule,
        e_calculate_rule AS eCalculateRule,
        time_rule AS timeRule,
        test_interval AS testInterval,
        test_type AS testType,
        e_param AS eParam,
        remark,
        test_date as testDate,
        main_equipment_id as mainEquipmentId,
        create_user AS createUser,
        create_time AS createTime,
        update_user AS updateUser,
        update_time AS updateTime,
        deleted
    </sql>

    <select id="queryList" resultType="com.huatek.busi.dto.LabReportMarkStatisticDto">
        SELECT
            <include refid="Base_Column_List"/>
        FROM
            x_lab_report_mark_statistic
        WHERE
            deleted = 0
    </select>

    <update id="deleteByRIdAndTestDate">
        UPDATE x_lab_report_mark_statistic
        SET deleted = 1
        WHERE
            ref_id = #{refId}
    </update>

    <update id="deleteBlqByRIdAndTestDate">
        UPDATE x_lab_arrester_report_mark_statistic
        SET deleted = 1
        WHERE
            ref_id = #{refId}
    </update>

    <update id="deleteDlByRIdAndTestDate">
        UPDATE x_lab_cable_report_mark_statistic
        SET deleted = 1
        WHERE
            ref_id = #{refId}
    </update>

    <update id="deleteKggByRIdAndTestDate">
        UPDATE x_lab_switchgear_report_mark_statistic
        SET deleted = 1
        WHERE
            ref_id = #{refId}
    </update>

    <update id="deleteGenByRIdAndTestDate">
        UPDATE x_lab_generator_report_mark_statistic
        SET deleted = 1
        WHERE
            ref_id = #{refId}
    </update>

    <update id="updateByRefIds" parameterType="java.util.List">
        UPDATE x_lab_report_mark_statistic
        SET deleted = 1
        WHERE
            ref_id in
        <foreach collection="refIds" index="index" item="refId" open="(" separator="," close=")">
            #{refId}
        </foreach>
    </update>

    <update id="updateBlqByRefIds" parameterType="java.util.List">
        UPDATE x_lab_arrester_report_mark_statistic
        SET deleted = 1
        WHERE
        ref_id in
        <foreach collection="refIds" index="index" item="refId" open="(" separator="," close=")">
            #{refId}
        </foreach>
    </update>

    <update id="updateDlByRefIds" parameterType="java.util.List">
        UPDATE x_lab_cable_report_mark_statistic
        SET deleted = 1
        WHERE
        ref_id in
        <foreach collection="refIds" index="index" item="refId" open="(" separator="," close=")">
            #{refId}
        </foreach>
    </update>

    <update id="updateKggByRefIds" parameterType="java.util.List">
        UPDATE x_lab_switchgear_report_mark_statistic
        SET deleted = 1
        WHERE
        ref_id in
        <foreach collection="refIds" index="index" item="refId" open="(" separator="," close=")">
            #{refId}
        </foreach>
    </update>

    <update id="updateGenByRefIds" parameterType="java.util.List">
        UPDATE x_lab_generator_report_mark_statistic
        SET deleted = 1
        WHERE
        ref_id in
        <foreach collection="refIds" index="index" item="refId" open="(" separator="," close=")">
            #{refId}
        </foreach>
    </update>

    <select id="findMaxValue" parameterType="java.lang.String" resultType="java.lang.Double">
        SELECT
            max(
                cast(
                    current_value AS DECIMAL (10, 2)
                )
            ) AS currentValue
        FROM
            x_lab_report_mark_statistic
        WHERE
            e_rule = #{key}
        AND main_equipment_id = #{mainTreeId}
        and deleted = 0
        AND test_date = (
            SELECT
                max(test_date)
            FROM
                x_lab_report_mark_statistic
            WHERE
                deleted = 0
                AND main_equipment_id = #{mainTreeId}
                AND e_rule = #{key}
        )
    </select>

    <select id="findMinValue" parameterType="java.lang.String" resultType="java.lang.Double">
        SELECT
            min(
                cast(
                    current_value AS DECIMAL (10, 2)
                )
            ) AS currentValue
        FROM
            x_lab_report_mark_statistic
        WHERE
            e_rule = #{key}
        AND main_equipment_id = #{mainTreeId}
        and deleted = 0
        AND test_date = (
            SELECT
                max(test_date)
            FROM
                x_lab_report_mark_statistic
            WHERE
                deleted = 0
                AND main_equipment_id = #{mainTreeId}
                AND e_rule = #{key}
        )
    </select>

    <select id="findMaxValueByLTime" parameterType="java.lang.String" resultType="java.lang.Double">
        SELECT
            max(
                cast(
                    current_value AS DECIMAL (10, 2)
                )
            ) AS currentValue
        FROM
            x_lab_report_mark_statistic
        WHERE
            e_rule = #{key}
        AND main_equipment_id = #{mainTreeId}
        and deleted = 0
        AND test_date = (
            SELECT
                min(test_date)
            FROM
                x_lab_report_mark_statistic
            WHERE
                deleted = 0
                AND main_equipment_id = #{mainTreeId}
                AND e_rule = #{key}
        )
    </select>

    <select id="findMinValueByLTime" parameterType="java.lang.String" resultType="java.lang.Double">
        SELECT
            min(
                cast(
                    current_value AS DECIMAL (10, 2)
                )
            ) AS currentValue
        FROM
            x_lab_report_mark_statistic
        WHERE
            e_rule = #{key}
        AND main_equipment_id = #{mainTreeId}
        and deleted = 0
        AND test_date = (
            SELECT
                min(test_date)
            FROM
                x_lab_report_mark_statistic
            WHERE
                deleted = 0
                AND main_equipment_id = #{mainTreeId}
                AND e_rule = #{key}
        )
    </select>

    <select id="findValueByLTime" parameterType="java.lang.String" resultType="com.huatek.busi.vo.labValueVo">
        SELECT
            cast(
                current_value AS DECIMAL (10, 2)
            ) AS currentValue
        FROM
            x_lab_report_mark_statistic
        WHERE
            e_rule = #{key}
        AND main_equipment_id = #{mainTreeId}
        and deleted = 0
        AND test_date = (
            SELECT
                min(test_date)
            FROM
                x_lab_report_mark_statistic
            WHERE
                deleted = 0
                AND main_equipment_id = #{mainTreeId}
                AND e_rule = #{key}
        )
    </select>

    <select id="findValue" parameterType="java.lang.String" resultType="com.huatek.busi.vo.labValueVo">
        SELECT
            cast(
                current_value AS DECIMAL (10, 2)
            ) AS currentValue
        FROM
            x_lab_report_mark_statistic
        WHERE
            e_rule = #{key}
        AND main_equipment_id = #{mainTreeId}
        and deleted = 0
        AND test_date = (
            SELECT
                max(test_date)
            FROM
                x_lab_report_mark_statistic
            WHERE
                deleted = 0
                AND main_equipment_id = #{mainTreeId}
                AND e_rule = #{key}
        )
    </select>

    <select id="findMainTreeId" parameterType="java.lang.String" resultType="com.huatek.busi.entity.LabReportMarkStatistic">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        x_lab_report_mark_statistic
        WHERE
        deleted = 0
        and main_equipment_id = #{mainTreeId}
    </select>

    <select id="countTimes" parameterType="java.lang.String" resultType="com.huatek.busi.vo.epa.EpaEvaluatRepairDataVo">
        SELECT
            COUNT(*) AS total,
            mark AS parts,
            current_value AS type,
            e_rule AS name
        FROM
            x_lab_report_mark_statistic
        WHERE
            main_equipment_id = #{mainEquipmentId}
            AND deleted = 0
            AND e_rule IN ('0101','0102','0100','0099','0098')
            AND current_value != 0
        GROUP BY
            mark,
            current_value
    </select>

    <select id="findRules" parameterType="java.lang.String" resultType="com.huatek.busi.vo.labRulesVo">
        SELECT
            e_rule AS eRule,
            e_calculate_rule AS eCalcualteRule,
            time_rule AS timeRule
        FROM
            x_lab_report_mark_statistic
        WHERE
            e_rule = #{key}
            AND main_equipment_id = #{mainTreeId}
            AND deleted = 0
        ORDER BY
            create_time DESC
            LIMIT 1
    </select>

    <select id="findAllManualAssessmentData" parameterType="java.lang.String" resultType="com.huatek.busi.entity.LabReportMarkStatistic">
        (SELECT
            <include refid="Base_Column_List"/>
        FROM
            x_lab_report_mark_statistic a
        WHERE
            a.main_equipment_id = #{mainTreeId}
            AND a.deleted = '0'
            AND a.test_date = (
            SELECT
                MAX( test_date )
            FROM
                x_lab_report_mark_statistic b
            WHERE
                b.main_equipment_id = #{mainTreeId}
                AND b.deleted = '0'
                AND b.e_param = a.e_param
                AND b.time_rule = 'X'
            )
        ORDER BY
            a.e_rule DESC)
        UNION
        (SELECT
            <include refid="Base_Column_List"/>
        FROM
            x_lab_report_mark_statistic a
        WHERE
            a.main_equipment_id = #{mainTreeId}
            AND a.deleted = '0'
            AND a.test_date = (
            SELECT
                MIN( test_date )
            FROM
                x_lab_report_mark_statistic b
            WHERE
                b.main_equipment_id = #{mainTreeId}
                AND b.deleted = '0'
                AND b.e_param = a.e_param
                AND b.time_rule = 'L'
            )
        ORDER BY
        a.e_rule DESC)
    </select>

    <select id="findId" parameterType="java.lang.String" resultType="string">
        SELECT
            id
        FROM
            sys_dictionnaire
        WHERE
            code = #{key}
    </select>

    <select id="list" parameterType="com.huatek.busi.vo.LabReportMarkVo" resultType="com.huatek.busi.dto.LabReportMarkDto">
        SELECT
            A.test_interval AS testInterval,
            A.test_type AS testType,
            A.mark AS offlineParam,
            A.valid_value_range AS valueRange,
            A.current_value AS currentValue,
            E.dname AS unit,
            A.e_param AS param,
            A.remark AS remark,
            B.`name` AS equipmentName,
            D.dname AS testResult,
            C.test_item AS testItem,
            C.checker AS checker,
            C.test_equ AS testEquipment,
            C.test_date AS testDate,
            C.test_staff AS testStaff,
            C.maintenance_unit AS maintenance,
            F.sheet_name AS sheetName,
        	G.absolute_rate AS absRate,
        	G.relative_rate AS relRate,
        	G.deviation_rate AS devRate
        FROM
            x_lab_report_mark_statistic A
            LEFT JOIN busi_site_tree B ON A.main_equipment_id = B.id
            LEFT JOIN x_lab_report_basis_data C ON A.report_id = C.id
            LEFT JOIN sys_dictionnaire_detail D ON A.test_result = D.dcode
            LEFT JOIN sys_dictionnaire_detail E ON A.unit = E.dcode
            LEFT JOIN x_r_master_data F ON C.m_id = F.id
        	LEFT JOIN x_param_mark G ON G.report_id = A.report_id AND G.ref_id = A.ref_id AND G.mark_id = A.mark_id
        WHERE
            A.deleted = #{deleted}
            AND B.`status` = #{status}
            AND C.deleted = #{deleted}
            AND F.deleted = #{deleted}
            <if test="markId != null and markId != ''">
            AND A.mark_id = #{markId}
            </if>
            <if test="resultId != null and resultId != ''">
            AND D.pid = #{resultId}
            </if>
            <if test="unitId != null and unitId != ''">
            AND E.pid = #{unitId}
            </if>
        ORDER BY
            A.create_time DESC
    </select>

    <select id="listBlq" parameterType="com.huatek.busi.vo.LabReportMarkVo" resultType="com.huatek.busi.dto.LabReportMarkDto">
        SELECT
            A.test_interval AS testInterval,
            A.test_type AS testType,
            A.mark AS offlineParam,
            A.valid_value_range AS valueRange,
            A.current_value AS currentValue,
            E.dname AS unit,
            A.e_param AS param,
            A.remark AS remark,
            B.`name` AS eqName,
            D.dname AS testResult,
            C.test_item AS testItem,
            C.checker AS checker,
            C.test_equ AS testEquipment,
            C.test_date AS testDate,
            C.test_staff AS testStaff,
            C.maintenance_unit AS maintenance,
            F.sheet_name AS sheetName,
        	G.absolute_rate AS absRate,
        	G.relative_rate AS relRate,
        	G.deviation_rate AS devRate
        FROM
            x_lab_arrester_report_mark_statistic A
            LEFT JOIN busi_site_tree B ON A.main_equipment_id = B.id
            LEFT JOIN x_lab_arrester_report_basis_data C ON A.report_id = C.id
            LEFT JOIN sys_dictionnaire_detail D ON A.test_result = D.dcode
            LEFT JOIN sys_dictionnaire_detail E ON A.unit = E.dcode
            LEFT JOIN x_r_arrester_data F ON C.m_id = F.id
        	LEFT JOIN x_param_mark G ON G.report_id = A.report_id AND G.ref_id = A.ref_id AND G.mark_id = A.mark_id
        WHERE
            A.deleted = #{deleted}
            AND B.`status` = #{status}
            AND C.deleted = #{deleted}
            AND F.deleted = #{deleted}
            <if test="markId != null and markId != ''">
                AND A.mark_id = #{markId}
            </if>
            <if test="resultId != null and resultId != ''">
                AND D.pid = #{resultId}
            </if>
            <if test="unitId != null and unitId != ''">
                AND E.pid = #{unitId}
            </if>
        ORDER BY
            A.create_time DESC
    </select>

    <select id="listKgg" parameterType="com.huatek.busi.vo.LabReportMarkVo" resultType="com.huatek.busi.dto.LabReportMarkDto">
        SELECT
            A.test_interval AS testInterval,
            A.test_type AS testType,
            A.mark AS offlineParam,
            A.valid_value_range AS valueRange,
            A.current_value AS currentValue,
            E.dname AS unit,
            A.e_param AS param,
            A.remark AS remark,
            B.`name` AS eqName,
            D.dname AS testResult,
            C.test_item AS testItem,
            C.checker AS checker,
            C.test_equ AS testEquipment,
            C.test_date AS testDate,
            C.test_staff AS testStaff,
            C.maintenance_unit AS maintenance,
            F.sheet_name AS sheetName,
        	G.absolute_rate AS absRate,
        	G.relative_rate AS relRate,
        	G.deviation_rate AS devRate
        FROM
            x_lab_switchgear_report_mark_statistic A
            LEFT JOIN busi_site_tree B ON A.main_equipment_id = B.id
            LEFT JOIN x_lab_switchgear_report_basis_data C ON A.report_id = C.id
            LEFT JOIN sys_dictionnaire_detail D ON A.test_result = D.dcode
            LEFT JOIN sys_dictionnaire_detail E ON A.unit = E.dcode
            LEFT JOIN x_r_switchgear_data F ON C.m_id = F.id
        	LEFT JOIN x_param_mark G ON G.report_id = A.report_id AND G.ref_id = A.ref_id AND G.mark_id = A.mark_id
        WHERE
            A.deleted = #{deleted}
            AND B.`status` = #{status}
            AND C.deleted = #{deleted}
            AND F.deleted = #{deleted}
            <if test="markId != null and markId != ''">
                AND A.mark_id = #{markId}
            </if>
            <if test="resultId != null and resultId != ''">
                AND D.pid = #{resultId}
            </if>
            <if test="unitId != null and unitId != ''">
                AND E.pid = #{unitId}
            </if>
        ORDER BY
            A.create_time DESC
    </select>

    <select id="listDl" parameterType="com.huatek.busi.vo.LabReportMarkVo" resultType="com.huatek.busi.dto.LabReportMarkDto">
        SELECT
            A.test_interval AS testInterval,
            A.test_type AS testType,
            A.mark AS offlineParam,
            A.valid_value_range AS valueRange,
            A.current_value AS currentValue,
            E.dname AS unit,
            A.e_param AS param,
            A.remark AS remark,
            B.`name` AS eqName,
            D.dname AS testResult,
            C.test_item AS testItem,
            C.checker AS checker,
            C.test_equ AS testEquipment,
            C.test_date AS testDate,
            C.test_staff AS testStaff,
            C.maintenance_unit AS maintenance,
            F.sheet_name AS sheetName,
        	G.absolute_rate AS absRate,
        	G.relative_rate AS relRate,
        	G.deviation_rate AS devRate
        FROM
            x_lab_cable_report_mark_statistic A
            LEFT JOIN busi_site_tree B ON A.main_equipment_id = B.id
            LEFT JOIN x_lab_cable_report_basis_data C ON A.report_id = C.id
            LEFT JOIN sys_dictionnaire_detail D ON A.test_result = D.dcode
            LEFT JOIN sys_dictionnaire_detail E ON A.unit = E.dcode
            LEFT JOIN x_r_cable_data F ON C.m_id = F.id
        	LEFT JOIN x_param_mark G ON G.report_id = A.report_id AND G.ref_id = A.ref_id AND G.mark_id = A.mark_id
        WHERE
            A.deleted = #{deleted}
            AND B.`status` = #{status}
            AND C.deleted = #{deleted}
            AND F.deleted = #{deleted}
            <if test="markId != null and markId != ''">
                AND A.mark_id = #{markId}
            </if>
            <if test="resultId != null and resultId != ''">
                AND D.pid = #{resultId}
            </if>
            <if test="unitId != null and unitId != ''">
                AND E.pid = #{unitId}
            </if>
        ORDER BY
            A.create_time DESC
    </select>

    <select id="listGen" parameterType="com.huatek.busi.vo.LabReportMarkVo" resultType="com.huatek.busi.dto.LabReportMarkDto">
        SELECT
            A.test_interval AS testInterval,
            A.test_type AS testType,
            A.mark AS offlineParam,
            A.valid_value_range AS valueRange,
            A.current_value AS currentValue,
            E.dname AS unit,
            A.e_param AS param,
            A.remark AS remark,
            B.`name` AS eqName,
            D.dname AS testResult,
            C.test_item AS testItem,
            C.checker AS checker,
            C.test_equ AS testEquipment,
            C.test_date AS testDate,
            C.test_staff AS testStaff,
            C.maintenance_unit AS maintenance,
            F.sheet_name AS sheetName,
        	G.absolute_rate AS absRate,
        	G.relative_rate AS relRate,
        	G.deviation_rate AS devRate
        FROM
            x_lab_generator_report_mark_statistic A
            LEFT JOIN busi_site_tree B ON A.main_equipment_id = B.id
            LEFT JOIN x_lab_generator_report_basis_data C ON A.report_id = C.id
            LEFT JOIN sys_dictionnaire_detail D ON A.test_result = D.dcode
            LEFT JOIN sys_dictionnaire_detail E ON A.unit = E.dcode
            LEFT JOIN x_r_generator_data F ON C.m_id = F.id
        	LEFT JOIN x_param_mark G ON G.report_id = A.report_id AND G.ref_id = A.ref_id AND G.mark_id = A.mark_id
        WHERE
            A.deleted = #{deleted}
            AND B.`status` = #{status}
            AND C.deleted = #{deleted}
            AND F.deleted = #{deleted}
            <if test="markId != null and markId != ''">
                AND A.mark_id = #{markId}
            </if>
            <if test="resultId != null and resultId != ''">
                AND D.pid = #{resultId}
            </if>
            <if test="unitId != null and unitId != ''">
                AND E.pid = #{unitId}
            </if>
        ORDER BY
            A.create_time DESC
    </select>

    <select id="searchHistoryData" parameterType="com.huatek.busi.vo.LabReportMarkVo" resultType="com.huatek.busi.dto.LabReportMarkDto">
        SELECT DISTINCT A.id,
        A.current_value AS currentValue,
        A.test_date AS testDate,
        A.create_time AS createTime,
        D.file_name AS fileName,
        B.dname AS unit,
        C.absolute_rate AS absRate,
        C.relative_rate AS relRate,
        C.deviation_rate AS devRate
        FROM
        x_lab_report_mark_statistic A
        LEFT JOIN sys_dictionnaire_detail B ON A.unit = B.dcode
        LEFT JOIN x_param_mark C ON C.report_id = A.report_id
        AND C.ref_id = A.ref_id
        AND C.mark_id = A.mark_id
        LEFT JOIN sys_file D ON D.business_id = A.ref_id
        WHERE
        A.`deleted` = #{deleted}
        <if test="markId !=null and markId != ''">
        AND A.mark_id = #{markId}
        </if>
        <if test="unitId !=null and unitId != ''">
        AND B.pid = #{unitId}
        </if>
        <if test="startTime !=null">
            AND A.test_date<![CDATA[ >= ]]>#{startTime}
        </if>
        <if test="endTime !=null">
            AND A.test_date<![CDATA[ <= ]]>#{endTime}
        </if>
        ORDER BY
        A.create_time DESC
    </select>

    <select id="getHistoryData" parameterType="com.huatek.busi.vo.LabReportMarkVo" resultType="com.huatek.busi.dto.LabReportMarkDto">
        ( SELECT DISTINCT
        A.id,
        A.current_value AS currentValue,
        A.test_date AS testDate,
        D.file_name AS fileName,
        C.absolute_rate AS absRate,
        C.relative_rate AS relRate,
        C.deviation_rate AS devRate
        FROM
        x_lab_report_mark_statistic A
        LEFT JOIN x_param_mark C ON C.report_id = A.report_id
        AND C.ref_id = A.ref_id
        AND C.mark_id = A.mark_id
        LEFT JOIN sys_file D ON D.business_id = A.ref_id
        WHERE
        A.`deleted` = #{deleted}
        <if test="markId !=null and markId != ''">
            AND A.mark_id = #{markId}
        </if>
        AND D.file_type = #{fileType}
        <if test="testDate !=null">
            and A.test_date <![CDATA[ < ]]> #{testDate}
        </if>
        ORDER BY
        A.test_date DESC
        LIMIT 10
        ) UNION
        (
        SELECT DISTINCT
        A.id,
        A.current_value AS currentValue,
        A.test_date AS testDate,
        D.file_name AS fileName,
        C.absolute_rate AS absRate,
        C.relative_rate AS relRate,
        C.deviation_rate AS devRate
        FROM
        x_lab_report_mark_statistic A
        LEFT JOIN x_param_mark C ON C.report_id = A.report_id
        AND C.ref_id = A.ref_id
        AND C.mark_id = A.mark_id
        LEFT JOIN sys_file D ON D.business_id = A.ref_id
        WHERE
        A.`deleted` = #{deleted}
        <if test="markId !=null and markId != ''">
            AND A.mark_id = #{markId}
        </if>
        AND D.file_type = #{fileType}
        <if test="testDate !=null">
            and A.test_date <![CDATA[ >= ]]> #{testDate}
        </if>
        ORDER BY
        A.test_date ASC
        LIMIT 10
        )
        ORDER BY
        testDate DESC
    </select>

    <select id="getHistoryBlqData" parameterType="com.huatek.busi.vo.LabReportMarkVo" resultType="com.huatek.busi.dto.LabReportMarkDto">
        ( SELECT DISTINCT
        A.id,
        A.current_value AS currentValue,
        A.test_date AS testDate,
        D.file_name AS fileName,
        C.absolute_rate AS absRate,
        C.relative_rate AS relRate,
        C.deviation_rate AS devRate
        FROM
        x_lab_arrester_report_mark_statistic A
        LEFT JOIN x_param_mark C ON C.report_id = A.report_id
        AND C.ref_id = A.ref_id
        AND C.mark_id = A.mark_id
        LEFT JOIN sys_file D ON D.business_id = A.ref_id
        WHERE
        A.`deleted` = #{deleted}
        <if test="markId !=null and markId != ''">
            AND A.mark_id = #{markId}
        </if>
        AND D.file_type = #{fileType}
        <if test="testDate !=null">
            and A.test_date <![CDATA[ < ]]> #{testDate}
        </if>
        ORDER BY
        A.test_date DESC
        LIMIT 10
        ) UNION
        (
        SELECT DISTINCT
        A.id,
        A.current_value AS currentValue,
        A.test_date AS testDate,
        D.file_name AS fileName,
        C.absolute_rate AS absRate,
        C.relative_rate AS relRate,
        C.deviation_rate AS devRate
        FROM
        x_lab_arrester_report_mark_statistic A
        LEFT JOIN x_param_mark C ON C.report_id = A.report_id
        AND C.ref_id = A.ref_id
        AND C.mark_id = A.mark_id
        LEFT JOIN sys_file D ON D.business_id = A.ref_id
        WHERE
        A.`deleted` = #{deleted}
        <if test="markId !=null and markId != ''">
            AND A.mark_id = #{markId}
        </if>
        AND D.file_type = #{fileType}
        <if test="testDate !=null">
            and A.test_date <![CDATA[ >= ]]> #{testDate}
        </if>
        ORDER BY
        A.test_date ASC
        LIMIT 10
        )
        ORDER BY
        testDate DESC
    </select>

    <select id="getHistoryDlData" parameterType="com.huatek.busi.vo.LabReportMarkVo" resultType="com.huatek.busi.dto.LabReportMarkDto">
        ( SELECT DISTINCT
        A.id,
        A.current_value AS currentValue,
        A.test_date AS testDate,
        D.file_name AS fileName,
        C.absolute_rate AS absRate,
        C.relative_rate AS relRate,
        C.deviation_rate AS devRate
        FROM
        x_lab_cable_report_mark_statistic A
        LEFT JOIN x_param_mark C ON C.report_id = A.report_id
        AND C.ref_id = A.ref_id
        AND C.mark_id = A.mark_id
        LEFT JOIN sys_file D ON D.business_id = A.ref_id
        WHERE
        A.`deleted` = #{deleted}
        <if test="markId !=null and markId != ''">
            AND A.mark_id = #{markId}
        </if>
        AND D.file_type = #{fileType}
        <if test="testDate !=null">
            and A.test_date <![CDATA[ < ]]> #{testDate}
        </if>
        ORDER BY
        A.test_date DESC
        LIMIT 10
        ) UNION
        (
        SELECT DISTINCT
        A.id,
        A.current_value AS currentValue,
        A.test_date AS testDate,
        D.file_name AS fileName,
        C.absolute_rate AS absRate,
        C.relative_rate AS relRate,
        C.deviation_rate AS devRate
        FROM
        x_lab_cable_report_mark_statistic A
        LEFT JOIN x_param_mark C ON C.report_id = A.report_id
        AND C.ref_id = A.ref_id
        AND C.mark_id = A.mark_id
        LEFT JOIN sys_file D ON D.business_id = A.ref_id
        WHERE
        A.`deleted` = #{deleted}
        <if test="markId !=null and markId != ''">
            AND A.mark_id = #{markId}
        </if>
        AND D.file_type = #{fileType}
        <if test="testDate !=null">
            and A.test_date <![CDATA[ >= ]]> #{testDate}
        </if>
        ORDER BY
        A.test_date ASC
        LIMIT 10
        )
        ORDER BY
        testDate DESC
    </select>

    <select id="getHistoryKggData" parameterType="com.huatek.busi.vo.LabReportMarkVo" resultType="com.huatek.busi.dto.LabReportMarkDto">
        ( SELECT DISTINCT
        A.id,
        A.current_value AS currentValue,
        A.test_date AS testDate,
        D.file_name AS fileName,
        C.absolute_rate AS absRate,
        C.relative_rate AS relRate,
        C.deviation_rate AS devRate
        FROM
        x_lab_switchgear_report_mark_statistic A
        LEFT JOIN x_param_mark C ON C.report_id = A.report_id
        AND C.ref_id = A.ref_id
        AND C.mark_id = A.mark_id
        LEFT JOIN sys_file D ON D.business_id = A.ref_id
        WHERE
        A.`deleted` = #{deleted}
        <if test="markId !=null and markId != ''">
            AND A.mark_id = #{markId}
        </if>
        AND D.file_type = #{fileType}
        <if test="testDate !=null">
            and A.test_date <![CDATA[ < ]]> #{testDate}
        </if>
        ORDER BY
        A.test_date DESC
        LIMIT 10
        ) UNION
        (
        SELECT DISTINCT
        A.id,
        A.current_value AS currentValue,
        A.test_date AS testDate,
        D.file_name AS fileName,
        C.absolute_rate AS absRate,
        C.relative_rate AS relRate,
        C.deviation_rate AS devRate
        FROM
        x_lab_switchgear_report_mark_statistic A
        LEFT JOIN x_param_mark C ON C.report_id = A.report_id
        AND C.ref_id = A.ref_id
        AND C.mark_id = A.mark_id
        LEFT JOIN sys_file D ON D.business_id = A.ref_id
        WHERE
        A.`deleted` = #{deleted}
        <if test="markId !=null and markId != ''">
            AND A.mark_id = #{markId}
        </if>
        AND D.file_type = #{fileType}
        <if test="testDate !=null">
            and A.test_date <![CDATA[ >= ]]> #{testDate}
        </if>
        ORDER BY
        A.test_date ASC
        LIMIT 10
        )
        ORDER BY
        testDate DESC
    </select>

    <select id="getHistoryGenData" parameterType="com.huatek.busi.vo.LabReportMarkVo" resultType="com.huatek.busi.dto.LabReportMarkDto">
        ( SELECT DISTINCT
        A.id,
        A.current_value AS currentValue,
        A.test_date AS testDate,
        D.file_name AS fileName,
        C.absolute_rate AS absRate,
        C.relative_rate AS relRate,
        C.deviation_rate AS devRate
        FROM
        x_lab_generator_report_mark_statistic A
        LEFT JOIN x_param_mark C ON C.report_id = A.report_id
        AND C.ref_id = A.ref_id
        AND C.mark_id = A.mark_id
        LEFT JOIN sys_file D ON D.business_id = A.ref_id
        WHERE
        A.`deleted` = #{deleted}
        <if test="markId !=null and markId != ''">
            AND A.mark_id = #{markId}
        </if>
        AND D.file_type = #{fileType}
        <if test="testDate !=null">
            and A.test_date <![CDATA[ < ]]> #{testDate}
        </if>
        ORDER BY
        A.test_date DESC
        LIMIT 10
        ) UNION
        (
        SELECT DISTINCT
        A.id,
        A.current_value AS currentValue,
        A.test_date AS testDate,
        D.file_name AS fileName,
        C.absolute_rate AS absRate,
        C.relative_rate AS relRate,
        C.deviation_rate AS devRate
        FROM
        x_lab_generator_report_mark_statistic A
        LEFT JOIN x_param_mark C ON C.report_id = A.report_id
        AND C.ref_id = A.ref_id
        AND C.mark_id = A.mark_id
        LEFT JOIN sys_file D ON D.business_id = A.ref_id
        WHERE
        A.`deleted` = #{deleted}
        <if test="markId !=null and markId != ''">
            AND A.mark_id = #{markId}
        </if>
        AND D.file_type = #{fileType}
        <if test="testDate !=null">
            and A.test_date <![CDATA[ >= ]]> #{testDate}
        </if>
        ORDER BY
        A.test_date ASC
        LIMIT 10
        )
        ORDER BY
        testDate DESC
    </select>
    <select id="getLastImportInfo" resultType="com.huatek.busi.dto.LabReportMarkStatisticDto">
        SELECT
            current_value AS currentValue,
            e_rule AS eRule
        FROM
            x_lab_report_mark_statistic
        where
            (e_rule, create_time) in (select e_rule, max(create_time) from x_lab_report_mark_statistic group by e_rule )
            and deleted = 0
            and main_equipment_id = #{equipmentId}
            and equ_type_rule = #{type}
        <if test='type == "T"'>
            and e_rule in ( '0032',
                            '0033',
                            '0034',
                            '0035',
                            '0036',
                            '0037',
                            '0038',
                            '0039',
                            '0040',
                            '0041',
                            '0042',
                            '0043',
                            '0044',
                            '0045')
        </if>
        <if test='type == "G"'>
            and e_rule in ( '0046',
                            '0047',
                            '0048',
                            '0049',
                            '0050',
                            '0051',
                            '0052',
                            '0053',
                            '0054',
                            '0055',
                            '0056',
                            '0057')
        </if>
    </select>
</mapper>
