/*
 *      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.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.device.entity.ElevatorAlarm;
import org.springblade.device.entity.ElevatorAlarmDirector;
import org.springblade.device.entity.ElevatorAlarmSetting;
import org.springblade.device.entity.ElevatorDevice;
import org.springblade.device.service.IElevatorAlarmDirectorService;
import org.springblade.device.service.IElevatorAlarmSettingService;
import org.springblade.device.service.IElevatorDeviceService;
import org.springblade.device.vo.ElevatorAlarmVO;
import org.springblade.device.mapper.ElevatorAlarmMapper;
import org.springblade.device.service.IElevatorAlarmService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.third.sms.dto.IotSmsTemplateParam;
import org.springblade.third.sms.dto.PepSmsSendDto;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

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

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

	private IElevatorDeviceService elevatorService;
	private IElevatorAlarmDirectorService alarmDirectorService;
	private IElevatorAlarmSettingService alarmSettingService;



	@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<ElevatorDevice> elevatorWrapper = new QueryWrapper<>(new ElevatorDevice()).lambda().eq(ElevatorDevice::getRegisterCode, registerCode);
		ElevatorDevice 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<ElevatorAlarmDirector> directorWrapper = new QueryWrapper<>(new ElevatorAlarmDirector()).lambda().eq(ElevatorAlarmDirector::getResidentialId, residentialId);
		List<ElevatorAlarmDirector> directorList = alarmDirectorService.list(directorWrapper);
		if (directorList == null || directorList.size() <= 0){
			log.warn("该小区暂未设置电梯告警负责人");
			return false;
		}

		LambdaQueryWrapper<ElevatorAlarmSetting> residentialSettingWrapper = new QueryWrapper<>(new ElevatorAlarmSetting()).lambda().eq(ElevatorAlarmSetting::getLevel, 2).eq(ElevatorAlarmSetting::getAlarmType, alarmType).eq(ElevatorAlarmSetting::getResidentialId, residentialId);
		LambdaQueryWrapper<ElevatorAlarmSetting> globalSettingWrapper = new QueryWrapper<>(new ElevatorAlarmSetting()).lambda().eq(ElevatorAlarmSetting::getLevel, 1).eq(ElevatorAlarmSetting::getAlarmType, alarmType);
		ElevatorAlarmSetting residentialSetting = alarmSettingService.getOne(residentialSettingWrapper);
		ElevatorAlarmSetting 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<ElevatorAlarmDirector> directorList, String residentialName, String liftName, String alarmName, String alarmTime) {
		List<PepSmsSendDto> iotSmsSendDtos = new ArrayList<>();
		for (int i = 0; i < directorList.size(); i++) {
			ElevatorAlarmDirector alarmDirector = directorList.get(i);
			PepSmsSendDto iotSmsSendDto = new PepSmsSendDto();
			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);
		}
	}

}
