package com.gyee.frame.service.contrast;

import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.model.auto.*;
import com.gyee.frame.model.custom.BenchMarkVo;
import com.gyee.frame.model.custom.DataVo;
import com.gyee.frame.service.FaultEliminateDefectsService;
import com.gyee.frame.service.StatetransitionratesService;
import com.gyee.frame.service.Windpowerinfoday3Service;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.StringUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;


@Service
public class ContrastPjService {
	@Resource
	private Windpowerinfoday3Service windpowerinfoday3Service;
	@Resource
	private FaultEliminateDefectsService faultEliminateDefectsService;
	@Resource
	private StatetransitionratesService statetransitionratesService;



	public List<BenchMarkVo> benchmarkPjList(String wpId,String pjId, String beginDate, String endDate) {
		List<BenchMarkVo> vos = new ArrayList<>();
		Date tempDate = null;
		if (StringUtils.isNotEmpty(beginDate) && StringUtils.isNotEmpty(endDate)) {
			Date begin = DateUtils.parseDate(beginDate);
			Date end = DateUtils.parseDate(endDate);

			// 获得复位及时率、状态转换及时率数据
			Map<String, Statetransitionrates> stmap = statetransitionratesService.getStatetransitionratesByWpTotal(null, begin, end);
			// 获得消缺及时率数据
			Map<String, FaultEliminateDefects> fdmap = faultEliminateDefectsService.getFaultEliminateDefectsByWpTotal(null, begin, end);

			List<String> wpids = new ArrayList<String>();
			if (StringUtils.notEmp(wpId)) {
				if (wpId.indexOf(",") != -1) {
					String[] strs = wpId.split(",");
					if (strs.length > 0) {
						for (int i = 0; i < strs.length; i++) {
							if(InitialRunner.pjsmap.containsKey(strs[i]))
							{
								List<Project> pjls=InitialRunner.pjsmap.get(strs[i]);
								for(Project pj:pjls)
								{
									wpids.add(pj.getId());
								}
							}
						}
					}
				} else {
					if(InitialRunner.pjsmap.containsKey(wpId))
					{
						List<Project> pjls=InitialRunner.pjsmap.get(wpId);
						for(Project pj:pjls)
						{
							wpids.add(pj.getId());
						}
					}
				}
			}

			List<String> pjids = new ArrayList<String>();
			if (StringUtils.notEmp(pjId)) {
				if (pjId.indexOf(",") != -1) {
					String[] strs = pjId.split(",");
					if (strs.length > 0) {
						for (int i = 0; i < strs.length; i++) {
							pjids.add(strs[i]);
						}
					}
				} else {
					pjids.add(pjId);
				}
			}
			List<Windpowerinfoday3> ls =null;
			if (!pjids.isEmpty()) {
				ls =windpowerinfoday3Service.getWindpowerinfoday3(pjids, begin, end, "project");
			} else if (!wpids.isEmpty()) {
				ls =windpowerinfoday3Service.getWindpowerinfoday3(wpids, begin, end, "project");
			}else {
				ls =windpowerinfoday3Service.getWindpowerinfoday3(new ArrayList<>(), begin, end, "project");
			}

			Map<String, Windpowerstation> wpmap = InitialRunner.wpmap;

			Map<String, Project> pjmap = InitialRunner.pjmap;

			Map<String, Windpowerinfoday3> wdmap = new HashMap<String, Windpowerinfoday3>();
			if (!ls.isEmpty()) {
				for (Windpowerinfoday3 wd : ls) {
					if (wdmap.containsKey(wd.getProjectid())) {
						Windpowerinfoday3 w = wdmap.get(wd.getProjectid());
						w.setWindpowerstationid(wd.getWindpowerstationid());
						w.setProjectid(wd.getProjectid());
						double losspower1 = null != wd.getDaynhxdssdl() ? wd.getDaynhxdssdl() : 0.0;
						double losspower2 = null != wd.getDaynhgzssdl() ? wd.getDaynhgzssdl() : 0.0;
						double losspower3 = null != wd.getDaynhwhssdl() ? wd.getDaynhwhssdl() : 0.0;
						double losspower4 = null != wd.getDaynhqfdl() ? wd.getDaynhqfdl() : 0.0;
						double scadafdl = null != wd.getDaydl2() ? wd.getDaydl2() : 0; // scada发电量

						w.setDaynhxdssdl(w.getDaynhxdssdl() + losspower1);
						w.setDaynhgzssdl(w.getDaynhgzssdl() + losspower2);
						w.setDaynhwhssdl(w.getDaynhwhssdl() + losspower3);
						w.setDaynhqfdl(w.getDaynhqfdl() + losspower4);
						w.setDaydl2(w.getDaydl2() + scadafdl);
						wdmap.put(wd.getProjectid(), w);
					} else {
						Windpowerinfoday3 w = new Windpowerinfoday3();
						w.setWindpowerstationid(wd.getWindpowerstationid());
						w.setProjectid(wd.getProjectid());
						double scadafdl = null != wd.getDaydl2() ? wd.getDaydl2() : 0; // scada发电量
						double losspower1 = null != wd.getDaynhxdssdl() ? wd.getDaynhxdssdl() : 0.0;
						double losspower2 = null != wd.getDaynhgzssdl() ? wd.getDaynhgzssdl() : 0.0;
						double losspower3 = null != wd.getDaynhwhssdl() ? wd.getDaynhwhssdl() : 0.0;
						double losspower4 = null != wd.getDaynhqfdl() ? wd.getDaynhqfdl() : 0.0;
						w.setDaydl2(scadafdl);
						w.setDaynhxdssdl(losspower1);
						w.setDaynhgzssdl(losspower2);
						w.setDaynhwhssdl(losspower3);
						w.setDaynhqfdl(losspower4);
						wdmap.put(wd.getProjectid(), w);
					}
				}

				Set<Map.Entry<String, Windpowerinfoday3>> set = wdmap.entrySet();
				for (Iterator<Map.Entry<String, Windpowerinfoday3>> it = set.iterator(); it.hasNext();) {
					Map.Entry<String, Windpowerinfoday3> entry = (Map.Entry<String, Windpowerinfoday3>) it.next();
					// System.out.println(entry.getKey() + "--->" +
					// entry.getValue());

					Windpowerinfoday3 wd = entry.getValue();
					BenchMarkVo vo = new BenchMarkVo();

					if (pjmap.containsKey(wd.getProjectid())) {
						Project pj = pjmap.get(wd.getProjectid());
						vo.setPjId(wd.getProjectid());
						vo.setPjName(pj.getName());

					}
					if (wpmap.containsKey(wd.getWindpowerstationid())) {
						Windpowerstation wp = wpmap.get(wd.getWindpowerstationid());
						vo.setWpId(wd.getWindpowerstationid());
						vo.setWpName(wp.getName());
					}
					// vo.setRecordDate(DateUtils.truncate(wd.getRecorddate()));
					double scadafdl = null != wd.getDaydl2() ? (new BigDecimal(wd.getDaydl2()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP).doubleValue()) : 0; // scada发电量
					double fdl = null != wd.getDaydl2() ? wd.getDaydl2() : 0; // 发电量
					double qfss = null != wd.getDaynhqfdl() ? (new BigDecimal(wd.getDaynhqfdl()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP).doubleValue()) : 0;// 欠发损失发电量
					double gzss = null != wd.getDaynhgzssdl() ? (new BigDecimal(wd.getDaynhgzssdl()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP).doubleValue()) : 0;// 故障损失发电量
					double whss = null != wd.getDaynhwhssdl() ? (new BigDecimal(wd.getDaynhwhssdl()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP).doubleValue()) : 0;// 维护损失发电量
					double xdss = null != wd.getDaynhxdssdl() ? (new BigDecimal(wd.getDaynhxdssdl()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP).doubleValue()) : 0;// 限电损失发电量
					double llfdl = scadafdl + qfss + gzss + whss + xdss;// 理论发电量
					vo.setFdl(fdl);// 发电量
					vo.setFjhtyssdl(gzss);// 非计划停运损失电量
					vo.setSbyxssdl(qfss);// 设备运行状态损失电量
					vo.setSlssl(0.0);// 受累损失电量

					vo.setScadafdl(scadafdl);
					vo.setGzss(gzss);
					vo.setWhss(whss);
					vo.setQfss(qfss);
					vo.setXdss(xdss);
					vo.setSlss(0.0);

					if (llfdl != 0) {
						double temp = xdss;

						BigDecimal b1 = new BigDecimal(temp);
						BigDecimal b2 = new BigDecimal(llfdl);

						temp = b1.divide(b2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
						// temp 弃风率
						vo.setQfl(temp);
						temp = qfss;

						b1 = new BigDecimal(temp);
						b2 = new BigDecimal(llfdl);

						temp = b1.divide(b2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
						// temp 性能损失率
						vo.setXnssl(temp);
						temp = gzss;

						b1 = new BigDecimal(temp);
						b2 = new BigDecimal(llfdl);

						temp = b1.divide(b2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
						// temp 故障损失率
						vo.setGzssl(temp);

						temp = whss;

						b1 = new BigDecimal(temp);
						b2 = new BigDecimal(llfdl);

						temp = b1.divide(b2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
						// temp 检修损失率
						vo.setJxssl(temp);
						temp = 0;

						b1 = new BigDecimal(temp);
						b2 = new BigDecimal(llfdl);

						temp = b1.divide(b2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
						// temp 受累损失率
						vo.setSlssl(temp);
						temp = scadafdl;

						b1 = new BigDecimal(temp);
						b2 = new BigDecimal(llfdl);

						temp = b1.divide(b2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
						// temp 风能利用率
						vo.setFnlyl(temp);
					} else {
						// temp 风能利用率
						vo.setFnlyl(0.0);
						// temp 故障损失率
						vo.setGzssl(0.0);
						// temp 检修损失率
						vo.setJxssl(0.0);
						// temp 弃风率
						vo.setQfl(0.0);
						// temp 受累损失率
						vo.setSlssl(0.0);
						// temp 性能损失率
						vo.setXnssl(0.0);
					}

					if (stmap.containsKey(wd.getWindpowerstationid())) {
						Statetransitionrates st = stmap.get(wd.getWindpowerstationid());

						double temp1 = st.getTimerate1();// 小于3米5分钟转换及时次数
						double temp2 = st.getTimerate13();// 小于3米状态转换总次数

						if (temp2 != 0) {
							double temp = 0;

							BigDecimal b1 = new BigDecimal(temp1);
							BigDecimal b2 = new BigDecimal(temp2);

							temp = b1.divide(b2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
							// temp 复位及时率
							vo.setFwjsl(temp);
						} else {
							vo.setFwjsl(100.0);
						}

						temp1 = st.getTimerate16();// 5分钟复位及时次数
						temp2 = st.getTimerate20();// 总复位次数

						if (temp2 != 0) {
							double temp = 0;

							BigDecimal b1 = new BigDecimal(temp1);
							BigDecimal b2 = new BigDecimal(temp2);

							temp = b1.divide(b2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
							// temp 状态转换及时率
							vo.setZtzhjsl(temp);
						} else {
							vo.setZtzhjsl(100.0);
						}

					} else {
						vo.setZtzhjsl(100.0);
						vo.setFwjsl(100.0);
					}
					if (fdmap.containsKey(wd.getWindpowerstationid())) {
						FaultEliminateDefects fd = fdmap.get(wd.getWindpowerstationid());

						double temp1 = fd.getRightcount();// 消缺及时次数
						double temp2 = fd.getFaultcount();// 故障次数

						if (temp2 != 0) {
							double temp = 0;

							BigDecimal b1 = new BigDecimal(temp1);
							BigDecimal b2 = new BigDecimal(temp2);

							temp = b1.divide(b2, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
							// temp 消缺及时率
							vo.setXqjsl(temp);
						} else {
							vo.setXqjsl(100.0);
						}

					} else {
						vo.setXqjsl(100.0);
					}

					vos.add(vo);

				}
			}

		}
		return vos;
	}

	public List<DataVo> findPjPowerChat(String wpId,String pjId, String beginDate, String endDate) {

		Date begin = DateUtils.parseDate(beginDate);
		Date end = DateUtils.parseDate(endDate);

		List<DataVo> vos = new ArrayList<DataVo>();
		List<String> wpids = new ArrayList<String>();
		if (StringUtils.notEmp(wpId)) {
			if (wpId.indexOf(",") != -1) {
				String[] strs = wpId.split(",");
				if (strs.length > 0) {
					for (int i = 0; i < strs.length; i++) {
						if(InitialRunner.pjsmap.containsKey(strs[i]))
						{
							List<Project> pjls=InitialRunner.pjsmap.get(strs[i]);
							for(Project pj:pjls)
							{
								wpids.add(pj.getId());
							}
						}
					}
				}
			} else {
				if(InitialRunner.pjsmap.containsKey(wpId))
				{
					List<Project> pjls=InitialRunner.pjsmap.get(wpId);
					for(Project pj:pjls)
					{
						wpids.add(pj.getId());
					}
				}
			}
		}

		List<String> pjids = new ArrayList<String>();
		if (StringUtils.notEmp(pjId)) {
			if (pjId.indexOf(",") != -1) {
				String[] strs = pjId.split(",");
				if (strs.length > 0) {
					for (int i = 0; i < strs.length; i++) {
						pjids.add(strs[i]);
					}
				}
			} else {
				pjids.add(pjId);
			}
		}
		List<Windpowerinfoday3> ls =null;
		if (!pjids.isEmpty()) {
			ls =windpowerinfoday3Service.getWindpowerinfoday3(pjids, begin, end, "project");
		} else if (!wpids.isEmpty()) {
			ls =windpowerinfoday3Service.getWindpowerinfoday3(wpids, begin, end, "project");
		}else {
			ls =windpowerinfoday3Service.getWindpowerinfoday3(new ArrayList<>(), begin, end, "project");
		}
		// Map<String, Windpowerstation> wpmap = CacheContext.wpmap;
		Map<String, Project> pjmap = InitialRunner.pjmap;
		Map<String, DataVo> vomap = new HashMap<String, DataVo>();
		if (!ls.isEmpty()) {
			for (int i = 0; i < ls.size(); i++) {
				Windpowerinfoday3 wd = ls.get(i);
				if (vomap.containsKey(wd.getProjectid())) {
					DataVo vo = vomap.get(wd.getProjectid());
					double losspower1 = null != wd.getDaynhxdssdl2() ? wd.getDaynhxdssdl2() : 0.0;
					double losspower2 = null != wd.getDaynhgzssdl2() ? wd.getDaynhgzssdl2() : 0.0;
					double losspower3 = null != wd.getDaynhwhssdl2() ? wd.getDaynhwhssdl2() : 0.0;
					double losspower4 = null != wd.getDaynhqfdl2() ? wd.getDaynhqfdl2() : 0.0;
					double fdl = null != wd.getDaydl2() ? wd.getDaydl2() : 0.0;

					vo.setValue1(vo.getValue1() + losspower1);// 限电损失
					vo.setValue2(vo.getValue2() + losspower2);// 故障损失
					vo.setValue3(vo.getValue3() + losspower3);// 维护损失
					vo.setValue4(vo.getValue4() + losspower4);// 欠发损失
					vo.setValue5(0.0);// 受累损失
					vo.setValue6(vo.getValue6() + fdl);// 发电量
					vomap.put(wd.getProjectid(), vo);
				} else {
					DataVo vo = new DataVo();

					vo.setValue1(null != wd.getDaynhxdssdl2() ? wd.getDaynhxdssdl2() : 0.0);// 限电损失
					vo.setValue2(null != wd.getDaynhgzssdl2() ? wd.getDaynhgzssdl2() : 0.0);// 故障损失
					vo.setValue3(null != wd.getDaynhwhssdl2() ? wd.getDaynhwhssdl2() : 0.0);// 维护损失
					vo.setValue4(null != wd.getDaynhqfdl2() ? wd.getDaynhqfdl2() : 0.0);// 欠发损失

					vo.setValue5(0.0);// 受累损失
					vo.setValue6(null != wd.getDaydl2() ? (new BigDecimal(wd.getDaydl2()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP).doubleValue()) : 0.0);// 发电量

					// vo.setValue7(null != wd.getGridelectricity() ?
					// wd.getGridelectricity() : 0.0);// 上网电量
					// vo.setValue8(null != wd.getBuyelectricity() ?
					// wd.getBuyelectricity() : 0.0);// 购网电量

					vo.setTime(DateUtils.truncate(DateUtils.addDays(wd.getRecorddate(), 1)).getTime());

					if (pjmap.containsKey(wd.getProjectid())) {
						Project pj = pjmap.get(wd.getProjectid());
						vo.setName(pj.getName());

					}
					// if(wpmap.containsKey(wd.getWindpowerstationid()))
					// {
					// Windpowerstation
					// wp=wpmap.get(wd.getWindpowerstationid());
					// vo.setName(wp.getName());
					// }
					vomap.put(wd.getProjectid(), vo);
				}

			}
			Set<Map.Entry<String, DataVo>> set = vomap.entrySet();
			for (Iterator<Map.Entry<String, DataVo>> it = set.iterator(); it.hasNext();) {
				Map.Entry<String, DataVo> entry = (Map.Entry<String, DataVo>) it.next();
				// System.out.println(entry.getKey() + "--->" +
				// entry.getValue());
				DataVo vo = entry.getValue();
				vo.setValue1(new BigDecimal(vo.getValue1()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP).doubleValue());
				vo.setValue2(new BigDecimal(vo.getValue2()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP).doubleValue());
				vo.setValue3(new BigDecimal(vo.getValue3()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP).doubleValue());
				vo.setValue4(new BigDecimal(vo.getValue4()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP).doubleValue());
				vo.setValue5(StringUtils.round(vo.getValue5(), 2));
				vo.setValue6(new BigDecimal(vo.getValue6()).divide(new BigDecimal(10000), 2, RoundingMode.HALF_UP).doubleValue());
				// vo.setValue7(StringUtils.round(vo.getValue7(), 2));
				// vo.setValue8(StringUtils.round(vo.getValue8(), 2));
				vos.add(vo);
			}
		}

		return vos;
	}
}
