package org.springblade.modules.nh.factory.saj.scheduler;

import cn.hutool.core.io.unit.DataUnit;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.AllArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springblade.common.constant.NhConstants;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.modules.nh.factory.common.EachRunner;
import org.springblade.modules.nh.factory.ginlong.dto.userstationlist.GetStationR;
import org.springblade.modules.nh.factory.goodwe.dto.getinventersmoredetailinfobysn.D;
import org.springblade.modules.nh.factory.saj.SajApi;
import org.springblade.modules.nh.factory.saj.req.QueryDeviceHistoryReq;
import org.springblade.modules.nh.factory.saj.req.QueryPlantEnergyReq;
import org.springblade.modules.nh.factory.saj.resp.*;
import org.springblade.modules.nh.pojo.dto.sync.DayEnergyDTO;
import org.springblade.modules.nh.pojo.entity.MonitorEntryStatEntity;
import org.springblade.modules.nh.pojo.entity.MonitorInverterStatEntity;
import org.springblade.modules.nh.pojo.entity.PowerStationEntity;
import org.springblade.modules.nh.service.*;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 实时获取电站当前的发电量和功率
 */
@Service
@AllArgsConstructor
public class SajStationStatusEachRunner extends EachRunner<String> {

	private final SajApi sajApi;
	private final IMonitorEntryStatService monitorEntryStatService;
	private final IMonitorEntryYearStatService yearStatService;
	private final IPowerStationService powerStationService;
	private final IMonitorInverterStatService inverterStatService;
	private final IMonitorEntryDailyStatService dailyStatService;


	@Override
	public void handle(String stationId, Map<String, Object> ctx) throws IOException {
		handleData(stationId, 1);
	}

	public void handleData(String stationId,int index) throws IOException {
		QueryPlantEnergyReq req = new QueryPlantEnergyReq();
		req.setPlantId(stationId);
		req.setClientDate(DateUtil.formatDateTime(new Date()));
		SajRet<SajPlantData> resp = sajApi.getPlantStatisticsData(req);
		if(resp.getCode()==200){
			SajPlantData plant = resp.getData();

			int state = 1;
			//逆变器状态（1正常运行,2告警运行,3离线,4历史逆变器,5未监控）
			switch (plant.getDeviceStatus()){
				case 1:state=1;break;
				case 3:state=3;break;
				default:state=2;break;
			}
			GetStationR r = newGetStationR();
			r.setId(req.getPlantId());
			r.setDayEnergy(plant.getTodayPvEnergy());
			r.setMonthEnergy(plant.getMonthPvEnergy());
			r.setYearEnergy(plant.getYearPvEnergy());
			r.setAllEnergy(plant.getTotalPvEnergy());
			r.setPower(plant.getPowerNow());
			List<GetStationR> syncList = Lists.newArrayList();
			syncList.add(r);

			LambdaUpdateWrapper<MonitorEntryStatEntity> updateWrapper = new LambdaUpdateWrapper<>();
			updateWrapper.eq(MonitorEntryStatEntity::getOutStationId,stationId)
				.set(MonitorEntryStatEntity::getState,state)
				.set(MonitorEntryStatEntity::getEtd,plant.getTodayPvEnergy())
				.set(MonitorEntryStatEntity::getEtm,plant.getMonthPvEnergy())
				.set(MonitorEntryStatEntity::getEty,plant.getYearPvEnergy())
				.set(MonitorEntryStatEntity::getEta,plant.getTodayPvEnergy())
				.set(MonitorEntryStatEntity::getSyncTime,new Date());
			monitorEntryStatService.update(updateWrapper);

			LambdaUpdateWrapper<MonitorInverterStatEntity> updateWrapper2 = new LambdaUpdateWrapper<>();
			updateWrapper2.eq(MonitorInverterStatEntity::getSn,plant.getDeviceSn())
				.set(MonitorInverterStatEntity::getState,state);
			inverterStatService.update(updateWrapper2);

			LambdaUpdateWrapper<PowerStationEntity> updateWrapper3 = new LambdaUpdateWrapper<>();
			updateWrapper3.eq(PowerStationEntity::getOutStationId,stationId)
				.set(PowerStationEntity::getStatus,state);
			powerStationService.update(updateWrapper3);

			dailyStatService.syncEntryDailyStat(syncList, NhConstants.SYS_TYPE_SAJ);
		}
	}

	private GetStationR newGetStationR(){
		GetStationR r = new GetStationR();
		r.setState(1);
		r.setDayEnergy(0D);
		r.setMonthEnergy(0D);
		r.setYearEnergy(0D);
		r.setAllEnergy(0D);
		r.setDayEnergyStr("kwh");
		r.setMonthEnergyStr("kwh");
		r.setYearEnergyStr("kwh");
		r.setAllEnergyStr("kwh");
		r.setVendorIndex(1);
		r.setPower(0D);
		r.setPowerStr("kw");
		r.setDataTimestamp(System.currentTimeMillis());
		return r;
	}

}
