package org.springblade.third.elevator.controller;

import cn.hutool.core.bean.BeanUtil;
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.community.entity.Residential;
import org.springblade.community.feign.IResidentialClient;
import org.springblade.device.entity.ElevatorAlarm;
import org.springblade.device.entity.ElevatorDevice;
import org.springblade.device.feign.IElevatorAlarmClient;
import org.springblade.device.feign.IElevatorDeviceClient;
import org.springblade.third.elevator.util.ElevatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@AllArgsConstructor
@RequestMapping("openinterface/elevator")
@Slf4j
public class OpenElevatorController {

	@Autowired
	private ElevatorUtil elevatorUtil;
	@Autowired
	private IElevatorAlarmClient elevatorAlarmClient;
	@Autowired
	private IElevatorDeviceClient elevatorDeviceClient;
	@Autowired
	private IResidentialClient residentialClient;

	@GetMapping("/getToken")
	public String getToken() {

		String token = elevatorUtil.getEffectiveToken();

		return token;
	}

	/**
	 * 电梯告警接收地址
	 * @param
	 * @return
	 */
	@PostMapping("/receive/elevator/alarm")
	public Map<String, Object> receiveElevatorAlarm(@RequestBody Map<String, Object> map){
		Object alarmTime = map.get("alarmTime");
		Object alarmEndTime = map.get("alarmEndTime");
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Etc/GMT-8"));
		if (alarmTime != null){
			Long temTime = (alarmTime.toString().length() == 10) ? Long.parseLong(alarmTime.toString()) * 1000 : Long.parseLong(alarmTime.toString());
			map.replace("alarmTime", simpleDateFormat.format(temTime));
		}
		if (alarmEndTime != null){
			Long temTime = (alarmEndTime.toString().length() == 10) ? Long.parseLong(alarmEndTime.toString()) * 1000 : Long.parseLong(alarmEndTime.toString());
			map.replace("alarmEndTime", simpleDateFormat.format(temTime));
		}

		ElevatorAlarm elevatorAlarm = new ElevatorAlarm();
		BeanUtil.copyProperties(map, elevatorAlarm);

		Map<String, Object> resultMap = new HashMap<>();
		if (BeanUtil.isNotEmpty(elevatorAlarm)){
			boolean result = elevatorAlarmClient.save(elevatorAlarm);
			if (result){
				// 回写tenantid、orgposition
				LambdaQueryWrapper<ElevatorDevice> elevatorWrapper = new QueryWrapper<>(new ElevatorDevice()).lambda().eq(ElevatorDevice::getRegisterCode, elevatorAlarm.getRegisterCode());
				ElevatorDevice elevator = elevatorDeviceClient.getOne(elevatorWrapper);
				if (elevator != null) {
					Long residentialId = elevator.getResidentialId();
					if (residentialId != null) {
						Residential residential = residentialClient.getById(residentialId);
						elevatorAlarm.setResidentialId(residentialId);
						elevatorAlarm.setResidentialName(residential.getName());
						elevatorAlarm.setTenantId(residential.getTenantId());
						elevatorAlarm.setOrgPosition(residential.getOrgPosition());
						elevatorAlarmClient.updateById(elevatorAlarm);
					} else {
						log.warn("【" + elevator.getNbhdName() + "+" + elevator.getRegisterCode() + "】该电梯暂未绑定小区");
					}
				} else {
					log.warn("找不到该注册码【" + elevatorAlarm.getRegisterCode() +"】对应的电梯记录");
				}

				// 发送通知
				boolean directorAndSendMsg = elevatorAlarmClient.getDirectorAndSendMsg(elevatorAlarm);

				resultMap.put("code", 0);
				resultMap.put("message", "OK");
				return resultMap;
			}
		}
		resultMap.put("code", -1);
		resultMap.put("message", "ERROR");
		return resultMap;
	}



}
