/*
 *      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.wrapper;

import org.springblade.community.entity.*;
import org.springblade.community.feign.*;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.device.entity.DoorDevice;
import org.springblade.device.vo.DoorDeviceVO;
import org.springblade.system.feign.ISysClient;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2021-04-19
 */
public class DoorDeviceWrapper extends BaseEntityWrapper<DoorDevice, DoorDeviceVO>  {

	private static ISysClient sysClient;
	private static IAgencyClient agencyClient;
	private static IResidentialClient residentialClient;
	private static IBuildingClient buildingClient;
	private static IUnitClient unitClient;
	private static IFloorClient floorClient;
	private static IRoomClient roomClient;

	static {
		sysClient = SpringUtil.getBean(ISysClient.class);
		agencyClient = SpringUtil.getBean(IAgencyClient.class);
		residentialClient = SpringUtil.getBean(IResidentialClient.class);
		buildingClient = SpringUtil.getBean(IBuildingClient.class);
		unitClient = SpringUtil.getBean(IUnitClient.class);
		floorClient = SpringUtil.getBean(IFloorClient.class);
		roomClient = SpringUtil.getBean(IRoomClient.class);
	}
	public static DoorDeviceWrapper build() {
		return new DoorDeviceWrapper();
 	}

	@Override
	public DoorDeviceVO entityVO(DoorDevice doorDevice) {
		DoorDeviceVO doorDeviceVO = Objects.requireNonNull(BeanUtil.copy(doorDevice, DoorDeviceVO.class));
		R<String> heartbeatItvR = sysClient.getParamValue("door_device_heartbeat_intervals");
		Date heartbeatTime = doorDevice.getHeartbeatTime();
		if(heartbeatTime != null){
			long nowTime = System.currentTimeMillis();
			if(heartbeatTime.getTime() > nowTime - Long.parseLong(heartbeatItvR.getData())){
				//在线
				doorDeviceVO.setDeviceStatus(1);
			}else{
				//离线
				doorDeviceVO.setDeviceStatus(0);
			}
		}
		if (doorDevice.getAgencyId()!=null) {
			Agency agency = agencyClient.getById(doorDevice.getAgencyId());
			if (agency!=null) {
				doorDeviceVO.setAgencyName(agency.getName());
			}
		}

		if(doorDevice.getResidentialId() != null){
			Residential residential = residentialClient.getById(doorDevice.getResidentialId());
			if(residential != null){
				doorDeviceVO.setResidentialName(residential.getName());
			}
		}
		if(doorDevice.getBuildingId() != null){
			Building building = buildingClient.getById(doorDevice.getBuildingId());
			if(building != null){
				doorDeviceVO.setBuildingName(building.getName());
			}
		}
		if(doorDevice.getUnitId() != null){
			Unit unit = unitClient.getById(doorDevice.getUnitId());
			if(unit != null){
				doorDeviceVO.setUnitName(unit.getName());
			}
		}
		if(doorDevice.getFloorId() != null){
			Floor floor = floorClient.getById(doorDevice.getFloorId());
			if(floor != null){
				doorDeviceVO.setFloorName(floor.getName());
			}
		}
		if(doorDevice.getRoomId() != null){
			Room room = roomClient.getById(doorDevice.getRoomId());
			if(room != null){
				doorDeviceVO.setRoomName(room.getName());
			}
		}

		return doorDeviceVO;
	}

}
