/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.community.elevator.elevatoralarm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.alarm.alarmgroup.service.IAlarmGroupService;
import org.springblade.alarm.alarmgroupuser.service.IAlarmGroupUserService;
import org.springblade.community.elevator.alarmdirector.entity.AlarmDirector;
import org.springblade.community.elevator.alarmdirector.service.IAlarmDirectorService;
import org.springblade.community.elevator.elevator.entity.Elevator;
import org.springblade.community.elevator.elevator.service.IElevatorService;
import org.springblade.community.elevator.elevatoralarm.entity.ElevatorAlarm;
import org.springblade.community.elevator.elevatoralarm.mapper.ElevatorAlarmMapper;
import org.springblade.community.elevator.elevatoralarm.service.IElevatorAlarmService;
import org.springblade.community.elevator.elevatoralarm.vo.ElevatorAlarmVO;
import org.springblade.community.elevator.elevatoralarmsetting.entity.AlarmSetting;
import org.springblade.community.elevator.elevatoralarmsetting.service.IAlarmSettingService;
import org.springblade.community.residential.service.IResidentialService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.openinterface.dto.IotSmsSendDto;
import org.springblade.openinterface.dto.IotSmsTemplateParam;
import org.springblade.smartapplication.smartsmoke.service.ISmartDeviceService;
import org.springblade.utils.AlarmSendSmsAndVoiceUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

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

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2021-03-05
 */
@Service
@Slf4j
@AllArgsConstructor
public class ElevatorAlarmServiceImpl extends BaseServiceImpl<ElevatorAlarmMapper, ElevatorAlarm> implements IElevatorAlarmService {

	private IAlarmSettingService alarmSettingService;
	private IElevatorService elevatorService;
	private IAlarmDirectorService alarmDirectorService;

	private RedisTemplate<String,String> redisTemplate;
	private ISmartDeviceService smartDeviceService;
	private IResidentialService residentialService;
	private IAlarmGroupService alarmGroupService;
	private IAlarmGroupUserService alarmGroupUserService;

	@Override
	public IPage<ElevatorAlarmVO> selectElevatorAlarmPage(IPage<ElevatorAlarmVO> page, ElevatorAlarmVO elevatorAlarm) {
		return page.setRecords(baseMapper.selectElevatorAlarmPage(page, elevatorAlarm));
	}

	@Override
	public boolean getDirectorAndSendMsg(ElevatorAlarm elevatorAlarm) {

		String registerCode = elevatorAlarm.getRegisterCode();
		String alarmType = elevatorAlarm.getAlarmType();
		String alarmTime = DateTimeFormatter.ofPattern("yyyy年MM月dd日HH时mm分ss秒").format(elevatorAlarm.getAlarmTime());

		LambdaQueryWrapper<Elevator> elevatorWrapper = new QueryWrapper<>(new Elevator()).lambda().eq(Elevator::getRegisterCode, registerCode);
		Elevator elevator = elevatorService.getOne(elevatorWrapper);
		if (elevator == null){
			log.warn("找不到该注册码【" + registerCode +"】对应的电梯记录");
			return false;
		}
		Long residentialId = elevator.getResidentialId();
		if (residentialId == null){
			log.warn("【" + elevator.getNbhdName() + "+" + elevator.getRegisterCode() + "】该电梯暂未绑定小区");
			return false;
		}

		LambdaQueryWrapper<AlarmDirector> directorWrapper = new QueryWrapper<>(new AlarmDirector()).lambda().eq(AlarmDirector::getResidentialId, residentialId);
		List<AlarmDirector> directorList = alarmDirectorService.list(directorWrapper);
		if (directorList == null || directorList.size() <= 0){
			log.warn("该小区暂未设置电梯告警负责人");
			return false;
		}

		LambdaQueryWrapper<AlarmSetting> residentialSettingWrapper = new QueryWrapper<>(new AlarmSetting()).lambda().eq(AlarmSetting::getLevel, 2).eq(AlarmSetting::getAlarmType, alarmType).eq(AlarmSetting::getResidentialId, residentialId);
		LambdaQueryWrapper<AlarmSetting> globalSettingWrapper = new QueryWrapper<>(new AlarmSetting()).lambda().eq(AlarmSetting::getLevel, 1).eq(AlarmSetting::getAlarmType, alarmType);
		AlarmSetting residentialSetting = alarmSettingService.getOne(residentialSettingWrapper);
		AlarmSetting globalSetting = alarmSettingService.getOne(globalSettingWrapper);

		if (residentialSetting == null){
			//暂无小区配置
			if (globalSetting == null){
				//暂无全局配置，不用发短信
			} else {
				//全局配置有，给负责人发送通知
				if (globalSetting.getIsNotice() == 1){
					sendSms(directorList, elevator.getResidentialName(), elevator.getLiftName(), globalSetting.getAlarmName(), alarmTime);
				}
			}
		} else {
			// 按照小区配置给负责人发送通知
			if (residentialSetting.getIsNotice() == 1){
				sendSms(directorList, elevator.getResidentialName(), elevator.getLiftName(), residentialSetting.getAlarmName(), alarmTime);
			}
		}
		return true;
	}

	public void sendSms(List<AlarmDirector> directorList, String residentialName, String liftName, String alarmName, String alarmTime){
		List<IotSmsSendDto> iotSmsSendDtos = new ArrayList<>();
		for (int i = 0; i < directorList.size(); i++) {
			AlarmDirector alarmDirector = directorList.get(i);
			IotSmsSendDto iotSmsSendDto = new IotSmsSendDto();
			IotSmsTemplateParam iotSmsTemplateParam = new IotSmsTemplateParam();
			iotSmsTemplateParam.setParam1(alarmDirector.getPersonName());//接收人称呼


			iotSmsTemplateParam.setParam2(alarmTime);//告警时间
			iotSmsTemplateParam.setParam3(residentialName);//小区名称

			iotSmsTemplateParam.setParam4(liftName);//设备名称
			iotSmsTemplateParam.setParam5(alarmName);


			iotSmsSendDto.setTemplate_param(iotSmsTemplateParam);
			iotSmsSendDto.setMobile(alarmDirector.getPersonTel());
			iotSmsSendDto.setTemplate_id("91556324");
			iotSmsSendDto.setPrefix("ctwing/smoke3");

			iotSmsSendDto.setTimestamp(alarmTime);
			iotSmsSendDtos.add(iotSmsSendDto);
		}

		new AlarmSendSmsAndVoiceUtil(redisTemplate,smartDeviceService,residentialService,alarmGroupService,alarmGroupUserService).sendNormalSms(iotSmsSendDtos);

	}


}
