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

import lombok.AllArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springblade.common.constant.NhConstants;
import org.springblade.common.utils.CommonUtil;
import org.springblade.modules.nh.factory.common.EachRunner;
import org.springblade.modules.nh.factory.ginlong.dto.inverterdetaillist.GetInverterDetailR;
import org.springblade.modules.nh.factory.saj.SajApi;
import org.springblade.modules.nh.factory.saj.resp.DeviceRealtimeData;
import org.springblade.modules.nh.factory.saj.resp.SajRet;
import org.springblade.modules.nh.pojo.dto.sync.InverterHourlyEnergyDTO;
import org.springblade.modules.nh.pojo.entity.MonitorInverterStatEntity;
import org.springblade.modules.nh.service.IMonitorInverterDailyStatService;
import org.springblade.modules.nh.service.IMonitorInverterStatService;
import org.springframework.stereotype.Service;

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

/**
 * 实时设备数据
 */
@Service
@AllArgsConstructor
public class SajDeviceRealtimeEachRunner extends EachRunner<String> {

	private final SajApi sajApi;
	private final IMonitorInverterStatService inverterStatService;
	private final IMonitorInverterDailyStatService inverterDailyStatService;

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

	public void handleData(String sn) {
		MonitorInverterStatEntity inverterStat = inverterStatService.getBySn(sn);
		if(inverterStat==null) return;
		SajRet<DeviceRealtimeData> resp =  sajApi.getDeviceRealtime(sn);
		if(resp.getCode()==200){
			DeviceRealtimeData data = resp.getData();
			if(data!=null){
				GetInverterDetailR inverterDetailR = convR(data);
				inverterDetailR.setVendorIndex(1);
				inverterDetailR.setStationId(inverterStat.getOutStationId());
				try{
					List<GetInverterDetailR> inverterDetailRList = Lists.newArrayList();
					inverterDetailRList.add(inverterDetailR);
					inverterStatService.syncInverterDetails(inverterDetailRList, NhConstants.SYS_TYPE_SAJ, new HashSet<>());
				}catch (Exception e){
					e.printStackTrace();
				}
				try{
					List<InverterHourlyEnergyDTO> energyList = Lists.newArrayList();
					InverterHourlyEnergyDTO hourlyEnergyDTO = new InverterHourlyEnergyDTO();
					hourlyEnergyDTO.setJlgl(inverterDetailR.getPac());
					hourlyEnergyDTO.setEnergy(inverterDetailR.getEToday());
					hourlyEnergyDTO.setSn(inverterDetailR.getSn());
					hourlyEnergyDTO.setSyncTime(new Date());
					energyList.add(hourlyEnergyDTO);
					inverterDailyStatService.syncHourlyData(NhConstants.SYS_TYPE_SAJ,energyList);
				}catch (Exception e){
					e.printStackTrace();
				}
			}
		}
	}

	private GetInverterDetailR convR(DeviceRealtimeData r) {
		GetInverterDetailR ret = new GetInverterDetailR();

		ret.setDataTimestamp(new Date().getTime());
		ret.setId(r.getDeviceSn());
		ret.setName("逆变器");
		ret.setSn(r.getDeviceSn());
		ret.setPower(0d);
		ret.setPowerStr("kw");
		//01.正常 02.离线 3.报警 4.故障 5.自定义报警
		//平台1.在线 2.离线，3.报警
		ret.setState(1);

		for(int i=1;i<=12;i++){
			String pvkey = "pv"+i+"volt";
			Object pv= CommonUtil.reflectGetField(r,pvkey);
			String pikey = "pv"+i+"curr";
			Object pi= CommonUtil.reflectGetField(r,pikey);
			if(pv!=null){
				Double upv = Double.parseDouble(pv.toString());
				if(upv>0){
					CommonUtil.reflectSetField(ret,"uPv"+i, upv);
					CommonUtil.reflectSetField(ret,"iPv"+i, Double.parseDouble(pi.toString()));
				}else{
					break;
				}

			}
		}

		ret.setFac(r.getRGridFreq());

		ret.setReactivePower(0D);
		ret.setReactivePowerStr("kwh");
		//
		ret.setApparentPower(0D);
		ret.setApparentPowerStr("kwh");

		ret.setPowerFactor(0D);

		ret.setPac(r.getTotalGridPowerWatt());
		ret.setPacStr("w");

		ret.setETotal(r.getTotalPvEnergy());
		ret.setETotalStr("kwh");

		ret.setEToday(r.getTodayPvEnergy());
		ret.setETodayStr("kwh");

		ret.setUAc1(r.getRGridVolt());
		ret.setIAc1(r.getRGridCurr());
		ret.setUAc2(r.getSGridVolt());
		ret.setIAc2(r.getSGridCurr());
		ret.setUAc3(r.getTGridVolt());
		ret.setIAc3(r.getTGridCurr());

		ret.setInverterTemperature(r.getAmbTempC());
		return ret;
	}


}
