<?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.AnalysisReportSummaryMapper">

    <insert id="insertSystemMonthSummaryData">
        insert into
        wl_analysis_report_summary (
        system_id,
        summary_info,
        system_name,
        report_type,
        report_date
        )
        select
        s.id,
        CONCAT(s.name,',',#{beginDate},' - ',#{endDate} ,':',CHAR(10),
        CONCAT('正常设备',IFNULL(f1.totalNormalEquipNum,0),'台,','正常率:',concat(f1.normalEquipNumPercent,'%'),'，',
        '同比上月',IF(IFNULL(f1.totalNormalEquipNum,0)- IFNULL(f1.lastMonthTotalNormalEquipNum,0) >0 ,'增加','减少'),
        if(IFNULL(f1.lastMonthTotalNormalEquipNum,0) = 0 ,'---',CONCAT(format(ABS(IFNULL(f1.totalNormalEquipNum,0)/f1.lastMonthTotalNormalEquipNum -1)* 100,2),'%')),'，'),
        CONCAT('故障设备',IFNULL(f1.totalFaultEquipNum,0),'台,','故障率:',concat(IFNULL(f1.faultEquipPercent,0),'%'),'，',
        '同比上月',IF(IFNULL(f1.totalFaultEquipNum,0)- IFNULL(f1.lastMonthTotalFaultEquipNum,0) >0 ,'增加','减少'),
        if(IFNULL(f1.lastMonthTotalFaultEquipNum,0) = 0 ,'---',CONCAT(format(ABS(IFNULL(f1.totalFaultEquipNum,0)/f1.lastMonthTotalFaultEquipNum -1)* 100,2),'%')),'，'),
        CONCAT('告警设备',IFNULL(f1.totalAlarmEquipNum,0),'台,',
        '同比上月',IF(IFNULL(f1.totalAlarmEquipNum,0)- IFNULL(f1.lastMonthTotalAlarmEquipNum,0) >0 ,'增加','减少'),
        if(IFNULL(f1.lastMonthTotalAlarmEquipNum,0) = 0 ,'---',CONCAT(format(ABS(IFNULL(f1.totalAlarmEquipNum,0)/f1.lastMonthTotalAlarmEquipNum  -1)* 100,2),'%')),'，'),
        CONCAT('告警总数',IFNULL(f1.totalAlarmInfoNum,0),'条,',
        '同比上月',IF(IFNULL(f1.totalAlarmInfoNum,0)- IFNULL(f1.lastMonthTotalAlarmInfoNum,0) >0 ,'增加','减少'),
        if(IFNULL(f1.lastMonthTotalAlarmInfoNum,0) = 0 ,'---', CONCAT(format(ABS(IFNULL(f1.totalAlarmInfoNum,0)/f1.lastMonthTotalAlarmInfoNum -1)* 100,2),'%'))
        ,'。')) as content,
        s.`name`,
        3 as report_type,
        #{endDate} as report_date
        from
        f_fire_fighting_system s
        LEFT JOIN
        (select
        s1.*,
        IFNULL(s2.totalAlarmEquipNum,0) as lastMonthTotalAlarmEquipNum,
        IFNULL(s2.totalAlarmInfoNum,0) as lastMonthTotalAlarmInfoNum,
        IFNULL(s2.totalNormalEquipNum,0) as lastMonthTotalNormalEquipNum,
        IFNULL(s2.totalEquipNum,0) as lastMonthTotalEquipNum,
        IFNULL(s2.totalFaultEquipNum,0) as lastMonthTotalFaultEquipNum
        from
        (SELECT
        u1.system_id,
        IFNULL(sum(num),0) as totalEquipNum,
        IFNULL(sum(normalEquipNum),0) as totalNormalEquipNum,
        IFNULL(sum(faultEquipNum),0) as totalFaultEquipNum,
        IFNULL(sum(alarmEquipNum),0) as totalAlarmEquipNum,
        IFNULL(sum(alarmInfoNum),0) as totalAlarmInfoNum,
        FORMAT(  (abs( ( sum(normalEquipNum) / IF ( sum(num) = 0, 1, sum(num)) ) ) * 100
        ),2)as normalEquipNumPercent,
        FORMAT(  (abs( (sum(faultEquipNum) / IF ( sum(num) = 0, 1, sum(num)) ) ) * 100
        ),2)  as faultEquipPercent
        from
        (select
        a1.*,
        (a1.num - a1.alarmEquipNum) as normalEquipNum
        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 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)
        <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.index_true_num >0
        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
        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[<>]]> ''
        GROUP BY equipment.code,s.id, equipment.name ORDER BY system_id, code
        ) a) as  a1) u1
        GROUP BY u1.system_id) s1
        LEFT JOIN
        (SELECT
        u1.system_id,
        sum(num) as totalEquipNum,
        sum(normalEquipNum) as totalNormalEquipNum,
        sum(faultEquipNum) as totalFaultEquipNum,
        sum(alarmEquipNum) as totalAlarmEquipNum,
        sum(alarmInfoNum) as totalAlarmInfoNum
        from
        (select
        a1.*,
        (a1.num - a1.alarmEquipNum) as normalEquipNum
        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_type  LIKE  CONCAT('%','Fault')
        and report.index_true_num >0
        and report.report_date >= DATE_SUB(#{beginDate},INTERVAL 1 MONTH)
        and report.report_date <![CDATA[<=]]> DATE_SUB(#{endDate},INTERVAL 1 MONTH)
        )  as faultEquipNum,
        (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)
        <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.index_true_num >0
        and report.report_date >= DATE_SUB(#{beginDate},INTERVAL 1 MONTH)
        and report.report_date <![CDATA[<=]]> DATE_SUB(#{endDate},INTERVAL 1 MONTH)
        )  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 >= DATE_SUB(#{beginDate},INTERVAL 1 MONTH)
        and report.report_date<![CDATA[<=]]> DATE_SUB(#{endDate},INTERVAL 1 MONTH)
        )  as alarmInfoNum
        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[<>]]> ''
        GROUP BY equipment.code,s.id, equipment.name ORDER BY system_id, code
        ) a) as  a1) u1
        GROUP BY u1.system_id) s2 ON s1.system_id = s2.system_id
        ) f1 on  s.id = f1.system_id
        where s.id = #{systemId}
    </insert>
    <insert id="insertSystemMonthSummaryDataTest">
        insert into
        wl_analysis_report_summary (
        system_id,
        summary_info,
        system_name,
        report_type,
        report_date
        )
        select
        s.id,
        CONCAT(s.name,',',#{beginDate},' - ',#{endDate} ,':',CHAR(10),
        CONCAT('正常设备',IFNULL(f1.totalNormalEquipNum,0),'台,','正常率:',concat(f1.normalEquipNumPercent,'%'),'，',
        '同比上月',IF(IFNULL(f1.totalNormalEquipNum,0)- IFNULL(f1.lastMonthTotalNormalEquipNum,0) >0 ,'增加','减少'),
        if(IFNULL(f1.lastMonthTotalNormalEquipNum,0) = 0 ,'---',CONCAT(format(ABS(IFNULL(f1.totalNormalEquipNum,0)/f1.lastMonthTotalNormalEquipNum -1)* 100,2),'%')),'，'),
        CONCAT('故障设备',IFNULL(f1.totalFaultEquipNum,0),'台,','故障率:',concat(IFNULL(f1.faultEquipPercent,0),'%'),'，',
        '同比上月',IF(IFNULL(f1.totalFaultEquipNum,0)- IFNULL(f1.lastMonthTotalFaultEquipNum,0) >0 ,'增加','减少'),
        if(IFNULL(f1.lastMonthTotalFaultEquipNum,0) = 0 ,'---',CONCAT(format(ABS(IFNULL(f1.totalFaultEquipNum,0)/f1.lastMonthTotalFaultEquipNum -1)* 100,2),'%')),'，'),
        CONCAT('告警设备',IFNULL(f1.totalAlarmEquipNum,0),'台,',
        '同比上月',IF(IFNULL(f1.totalAlarmEquipNum,0)- IFNULL(f1.lastMonthTotalAlarmEquipNum,0) >0 ,'增加','减少'),
        if(IFNULL(f1.lastMonthTotalAlarmEquipNum,0) = 0 ,'---',CONCAT(format(ABS(IFNULL(f1.totalAlarmEquipNum,0)/f1.lastMonthTotalAlarmEquipNum  -1)* 100,2),'%')),'，'),
        CONCAT('告警总数',IFNULL(f1.totalAlarmInfoNum,0),'条,',
        '同比上月',IF(IFNULL(f1.totalAlarmInfoNum,0)- IFNULL(f1.lastMonthTotalAlarmInfoNum,0) >0 ,'增加','减少'),
        if(IFNULL(f1.lastMonthTotalAlarmInfoNum,0) = 0 ,'---', CONCAT(format(ABS(IFNULL(f1.totalAlarmInfoNum,0)/f1.lastMonthTotalAlarmInfoNum -1)* 100,2),'%'))
        ,'。')) as content,
        s.`name`,
        3 as report_type,
        #{endDate} as report_date
        from
        f_fire_fighting_system s
        LEFT JOIN
        (select
        s1.*,
        IFNULL(s2.totalAlarmEquipNum,0) as lastMonthTotalAlarmEquipNum,
        IFNULL(s2.totalAlarmInfoNum,0) as lastMonthTotalAlarmInfoNum,
        IFNULL(s2.totalNormalEquipNum,0) as lastMonthTotalNormalEquipNum,
        IFNULL(s2.totalEquipNum,0) as lastMonthTotalEquipNum,
        IFNULL(s2.totalFaultEquipNum,0) as lastMonthTotalFaultEquipNum
        from
        (SELECT
        u1.system_id,
        IFNULL(sum(num),0) as totalEquipNum,
        IFNULL(sum(normalEquipNum),0) as totalNormalEquipNum,
        IFNULL(sum(faultEquipNum),0) as totalFaultEquipNum,
        IFNULL(sum(alarmEquipNum),0) as totalAlarmEquipNum,
        IFNULL(sum(alarmInfoNum),0) as totalAlarmInfoNum,
        FORMAT(  (abs( ( sum(normalEquipNum) / IF ( sum(num) = 0, 1, sum(num)) ) ) * 100
        ),2)as normalEquipNumPercent,
        FORMAT(  (abs( (sum(faultEquipNum) / IF ( sum(num) = 0, 1, sum(num)) ) ) * 100
        ),2)  as faultEquipPercent
        from
        (select
        a1.*,
        (a1.num - a1.alarmEquipNum) as normalEquipNum
        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 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)
        <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.index_true_num >0
        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
        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[<>]]> ''
        GROUP BY equipment.code,s.id, equipment.name ORDER BY system_id, code
        ) a) as  a1) u1
        GROUP BY u1.system_id) s1
        LEFT JOIN
        (SELECT
        u1.system_id,
        sum(num) as totalEquipNum,
        sum(normalEquipNum) as totalNormalEquipNum,
        sum(faultEquipNum) as totalFaultEquipNum,
        sum(alarmEquipNum) as totalAlarmEquipNum,
        sum(alarmInfoNum) as totalAlarmInfoNum
        from
        (select
        a1.*,
        (a1.num - a1.alarmEquipNum) as normalEquipNum
        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_type  LIKE  CONCAT('%','Fault')
        and report.index_true_num >0
        and report.report_date >= DATE_SUB(#{beginDate},INTERVAL 1 MONTH)
        and report.report_date <![CDATA[<=]]> DATE_SUB(#{endDate},INTERVAL 1 MONTH)
        )  as faultEquipNum,
        (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)
        <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.index_true_num >0
        and report.report_date >= DATE_SUB(#{beginDate},INTERVAL 1 MONTH)
        and report.report_date <![CDATA[<=]]> DATE_SUB(#{endDate},INTERVAL 1 MONTH)
        )  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 >= DATE_SUB(#{beginDate},INTERVAL 1 MONTH)
        and report.report_date<![CDATA[<=]]> DATE_SUB(#{endDate},INTERVAL 1 MONTH)
        )  as alarmInfoNum
        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[<>]]> ''
        GROUP BY equipment.code,s.id, equipment.name ORDER BY system_id, code
        ) a) as  a1) u1
        GROUP BY u1.system_id) s2 ON s1.system_id = s2.system_id
        ) f1 on  s.id = f1.system_id
        where s.id = #{systemId}
    </insert>
</mapper>
