/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称:
 * 创建日期:2023/6/26
 */
package org.springblade.modules.report;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.Data;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.baseinfo.dict.service.IBaseInfoDictService;
import org.springblade.modules.baseinfo.org.service.IDeptRegionService;
import org.springblade.modules.baseinfo.region.service.IBaseInfoRegionService;
import org.springblade.modules.business.check.order.service.IRtuCheckOrderService;
import org.springblade.modules.report.entity.*;
import org.springblade.modules.report.service.*;
import org.springblade.modules.system.service.IDeptService;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/***
 * Date:2023/6/26
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Data
public class WeekOrgReportBean {

	private static IWeekOrgCountService weekOrgCountService;

	private static IWeekOrgCountService getWeekOrgCountService() {
		if (weekOrgCountService == null) {
			weekOrgCountService = SpringUtil.getBean(IWeekOrgCountService.class);
		}
		return weekOrgCountService;
	}

	private static IWeekOrgCheckOrderService weekOrgCheckOrderService;

	private static IWeekOrgCheckOrderService getWeekOrgCheckOrderService() {
		if (weekOrgCheckOrderService == null) {
			weekOrgCheckOrderService = SpringUtil.getBean(IWeekOrgCheckOrderService.class);
		}
		return weekOrgCheckOrderService;
	}

	private static IWeekOrgCheckOrderDayService weekOrgCheckOrderDayService;

	private static IWeekOrgCheckOrderDayService getWeekOrgCheckOrderDayService() {
		if (weekOrgCheckOrderDayService == null) {
			weekOrgCheckOrderDayService = SpringUtil.getBean(IWeekOrgCheckOrderDayService.class);
		}
		return weekOrgCheckOrderDayService;
	}

	private static IWeekOrgRtuUpService weekOrgRtuUpService;

	private static IWeekOrgRtuUpService getWeekOrgRtuUpService() {
		if (weekOrgRtuUpService == null) {
			weekOrgRtuUpService = SpringUtil.getBean(IWeekOrgRtuUpService.class);
		}
		return weekOrgRtuUpService;
	}


	private static IWeekOrgRtuFailureService weekOrgRtuFailureService;

	private static IWeekOrgRtuFailureService getWeekOrgRtuFailureService() {
		if (weekOrgRtuFailureService == null) {
			weekOrgRtuFailureService = SpringUtil.getBean(IWeekOrgRtuFailureService.class);
		}
		return weekOrgRtuFailureService;
	}


	private static IWeekOrgCheckOrderReportService weekOrgCheckOrderReportService;

	private static IWeekOrgCheckOrderReportService getWeekOrgCheckOrderReportService() {
		if (weekOrgCheckOrderReportService == null) {
			weekOrgCheckOrderReportService = SpringUtil.getBean(IWeekOrgCheckOrderReportService.class);
		}
		return weekOrgCheckOrderReportService;
	}

	private static IWeekOrgEquipmentReplaceService weekOrgEquipmentReplaceService;

	private static IWeekOrgEquipmentReplaceService getWeekOrgEquipmentReplaceService() {
		if (weekOrgEquipmentReplaceService == null) {
			weekOrgEquipmentReplaceService = SpringUtil.getBean(IWeekOrgEquipmentReplaceService.class);
		}
		return weekOrgEquipmentReplaceService;
	}

	private static IDeptService deptService;

	private static IDeptService getDeptService() {
		if (deptService == null) {
			deptService = SpringUtil.getBean(IDeptService.class);
		}
		return deptService;
	}

	private static IDeptRegionService deptRegionService;

	private static IDeptRegionService getDeptRegionService() {
		if (deptRegionService == null) {
			deptRegionService = SpringUtil.getBean(IDeptRegionService.class);
		}
		return deptRegionService;
	}

	private static IBaseInfoRegionService regionService;

	private static IBaseInfoRegionService getRegionService() {
		if (regionService == null) {
			regionService = SpringUtil.getBean(IBaseInfoRegionService.class);
		}
		return regionService;
	}

	private static IRtuCheckOrderService rtuCheckOrderService;

	private static IRtuCheckOrderService getRtuCheckOrderService() {
		if (rtuCheckOrderService == null) {
			rtuCheckOrderService = SpringUtil.getBean(IRtuCheckOrderService.class);
		}
		return rtuCheckOrderService;
	}

	private static IBaseInfoDictService dictService;

	private static IBaseInfoDictService getDictService() {
		if (dictService == null) {
			dictService = SpringUtil.getBean(IBaseInfoDictService.class);
		}
		return dictService;
	}

	/**
	 * 报表副标题
	 *
	 * @param n  模板名称
	 * @param n1 数据集名称
	 * @param p
	 * @return
	 */
	public List<Map<String, Object>> getReportCountTime(String n, String n1, Map<String, Object> p) {
		String id = (String) p.get("id");
		WeekOrgCountInfoEntity weekReportInfoEntity = getWeekOrgCountService().getById(Func.toLong(id));
		List<Map<String, Object>> list = new LinkedList<>();
		Map<String, Object> d = new HashMap<>();
		d.put("reportTitle", weekReportInfoEntity.getReportTitle());
		list.add(d);
		return list;
	}

	/**
	 * 总体统计信息
	 *
	 * @param n
	 * @param n1
	 * @param p
	 * @return
	 */
	public List<Map<String, Object>> getRunStatusCountInfo(String n, String n1, Map<String, Object> p) {
		String id = (String) p.get("id");
		WeekOrgCountInfoEntity weekReportInfoEntity = getWeekOrgCountService().getById(Func.toLong(id));
		List<Map<String, Object>> list = new LinkedList<>();
		Map<String, Object> d = new HashMap<>();
		d.put("rtuCount", weekReportInfoEntity.getRtuCount());
		d.put("checkOrderCount", weekReportInfoEntity.getCheckOrderCount());
		d.put("reportCheckOrderCount", weekReportInfoEntity.getReportCheckOrderCount());
		d.put("warnRtuCount", weekReportInfoEntity.getWarnRtuCount());
		d.put("offlineRtuCount", weekReportInfoEntity.getOfflineRtuCount());
		d.put("othersWarnRtuCount", weekReportInfoEntity.getOthersWarnRtuCount());
		d.put("unconfirmOrderCount", weekReportInfoEntity.getUnconfirmOrderCount());
		d.put("processingOrderCount", weekReportInfoEntity.getProcessingOrderCount());
		list.add(d);
		return list;
	}


	/**
	 * 按盟市统计维修任务信息
	 *
	 * @param n
	 * @param n1
	 * @param p
	 * @return
	 */
	public List<Map<String, Object>> getCheckOrderCount(String n, String n1, Map<String, Object> p) {
		String id = (String) p.get("id");
		List<Map<String, Object>> list = new LinkedList<>();
		LambdaQueryWrapper<WeekOrgCheckOrderInfoEntity> wrapper = Wrappers.<WeekOrgCheckOrderInfoEntity>query().lambda();
		wrapper.eq(WeekOrgCheckOrderInfoEntity::getIsDeleted, 0);
		wrapper.eq(WeekOrgCheckOrderInfoEntity::getReportId, Func.toLong(id));
		wrapper.orderByAsc(WeekOrgCheckOrderInfoEntity::getOrgName);
		List<WeekOrgCheckOrderInfoEntity> orderInfoEntityList = getWeekOrgCheckOrderService().list(wrapper);
		int index = 1;
		for (WeekOrgCheckOrderInfoEntity entity : orderInfoEntityList) {
			Map<String, Object> d = new HashMap<>();
			d.put("index", index);
			d.put("orgName", entity.getOrgName());
			d.put("orderCount", entity.getOrderCount());
			d.put("unconfirmOrderCount", entity.getUnconfirmOrderCount());
			d.put("processingOrderCount", entity.getProcessingOrderCount());
			d.put("reportOrderCount", entity.getReportOrderCount());
			list.add(d);
			index++;
		}
		return list;
	}

	/**
	 * 按盟市统计维修任务信息，图表，柱状图
	 *
	 * @param n
	 * @param n1
	 * @param p
	 * @return
	 */
	public List<Map<String, Object>> getCheckOrderGroupCount(String n, String n1, Map<String, Object> p) {
		String id = (String) p.get("id");
		List<Map<String, Object>> list = new LinkedList<>();
		LambdaQueryWrapper<WeekOrgCheckOrderInfoEntity> wrapper = Wrappers.<WeekOrgCheckOrderInfoEntity>query().lambda();
		wrapper.eq(WeekOrgCheckOrderInfoEntity::getIsDeleted, 0);
		wrapper.eq(WeekOrgCheckOrderInfoEntity::getReportId, Func.toLong(id));
		wrapper.orderByAsc(WeekOrgCheckOrderInfoEntity::getOrgName);
		List<WeekOrgCheckOrderInfoEntity> orderInfoEntityList = getWeekOrgCheckOrderService().list(wrapper);
		for (WeekOrgCheckOrderInfoEntity entity : orderInfoEntityList) {
			Map<String, Object> d = new HashMap<>();
			d.put("orgName", entity.getOrgName());
			d.put("value", entity.getOrderCount());
			d.put("type", "维修任务总量");
			list.add(d);
			d = new HashMap<>();
			d.put("orgName", entity.getOrgName());
			d.put("value", entity.getReportOrderCount());
			d.put("type", "周处理量");
			list.add(d);
		}
		return list;
	}

	/**
	 * 按天统计维修任务
	 *
	 * @param n
	 * @param n1
	 * @param p
	 * @return
	 */
	public List<Map<String, Object>> getCheckOrderDayLineCount(String n, String n1, Map<String, Object> p) {
		String id = (String) p.get("id");
		WeekOrgCountInfoEntity weekReportInfoEntity = getWeekOrgCountService().getById(Func.toLong(id));
		LambdaQueryWrapper<WeekOrgCheckOrderDayInfoEntity> wrapper = Wrappers.<WeekOrgCheckOrderDayInfoEntity>query().lambda();
		wrapper.eq(WeekOrgCheckOrderDayInfoEntity::getIsDeleted, 0);
		wrapper.ge(WeekOrgCheckOrderDayInfoEntity::getCountTime, weekReportInfoEntity.getReportStartTime());
		wrapper.lt(WeekOrgCheckOrderDayInfoEntity::getCountTime, weekReportInfoEntity.getReportEndTime());
		wrapper.orderByAsc(WeekOrgCheckOrderDayInfoEntity::getCountTime);
		List<WeekOrgCheckOrderDayInfoEntity> orderInfoEntityList = getWeekOrgCheckOrderDayService().list(wrapper);
		List<Map<String, Object>> list = new LinkedList<>();
		for (WeekOrgCheckOrderDayInfoEntity entity : orderInfoEntityList) {
			Map<String, Object> d = new HashMap<>();
			d.put("week", entity.getWeekName());
			d.put("value", entity.getOrderCount());
			d.put("type", "维修任务总量");
			list.add(d);
			d = new HashMap<>();
			d.put("week", entity.getWeekName());
			d.put("type", "完成量");
			d.put("value", entity.getReportOrderCount());
			list.add(d);
		}
		return list;
	}

	/***
	 * 测站上报率统计
	 * @param n
	 * @param n1
	 * @param p
	 * @return
	 */
	public List<Map<String, Object>> getRtuUpTableCount(String n, String n1, Map<String, Object> p) {
		String id = (String) p.get("id");
		List<Map<String, Object>> list = new LinkedList<>();
		LambdaQueryWrapper<WeekOrgRtuUpInfoEntity> wrapper = Wrappers.<WeekOrgRtuUpInfoEntity>query().lambda();
		wrapper.eq(WeekOrgRtuUpInfoEntity::getIsDeleted, 0);
		wrapper.eq(WeekOrgRtuUpInfoEntity::getReportId, Func.toLong(id));
		wrapper.orderByAsc(WeekOrgRtuUpInfoEntity::getOrgName);
		List<WeekOrgRtuUpInfoEntity> orderInfoEntityList = getWeekOrgRtuUpService().list(wrapper);
		int index = 1;
		for (WeekOrgRtuUpInfoEntity entity : orderInfoEntityList) {
			Map<String, Object> d = new HashMap<>();
			d.put("index", index);
			d.put("orgName", entity.getOrgName());
			d.put("rtuCount", entity.getRtuCount());
			d.put("upCount", entity.getUpCount());
			if (null != entity.getUpCount() && entity.getUpCount() > 0) {
				d.put("percent", String.format("%.2f", ((float) entity.getUpCount() / (float) entity.getRtuCount()) * 100.0)+"%");
			} else {
				d.put("percent", "0%");
			}
			list.add(d);
			index += 1;
		}
		return list;
	}

	/**
	 * 测站上报，图表，柱状图
	 *
	 * @param n
	 * @param n1
	 * @param p
	 * @return
	 */
	public List<Map<String, Object>> getRtuUpGroupCount(String n, String n1, Map<String, Object> p) {
		String id = (String) p.get("id");
		LambdaQueryWrapper<WeekOrgRtuUpInfoEntity> wrapper = Wrappers.<WeekOrgRtuUpInfoEntity>query().lambda();
		wrapper.eq(WeekOrgRtuUpInfoEntity::getIsDeleted, 0);
		wrapper.eq(WeekOrgRtuUpInfoEntity::getReportId, Func.toLong(id));
		wrapper.orderByAsc(WeekOrgRtuUpInfoEntity::getOrgName);
		List<WeekOrgRtuUpInfoEntity> orderInfoEntityList = getWeekOrgRtuUpService().list(wrapper);
		List<Map<String, Object>> list = new LinkedList<>();
		for (WeekOrgRtuUpInfoEntity entity : orderInfoEntityList) {
			Map<String, Object> d = new HashMap<>();
			d.put("orgName", entity.getOrgName());
			d.put("value", entity.getRtuCount());
			d.put("type", "测站数量");
			list.add(d);
			d = new HashMap<>();
			d.put("orgName", entity.getOrgName());
			d.put("value", entity.getUpCount());
			d.put("type", "上报测站数量");
			list.add(d);
		}
		return list;
	}

	/**
	 * 测站故障分类统计
	 *
	 * @param n
	 * @param n1
	 * @param p
	 * @return
	 */
	public List<Map<String, Object>> getRtuFailureCount(String n, String n1, Map<String, Object> p) {
		List<Map<String, Object>> list = new LinkedList<>();
		String id = (String) p.get("id");
		LambdaQueryWrapper<WeekOrgRtuFailureInfoEntity> wrapper = Wrappers.<WeekOrgRtuFailureInfoEntity>query().lambda();
		wrapper.eq(WeekOrgRtuFailureInfoEntity::getIsDeleted, 0);
		wrapper.eq(WeekOrgRtuFailureInfoEntity::getReportId, Func.toLong(id));
		wrapper.orderByAsc(WeekOrgRtuFailureInfoEntity::getEquipmentName);
		List<WeekOrgRtuFailureInfoEntity> orderInfoEntityList = getWeekOrgRtuFailureService().list(wrapper);
		for (WeekOrgRtuFailureInfoEntity entity : orderInfoEntityList) {
			Map<String, Object> d = new HashMap<>();
			d.put("equipmentName", entity.getEquipmentName());
			d.put("failureCount", entity.getFailureCount());
			list.add(d);
		}
		if (list.size() == 0) {
			Map<String, Object> d = new HashMap<>();
			d.put("equipmentName", "RTU");
			d.put("failureCount", 0);
			list.add(d);
			d = new HashMap<>();
			d.put("equipmentName", "雨量计");
			d.put("failureCount", 0);
			list.add(d);
			d = new HashMap<>();
			d.put("equipmentName", "水位计");
			d.put("failureCount", 0);
			list.add(d);
			d = new HashMap<>();
			d.put("equipmentName", "墒情计");
			d.put("failureCount", 0);
			list.add(d);
			d = new HashMap<>();
			d.put("equipmentName", "蓄电池");
			d.put("failureCount", 0);
			list.add(d);
			d = new HashMap<>();
			d.put("equipmentName", "太阳能板");
			d.put("failureCount", 0);
			list.add(d);
			d = new HashMap<>();
			d.put("equipmentName", "物联网卡欠费");
			d.put("failureCount", 0);
			list.add(d);
			d = new HashMap<>();
			d.put("equipmentName", "网络信号故障");
			d.put("failureCount", 0);
			list.add(d);
		}
		return list;
	}

	/**
	 * 维修站点清单
	 *
	 * @param n
	 * @param n1
	 * @param p
	 * @return
	 */
	public List<Map<String, Object>> getRtuOrderProcessTableCount(String n, String n1, Map<String, Object> p) {
		List<Map<String, Object>> list = new LinkedList<>();
		String id = (String) p.get("id");
		LambdaQueryWrapper<WeekOrgCheckOrderReportInfoEntity> wrapper = Wrappers.<WeekOrgCheckOrderReportInfoEntity>query().lambda();
		wrapper.eq(WeekOrgCheckOrderReportInfoEntity::getIsDeleted, 0);
		wrapper.eq(WeekOrgCheckOrderReportInfoEntity::getReportId, Func.toLong(id));
		wrapper.orderByAsc(WeekOrgCheckOrderReportInfoEntity::getOrgName);
		List<WeekOrgCheckOrderReportInfoEntity> orderInfoEntityList = getWeekOrgCheckOrderReportService().list(wrapper);
		int index = 1;
		for (WeekOrgCheckOrderReportInfoEntity entity : orderInfoEntityList) {
			Map<String, Object> d = new HashMap<>();
			d.put("index", index);
			d.put("orgName", entity.getOrgName());
			d.put("rtuName", entity.getRtuName());
			d.put("warnInfo", entity.getWarnInfo());
			d.put("processDesc", entity.getProcessDesc());
			list.add(d);
			index += 1;
		}
		return list;
	}

	/**
	 * 设备更换清单
	 *
	 * @param n
	 * @param n1
	 * @param p
	 * @return
	 */
	public List<Map<String, Object>> getRtuEquiomentReplaceTableCount(String n, String n1, Map<String, Object> p) {
		List<Map<String, Object>> list = new LinkedList<>();
		String id = (String) p.get("id");
		LambdaQueryWrapper<WeekOrgEquipmentReplaceInfoEntity> wrapper = Wrappers.<WeekOrgEquipmentReplaceInfoEntity>query().lambda();
		wrapper.eq(WeekOrgEquipmentReplaceInfoEntity::getIsDeleted, 0);
		wrapper.eq(WeekOrgEquipmentReplaceInfoEntity::getReportId, Func.toLong(id));
		wrapper.orderByAsc(WeekOrgEquipmentReplaceInfoEntity::getOrgName);
		List<WeekOrgEquipmentReplaceInfoEntity> orderInfoEntityList = getWeekOrgEquipmentReplaceService().list(wrapper);
		int index = 1;
		for (WeekOrgEquipmentReplaceInfoEntity entity : orderInfoEntityList) {
			Map<String, Object> d = new HashMap<>();
			d.put("index", index);
			d.put("orgName", entity.getOrgName());
			d.put("equipmentName", entity.getEquipmentName());
			d.put("replaceCount", entity.getReplaceCount());
			list.add(d);
			index += 1;
		}
		return list;
	}


}
