package com.gyee.frame.service.contrast;

import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.model.auto.FaultEliminateDefects;
import com.gyee.frame.model.auto.Statetransitionrates;
import com.gyee.frame.model.auto.Windturbine;
import com.gyee.frame.model.auto.Windturbineinfoday3;
import com.gyee.frame.model.custom.BenchMarkVo;
import com.gyee.frame.service.FaultEliminateDefectsService;
import com.gyee.frame.service.StatetransitionratesService;
import com.gyee.frame.service.Windturbineinfoday3Service;
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.text.SimpleDateFormat;
import java.util.*;


@Service
public class ContrastWtService {
	@Resource
	private Windturbineinfoday3Service windturbineinfoday3Service;
	@Resource
	private FaultEliminateDefectsService faultEliminateDefectsService;
	@Resource
	private StatetransitionratesService statetransitionratesService;

	public List<BenchMarkVo> benchmarkWtList(String wpId,String wtId, String beginDate, String endDate)
	{
		List<BenchMarkVo> vos=new ArrayList<>();
		getBenchMarkInfo(vos,wpId,wtId, beginDate, endDate);
		return vos;
	}
	public void getBenchMarkInfo(List<BenchMarkVo> vos,String wpId,String wtId, String beginDate, String endDate)   {
		Map<String, BenchMarkVo> bmmap = new HashMap<String, BenchMarkVo>();

		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++) {
						wpids.add(strs[i]);
					}
				}
			} else {
				wpids.add(wpId);
			}
		}
		setBenchmarkInfo(bmmap, wpids, wtId,DateUtils.parseDate(beginDate),DateUtils.parseDate(endDate));

		Set<Map.Entry<String, BenchMarkVo>> set = bmmap.entrySet();
		for (Iterator<Map.Entry<String, BenchMarkVo>> it = set.iterator(); it.hasNext();) {
			Map.Entry<String, BenchMarkVo> entry = (Map.Entry<String, BenchMarkVo>) it.next();
			// System.out.println(entry.getKey() + "--->" + entry.getValue());
			BenchMarkVo bm = entry.getValue();
			totalBenchmarkInfo(bm);
			vos.add(bm);
		}

	}

	private void setBenchmarkInfo(Map<String, BenchMarkVo> bmmap, List<String> wpids,String wtId, Date beginDate, Date endDate) {
		if (!wpids.isEmpty()) {
			for (String wpid : wpids) {
				// 获得复位及时率、状态转换及时率数据
				Map<String, Statetransitionrates> stmap = statetransitionratesService.getStatetransitionratesByWtTotal(wpid, beginDate, endDate, 1);
				// 获得消缺及时率数据
				Map<String, FaultEliminateDefects> fdmap = faultEliminateDefectsService.getFaultEliminateDefectsByWtTotal(wpid, beginDate, endDate, 1);

				List<Windturbineinfoday3> ls =windturbineinfoday3Service.getWindturbineinfoday3(wpid,wtId, beginDate, endDate) ;

				Map<String, Windturbine> wtmap = InitialRunner.wtmap;

				if (!ls.isEmpty()) {
					for (Windturbineinfoday3 wd : ls) {

						if (bmmap.containsKey(wd.getWindturbineid())) {
							BenchMarkVo bm = bmmap.get(wd.getWindturbineid());
							bm.setScadafdl(bm.getScadafdl() + wd.getDaydl2());
							bm.setGzss(bm.getGzss() + wd.getDaynhgzssdl());
							bm.setWhss(bm.getWhss() + wd.getDaynhwhssdl());
							bm.setQfss(bm.getQfss() + wd.getDaynhqfdl());
							bm.setXdss(bm.getXdss() + wd.getDaynhxdssdl());
							bmmap.put(wd.getWindturbineid(), bm);
						} else {
							BenchMarkVo bm = new BenchMarkVo();
							intialBenchMarkTemp(bm);
							bm.setScadafdl(bm.getScadafdl() + wd.getDaydl2());
							bm.setGzss(bm.getGzss() + wd.getDaynhgzssdl());
							bm.setWhss(bm.getWhss() + wd.getDaynhwhssdl());
							bm.setQfss(bm.getQfss() + wd.getDaynhqfdl());
							bm.setXdss(bm.getXdss() + wd.getDaynhxdssdl());
							if (wtmap.containsKey(wd.getWindturbineid())) {
								Windturbine wt = wtmap.get(wd.getWindturbineid());
								// bm.setWtName(wt.getName());
								bm.setWtName(wt.getCode());
							}
							bm.setWtId(wd.getWindturbineid());

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

								Double temp1 = st.getTimerate1();// 小于3米5分钟转换及时次数
								Double temp2 = st.getTimerate13();// 小于3米状态转换总次数
								bm.setZtcs(bm.getZtcs() + temp1.intValue());
								bm.setZtzcs(bm.getZtzcs() + temp2.intValue());
								temp1 = st.getTimerate16();// 5分钟复位及时次数
								temp2 = st.getTimerate20();// 总复位次数
								bm.setFwcs(bm.getFwcs() + temp1.intValue());
								bm.setFwzcs(bm.getFwzcs() + temp2.intValue());
							}
							if (fdmap.containsKey(wd.getWindturbineid())) {
								FaultEliminateDefects fd = fdmap.get(wd.getWindturbineid());

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

								bm.setXqcs(bm.getXqcs() + temp1.intValue());
								bm.setXqzcs(bm.getXqzcs() + temp2.intValue());

							}
							bmmap.put(wd.getWindturbineid(), bm);

						}

					}
				}
			}
		}
	}
	private void intialBenchMarkTemp(BenchMarkVo bm) {
		bm.setScadafdl(0.0);
		bm.setGzss(0.0);
		bm.setWhss(0.0);
		bm.setQfss(0.0);
		bm.setXdss(0.0);
		bm.setSlss(0.0);
		bm.setFwcs(0);
		bm.setFwzcs(0);
		bm.setZtcs(0);
		bm.setZtzcs(0);
		bm.setXqcs(0);
		bm.setXqzcs(0);
	}

	private void totalBenchmarkInfo(BenchMarkVo bm) {
		double scadafdl = bm.getScadafdl(); // scada发电量
		double qfss = bm.getQfss();// 欠发损失发电量
		double gzss = bm.getGzss();// 故障损失发电量
		double whss = bm.getWhss();// 维护损失发电量
		double xdss = bm.getXdss();// 限电损失发电量
		double llfdl = scadafdl + qfss + gzss + whss + xdss;// 理论发电量

		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 弃风率
			bm.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 性能损失率
			bm.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 故障损失率
			bm.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 检修损失率
			bm.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 受累损失率
			bm.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 风能利用率
			bm.setFnlyl(temp);
			// 受累损失电量
			bm.setSlssl(0.0);
		} else {
			// temp 风能利用率
			bm.setFnlyl(0.0);
			// temp 故障损失率
			bm.setGzssl(0.0);
			// temp 检修损失率
			bm.setJxssl(0.0);
			// temp 弃风率
			bm.setQfl(0.0);
			// temp 受累损失率
			bm.setSlssl(0.0);
			// 受累损失电量
			bm.setSlssl(0.0);
			// temp 性能损失率
			bm.setXnssl(0.0);
		}

		if (bm.getFwzcs() != 0) {

			double temp1 = bm.getFwcs();// 5分钟复位及时次数
			double temp2 = bm.getFwzcs();// 总复位次数

			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 复位及时率
			bm.setFwjsl(temp);
		} else {
			bm.setFwjsl(100.0);
		}

		if (bm.getZtzcs() != 0) {

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

			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 状态转换及时率
			bm.setZtzhjsl(temp);
		} else {
			bm.setZtzhjsl(100.0);
		}

		if (bm.getXqzcs() != 0) {

			double temp1 = bm.getXqcs();// 消缺及时次数
			double temp2 = bm.getXqzcs();// 故障总次数

			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 消缺及时率
			bm.setXqjsl(temp);
		} else {
			bm.setXqjsl(100.0);
		}
	}


	public List<BenchMarkVo> benchmarkWpInWtList( String wpId,String wtId, String beginDate, String endDate) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		Date begin=DateUtils.parseDate(beginDate);
		Date end=DateUtils.parseDate(endDate);
		// 获得复位及时率、状态转换及时率数据
		Map<String, Map<String, Statetransitionrates>> stmap = statetransitionratesService.getStatetransitionratesByWt(wpId, begin, end);
		// 获得消缺及时率数据
		Map<String, Map<String, FaultEliminateDefects>> fdmap = faultEliminateDefectsService.getFaultEliminateDefectsByWt(wpId, begin, end);


		List<Windturbineinfoday3> ls =windturbineinfoday3Service.getWindturbineinfoday3(wpId,wtId, begin, end) ;

		Map<String, Windturbine> wtmap = InitialRunner.wtmap;
		List<BenchMarkVo> vos=new ArrayList<>();
		if (!ls.isEmpty()) {

			String name = null;

			for (Windturbineinfoday3 wd : ls) {
				if (wtmap.containsKey(wd.getWindturbineid())) {
					Windturbine wp = wtmap.get(wd.getWindturbineid());
					// name = wp.getName();
					name = wp.getCode();

				}
				BenchMarkVo vo = new BenchMarkVo();
				vo.setWtId(wd.getWindturbineid());
				vo.setWtName(name);
				vo.setRecordDate(DateUtils.truncate(wd.getRecorddate()));
				double scadafdl = null != wd.getDaydl2() ? wd.getDaydl2() : 0; // scada发电量
				double fdl = null != wd.getDaydl2() ? wd.getDaydl2() : 0; // 发电量

				double qfss = null != wd.getDaynhqfdl() ? wd.getDaynhqfdl() : 0;// 欠发损失发电量
				double gzss = null != wd.getDaynhgzssdl() ? wd.getDaynhgzssdl() : 0;// 故障损失发电量
				double whss = null != wd.getDaynhwhssdl() ? wd.getDaynhwhssdl() : 0;// 维护损失发电量
				double xdss = null != wd.getDaynhxdssdl() ? wd.getDaynhxdssdl() : 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.setQfl(0.0);
					// temp 故障损失率
					vo.setGzssl(0.0);
					// temp 检修损失率
					vo.setJxssl(0.0);
					// temp 受累损失率
					vo.setSlssl(0.0);
					// temp 风能利用率
					vo.setFnlyl(0.0);
					// temp 性能损失率
					vo.setXnssl(0.0);
				}
				String day = df.format(wd.getRecorddate());
				if (stmap.containsKey(wd.getWindturbineid())) {
					Map<String, Statetransitionrates> smap = stmap.get(wd.getWindturbineid());
					if (smap.containsKey(day)) {
						Statetransitionrates st = smap.get(day);

						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.getWindturbineid())) {
					Map<String, FaultEliminateDefects> fmap = fdmap.get(wd.getWindturbineid());
					if (fmap.containsKey(day)) {
						FaultEliminateDefects fd = fmap.get(day);

						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 Map<String, BenchMarkVo> benchmarkZjInWtList( String wpId,String wtId, String beginDate, String endDate) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Map<String, BenchMarkVo> bmmap=new HashMap<>();
		Date begin=DateUtils.parseDate(beginDate);
		Date end=DateUtils.parseDate(endDate);
		// 获得复位及时率、状态转换及时率数据
		Map<String, Statetransitionrates> stmap = statetransitionratesService.getStatetransitionratesByWtTotal(wpId, begin, end, 1);
		// 获得消缺及时率数据
		Map<String, FaultEliminateDefects> fdmap = faultEliminateDefectsService.getFaultEliminateDefectsByWtTotal(wpId, begin, end, 1);


		List<Windturbineinfoday3> ls =windturbineinfoday3Service.getWindturbineinfoday3(wpId,wtId, begin, end) ;

		Map<String, Windturbine> wtmap = InitialRunner.wtmap;

		if (!ls.isEmpty()) {
			for (Windturbineinfoday3 wd : ls) {

				if (bmmap.containsKey(wd.getWindturbineid())) {
					BenchMarkVo bm = bmmap.get(wd.getWindturbineid());
					bm.setScadafdl(bm.getScadafdl() + wd.getDaydl2());
					bm.setGzss(bm.getGzss() + wd.getDaynhgzssdl());
					bm.setWhss(bm.getWhss() + wd.getDaynhwhssdl());
					bm.setQfss(bm.getQfss() + wd.getDaynhqfdl());
					bm.setXdss(bm.getXdss() + wd.getDaynhxdssdl());
					bmmap.put(wd.getWindturbineid(), bm);
				} else {
					BenchMarkVo bm = new BenchMarkVo();
					intialBenchMarkTemp(bm);
					bm.setScadafdl(bm.getScadafdl() + wd.getDaydl2());
					bm.setGzss(bm.getGzss() + wd.getDaynhgzssdl());
					bm.setWhss(bm.getWhss() + wd.getDaynhwhssdl());
					bm.setQfss(bm.getQfss() + wd.getDaynhqfdl());
					bm.setXdss(bm.getXdss() + wd.getDaynhxdssdl());
					if (wtmap.containsKey(wd.getWindturbineid())) {
						Windturbine wt = wtmap.get(wd.getWindturbineid());
						// bm.setWtName(wt.getName());
						bm.setWtName(wt.getCode());
					}
					bm.setWtId(wd.getWindturbineid());

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

						Double temp1 = st.getTimerate1();// 小于3米5分钟转换及时次数
						Double temp2 = st.getTimerate13();// 小于3米状态转换总次数
						bm.setZtcs(bm.getZtcs() + temp1.intValue());
						bm.setZtzcs(bm.getZtzcs() + temp2.intValue());
						temp1 = st.getTimerate16();// 5分钟复位及时次数
						temp2 = st.getTimerate20();// 总复位次数
						bm.setFwcs(bm.getFwcs() + temp1.intValue());
						bm.setFwzcs(bm.getFwzcs() + temp2.intValue());
					}
					if (fdmap.containsKey(wd.getWindturbineid())) {
						FaultEliminateDefects fd = fdmap.get(wd.getWindturbineid());

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

						bm.setXqcs(bm.getXqcs() + temp1.intValue());
						bm.setXqzcs(bm.getXqzcs() + temp2.intValue());

					}
					bmmap.put(wd.getWindturbineid(), bm);

				}

			}
		}
		return bmmap;
	}
}
