package org.springblade.third.iotdevice.util;

import org.springblade.community.entity.Residential;
import org.springblade.community.feign.IResidentialClient;
import org.springblade.device.entity.IotAlarm;
import org.springblade.device.entity.IotAlarmGroup;
import org.springblade.device.entity.IotAlarmGroupUser;
import org.springblade.device.entity.IotDevice;
import org.springblade.device.feign.IIotAlarmGroupClient;
import org.springblade.device.feign.IIotAlarmGroupUserClient;
import org.springblade.device.feign.IIotDeviceClient;
import org.springblade.device.vo.IotAlarmGroupUserVO;
import org.springblade.device.vo.IotAlarmGroupVO;
import org.springblade.third.sms.dto.IotSmsTemplateParam;
import org.springblade.third.sms.dto.PepSmsSendDto;
import org.springblade.third.sms.dto.PepVoiceSendDto;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lidexi
 * @date 2021/5/28 10:14
 * 构建信息语音参数对象
 */

public class SendMessageBuildUtil {

	/**
	 * 构建短信发送对象
	 * @param alarm
	 * @return
	 */
	public static List<PepSmsSendDto> buildIotSmsSendDto(IotAlarm alarm, IIotDeviceClient iotDeviceClient, IResidentialClient residentialClient
			, IIotAlarmGroupClient iotAlarmGroupClient, IIotAlarmGroupUserClient iotAlarmGroupUserClient){

		List<PepSmsSendDto> iotSmsSendDtos = new ArrayList<>();

		IotDevice searchDevice = new IotDevice();
		searchDevice.setDeviceId(alarm.getDeviceId());
		IotDevice smartDevice = iotDeviceClient.detail(searchDevice);

		Residential residential = residentialClient.getById(smartDevice.getResidentialId());
		if (smartDevice != null) {
			//通过设备的类型和所属小区id搜索告警组
			IotAlarmGroup searchGroup = new IotAlarmGroup();
			searchGroup.setResidentialId(residential.getId());
			searchGroup.setTag(smartDevice.getDeviceType());
			List<IotAlarmGroupVO> alarmGroups = iotAlarmGroupClient.list(searchGroup);
			if (alarmGroups.size() > 0) {
				//获取告警组中的所有人
				IotAlarmGroupUser searchUser = new IotAlarmGroupUser();
				searchUser.setGroupId(alarmGroups.get(0).getId());
				List<IotAlarmGroupUserVO> alarmGroupUsers = iotAlarmGroupUserClient.list(searchUser);
				for (int i = 0; i < alarmGroupUsers.size(); i++) {
					PepSmsSendDto iotSmsSendDto = new PepSmsSendDto();
					IotSmsTemplateParam iotSmsTemplateParam = new IotSmsTemplateParam();
					iotSmsTemplateParam.setParam1(alarmGroupUsers.get(i).getPerson());//接收人称呼
					String recordTime = alarm.getRecordTime();
					DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
					StringBuffer sb = new StringBuffer();
					try {
						LocalDateTime parse = LocalDateTime.parse(recordTime,df);
						int year = parse.getYear();
						sb.append(year+"年");
						Month month = parse.getMonth();
						sb.append(month.getValue()+"月");
						int day = parse.getDayOfMonth();
						sb.append(day+"日");
						int hour = parse.getHour();
						sb.append(hour+"时");
						int minutes = parse.getMinute();
						sb.append(minutes+"分");
						int seconds = parse.getSecond();
						sb.append(seconds+"秒");
					} catch (Exception e) {
						e.printStackTrace();
					}

					iotSmsTemplateParam.setParam2(sb.toString());//告警时间
					if (residential != null) {
						iotSmsTemplateParam.setParam3(residential.getName());//小区名称
					} else {
						iotSmsTemplateParam.setParam3("未知小区");//小区名称
					}

					iotSmsTemplateParam.setParam4(smartDevice.getDeviceName());//设备名称
					iotSmsTemplateParam.setParam5(alarm.getAlarmDesc());


					iotSmsSendDto.setTemplate_param(iotSmsTemplateParam);
					iotSmsSendDto.setMobile(alarmGroupUsers.get(i).getPhone());
					iotSmsSendDto.setTemplate_id("91556324");
					iotSmsSendDto.setPrefix("ctwing/smoke3");
					Long timestamp = null;
					try {
						timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(alarm.getRecordTime()).getTime();
					} catch (ParseException e) {
						e.printStackTrace();
					}
					iotSmsSendDto.setTimestamp(timestamp.toString());
					iotSmsSendDtos.add(iotSmsSendDto);
				}
			}


		}
		return iotSmsSendDtos;
	}

