package org.springblade.modules.nh.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import org.springblade.common.utils.CommonUtil;
import org.springblade.core.http.util.HttpUtil;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.redis.cache.BladeRedis;
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.StringUtil;
import org.springblade.modules.nh.excel.DeviceExcel;
import org.springblade.modules.nh.mapper.DeviceMapper;
import org.springblade.modules.nh.pojo.dto.DeviceDTO;
import org.springblade.modules.nh.pojo.dto.WeatherHistory;
import org.springblade.modules.nh.pojo.entity.DeviceEntity;
import org.springblade.modules.nh.pojo.entity.InverterDataEntity;
import org.springblade.modules.nh.pojo.entity.PowerStationEntity;
import org.springblade.modules.nh.pojo.vo.DeviceVO;
import org.springblade.modules.nh.service.IDeviceService;
import org.springblade.modules.nh.service.IInverterDataService;
import org.springblade.modules.nh.service.INHCommonService;
import org.springblade.modules.nh.service.IPowerStationService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 设备管理 服务实现类
 *
 * @author AlexWang
 * @since 2024-05-31
 */
@Service
@AllArgsConstructor
public class NHCommonServiceImpl implements INHCommonService {

	private final BladeRedis bladeRedis;

	@Override
	public R queryWeather(String area) {
		if(StringUtil.isBlank(area)){
			area = "合肥";
		}
		String  resp = CommonUtil.queryWeather(area);
		JSONObject json = JSON.parseObject(resp);
		if(json.getInteger("showapi_res_code")==0){
			return R.data(json.getJSONObject("showapi_res_body"));
		}else{
			return R.fail(json.getString("showapi_res_error"));
		}
	}

	@Override
	public R queryWeatherHistory(String area, String day) {
		Date data = DateUtil.parse(day,"yyyy-MM-dd");
		String month = DateUtil.format(data,"yyyyMM");
		String dayStr = DateUtil.format(data,"yyyyMMdd");
		String key = area+"-"+month;
		if(bladeRedis.exists(key)){
			List<WeatherHistory> weatherList = bladeRedis.get(key);
			return R.data(getWeatherByDayInMonth(weatherList,dayStr));
		}

		String  resp = CommonUtil.queryWeatherByMonth(area,month);
		JSONObject json = JSON.parseObject(resp);
		if(json.getInteger("showapi_res_code")==0){
			JSONArray array = json.getJSONObject("showapi_res_body").getJSONArray("list");
			List<WeatherHistory> weatherList = array.toJavaList(WeatherHistory.class);
			bladeRedis.setEx(key,weatherList,30*60L);
			return R.data(getWeatherByDayInMonth(weatherList,dayStr));
		}else{
			return R.fail(json.getString("showapi_res_error"));
		}
	}

	private WeatherHistory getWeatherByDayInMonth(List<WeatherHistory> weatherList, String day){
		WeatherHistory result = null;
		for(WeatherHistory weatherHistory : weatherList){
			if(weatherHistory.getTime().equals(day)){
				result = weatherHistory;
				break;
			}
		}
		return result;
	}
}
