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

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import org.springblade.common.constant.EstateConstant;
import org.springblade.community.entity.*;
import org.springblade.community.feign.*;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.enterprise.entity.Enterprise;
import org.springblade.enterprise.feign.IEnterpriseClient;
import org.springblade.estate.entity.SnapRecord;
import org.springblade.estate.vo.SnapRecordVO;
import org.springblade.system.expand.feign.IConfClient;
import org.springblade.system.feign.ISysClient;

import java.math.BigDecimal;
import java.util.Objects;
import java.util.Optional;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2022-01-05
 */
public class SnapRecordWrapper extends BaseEntityWrapper<SnapRecord, SnapRecordVO>  {

	private static IConfClient confClient;
	private static IAgencyClient agencyClient;
	private static IResidentialClient residentialClient;
	private static IBuildingClient buildingClient;
	private static IUnitClient unitClient;
	private static IRoomClient roomClient;
	private static IEnterpriseClient enterpriseClient;
	private static ISysClient sysClient;
	private static BigDecimal highTemperature;

	public static SnapRecordWrapper build() {
		return new SnapRecordWrapper();
 	}

 	static {
		confClient = SpringUtil.getBean(IConfClient.class);
		agencyClient = SpringUtil.getBean(IAgencyClient.class);
		residentialClient = SpringUtil.getBean(IResidentialClient.class);
		buildingClient = SpringUtil.getBean(IBuildingClient.class);
		unitClient = SpringUtil.getBean(IUnitClient.class);
		roomClient = SpringUtil.getBean(IRoomClient.class);
		enterpriseClient = SpringUtil.getBean(IEnterpriseClient.class);
		sysClient = SpringUtil.getBean(ISysClient.class);
		highTemperature = Convert.toBigDecimal(sysClient.getParamValue(EstateConstant.HIGH_TEMPERATURE_NAME).getData());
	}

	@Override
	public SnapRecordVO entityVO(SnapRecord snapRecord) {
		SnapRecordVO snapRecordVO = Objects.requireNonNull(BeanUtil.copy(snapRecord, SnapRecordVO.class));
		snapRecordVO.setFaceUrl(confClient.addPrefix(snapRecordVO.getFaceUrl()));
		Agency agency = agencyClient.getById(snapRecord.getAgencyId());
		if (agency!=null) {
			snapRecordVO.setAgencyName(agency.getName());
		}
		if(snapRecord.getResidentialId() != null){
			Residential residential = residentialClient.getById(snapRecord.getResidentialId());
			snapRecordVO.setResidentialName(residential.getName());
		}
		if(snapRecord.getBuildingId() != null){
			Building building = buildingClient.getById(snapRecord.getBuildingId());
			snapRecordVO.setBuildingName(building.getName());
		}
		if(snapRecord.getUnitId() != null){
			Unit unit = unitClient.getById(snapRecord.getUnitId());
			snapRecordVO.setUnitName(unit.getName());
		}
		if(snapRecord.getRoomId() != null){
			Room room = roomClient.getById(snapRecord.getRoomId());
			snapRecordVO.setRoomName(room.getName());
		}
		if(snapRecord.getEnterpriseId() != null){
			Enterprise enterprise = enterpriseClient.getById(snapRecord.getEnterpriseId());
			snapRecordVO.setEnterpriseName(enterprise.getEnterpriseName());
		}
		BigDecimal temperature = Optional.ofNullable(Convert.toBigDecimal(snapRecord.getTemperature())).orElse(BigDecimal.ZERO);
		snapRecordVO.setHighTemperature(temperature.compareTo(highTemperature) >= 0);

		return snapRecordVO;
	}

}