	/**
	 * 构建语音发送对象
	 *
	 * @param alarm
	 * @return
	 */
	public static List<PepVoiceSendDto> buildIotVoiceSendDto(IotAlarm alarm,IIotDeviceClient iotDeviceClient, IResidentialClient residentialClient
		, IIotAlarmGroupClient iotAlarmGroupClient, IIotAlarmGroupUserClient iotAlarmGroupUserClient) {
		List<PepVoiceSendDto> iotSmsSendDtos = new ArrayList<>();

		IotDevice searchDevice = new IotDevice();
		searchDevice.setDeviceId(alarm.getDeviceId());
		IotDevice smartDevice = iotDeviceClient.detail(searchDevice);

		Residential residential = residentialClient.getById(smartDevice.getResidentialId());
		if (smartDevice != null) {
			//通过设备的类型和所属小区id搜索告警组
			IotAlarmGroup searchGroup = new IotAlarmGroup();
			searchGroup.setResidentialId(residential.getId());
			searchGroup.setTag(smartDevice.getDeviceType());
			List<IotAlarmGroupVO> alarmGroups = iotAlarmGroupClient.list(searchGroup);
			if (alarmGroups.size() > 0) {
				//获取告警组中的所有人
				IotAlarmGroupUser searchUser = new IotAlarmGroupUser();
				searchUser.setGroupId(alarmGroups.get(0).getId());
				List<IotAlarmGroupUserVO> alarmGroupUsers = iotAlarmGroupUserClient.list(searchUser);
				for (int i = 0; i < alarmGroupUsers.size(); i++) {
					PepVoiceSendDto iotSmsSendDto = new PepVoiceSendDto();
					String recordTime = alarm.getRecordTime();
					DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
					StringBuffer sb = new StringBuffer();
					try {
						LocalDateTime parse = LocalDateTime.parse(recordTime,df);
						int year = parse.getYear();
						sb.append(year+"年");
						Month month = parse.getMonth();
						if(month.getValue()>=10){
							sb.append(month.getValue()+"月");
						}else{
							sb.append("0"+month.getValue()+"月");
						}

						int day = parse.getDayOfMonth();
						if(day>=10){
							sb.append(day+"日");
						}else{
							sb.append("0"+day+"日");
						}

						int hour = parse.getHour();
						if(hour>=10){
							sb.append(hour+"时");
						}else{
							sb.append("0"+hour+"时");
						}

						int minutes = parse.getMinute();
						if(minutes>=10){
							sb.append(minutes+"分");
						}else{
							sb.append("0"+minutes+"分");
						}

						int seconds = parse.getSecond();
						if(seconds>=10){
							sb.append(seconds+"秒");
						}else{
							sb.append("0"+seconds+"秒");
						}

					} catch (Exception e) {
						e.printStackTrace();
					}

					iotSmsSendDto.setMobile(alarmGroupUsers.get(i).getPhone());
					iotSmsSendDto.setContent("您好，"+residential.getName()+"的"+smartDevice.getDeviceName()+"设备在"+sb.toString()+"发生"+alarm.getAlarmDesc()+"，请及时处理。");
					System.out.println(iotSmsSendDto.getContent());
//					iotSmsSendDto.setContent("您好，潘家园街道办事处武农武圣西里6号楼3门501的武农武圣西里6号楼3门501设备在2020年05月13日23时55分19秒发生XX报警，请及时处理。");
					Long timestamp = null;
					try {
						timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(recordTime).getTime();
					} catch (ParseException e) {
						e.printStackTrace();
					}
					iotSmsSendDto.setTimestamp(timestamp.toString());
					iotSmsSendDtos.add(iotSmsSendDto);
				}
			}
		}
		return iotSmsSendDtos;
	}
}
