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


    <select id="returnDistanceByTaskId" resultType="java.lang.String">
        select
			(
					ACOS(
						SIN(
							(#{lat} * PI()) / 180
						) * SIN(
							(
								el.latitude * PI()
							) / 180
						) + COS(
							(#{lat} * PI()) / 180
						) * COS(
							(
								el.latitude * PI()
							) / 180
						) * COS(
							(#{lon} * PI()) / 180 - (
								el.longitude * PI()
							) / 180
						)
					) * 6371000
				) AS distance
			from tz_dispatch_task ta
			left JOIN tz_alert_called al on al.sequence_nbr = ta.alert_id
			left JOIN tcb_elevator el on el.sequence_nbr = al.equipment_id

			where ta.sequence_nbr = #{taskId}
    </select>


	<select id="getTaskInfoByTaskId" resultType="com.yeejoin.amos.boot.module.tzs.api.dto.WechatMyTaskDto">
			select
			t.sequence_nbr,
			t.alert_id as alertId,
			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,
			t.response_user_name as taskResponseUser,
			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,
			e.longitude as longitude,
			e.latitude as latitude
			from tz_dispatch_task t
			LEFT JOIN tz_alert_called a on a.sequence_nbr = t.alert_id
			LEFT JOIN tcb_elevator e on e.sequence_nbr = a.equipment_id
			LEFT JOIN tz_dispatch_paper p on p.sequence_nbr = t.paper_id
			where t.sequence_nbr = #{taskId}
    </select>

	<select id="getTaskListByPhonePager" resultType="com.yeejoin.amos.boot.module.tzs.api.dto.WechatMyTaskListDto">
			select
			t.sequence_nbr,
			a.alarm_type as taskType,
			a.alert_status as taskStatus,
			e.address,
			p.dispatch_time as dispatch_time,
			a.call_time as reportTime,
			p.feedback_finish_time as finishTime
			from tz_dispatch_task t
			LEFT JOIN tz_alert_called a on a.sequence_nbr = t.alert_id
			LEFT JOIN tcb_elevator e on e.sequence_nbr = a.equipment_id
			LEFT JOIN tz_dispatch_paper p on p.sequence_nbr = t.paper_id
			 where t.response_user_tel =#{phone}
			and a.alarm_type_code = #{typeCode}
			ORDER BY t.rec_date desc
			limit #{current},5
    </select>

	<select id="todayTaskPerson" resultType="com.yeejoin.amos.boot.module.tzs.api.dto.MainPersonDto">
			select
			t.response_user_name as name,
			t.response_user_tel as telephone,
			t.response_org_id as orgId,
			t.response_org_name as orgName,
			t.alert_id as alertId,
			m.address as orgAddress,
			u.address as unitAddress
			from tz_dispatch_task t
			left JOIN cb_maintenance_unit m on m.sequence_nbr = t.response_org_id
			left JOIN tcb_use_unit u on u.sequence_nbr = t.response_org_id
			where to_days(t.dispatch_time) = TO_DAYS(now())
    </select>


</mapper>
