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

import lombok.AllArgsConstructor;
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.IIotAlarmClient;
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.workorder.deviceorder.feign.IDeviceOrderClient;
import org.springblade.workorder.deviceorder.vo.DeviceOrderVO;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * Created By lidexi in 2020/12/29
 * 自动生成工单工具类
 **/
@Service
@AllArgsConstructor
public class OrderGenerateUtilService {


	private final IResidentialClient residentialClient;

	private final IIotDeviceClient iotDeviceClient;

	private final IIotAlarmGroupClient iotAlarmGroupClient;

	private final IIotAlarmGroupUserClient iotAlarmGroupUserClient;

	private final IDeviceOrderClient deviceOrderClient;

	private final IIotAlarmClient iotAlarmClient;
	/**
	 * 告警自动生成工单
	 * @param alarm
	 *
	 */
	public  void deviceAlarmOrderGenerate(IotAlarm alarm, IotDevice device){
		DeviceOrderVO deviceJob = new DeviceOrderVO();
		Long alarmId = alarm.getId();
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime ldt = LocalDateTime.parse(alarm.getRecordTime(),df);
		deviceJob.setAlarmTime(ldt);
		deviceJob.setAlarmReason(alarm.getName());
		deviceJob.setJobStatus(1);//处理中
		deviceJob.setDeviceId(alarm.getDeviceId());
		if (device != null) {
			deviceJob.setOrgPosition(device.getOrgPosition());
			deviceJob.setAlarmPosition(device.getDeviceInstallAddress());
			Residential residential = residentialClient.getById(device.getResidentialId());
			if (residential == null) {
				System.out.println("找不到该小区！！！！");
				return;
			}
			//查询小区的告警组中的处理人
			StringBuffer personBuffer = new StringBuffer();
			StringBuffer phoneBuffer = new StringBuffer();

			IotAlarmGroup searchGroup = new IotAlarmGroup();
			searchGroup.setResidentialId(residential.getId());
			searchGroup.setTag(device.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++) {
					if(i==0){
						personBuffer.append(alarmGroupUsers.get(i).getPerson());
						phoneBuffer.append(alarmGroupUsers.get(i).getPhone());
					}else{
						personBuffer.append(","+alarmGroupUsers.get(i).getPerson());
						phoneBuffer.append(","+alarmGroupUsers.get(i).getPhone());
					}
				}
			}
			deviceJob.setDeviceType(Integer.valueOf(device.getDeviceType()));
			deviceJob.setHandlePerson(personBuffer.toString());
			deviceJob.setDeviceName(device.getDeviceName());
			deviceJob.setHandlePhone(phoneBuffer.toString());
			deviceJob.setAlarmPosition(device.getDeviceInstallAddress());
			deviceJob.setTenantId(residential.getTenantId());
		}
		boolean result = deviceOrderClient.submit(deviceJob).isSuccess();
		//设置告警记录的工单id
		IotAlarm searchAlarm = new IotAlarm();
		searchAlarm.setAlarmId(alarmId.toString());
		IotAlarm alarm1 = iotAlarmClient.detail(searchAlarm);
		if(result&&alarm1!=null){
			alarm.setOrderId(deviceJob.getId().toString());
			iotAlarmClient.update(alarm);
		}

	}
}
