<?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.yeejoin.equipmanage.mapper.AnalysisReportMonthMapper">

    <insert id="insertSystemMonthData">
        insert into wl_analysis_report_month
        (
            system_id,
            num,
            equipment_name,
            equipment_code,
            fault_equip_num,
            fault_info_num,
            alarm_equip_num,
            alarm_info_num,
            total_info_num,
            report_month
        )
        select
            a1.*,
            DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 1 day),'%Y-%m')
        from
        (SELECT
        system_id,
        num,
        name,
        code,
        (SELECT
        count(distinct equipment_specific_id)
        FROM wl_equipment_alarm_report_day report
        where report.equipment_code = a.code and FIND_IN_SET(a.system_id,report.system_ids)
        and report.index_true_num >0
        and report.index_type  LIKE  CONCAT('%','Fault')
        and report.report_date >= #{beginDate}
        and report.report_date <![CDATA[<=]]> #{endDate}
        )  as faultEquipNum,
        (SELECT IFNULL(sum(report.index_true_num),0)
        FROM wl_equipment_alarm_report_day report
        where report.equipment_code = a.code and FIND_IN_SET(a.system_id,report.system_ids)
        and report.index_type  LIKE  CONCAT('%','Fault')
        and report.report_date >= #{beginDate}
        and report.report_date <![CDATA[<=]]> #{endDate}
        )  as faultInfoNum,
        (SELECT count(distinct equipment_specific_id)
        FROM wl_equipment_alarm_report_day report
        where report.equipment_code = a.code and FIND_IN_SET(a.system_id,report.system_ids)
        and report.index_true_num >0
        <if test="indexKeySuffix.size() > 0">
        and
            <foreach collection="indexKeySuffix" item="keySuffix" open="(" close=")" separator="or">
                report.index_type  LIKE  CONCAT('%',#{keySuffix})
            </foreach>
        </if>
        and report.report_date >= #{beginDate}
        and report.report_date <![CDATA[<=]]> #{endDate}
        )  as alarmEquipNum,
        (SELECT IFNULL(sum(report.index_true_num),0)
        FROM wl_equipment_alarm_report_day report
        where report.equipment_code = a.code and FIND_IN_SET(a.system_id,report.system_ids)
        <if test="indexKeySuffix.size() > 0">
            and
            <foreach collection="indexKeySuffix" item="keySuffix" open="(" close=")" separator="or">
                report.index_type  LIKE  CONCAT('%',#{keySuffix})
            </foreach>
        </if>
        and report.report_date >= #{beginDate}
        and report.report_date <![CDATA[<=]]> #{endDate}
        )  as alarmInfoNum,
        (SELECT IFNULL(sum(report.index_true_num),0)
        FROM wl_equipment_alarm_report_day report
        where report.equipment_code = a.code and FIND_IN_SET(a.system_id,report.system_ids)
        and report.report_date >= #{beginDate}
        and report.report_date <![CDATA[<=]]> #{endDate}
        )  as totalInfoNum
        FROM(
        SELECT
        s.id as system_id,
        equipment.code,
        equipment.name,
        count(1) AS num
        FROM
        wl_equipment_specific spec
        LEFT JOIN wl_equipment_detail detail ON spec.equipment_detail_id = detail.id
        LEFT JOIN wl_equipment equipment ON equipment.id =detail.equipment_id
        left join f_fire_fighting_system s on FIND_IN_SET(s.id,spec.system_id)
        where spec.system_id <![CDATA[<>]]>  '' and s.id = #{systemId}
        GROUP BY equipment.code,s.id, equipment.name ORDER BY system_id, code
        ) a) as  a1
    </insert>

    <insert id="insertSystemMonthDataTest">
        insert into wl_analysis_report_month
        (
        system_id,
        num,
        equipment_name,
        equipment_code,
        fault_equip_num,
        fault_info_num,
        alarm_equip_num,
        alarm_info_num,
        total_info_num,
        report_month
        )
        select
        a1.*,
        DATE_FORMAT(DATE_SUB(#{date},INTERVAL #{num} day),'%Y-%m')
        from
        (SELECT
        system_id,
        num,
        name,
        code,
        (SELECT
        count(distinct equipment_specific_id)
        FROM wl_equipment_alarm_report_day report
        where report.equipment_code = a.code and FIND_IN_SET(a.system_id,report.system_ids)
        and report.index_true_num >0
        and report.index_type  LIKE  CONCAT('%','Fault')
        and report.report_date >= #{beginDate}
        and report.report_date <![CDATA[<=]]> #{endDate}
        )  as faultEquipNum,
        (SELECT IFNULL(sum(report.index_true_num),0)
        FROM wl_equipment_alarm_report_day report
        where report.equipment_code = a.code and FIND_IN_SET(a.system_id,report.system_ids)
        and report.index_type  LIKE  CONCAT('%','Fault')
        and report.report_date >= #{beginDate}
        and report.report_date <![CDATA[<=]]> #{endDate}
        )  as faultInfoNum,
        (SELECT count(distinct equipment_specific_id)
        FROM wl_equipment_alarm_report_day report
        where report.equipment_code = a.code and FIND_IN_SET(a.system_id,report.system_ids)
        and report.index_true_num >0
        <if test="indexKeySuffix.size() > 0">
            and
            <foreach collection="indexKeySuffix" item="keySuffix" open="(" close=")" separator="or">
                report.index_type  LIKE  CONCAT('%',#{keySuffix})
            </foreach>
        </if>
        and report.report_date >= #{beginDate}
        and report.report_date <![CDATA[<=]]> #{endDate}
        )  as alarmEquipNum,
        (SELECT IFNULL(sum(report.index_true_num),0)
        FROM wl_equipment_alarm_report_day report
        where report.equipment_code = a.code and FIND_IN_SET(a.system_id,report.system_ids)
        <if test="indexKeySuffix.size() > 0">
            and
            <foreach collection="indexKeySuffix" item="keySuffix" open="(" close=")" separator="or">
                report.index_type  LIKE  CONCAT('%',#{keySuffix})
            </foreach>
        </if>
        and report.report_date >= #{beginDate}
        and report.report_date <![CDATA[<=]]> #{endDate}
        )  as alarmInfoNum,
        (SELECT IFNULL(sum(report.index_true_num),0)
        FROM wl_equipment_alarm_report_day report
        where report.equipment_code = a.code and FIND_IN_SET(a.system_id,report.system_ids)
        and report.report_date >= #{beginDate}
        and report.report_date <![CDATA[<=]]> #{endDate}
        )  as totalInfoNum
        FROM(
        SELECT
        s.id as system_id,
        equipment.code,
        equipment.name,
        count(1) AS num
        FROM
        wl_equipment_specific spec
        LEFT JOIN wl_equipment_detail detail ON spec.equipment_detail_id = detail.id
        LEFT JOIN wl_equipment equipment ON equipment.id =detail.equipment_id
        left join f_fire_fighting_system s on FIND_IN_SET(s.id,spec.system_id)
        where spec.system_id <![CDATA[<>]]>  '' and s.id = #{systemId}
        GROUP BY equipment.code,s.id, equipment.name ORDER BY system_id, code
        ) a) as  a1
    </insert>
</mapper>
