package org.springblade.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springblade.alarm.alarmgroup.entity.AlarmGroup;
import org.springblade.alarm.alarmgroup.service.IAlarmGroupService;
import org.springblade.alarm.alarmgroupuser.entity.AlarmGroupUser;
import org.springblade.alarm.alarmgroupuser.service.IAlarmGroupUserService;
import org.springblade.community.residential.entity.Residential;
import org.springblade.community.residential.service.IResidentialService;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.homecare.alarmlist.entity.Alarm;
import org.springblade.homecare.alarmlist.service.IAlarmService;
import org.springblade.jobs.devicejob.entity.DeviceJob;
import org.springblade.jobs.devicejob.service.IDeviceJobService;
import org.springblade.smartapplication.smartsmoke.entity.SmartDevice;
import org.springblade.smartapplication.smartsmoke.service.ISmartDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

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

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


	@Autowired
	private IResidentialService residentialService;

	@Autowired
	private ISmartDeviceService smartDeviceService;

	@Autowired
	private IAlarmGroupService alarmGroupService;

	@Autowired
	private IAlarmGroupUserService alarmGroupUserService;

	@Autowired
	private IDeviceJobService deviceJobService;

	@Autowired
	private IAlarmService alarmService;
	/**
	 * 告警自动生成工单
	 * @param alarm
	 *
	 */
	public  void deviceAlarmOrderGenerate(Alarm alarm){
		DeviceJob deviceJob = new DeviceJob();
		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());

		SmartDevice deviceWrapper = new SmartDevice();
		deviceWrapper.setImei(deviceJob.getDeviceId());
		SmartDevice device = smartDeviceService.getOne(Condition.getQueryWrapper(deviceWrapper));
		if (device != null) {
			deviceJob.setOrgPosition(device.getOrgPosition());
			deviceJob.setAlarmPosition(device.getDeviceInstallAddress());
			Residential residential = residentialService.getById(device.getResidentialId());
			if (residential == null) {
				System.out.println("找不到该小区！！！！");
				return;
			}
			//查询小区的告警组中的处理人
			StringBuffer personBuffer = new StringBuffer();
			StringBuffer phoneBuffer = new StringBuffer();
			List<AlarmGroup> alarmGroups = alarmGroupService.getBaseMapper().selectList(new QueryWrapper<>(new AlarmGroup()).lambda().eq(AlarmGroup::getResidentialId, device.getResidentialId()).eq(AlarmGroup::getTag, device.getDeviceType()));
			if(alarmGroups.size()>0) {
				//获取告警组中的处理人
				List<AlarmGroupUser> alarmGroupUsers = alarmGroupUserService.getBaseMapper().selectList(new QueryWrapper<>(new AlarmGroupUser()).lambda().eq(AlarmGroupUser::getGroupId, alarmGroups.get(0).getId()).eq(AlarmGroupUser::getIsHandler,1));
				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.setHandler(personBuffer.toString());
			deviceJob.setDeviceName(device.getDeviceName());
			deviceJob.setPhone(phoneBuffer.toString());
			deviceJob.setAlarmPosition(device.getDeviceInstallAddress());
			deviceJob.setTenantId(residential.getTenantId());
		}
		boolean result = deviceJobService.saveOrUpdate(deviceJob);
		//设置告警记录的工单id
		Alarm alarm1 = alarmService.getById(alarmId);
		if(result&&alarm1!=null){
			alarm.setOrderId(deviceJob.getId().toString());
			alarmService.updateById(alarm);
		}

	}
}
