package org.springblade.third.iotdevice.service.impl;

import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.community.feign.IResidentialClient;
import org.springblade.device.entity.IotAlarm;
import org.springblade.device.entity.IotDevice;
import org.springblade.device.feign.*;
import org.springblade.sms.service.IPepNoticeService;
import org.springblade.system.expand.feign.IOrgPositionClient;
import org.springblade.system.user.feign.IUserClient;
import org.springblade.third.iotdevice.service.IDataReportStrategy;
import org.springblade.third.iotdevice.util.SendMessageBuildUtil;
import org.springblade.third.sms.dto.PepSmsSendDto;
import org.springblade.third.sms.dto.PepVoiceSendDto;
import org.springblade.workorder.deviceorder.feign.IDeviceOrderClient;
import org.springframework.data.redis.core.RedisTemplate;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author lidexi
 * @date 2021/5/27 17:59
 * AEP平台数据上报-有profile文件的处理策略
 */
@Slf4j
@NoArgsConstructor
@AllArgsConstructor
public class ProfileDataReportStrategy implements IDataReportStrategy {

	private IIotDeviceClient iotDeviceClient;
	private IIotDeviceHeartbeatClient iotDeviceHeartbeatClient;
	private IIotAlarmClient iotAlarmClient;
	private IDeviceOrderClient deviceOrderClient;
	private OrderGenerateUtilService orderGenerateUtilService;
	private IResidentialClient residentialClient;
	private IIotAlarmGroupClient iotAlarmGroupClient;
	private IIotAlarmGroupUserClient iotAlarmGroupUserClient;
	private IPepNoticeService pepNoticeService;
	private IUserClient userClient;
	private RedisTemplate<String,String> redisTemplate;
	private IOrgPositionClient orgPositionClient;


	@Override
	public void dataReport(Map<String, Object> map) {
		log.error("dataReport --> " + JSONUtil.toJsonStr(map));
		String topic = map.get("topic").toString();
		if("v1/up/ad19prof".equals(topic)){
			String alarmContent = "燃气报警";
//			Map payload = (Map)map.get("payload");

			ArrayList payload1 = (ArrayList)map.get("payload");
			Map payload = (Map)payload1.get(0);

			Map serviceData = (Map)payload.get("serviceData");
			String imei = map.get("IMEI").toString();
			IotDevice searchDevice = new IotDevice();
			searchDevice.setImei(imei);
			IotDevice smartDevice = iotDeviceClient.detail(searchDevice);
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//			sf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
			String timestamp = map.get("timestamp").toString();
			Date date = new Date(Long.valueOf(timestamp));

			IotAlarm alarm = new IotAlarm();
			alarm.setDeviceId(smartDevice.getDeviceId());
			alarm.setIsAlarm(1);
			alarm.setDeviceName(smartDevice.getDeviceName());
			alarm.setRecordTime(sf.format(date));
			alarm.setDeviceType(smartDevice.getDeviceType());
			alarm.setAlarmType(1);
			alarm.setTenantId(smartDevice.getTenantId());
			alarm.setOrgPosition(smartDevice.getOrgPosition());
			alarm.setAlarmDesc(alarmContent);
			alarm.setName(alarmContent);
			iotAlarmClient.save(alarm);


			//构建短信发送对象
			List<PepSmsSendDto> pepSmsSendDtos = SendMessageBuildUtil.buildIotSmsSendDto(alarm,iotDeviceClient
				,residentialClient,iotAlarmGroupClient,iotAlarmGroupUserClient);
			pepSmsSendDtos.stream().forEach(pepSmsSendDto -> this.pepNoticeService.sendSms(pepSmsSendDto));

			//构建语音发送对象
			List<PepVoiceSendDto> pepVoiceSendDtos = SendMessageBuildUtil.buildIotVoiceSendDto(alarm,iotDeviceClient
				,residentialClient,iotAlarmGroupClient,iotAlarmGroupUserClient);
			pepVoiceSendDtos.stream().forEach(pepVoiceSendDto -> this.pepNoticeService.sendVoice(pepVoiceSendDto));
		}
	}
}
