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

    <select id="allPage" resultType="com.yeejoin.equipmanage.common.dto.AnalysisReportLogDto">
		SELECT
			*,
	        cb_org_usr.biz_org_code AS stationCode,
	     '消防巡查报表'   AS reportName,
		 cb_org_usr.biz_org_name AS stationName,
		 (CASE wl_analysis_report_log.report_type
        WHEN 0 THEN '日报表'
        WHEN 1 THEN '周报表'
        ELSE '月报表'
        END) reportTypeName
		FROM
			wl_analysis_report_log,cb_org_usr
		<where>
			 cb_org_usr.biz_org_type = 'COMPANY' AND cb_org_usr.is_delete = 0  AND LENGTH(cb_org_usr.biz_org_code)>12
			 <if test="analysisReportLog.reportType != null">
			 AND wl_analysis_report_log.report_type = #{analysisReportLog.reportType}
			 </if>
			 <if test="analysisReportLog.startDate != null">
			 AND DATE_FORMAT(wl_analysis_report_log.start_date, '%Y-%m-%d') &gt;= DATE_FORMAT(#{analysisReportLog.startDate}, '%Y-%m-%d')   
			 </if>
			 <if test="analysisReportLog.endDate != null">
			 AND DATE_FORMAT(wl_analysis_report_log.end_date, '%Y-%m-%d') &lt;= DATE_FORMAT(#{analysisReportLog.endDate}, '%Y-%m-%d')   
			 </if>
			 <if test="analysisReportLog.stationCode != null">
			 AND cb_org_usr.biz_org_code like concat(#{analysisReportLog.stationCode},'%')
			 </if>
		</where>
		ORDER BY
		create_date DESC
    </select>
    
     <select id="getStation" resultType="Map">
         SELECT * FROM cb_org_usr WHERE biz_org_code = #{stationCode} AND biz_org_type = 'COMPANY'
     </select>
     
     
     <select id="getReport" resultType="Map">
         SELECT
			IFNULL(SUM( task_count ),0) AS task_count,
			IFNULL(SUM( complete_task_count ),0) AS complete_task_count,
			IFNULL(SUM( uncomplete_task_count ),0) AS uncomplete_task_count,
			IFNULL(SUM( problem_count ),0) AS problem_count,
			IFNULL(SUM( fixed_problem_count ),0) AS fixed_problem_count,
			IFNULL(SUM( unfixed_problem_count ),0) AS unfixed_problem_count,
			IFNULL(SUM( checked_equipment_count ),0) AS checked_equipment_count,
			IFNULL(SUM( equipment_count ),0) AS equipment_count,
			IFNULL(SUM( unchecked_equipment_count ),0) AS unchecked_equipment_count,
			IFNULL(SUM( person_count ),0) AS person_count,
			IFNULL(SUM( checked_point_count ),0) AS checked_point_count,
			IFNULL(SUM( certified_person_count ),0) AS certified_person_count,
			biz_org_name 
		FROM
			idx_width_report   
         <where>
	         <if test="analysisReportLog.startDate != null">
			 AND DATE_FORMAT(idx_width_report.date, '%Y-%m-%d') &gt;= DATE_FORMAT(#{analysisReportLog.startDate}, '%Y-%m-%d')   
			 </if>
			 <if test="analysisReportLog.endDate != null">
			 AND DATE_FORMAT(idx_width_report.date, '%Y-%m-%d') &lt;= DATE_FORMAT(#{analysisReportLog.endDate}, '%Y-%m-%d')   
			 </if>
			 <if test="analysisReportLog.stationCode != null">
			 AND idx_width_report.biz_org_code = #{analysisReportLog.stationCode} 
			 </if>
		 </where>
		 LIMIT 1
     </select>
     
     <select id="getDefect" resultType="Map">
         SELECT
			*
		 FROM
			idx_width_defect   
         <where>
	         <if test="analysisReportLog.startDate != null">
			 AND DATE_FORMAT(idx_width_defect.date, '%Y-%m-%d') &gt;= DATE_FORMAT(#{analysisReportLog.startDate}, '%Y-%m-%d')   
			 </if>
			 <if test="analysisReportLog.endDate != null">
			 AND DATE_FORMAT(idx_width_defect.date, '%Y-%m-%d') &lt;= DATE_FORMAT(#{analysisReportLog.endDate}, '%Y-%m-%d')   
			 </if>
			 <if test="analysisReportLog.stationCode != null">
			 AND idx_width_defect.biz_org_code = #{analysisReportLog.stationCode} 
			 </if>
		 </where>
     </select>

	<select id="getRootOrgUsr" resultType="Map">
		SELECT
			*
		FROM
			cb_org_usr
		<where>
			parent_id IS NULL
		</where>
		LIMIT 1
	</select>
	<select id="iotReportPage" resultType="com.yeejoin.equipmanage.common.dto.AnalysisReportLogDto">
		SELECT
		*,
		cb_org_usr.biz_org_code AS stationCode,
		'消防物联报表'   AS reportName,
		cb_org_usr.biz_org_name AS stationName,
		(CASE wl_analysis_report_log.report_type
		WHEN 0 THEN '日报表'
		WHEN 1 THEN '周报表'
		ELSE '月报表'
		END) reportTypeName
		FROM
		wl_analysis_report_log,cb_org_usr
		<where>
			cb_org_usr.biz_org_type = 'COMPANY' AND cb_org_usr.is_delete = 0  AND LENGTH(cb_org_usr.biz_org_code)>12
			<if test="analysisReportLog.reportType != null">
				AND wl_analysis_report_log.report_type = #{analysisReportLog.reportType}
			</if>
			<if test="analysisReportLog.startDate != null">
				AND DATE_FORMAT(wl_analysis_report_log.start_date, '%Y-%m-%d') &gt;= DATE_FORMAT(#{analysisReportLog.startDate}, '%Y-%m-%d')
			</if>
			<if test="analysisReportLog.endDate != null">
				AND DATE_FORMAT(wl_analysis_report_log.end_date, '%Y-%m-%d') &lt;= DATE_FORMAT(#{analysisReportLog.endDate}, '%Y-%m-%d')
			</if>
			<if test="analysisReportLog.stationCode != null">
				AND cb_org_usr.biz_org_code like concat(#{analysisReportLog.stationCode},'%')
			</if>
		</where>
		ORDER BY
		create_date DESC
	</select>
</mapper>
