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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.time.DateUtils;
import org.springblade.common.utils.CommonUtil;
import org.springblade.common.utils.UnitUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.modules.nh.factory.ginlong.dto.userstationlist.GetStationR;
import org.springblade.modules.nh.pojo.dto.sync.JlglHourlyDTO;
import org.springblade.modules.nh.pojo.entity.MonitorEntryDailyStatEntity;
import org.springblade.modules.nh.pojo.vo.MonitorEntryDailyStatVO;
import org.springblade.modules.nh.excel.MonitorEntryDailyStatExcel;
import org.springblade.modules.nh.mapper.MonitorEntryDailyStatMapper;
import org.springblade.modules.nh.service.IMonitorEntryDailyStatService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 电站日统计数据 服务实现类
 *
 * @author AlexWang
 * @since 2024-06-02
 */
@Slf4j
@Service
public class MonitorEntryDailyStatServiceImpl extends ServiceImpl<MonitorEntryDailyStatMapper, MonitorEntryDailyStatEntity> implements IMonitorEntryDailyStatService {

	@Override
	public IPage<MonitorEntryDailyStatVO> selectMonitorEntryDailyStatPage(IPage<MonitorEntryDailyStatVO> page, MonitorEntryDailyStatVO monitorEntryDailyStat) {
		return page.setRecords(baseMapper.selectMonitorEntryDailyStatPage(page, monitorEntryDailyStat));
	}


	@Override
	public List<MonitorEntryDailyStatExcel> exportMonitorEntryDailyStat(Wrapper<MonitorEntryDailyStatEntity> queryWrapper) {
		List<MonitorEntryDailyStatExcel> monitorEntryDailyStatList = baseMapper.exportMonitorEntryDailyStat(queryWrapper);
		//monitorEntryDailyStatList.forEach(monitorEntryDailyStat -> {
		//	monitorEntryDailyStat.setTypeName(DictCache.getValue(DictEnum.YES_NO, MonitorEntryDailyStat.getType()));
		//});
		return monitorEntryDailyStatList;
	}

	@Override
	public void syncEntryDailyStat(List<GetStationR> list, String sysType) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		List<MonitorEntryDailyStatEntity> batchlist = Lists.newArrayList();
		Date now = new Date();

		for (GetStationR s : list) {
			Long dataTimestamp = s.getDataTimestamp(); // 采集时间
			Date syncTime = new Date(dataTimestamp);
			if (!DateUtils.isSameDay(syncTime, now)) {
				continue;
			}
			int hours = syncTime.getHours(); // 0-23


			MonitorEntryDailyStatEntity dailyStatEntity = getByStatDateAndstationId(df.format(syncTime), s.getId());
			if (dailyStatEntity == null) {
				dailyStatEntity = new MonitorEntryDailyStatEntity();
				dailyStatEntity.setCreateTime(new Date());
			}

			try{
				Field field= dailyStatEntity.getClass().getDeclaredField(String.format("h%02d", hours + 1));
				ReflectionUtils.makeAccessible(field);
				ReflectionUtils.setField(field, dailyStatEntity, UnitUtil.convEnergy(s.getDayEnergy(), s.getDayEnergyStr()));
			}catch (Exception e){
				log.error("syncEntryDailyStat error,e:",e);
				continue;
			}
			dailyStatEntity.setSysType(sysType);
			dailyStatEntity.setSyncTime(syncTime);
			dailyStatEntity.setUpdateTime(new Date());
			dailyStatEntity.setStatDate(syncTime);
			dailyStatEntity.setOutStationId(s.getId());
			batchlist.add(dailyStatEntity);
		}

		this.saveOrUpdateBatch(batchlist);
	}

	private MonitorEntryDailyStatEntity getByStatDateAndstationId(String statDate, String outStationId) {
		LambdaQueryWrapper<MonitorEntryDailyStatEntity> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(MonitorEntryDailyStatEntity::getStatDate,statDate)
			.eq(MonitorEntryDailyStatEntity::getOutStationId,outStationId);
		return this.getOne(queryWrapper);
	}

	@Override
	public void syncEntryDailyJlglStat(List<JlglHourlyDTO> list, String date, String stationId, String sysType) {
		Date now = new Date();

		LambdaQueryWrapper<MonitorEntryDailyStatEntity> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(MonitorEntryDailyStatEntity::getStatDate,date)
			.eq(MonitorEntryDailyStatEntity::getOutStationId,stationId);
		MonitorEntryDailyStatEntity entity = this.getOne(queryWrapper);

		if (entity == null) {
			entity = new MonitorEntryDailyStatEntity();
			entity.setCreateTime(now);
			entity.setUpdateTime(now);
			entity.setSyncTime(now);
			entity.setStatDate(DateUtil.parse(date,"yyyy-MM-dd"));
			entity.setOutStationId(stationId);
			entity.setSysType(sysType);
		}
		for (JlglHourlyDTO s : list) {
			CommonUtil.reflectSetField(entity,String.format("jlgl%02d", s.getHour()),s.getJlgl());
		}
		this.saveOrUpdate(entity);
	}
}
