<?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.amos.boot.module.tzs.api.mapper.AlertCalledMapper">

	<select id="queryAlertStatusCount" resultType="java.util.Map">
		SELECT
		count( 1 ) calledCount,
		sum( CASE WHEN father_alert is null THEN
		1 ELSE 0 END ) majorAlertCount,
		sum( CASE WHEN alarm_type_code = '960'
		THEN 1 ELSE 0 END )
		sleepyIncidentCount,
		sum( CASE WHEN alarm_type_code
		= '961' THEN 1 ELSE 0 END )
		faultRescueCount,
		sum( CASE WHEN
		alarm_type_code = '962' THEN 1 ELSE 0 END )
		suggestionsCount
		FROM
		tz_alert_called
		WHERE 1 = 1
		<if test="beginDate != null and beginDate != ''">
			and call_time >= #{beginDate}
		</if>
		<if test="endDate != null and endDate != ''">
			and call_time <![CDATA[ <= ]]>
			#{endDate}
		</if>
		<if
			test="orgCode != null and orgCode != ''  and  recUserId != null and   recUserId != ''">
			and org_code= #{orgCode}
			or rec_user_id = #{recUserId}
		</if>
	</select>

	<select id="queryNearlyCount" resultType="java.util.Map">
		SELECT
		DATE_ADD(#{beginDate},INTERVAL -7 DAY) dateTime,
		sum( CASE WHEN
		father_alert is null THEN 1 ELSE 0 END ) majorAlertCount
		FROM
		tz_alert_called
		WHERE 1 = 1
		<if test="beginDate != null and beginDate != ''">
			and call_time >= DATE_ADD(#{beginDate},INTERVAL -7 DAY)
		</if>
		<if test="endDate != null and endDate != ''">
			and call_time <![CDATA[ <= ]]>
			DATE_ADD(#{endDate},INTERVAL -7 DAY)
		</if>
		<if
			test="orgCode != null and orgCode != ''  and  recUserId != null and   recUserId != ''">
			and org_code= #{orgCode}
			or rec_user_id = #{recUserId}
		</if>
		UNION ALL
		SELECT
		DATE_ADD(#{beginDate},INTERVAL -6 DAY) dateTime,
		sum(
		CASE WHEN father_alert is null THEN 1 ELSE 0 END ) majorAlertCount
		FROM
		tz_alert_called
		WHERE 1 = 1
		<if test="beginDate != null and beginDate != ''">
			and call_time >= DATE_ADD(#{beginDate},INTERVAL -6 DAY)
		</if>
		<if test="endDate != null and endDate != ''">
			and call_time <![CDATA[ <= ]]>
			DATE_ADD(#{endDate},INTERVAL -6 DAY)
		</if>
		<if
			test="orgCode != null and orgCode != ''  and  recUserId != null and   recUserId != ''">
			and org_code= #{orgCode}
			or rec_user_id = #{recUserId}
		</if>
		UNION ALL
		SELECT
		DATE_ADD(#{beginDate},INTERVAL -5 DAY) dateTime,
		sum(
		CASE WHEN father_alert is null THEN 1 ELSE 0 END ) majorAlertCount
		FROM
		tz_alert_called
		WHERE 1 = 1
		<if test="beginDate != null and beginDate != ''">
			and call_time >= DATE_ADD(#{beginDate},INTERVAL -5 DAY)
		</if>
		<if test="endDate != null and endDate != ''">
			and call_time <![CDATA[ <= ]]>
			DATE_ADD(#{endDate},INTERVAL -5 DAY)
		</if>
		<if
			test="orgCode != null and orgCode != ''  and  recUserId != null and   recUserId != ''">
			and org_code= #{orgCode}
			or rec_user_id = #{recUserId}
		</if>
		UNION ALL
		SELECT
		DATE_ADD(#{beginDate},INTERVAL -4 DAY) dateTime,
		sum(
		CASE WHEN father_alert is null THEN 1 ELSE 0 END ) majorAlertCount
		FROM
		tz_alert_called
		WHERE 1 = 1
		<if test="beginDate != null and beginDate != ''">
			and call_time >= DATE_ADD(#{beginDate},INTERVAL -4 DAY)
		</if>
		<if test="endDate != null and endDate != ''">
			and call_time <![CDATA[ <= ]]>
			DATE_ADD(#{endDate},INTERVAL -4 DAY)
		</if>
		<if
			test="orgCode != null and orgCode != ''  and  recUserId != null and   recUserId != ''">
			and org_code= #{orgCode}
			or rec_user_id = #{recUserId}
		</if>
		UNION ALL
		SELECT
		DATE_ADD(#{beginDate},INTERVAL -3 DAY) dateTime,
		sum(
		CASE WHEN father_alert is null THEN 1 ELSE 0 END ) majorAlertCount
		FROM
		tz_alert_called
		WHERE 1 = 1
		<if test="beginDate != null and beginDate != ''">
			and call_time >= DATE_ADD(#{beginDate},INTERVAL -3 DAY)
		</if>
		<if test="endDate != null and endDate != ''">
			and call_time <![CDATA[ <= ]]>
			DATE_ADD(#{endDate},INTERVAL -3 DAY)
		</if>
		<if
			test="orgCode != null and orgCode != ''  and  recUserId != null and   recUserId != ''">
			and org_code= #{orgCode}
			or rec_user_id = #{recUserId}
		</if>
		UNION ALL
		SELECT
		DATE_ADD(#{beginDate},INTERVAL -2 DAY) dateTime,
		sum(
		CASE WHEN father_alert is null THEN 1 ELSE 0 END ) majorAlertCount
		FROM
		tz_alert_called
		WHERE 1 = 1
		<if test="beginDate != null and beginDate != ''">
			and call_time >= DATE_ADD(#{beginDate},INTERVAL -2 DAY)
		</if>
		<if test="endDate != null and endDate != ''">
			and call_time <![CDATA[ <= ]]>
			DATE_ADD(#{endDate},INTERVAL -2 DAY)
		</if>
		<if
			test="orgCode != null and orgCode != ''  and  recUserId != null and   recUserId != ''">
			and org_code= #{orgCode}
			or rec_user_id = #{recUserId}
		</if>
		UNION ALL
		SELECT
		DATE_ADD(#{beginDate},INTERVAL -1 DAY) dateTime,
		sum(
		CASE WHEN father_alert is null THEN 1 ELSE 0 END ) majorAlertCount
		FROM
		tz_alert_called
		WHERE 1 = 1
		<if test="beginDate != null and beginDate != ''">
			and call_time >= DATE_ADD(#{beginDate},INTERVAL -1 DAY)
		</if>
		<if test="endDate != null and endDate != ''">
			and call_time <![CDATA[ <= ]]>
			DATE_ADD(#{endDate},INTERVAL -1 DAY)
		</if>
		<if
			test="orgCode != null and orgCode != ''  and  recUserId != null and   recUserId != ''">
			and org_code= #{orgCode}
			or rec_user_id = #{recUserId}
		</if>
	</select>

	<select id="queryAlertListByQueryDto" resultType="java.util.Map">
		SELECT
		a.sequence_nbr AS sequenceNbr,
		a.work_order_number AS workOrderNumber,
		a.rec_user_name AS
		creator,
		a.emergency_person AS emergency,
		a.call_time AS emergencyTime,
		a.emergency_call AS emergencyCall,
		a.device_id AS deviceId,
		e.address AS
		elevatorAddress,
		a.address AS address,
		a.alarm_type AS alertType,
		a.alert_source AS alertSource,
		a.alert_stage AS alertStage,
		CASE
		a.alert_status
		WHEN 0 THEN
		'未完成'
		WHEN 1 THEN
		'完成'
		ELSE
		''
		END AS alertStatus
		FROM
		tz_alert_called a
		LEFT JOIN tcb_elevator e ON e.rescue_code =
		a.device_id
		WHERE 1=1
		<if test="workOrderNumber != null and workOrderNumber != ''">
			AND a.work_order_number like
			CONCAT(CONCAT('%',#{workOrderNumber}),'%')
		</if>
		<if test="creator != null and creator != ''">
			AND a.rec_user_name like
			CONCAT(CONCAT('%',#{creator}),'%')
		</if>
		<if test="emergency != null and emergency != ''">
			AND a.emergency_person like
			CONCAT(CONCAT('%',#{emergency}),'%')
		</if>
		<if test="emergencyTimeStart != null ">
			and #{emergencyTimeStart} <![CDATA[ <= ]]>
			a.call_time
		</if>
		<if test="emergencyTimeEnd != null ">
			and a.call_time <![CDATA[ <= ]]>
			#{emergencyTimeEnd}
		</if>
		<if test="workOrderNumber != null and workOrderNumber != ''">
			and a.work_order_number like
			CONCAT(CONCAT('%',#{workOrderNumber}),'%')
		</if>
		<if test="emergencyCall != null and emergencyCall != ''">
			AND a.emergency_call like
			CONCAT(CONCAT('%',#{emergencyCall}),'%')
		</if>
		<if test="deviceId != null and deviceId != ''">
			AND a.device_id like CONCAT(CONCAT('%',#{deviceId}),'%')
		</if>
		<if test="elevatorAddress != null and elevatorAddress != ''">
			AND e.address like
			CONCAT(CONCAT('%',#{elevatorAddress}),'%')
		</if>
		<if test="address != null and address != ''">
			AND a.region_code like CONCAT(CONCAT('%',#{address}),'%')
		</if>
		<if test="alertType != null and alertType != ''">
			AND a.alarm_type_code = #{alertType}
		</if>
		<if test="alertSource != null and alertSource != ''">
			AND a.alert_source_code = #{alertSource}
		</if>
		<if test="alertStage != null and alertStage != ''">
			AND a.alert_stage_code = #{alertStage}
		</if>
		<if test="alertStatus != null and alertStatus != ''">
			AND a.alert_status = #{alertStatus}
		</if>

	</select>

	<select id="getTodayEmergencyCount" resultType="java.util.Map">
		SELECT
		sum(case when tz.alarm_type_code = '960' then 1 else 0 end) as sleepyTncidentTotal,
		sum(
		CASE
		WHEN
		tz.alarm_type_code = '960'
		AND alert_stage_code IN ('860','861', '862')
		THEN
		1
		ELSE
		0
		END
		) as rescueTotal ,
		sum(
		CASE
		WHEN tz.alarm_type_code = '960'
		AND
		tz.alert_stage_code in
		('864','865','866') THEN
		1
		ELSE
		0
		END
		) as
		rescueComplete
		FROM
		tz_alert_called tz left join tcb_elevator el on tz.equipment_id = el.sequence_nbr   where  tz.is_delete=0
		and tz.call_time >= (select current_date())
		and (select date_add(current_date(), interval 1 day))  >= tz.call_time
        <if test="regionCode != null and regionCode != ''">
            AND el.region_code  like  CONCAT(CONCAT('%',#{regionCode}),'%')
        </if>
	</select>
	<select id="getImportantEventCount" resultType="java.util.Map">
		select count(1) as importantEventCount from (
		SELECT
		ac.*
		FROM
		tz_alert_called ac
		INNER JOIN (
		SELECT
		alert_called_id
		FROM
		tz_alert_form_value
		WHERE
		tz_alert_form_value.field_value IS NOT NULL
		AND tz_alert_form_value.field_value != ''
		AND tz_alert_form_value.field_value != '0'
		AND field_code IN ('injured_num', 'die_num')
		and tz_alert_form_value.is_delete=0
		group by alert_called_id
		) afv ON ac.sequence_nbr = afv.alert_called_id
		WHERE
		ac.alarm_type_code = '960' and ac.is_delete=0
		) tzs left join  tcb_elevator te on tzs.equipment_id = te.sequence_nbr
		where 1=1
		and tzs.call_time >= (select current_date())
		and (select date_add(current_date(), interval 1 day))  >= tzs.call_time
		<if test="regionCode != null and regionCode != ''">
			AND te.region_code  like  CONCAT(CONCAT('%',#{regionCode}),'%')
		</if>
	</select>
	<select id="getImportantEventDetail" resultType="java.util.Map">
		select tzs.contact_phone,tzs.rec_user_name,
		tzs.alert_stage   as type,tzs.emergency_person,tzs.alarm_type,tzs.alert_source,tzs.address,
		tzs.device_id,tzs.contact_user,tzs.alarm_type_code,tzs.sequence_nbr,date_format(tzs.call_time,"%Y-%m-%d %H:%i:%S") call_time,
		tzs.rec_user_id,tzs.registration_code,tzs.emergency_call,tzs.type_code,tzs.equipment_id
		,te.latitude ,te.region_code ,te.longitude,tzs.injured from (
		SELECT
		ac.*,'有伤亡' as injured
		FROM
		tz_alert_called ac
		INNER JOIN (
		SELECT
		alert_called_id
		FROM
		tz_alert_form_value
		WHERE
		tz_alert_form_value.field_value IS NOT NULL
		AND tz_alert_form_value.field_value != ''
		AND tz_alert_form_value.field_value != '0'
		AND field_code IN ('injured_num', 'die_num')
		and tz_alert_form_value.is_delete=0
		group by alert_called_id
		) afv ON ac.sequence_nbr = afv.alert_called_id
		WHERE
		ac.alarm_type_code = '960' and ac.is_delete=0
		) tzs left join  tcb_elevator te on tzs.equipment_id = te.sequence_nbr
		where 1=1
		and tzs.call_time >= (select current_date())
		and (select date_add(current_date(), interval 1 day))  >= tzs.call_time
		<if test="regionCode != null and regionCode != ''">
			AND te.region_code  like  CONCAT(CONCAT('%',#{regionCode}),'%')
		</if>
	</select>

	<select id="getTodayEmergencyDetail" resultType="java.util.Map">
		select tzs.contact_phone,tzs.rec_user_name,
		tzs.alert_stage   as type,tzs.emergency_person,tzs.alarm_type,tzs.alert_source,tzs.address,
		tzs.device_id,tzs.contact_user,tzs.alarm_type_code,tzs.sequence_nbr,date_format(tzs.call_time,"%Y-%m-%d %H:%i:%S") call_time,
		tzs.rec_user_id,tzs.registration_code,tzs.emergency_call,tzs.type_code,tzs.equipment_id
		,te.latitude ,te.region_code ,te.longitude,tzs.injured from (
		SELECT
		ac.*,'有伤亡' as injured
		FROM
		tz_alert_called ac
		INNER JOIN (
		SELECT
		alert_called_id
		FROM
		tz_alert_form_value
		WHERE
		tz_alert_form_value.field_value IS NOT NULL
		AND tz_alert_form_value.field_value != ''
		AND field_code IN ('injured_num', 'die_num')
		and tz_alert_form_value.is_delete=0
		group by alert_called_id
		) afv ON ac.sequence_nbr = afv.alert_called_id
		WHERE
		ac.alarm_type_code = '960' and ac.is_delete=0
		) tzs left join  tcb_elevator te on tzs.equipment_id = te.sequence_nbr
		where 1=1
		and tzs.call_time >= (select current_date())
		and (select date_add(current_date(), interval 1 day))  >= tzs.call_time
		<if test="regionCode != null and regionCode != ''">
			AND te.region_code  like  CONCAT(CONCAT('%',#{regionCode}),'%')
		</if>
		<if test="rescueTotal != null and rescueTotal != ''">
			AND tzs.alert_stage_code  in ('860','861','862')
		</if>
		<if test="rescueComplete != null and rescueComplete != ''">
			AND tzs.alert_stage_code  in ('864','865','866')
		</if>

		union all

		select tzs.contact_phone,tzs.rec_user_name,
		tzs.alert_stage   as type,tzs.emergency_person,tzs.alarm_type,tzs.alert_source,tzs.address,
		tzs.device_id,tzs.contact_user,tzs.alarm_type_code,tzs.sequence_nbr,date_format(tzs.call_time,"%Y-%m-%d %H:%i:%S")  call_time,
		tzs.rec_user_id,tzs.registration_code,tzs.emergency_call,tzs.type_code,tzs.equipment_id
		,te.latitude ,te.region_code ,te.longitude,tzs.injured from (
		select  *,'无伤亡' as injured from tz_alert_called where sequence_nbr not in (
		SELECT
		alert_called_id
		FROM
		tz_alert_form_value
		WHERE
		tz_alert_form_value.field_value IS NOT NULL
		AND tz_alert_form_value.field_value != ''
		AND field_code IN ('injured_num', 'die_num')
		and tz_alert_form_value.is_delete=0
		group by alert_called_id
		) and alarm_type_code = '960' and is_delete=0
		) tzs left join tcb_elevator te  on tzs.equipment_id = te.sequence_nbr
		where 1=1

		and tzs.call_time >= (select current_date())
		and (select date_add(current_date(), interval 1 day))  >= tzs.call_time
		<if test="regionCode != null and regionCode != ''">
			AND te.region_code  like  CONCAT(CONCAT('%',#{regionCode}),'%')
		</if>
		<if test="rescueTotal != null and rescueTotal != ''">
			AND tzs.alert_stage_code  in ('860','861','862')
		</if>
		<if test="rescueComplete != null and rescueComplete != ''">
			AND tzs.alert_stage_code  in ('864','865','866')
		</if>

	</select>


	<select id="getBussinessInfoById" resultType="com.yeejoin.amos.boot.module.tzs.api.dto.WechatMyBussinessDto">
			select
			a.sequence_nbr,
			p.sequence_nbr as paperId,
			a.alert_stage as taskStatus,
			a.alarm_type as taskType,
			a.alarm_type_code as taskTypeCode,
			e.address,
			e.rescue_code as rescueCode,
			p.dispatch_time as dispatchTime,
			p.save_time as saveTime,
			e.use_unit as useUnit,
			e.use_unit_id as useUnitId,
			e.use_unit_authority as useUnitAuthority,
			a.emergency_call as emergencyCall,
			a.rec_date as recDate,
			p.arrive_time as arriveTime,
			p.save_org_name as saveOrg,
			p.feedback_finish_time as fixFinishTime,
			p.repair_org_name as responseOrg,
			p.repair_user as responseUser,
			p.remark as feedbackRemark,
			p.feedback_time as responseTime,
			p.feedback_uname as feedbackUname,
			p.feedback_finish_time as dealTime,
			p.deal_org_name as dealOrg,
			p.deal_user as dealUser
			from tz_alert_called a
			LEFT JOIN tcb_elevator e on e.sequence_nbr = a.equipment_id
			LEFT JOIN tz_dispatch_paper p on p.alert_id = a.sequence_nbr
			where a.sequence_nbr = #{alertId}
    </select>

	<select id="getBussinessListByPhonePager" resultType="com.yeejoin.amos.boot.module.tzs.api.dto.WechatMyBusinessListDto">
			select
			a.sequence_nbr,
			a.alarm_type as taskType,
			a.alert_status as taskStatus,
			e.address,
			a.call_time as dispatchTime,
			a.alert_stage as bussinessStatus,
			a.call_time as reportTime,
			p.feedback_finish_time as finishTime
			from tz_alert_called a
			LEFT JOIN tcb_elevator e on e.sequence_nbr = a.equipment_id
			LEFT JOIN tz_dispatch_paper p on p.alert_id = a.sequence_nbr
			where a.emergency_call =#{phone}
			and a.is_delete = 0
			and a.father_alert is null
			and a.alarm_type_code = #{bussinessCode}
			ORDER BY a.rec_date desc
			limit #{current},5
    </select>


	<select id="queryAlertRecordListByQueryDto" resultType="com.yeejoin.amos.boot.module.tzs.api.dto.AlertCalledRecordDto">
		SELECT
		a.call_time AS callTime,
		a.type ,
		a.alert_source AS alertSource,
		a.alarm_type AS alarmType,
		a.address
		FROM
		tz_alert_called a
		LEFT JOIN tcb_elevator e ON e.sequence_nbr = a.equipment_id
		WHERE 1=1
		<if test="callTimeStart != null ">
			and #{callTimeStart} <![CDATA[ <= ]]>
			a.call_time
		</if>
		<if test="callTimeEnd != null ">
			and a.call_time <![CDATA[ <= ]]>
			#{callTimeEnd}
		</if>
		<if test="type != null and type != ''">
			and a.type = #{type}
		</if>
		<if test="alertSource != null and alertSource != ''">
			AND a.alert_source =  #{alertSource}
		</if>
		<if test="alarmType != null and alarmType != ''">
			AND a.alarm_type =  #{alarmType}
		</if>


	</select>



</mapper>


