/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 山洪灾害运维系统
 * 创建日期:2023/8/7
 */
package org.springblade.jobtask;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.glassfish.jersey.internal.guava.ThreadFactoryBuilder;
import org.springblade.constant.BusinessConstant;
import org.springblade.constant.DictBusinessConstant;
import org.springblade.core.tool.utils.Func;
import org.springblade.enums.OrderCloseEnum;
import org.springblade.enums.OrderConfirmEnum;
import org.springblade.enums.OrderStatusEnum;
import org.springblade.enums.WarnKindEnum;
import org.springblade.modules.baseinfo.dict.entity.DictInfoEntity;
import org.springblade.modules.baseinfo.dict.service.IBaseInfoDictService;
import org.springblade.modules.baseinfo.org.entity.DeptRegionEntity;
import org.springblade.modules.baseinfo.org.service.IDeptRegionService;
import org.springblade.modules.baseinfo.region.entity.RegionInfoEntity;
import org.springblade.modules.baseinfo.region.service.IBaseInfoRegionService;
import org.springblade.modules.baseinfo.rtu.dto.RtuInfoDTO;
import org.springblade.modules.baseinfo.rtu.service.IRtuBaseInfoService;
import org.springblade.modules.business.check.order.dto.CheckOrderInfoDTO;
import org.springblade.modules.business.check.order.service.IRtuCheckOrderService;
import org.springblade.modules.business.check.process.service.IRtuOrderProcessService;
import org.springblade.modules.business.check.process.vo.OrderProcessInfoVO;
import org.springblade.modules.business.rtumanage.dto.RtuStatusDTO;
import org.springblade.modules.business.rtumanage.service.IRtuManageService;
import org.springblade.modules.business.warning.dto.RtuWarningInfoDTO;
import org.springblade.modules.business.warning.entity.RtuWarningInfoEntity;
import org.springblade.modules.business.warning.service.IRtuWarningService;
import org.springblade.modules.report.entity.*;
import org.springblade.modules.report.service.*;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;

/***
 * Date:2023/8/7
 * Title:报表统计
 * Description：按自治区进行周报统计、按盟市统计维修任务信息
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
@Component
@EnableScheduling
public class ReportCountProcessor {
	private static ThreadFactory reportCountThreadFactory = new ThreadFactoryBuilder().setNameFormat("report-count-thread-pool-%d").build();
	private static ExecutorService reportCountThreadPool = new ThreadPoolExecutor(1, 1,
		0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1024), reportCountThreadFactory, new ThreadPoolExecutor.AbortPolicy());


	@Resource
	private IWeekOrgCountService weekReportService;

	@Resource
	private IWeekOrgRtuFailureService weekOrgRtuFailureService;

	@Resource
	private IWeekOrgCheckOrderService weekOrgCheckOrderService;

	@Resource
	private IWeekOrgRtuUpService weekOrgRtuUpService;

	@Resource
	private IWeekOrgCheckOrderReportService weekOrgCheckOrderReportService;

	@Resource
	private IWeekOrgEquipmentReplaceService weekOrgEquipmentReplaceService;

	@Resource
	private IWeekOrgCheckOrderDayService orgCheckOrderDayService;

	@Resource
	private IRtuBaseInfoService rtuBaseInfoService;

	@Resource
	private IRtuCheckOrderService checkOrderService;

	@Resource
	private IRtuOrderProcessService orderProcessService;

	@Resource
	private IRtuWarningService rtuWarningService;

	@Resource
	private IBaseInfoDictService dictService;

	@Resource
	private IDeptRegionService deptRegionService;

	@Resource
	private IBaseInfoRegionService regionService;

	@Resource
	private IDeptService deptService;

	@Resource
	private IRtuManageService rtuManageService;

	@Value("${spring.task-config.report-task}")
	private boolean reportTaskRun;

//	@Scheduled(cron = "10 0 0 * * MON")
	public void reportWeekOrgCountTask() {
		if (reportTaskRun) {
			Date dt = new Date();
			log.info("周报表统计任务创建 {}", Func.formatDateTime(dt));
			WeekOrgCountTask task = new WeekOrgCountTask(dt);
			FutureTask<Integer> futureTask = new FutureTask<>(task);
			reportCountThreadPool.execute(futureTask);
		}
	}

//	@Scheduled(cron = "20 0 0 0/1 * *")
	public void reportWeekOrgOrderDayCountTask() {
		if (reportTaskRun) {
			Date dt = new Date();
			log.info("盟市按日维修任务数据统计任务创建 {}", Func.formatDateTime(dt));
			WeekOrgDayCountTask task = new WeekOrgDayCountTask(dt);
			FutureTask<Integer> futureTask = new FutureTask<>(task);
			reportCountThreadPool.execute(futureTask);
		}
	}

//	@Scheduled(cron = "30 0 0 1 * * ")
	public void reportMonthCountTask() {
		try {
			if (reportTaskRun) {
				Date dt = new Date();
				log.info("月统计任务创建 {}", Func.formatDateTime(dt));
			}
		} catch (Exception e) {
			log.error("{}", e.getMessage());
		}
	}

	private class WeekOrgDayCountTask implements Callable<Integer> {
		private Date reportTime;

		public WeekOrgDayCountTask(Date reportTime) {
			this.reportTime = reportTime;
		}

		@Override
		public Integer call() throws Exception {
			try {
				log.info("盟市周统计，按天统计维修任务 {} {}", Func.formatDateTime(reportTime), Func.formatDateTime(new Date()));
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(reportTime);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				calendar.add(Calendar.DAY_OF_MONTH, -1);
				log.info("统计日期  {}", Func.formatDateTime(calendar.getTime()));
				LambdaQueryWrapper<WeekOrgCheckOrderDayInfoEntity> wrapper = Wrappers.<WeekOrgCheckOrderDayInfoEntity>query().lambda();
				wrapper.eq(WeekOrgCheckOrderDayInfoEntity::getIsDeleted, 0);
				wrapper.eq(WeekOrgCheckOrderDayInfoEntity::getCountTime, calendar.getTime());
				long size = orgCheckOrderDayService.count(wrapper);
				if (size == 0) {
					calendar.setTime(reportTime);
					calendar.set(Calendar.HOUR_OF_DAY, 0);
					calendar.set(Calendar.MINUTE, 0);
					calendar.set(Calendar.SECOND, 0);
					calendar.set(Calendar.MILLISECOND, 0);
					Date endTm = calendar.getTime();
					calendar.add(Calendar.DAY_OF_MONTH, -1);
					int day = calendar.get(Calendar.DAY_OF_WEEK);
					String weekName = "";
					if (1 == day) {
						weekName = "周日";
					} else if (2 == day) {
						weekName = "周一";
					} else if (3 == day) {
						weekName = "周二";
					} else if (4 == day) {
						weekName = "周三";
					} else if (5 == day) {
						weekName = "周四";
					} else if (6 == day) {
						weekName = "周五";
					} else if (7 == day) {
						weekName = "周六";
					}
					Date startTm = calendar.getTime();
					log.info("统计日期区间  {} {}", Func.formatDateTime(startTm), Func.formatDateTime(endTm));
					String adCode = BusinessConstant.REGION_NM_ADCODE;
					WeekOrgCheckOrderDayInfoEntity dayInfoEntity = new WeekOrgCheckOrderDayInfoEntity();
					dayInfoEntity.setCountTime(calendar.getTime());
					dayInfoEntity.setWeekName(weekName);
					//任务总数
					CheckOrderInfoDTO checkOrderInfoDTO = new CheckOrderInfoDTO();
					checkOrderInfoDTO.setOrderClose(OrderCloseEnum.ACTIVE_OPEN.getCode());
					checkOrderInfoDTO.setAdCode(adCode);
					checkOrderInfoDTO.setReportEndDate(endTm);
					long count = checkOrderService.orderCount(checkOrderInfoDTO);
					dayInfoEntity.setOrderCount((int) count);

					//未确认任务
					checkOrderInfoDTO.setOrderConfirm(OrderConfirmEnum.ACTIVE_CREATE.getCode());
					checkOrderInfoDTO.setReportEndDate(endTm);
					count = checkOrderService.orderCount(checkOrderInfoDTO);
					dayInfoEntity.setUnconfirmOrderCount((int) count);

					//处理中任务
					checkOrderInfoDTO.setOrderConfirm(OrderConfirmEnum.ACTIVE_CONFIRM.getCode());
					checkOrderInfoDTO.setOrderClose(OrderCloseEnum.ACTIVE_OPEN.getCode());
					checkOrderInfoDTO.setReportEndDate(endTm);
					DictInfoEntity biz = dictService.getDict(DictBusinessConstant.ORDER_STATUS_CODE, OrderStatusEnum.STATUS_CONFIRM.getCode());
					if (biz != null) {
						checkOrderInfoDTO.setOrderStatusKey(biz.getId());
					}
					long unCloseOrderCount = checkOrderService.unCloseOrderCount(checkOrderInfoDTO);
					dayInfoEntity.setProcessingOrderCount((int) unCloseOrderCount);

					//已处理维修任务数量
					checkOrderInfoDTO.setOrderConfirm(OrderConfirmEnum.ACTIVE_CONFIRM.getCode());
					checkOrderInfoDTO.setOrderClose(OrderCloseEnum.ACTIVE_OPEN.getCode());
					checkOrderInfoDTO.setReportStartDate(startTm);
					checkOrderInfoDTO.setReportEndDate(endTm);
					DictInfoEntity orderProcessDict = dictService.getDict(DictBusinessConstant.ORDER_STATUS_CODE, OrderStatusEnum.STATUS_PROCESS.getCode());
					if (orderProcessDict != null) {
						checkOrderInfoDTO.setOrderStatusKey(orderProcessDict.getId());
					}
					long reports = checkOrderService.reportCount(checkOrderInfoDTO);
					dayInfoEntity.setReportOrderCount((int) reports);

					orgCheckOrderDayService.save(dayInfoEntity);
				}

			} catch (Exception e) {
				e.printStackTrace();
				log.error(e.getMessage());
			} finally {
				log.info("盟市周统计，按天统计维修任务，运行完成 {}", Func.formatDateTime(new Date()));
			}
			return 0;
		}
	}

	private class WeekOrgCountTask implements Callable<Integer> {

		private Date reportTime;

		public WeekOrgCountTask(Date reportTime) {
			this.reportTime = reportTime;
		}

		@Override
		public Integer call() throws Exception {
			try {
				log.info("盟市周统计 {} {}", Func.formatDateTime(reportTime), Func.formatDateTime(new Date()));
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(reportTime);
				calendar.add(Calendar.WEEK_OF_MONTH, 0);
				calendar.set(Calendar.DAY_OF_WEEK, 2);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				//统计第几周
				int week = calendar.get(Calendar.WEEK_OF_MONTH);
				if (week == 1) {
					week = 4;
				} else {
					week = week - 1;
				}
				LambdaQueryWrapper<WeekOrgCountInfoEntity> wrapper = Wrappers.<WeekOrgCountInfoEntity>query().lambda();
				wrapper.eq(WeekOrgCountInfoEntity::getIsDeleted, 0);
				wrapper.eq(WeekOrgCountInfoEntity::getReportTime, calendar.getTime());
				List<WeekOrgCountInfoEntity> weekReportInfoEntityList = weekReportService.list(wrapper);
				if (null == weekReportInfoEntityList || weekReportInfoEntityList.size() == 0) {
					String adCode = BusinessConstant.REGION_NM_ADCODE;
					//生成周报主记录
					WeekOrgCountInfoEntity entity = new WeekOrgCountInfoEntity();
					entity.setName("weekorgcount.ureport.xml");
					entity.setReportTime(calendar.getTime());
					SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy年MM月dd日");
					calendar.setTime(reportTime);
					calendar.add(Calendar.WEEK_OF_MONTH, -1);
					calendar.set(Calendar.DAY_OF_WEEK, 2);
					calendar.set(Calendar.HOUR_OF_DAY, 0);
					calendar.set(Calendar.MINUTE, 0);
					calendar.set(Calendar.SECOND, 0);
					calendar.set(Calendar.MILLISECOND, 0);
					entity.setReportStartTime(calendar.getTime());
					String weekTm1 = timeFormat.format(calendar.getTime());
					calendar.add(Calendar.DAY_OF_MONTH, 6);
					String weekTm2 = timeFormat.format(calendar.getTime());
					calendar.add(Calendar.DAY_OF_MONTH, 1);
					entity.setReportEndTime(calendar.getTime());
					entity.setReportTitle("统计时间：" + weekTm1 + "-" + weekTm2 + "第" + week + "周");
					//测站总数
					RtuInfoDTO rtuInfoDTO = new RtuInfoDTO();
					rtuInfoDTO.setAdCode(adCode);
					long rtuCount = rtuBaseInfoService.rtuCount(rtuInfoDTO);
					entity.setRtuCount((int) rtuCount);
					//任务总数
					CheckOrderInfoDTO checkOrderInfoDTO = new CheckOrderInfoDTO();
					checkOrderInfoDTO.setAdCode(adCode);
					checkOrderInfoDTO.setReportEndDate(entity.getReportEndTime());
					checkOrderInfoDTO.setOrderClose(OrderCloseEnum.ACTIVE_OPEN.getCode());
					long count = checkOrderService.orderCount(checkOrderInfoDTO);
					entity.setCheckOrderCount((int) count);
					//未确认任务
					checkOrderInfoDTO = new CheckOrderInfoDTO();
					checkOrderInfoDTO.setAdCode(adCode);
					checkOrderInfoDTO.setReportEndDate(entity.getReportEndTime());
					checkOrderInfoDTO.setOrderConfirm(OrderConfirmEnum.ACTIVE_CREATE.getCode());
					count = checkOrderService.orderCount(checkOrderInfoDTO);
					entity.setUnconfirmOrderCount((int) count);
					//处理中任务
					checkOrderInfoDTO = new CheckOrderInfoDTO();
					checkOrderInfoDTO.setAdCode(adCode);
					checkOrderInfoDTO.setReportEndDate(entity.getReportEndTime());
					checkOrderInfoDTO.setOrderConfirm(OrderConfirmEnum.ACTIVE_CONFIRM.getCode());
					checkOrderInfoDTO.setOrderClose(OrderCloseEnum.ACTIVE_OPEN.getCode());
					DictInfoEntity biz = dictService.getDict(DictBusinessConstant.ORDER_STATUS_CODE, OrderStatusEnum.STATUS_CONFIRM.getCode());
					if (biz != null) {
						checkOrderInfoDTO.setOrderStatusKey(biz.getId());
					}
					count = checkOrderService.orderCount(checkOrderInfoDTO);
					entity.setProcessingOrderCount((int) count);
					//本周已处理维修任务数量
					checkOrderInfoDTO = new CheckOrderInfoDTO();
					checkOrderInfoDTO.setAdCode(adCode);
					checkOrderInfoDTO.setReportStartDate(entity.getReportStartTime());
					checkOrderInfoDTO.setReportEndDate(entity.getReportEndTime());
					count = checkOrderService.orderReportCount(checkOrderInfoDTO);
					entity.setReportCheckOrderCount((int) count);
					//异常测站
					RtuWarningInfoDTO warningInfoDTO = new RtuWarningInfoDTO();
					warningInfoDTO.setAdCode(adCode);
					warningInfoDTO.setEndDate(entity.getReportEndTime());
					long warnCount = rtuWarningService.warningRtuCount(warningInfoDTO);
					entity.setWarnRtuCount((int) warnCount);
					//离线测站
					warningInfoDTO = new RtuWarningInfoDTO();
					warningInfoDTO.setAdCode(adCode);
					warningInfoDTO.setEndDate(entity.getReportEndTime());
					long offlineCount = rtuWarningService.warningOfflineRtuCount(warningInfoDTO);
					entity.setOfflineRtuCount((int) offlineCount);
					//其他异常测站
					entity.setOthersWarnRtuCount((int) (warnCount - offlineCount));
					weekReportService.save(entity);

					//统计一周故障
					Map<String, Integer> failureCountMap = new HashMap<>();
					failureCountMap.put("rtu", 0);
					failureCountMap.put("rainSensor", 0);
					failureCountMap.put("waterLevelSensor", 0);
					failureCountMap.put("groundLevelSensor", 0);
					failureCountMap.put("bt", 0);
					failureCountMap.put("sun", 0);
					failureCountMap.put("net", 0);
					failureCountMap.put("signl", 0);
					checkOrderInfoDTO = new CheckOrderInfoDTO();
					checkOrderInfoDTO.setAdCode(adCode);
					checkOrderInfoDTO.setReportStartDate(entity.getReportStartTime());
					checkOrderInfoDTO.setReportEndDate(entity.getReportEndTime());
					DictInfoEntity orderProcessDict = dictService.getDict(DictBusinessConstant.ORDER_STATUS_CODE, OrderStatusEnum.STATUS_PROCESS.getCode());
					if (orderProcessDict != null) {
						checkOrderInfoDTO.setOrderStatusKey(orderProcessDict.getId());
					}
					List<OrderProcessInfoVO> processInfoVOList = orderProcessService.selectReportList(checkOrderInfoDTO);
					if (null != processInfoVOList && processInfoVOList.size() > 0) {
						for (OrderProcessInfoVO processInfoVO : processInfoVOList) {
							StringBuilder stringBuffer = new StringBuilder();
							if (null != processInfoVO.getRtuStatus() && processInfoVO.getRtuStatus() == 1) {
								stringBuffer.append("站点故障；");
							}
							if (null != processInfoVO.getRtuReplace() && processInfoVO.getRtuReplace() == 1) {
								int v = failureCountMap.get("rtu");
								v += 1;
								failureCountMap.put("rtu", v);
								stringBuffer.append("更换RTU；");
							}
							if (null != processInfoVO.getRainSensorReplace() && processInfoVO.getRainSensorReplace() == 1) {
								int v = failureCountMap.get("rainSensor");
								v += 1;
								failureCountMap.put("rainSensor", v);
								stringBuffer.append("更换雨量计；");
							}
							if (null != processInfoVO.getWaterSensorReplace() && processInfoVO.getWaterSensorReplace() == 1) {
								int v = failureCountMap.get("waterLevelSensor");
								v += 1;
								failureCountMap.put("waterLevelSensor", v);
								stringBuffer.append("更换水位计；");
							}
							if (null != processInfoVO.getGroundSensorReplace() && processInfoVO.getGroundSensorReplace() == 1) {
								int v = failureCountMap.get("groundLevelSensor");
								v += 1;
								failureCountMap.put("groundLevelSensor", v);
								stringBuffer.append("更换墒情计；");
							}
							if (null != processInfoVO.getBatteryReplace() && processInfoVO.getBatteryReplace() == 1) {
								int v = failureCountMap.get("bt");
								v += 1;
								failureCountMap.put("bt", v);
								stringBuffer.append("更换蓄电池；");
							}
							if (null != processInfoVO.getSunPowerReplace() && processInfoVO.getSunPowerReplace() == 1) {
								int v = failureCountMap.get("sun");
								v += 1;
								failureCountMap.put("sun", v);
								stringBuffer.append("更换太阳能板；");
							}
							if (null != processInfoVO.getNetworkOverdue() && processInfoVO.getNetworkOverdue() == 1) {
								int v = failureCountMap.get("net");
								v += 1;
								failureCountMap.put("net", v);
								stringBuffer.append("物联网卡欠费；");
							}
							if (null != processInfoVO.getNetworkStatus() && processInfoVO.getNetworkStatus() == 1) {
								int v = failureCountMap.get("signl");
								v += 1;
								failureCountMap.put("signl", v);
								stringBuffer.append("网络故障；");
							}

							//一周站点维修信息清单
							WeekOrgCheckOrderReportInfoEntity reportInfoEntity = new WeekOrgCheckOrderReportInfoEntity();
							reportInfoEntity.setReportId(entity.getId());
							reportInfoEntity.setOrgName(processInfoVO.getAdCity());
							reportInfoEntity.setRtuName(processInfoVO.getRtuName());
							reportInfoEntity.setOrderId(processInfoVO.getOrderId());
							if (null != processInfoVO.getWarnId()) {
								RtuWarningInfoEntity rtuWarningInfoEntity = rtuWarningService.getById(processInfoVO.getWarnId());
								if (null != rtuWarningInfoEntity) {
									String warnKinds = "";
									if (WarnKindEnum.WARN_CLOCK.getCode() == rtuWarningInfoEntity.getWarningKind()) {
										warnKinds = "测站时钟异常";
									} else if (WarnKindEnum.WARN_UP_MIN_DELAY.getCode() == rtuWarningInfoEntity.getWarningKind()) {
										warnKinds = "5分钟上报延时";
									} else if (WarnKindEnum.WARN_OFFLINE.getCode() == rtuWarningInfoEntity.getWarningKind()) {
										warnKinds = "测站离线";
									} else if (WarnKindEnum.WARN_RAIN_MISS_OUT.getCode() == rtuWarningInfoEntity.getWarningKind()) {
										warnKinds = "雨量站小时报漏报";
									} else if (WarnKindEnum.WARN_WL_MISS_OUT.getCode() == rtuWarningInfoEntity.getWarningKind()) {
										warnKinds = "水位站小时报漏报";
									} else if (WarnKindEnum.WARN_OUTLIER_VALUE.getCode() == rtuWarningInfoEntity.getWarningKind()) {
										warnKinds = "雨量疑似异常值";
									} else if (WarnKindEnum.WARN_RAIN_UP_HOUR_DELAY.getCode() == rtuWarningInfoEntity.getWarningKind()) {
										warnKinds = "雨量站小时上报延时";
									} else if (WarnKindEnum.WARN_WL_UP_HOUR_DELAY.getCode() == rtuWarningInfoEntity.getWarningKind()) {
										warnKinds = "水位站小时上报延时";
									}
									reportInfoEntity.setWarnInfo(warnKinds);
								}
							}
							if (stringBuffer.toString().length() > 0) {
								reportInfoEntity.setProcessDesc(stringBuffer.toString());
							} else {
								reportInfoEntity.setProcessDesc(processInfoVO.getProcessDesc());
							}
							weekOrgCheckOrderReportService.save(reportInfoEntity);
						}
					}
					WeekOrgRtuFailureInfoEntity failureInfoEntity = new WeekOrgRtuFailureInfoEntity();
					failureInfoEntity.setReportId(entity.getId());
					failureInfoEntity.setEquipmentName("RTU");
					failureInfoEntity.setFailureCount(failureCountMap.get("rtu"));
					weekOrgRtuFailureService.save(failureInfoEntity);
					failureInfoEntity = new WeekOrgRtuFailureInfoEntity();
					failureInfoEntity.setReportId(entity.getId());
					failureInfoEntity.setEquipmentName("雨量计");
					failureInfoEntity.setFailureCount(failureCountMap.get("rainSensor"));
					weekOrgRtuFailureService.save(failureInfoEntity);
					failureInfoEntity = new WeekOrgRtuFailureInfoEntity();
					failureInfoEntity.setReportId(entity.getId());
					failureInfoEntity.setEquipmentName("水位计");
					failureInfoEntity.setFailureCount(failureCountMap.get("waterLevelSensor"));
					weekOrgRtuFailureService.save(failureInfoEntity);
					failureInfoEntity = new WeekOrgRtuFailureInfoEntity();
					failureInfoEntity.setReportId(entity.getId());
					failureInfoEntity.setEquipmentName("墒情计");
					failureInfoEntity.setFailureCount(failureCountMap.get("groundLevelSensor"));
					weekOrgRtuFailureService.save(failureInfoEntity);
					failureInfoEntity = new WeekOrgRtuFailureInfoEntity();
					failureInfoEntity.setReportId(entity.getId());
					failureInfoEntity.setEquipmentName("蓄电池");
					failureInfoEntity.setFailureCount(failureCountMap.get("bt"));
					weekOrgRtuFailureService.save(failureInfoEntity);
					failureInfoEntity = new WeekOrgRtuFailureInfoEntity();
					failureInfoEntity.setReportId(entity.getId());
					failureInfoEntity.setEquipmentName("太阳能板");
					failureInfoEntity.setFailureCount(failureCountMap.get("sun"));
					weekOrgRtuFailureService.save(failureInfoEntity);
					failureInfoEntity = new WeekOrgRtuFailureInfoEntity();
					failureInfoEntity.setReportId(entity.getId());
					failureInfoEntity.setEquipmentName("物联网卡欠费");
					failureInfoEntity.setFailureCount(failureCountMap.get("net"));
					weekOrgRtuFailureService.save(failureInfoEntity);
					failureInfoEntity = new WeekOrgRtuFailureInfoEntity();
					failureInfoEntity.setReportId(entity.getId());
					failureInfoEntity.setEquipmentName("网络信号故障");
					failureInfoEntity.setFailureCount(failureCountMap.get("signl"));
					weekOrgRtuFailureService.save(failureInfoEntity);

					//统计维修任务、上报率
					LambdaQueryWrapper<Dept> deptwrapper = Wrappers.<Dept>query().lambda();
					deptwrapper.eq(Dept::getIsDeleted, 0);
					deptwrapper.eq(Dept::getParentId, BusinessConstant.DEPT_XAM_ID);
					List<Dept> root = deptService.list(deptwrapper);
					for (Dept dept : root) {
						WeekOrgCheckOrderInfoEntity weekOrgCheckOrderInfoEntity = new WeekOrgCheckOrderInfoEntity();
						weekOrgCheckOrderInfoEntity.setReportId(entity.getId());
						weekOrgCheckOrderInfoEntity.setOrgName(dept.getDeptName());
						LambdaQueryWrapper<DeptRegionEntity> deptLambdaQueryWrapper = Wrappers.<DeptRegionEntity>query().lambda();
						deptLambdaQueryWrapper.eq(DeptRegionEntity::getDeptId, dept.getId());
						DeptRegionEntity deptRegionEntity = deptRegionService.getOne(deptLambdaQueryWrapper);
						LambdaQueryWrapper<RegionInfoEntity> nodeQueryWrapper = Wrappers.<RegionInfoEntity>query().lambda();
						nodeQueryWrapper.eq(RegionInfoEntity::getAdcd, deptRegionEntity.getAdcd());
						RegionInfoEntity nodeRegion = regionService.getOne(nodeQueryWrapper);
						int excludecode = 0;
						if (nodeRegion.getParentCode().equals(BusinessConstant.REGION_NM_ADCODE)) {
							excludecode = 1;
						}
						//按盟市统计维修任务
						checkOrderInfoDTO = new CheckOrderInfoDTO();
						checkOrderInfoDTO.setAdCode(deptRegionEntity.getAdcd());
						checkOrderInfoDTO.setExcludeCode(excludecode);
						checkOrderInfoDTO.setReportEndDate(entity.getReportEndTime());
						//任务数量
						checkOrderInfoDTO.setOrderClose(OrderCloseEnum.ACTIVE_OPEN.getCode());
						long orderCount = checkOrderService.orderCount(checkOrderInfoDTO);
						weekOrgCheckOrderInfoEntity.setOrderCount((int) orderCount);
						//未确认
						checkOrderInfoDTO = new CheckOrderInfoDTO();
						checkOrderInfoDTO.setAdCode(deptRegionEntity.getAdcd());
						checkOrderInfoDTO.setExcludeCode(excludecode);
						checkOrderInfoDTO.setReportEndDate(entity.getReportEndTime());
						checkOrderInfoDTO.setOrderClose(OrderCloseEnum.ACTIVE_OPEN.getCode());
						checkOrderInfoDTO.setOrderConfirm(OrderConfirmEnum.ACTIVE_CREATE.getCode());
						count = checkOrderService.unconfirmOrderCount(checkOrderInfoDTO);
						weekOrgCheckOrderInfoEntity.setUnconfirmOrderCount((int) count);
						//处理中
						checkOrderInfoDTO = new CheckOrderInfoDTO();
						checkOrderInfoDTO.setAdCode(deptRegionEntity.getAdcd());
						checkOrderInfoDTO.setExcludeCode(excludecode);
						checkOrderInfoDTO.setReportEndDate(entity.getReportEndTime());
						checkOrderInfoDTO.setOrderClose(OrderCloseEnum.ACTIVE_OPEN.getCode());
						checkOrderInfoDTO.setOrderConfirm(OrderConfirmEnum.ACTIVE_CONFIRM.getCode());
						biz = dictService.getDict(DictBusinessConstant.ORDER_STATUS_CODE, OrderStatusEnum.STATUS_CONFIRM.getCode());
						if (biz != null) {
							checkOrderInfoDTO.setOrderStatusKey(biz.getId());
						}
						count = checkOrderService.unCloseOrderCount(checkOrderInfoDTO);
						weekOrgCheckOrderInfoEntity.setProcessingOrderCount((int) count);
						//已处理
						checkOrderInfoDTO = new CheckOrderInfoDTO();
						checkOrderInfoDTO.setAdCode(deptRegionEntity.getAdcd());
						checkOrderInfoDTO.setExcludeCode(excludecode);
						checkOrderInfoDTO.setReportStartDate(entity.getReportStartTime());
						checkOrderInfoDTO.setReportEndDate(entity.getReportEndTime());
						long reportCount = checkOrderService.orderReportCount(checkOrderInfoDTO);
						weekOrgCheckOrderInfoEntity.setReportOrderCount((int) reportCount);
						weekOrgCheckOrderService.save(weekOrgCheckOrderInfoEntity);

						//盟市上报率统计
						WeekOrgRtuUpInfoEntity weekOrgRtuUpInfoEntity = new WeekOrgRtuUpInfoEntity();
						weekOrgRtuUpInfoEntity.setReportId(entity.getId());
						weekOrgRtuUpInfoEntity.setOrgName(dept.getDeptName());
						rtuInfoDTO = new RtuInfoDTO();
						rtuInfoDTO.setAdCode(deptRegionEntity.getAdcd());
						rtuInfoDTO.setExcludeCode(excludecode);
						count = rtuBaseInfoService.rtuCount(rtuInfoDTO);
						weekOrgRtuUpInfoEntity.setRtuCount(count);
						RtuStatusDTO rtuStatusDTO = new RtuStatusDTO();
						rtuStatusDTO.setAdCode(deptRegionEntity.getAdcd());
						rtuStatusDTO.setExcludeCode(excludecode);
						rtuStatusDTO.setBeginTime(entity.getReportStartTime());
						rtuStatusDTO.setEndTime(entity.getReportEndTime());
						count = rtuManageService.rtuUpDataCount(rtuStatusDTO);
						weekOrgRtuUpInfoEntity.setUpCount(count);
						weekOrgRtuUpService.save(weekOrgRtuUpInfoEntity);

						//一周站点设备更换信息
						Map<String, Integer> orgEquipmentReplace = new HashMap<>();
						orgEquipmentReplace.put("rtu", 0);
						orgEquipmentReplace.put("rainSensor", 0);
						orgEquipmentReplace.put("waterLevelSensor", 0);
						orgEquipmentReplace.put("groundLevelSensor", 0);
						orgEquipmentReplace.put("bt", 0);
						orgEquipmentReplace.put("sun", 0);
						orgEquipmentReplace.put("net", 0);
						checkOrderInfoDTO = new CheckOrderInfoDTO();
						checkOrderInfoDTO.setExcludeCode(excludecode);
						checkOrderInfoDTO.setAdCode(deptRegionEntity.getAdcd());
						checkOrderInfoDTO.setReportStartDate(entity.getReportStartTime());
						checkOrderInfoDTO.setReportEndDate(entity.getReportEndTime());
						if (orderProcessDict != null) {
							checkOrderInfoDTO.setOrderStatusKey(orderProcessDict.getId());
						}
						processInfoVOList = orderProcessService.selectReportList(checkOrderInfoDTO);
						if (null != processInfoVOList && processInfoVOList.size() > 0) {
							for (OrderProcessInfoVO processInfoVO : processInfoVOList) {
								if (null != processInfoVO.getRtuReplace() && processInfoVO.getRtuReplace() == 1) {
									int v = orgEquipmentReplace.get("rtu");
									v += 1;
									orgEquipmentReplace.put("rtu", v);
								}
								if (null != processInfoVO.getRainSensorReplace() && processInfoVO.getRainSensorReplace() == 1) {
									int v = orgEquipmentReplace.get("rainSensor");
									v += 1;
									orgEquipmentReplace.put("rainSensor", v);
								}
								if (null != processInfoVO.getWaterSensorReplace() && processInfoVO.getWaterSensorReplace() == 1) {
									int v = orgEquipmentReplace.get("waterLevelSensor");
									v += 1;
									orgEquipmentReplace.put("waterLevelSensor", v);
								}
								if (null != processInfoVO.getGroundSensorReplace() && processInfoVO.getGroundSensorReplace() == 1) {
									int v = orgEquipmentReplace.get("groundLevelSensor");
									v += 1;
									orgEquipmentReplace.put("groundLevelSensor", v);
								}
								if (null != processInfoVO.getBatteryReplace() && processInfoVO.getBatteryReplace() == 1) {
									int v = orgEquipmentReplace.get("bt");
									v += 1;
									orgEquipmentReplace.put("bt", v);
								}
								if (null != processInfoVO.getSunPowerReplace() && processInfoVO.getSunPowerReplace() == 1) {
									int v = orgEquipmentReplace.get("sun");
									v += 1;
									orgEquipmentReplace.put("sun", v);
								}
								if (null != processInfoVO.getNetworkOverdue() && processInfoVO.getNetworkOverdue() == 1) {
									int v = orgEquipmentReplace.get("net");
									v += 1;
									orgEquipmentReplace.put("net", v);
								}
							}
						}
						//一周站点更换信息清单
						int replaceCount = orgEquipmentReplace.get("rtu");
						if (replaceCount > 0) {
							WeekOrgEquipmentReplaceInfoEntity equipmentReplaceInfoEntity = new WeekOrgEquipmentReplaceInfoEntity();
							equipmentReplaceInfoEntity.setReportId(entity.getId());
							equipmentReplaceInfoEntity.setOrgName(dept.getDeptName());
							equipmentReplaceInfoEntity.setEquipmentName("RTU");
							equipmentReplaceInfoEntity.setReplaceCount(replaceCount);
							weekOrgEquipmentReplaceService.save(equipmentReplaceInfoEntity);
						}
						replaceCount = orgEquipmentReplace.get("rainSensor");
						if (replaceCount > 0) {
							WeekOrgEquipmentReplaceInfoEntity equipmentReplaceInfoEntity = new WeekOrgEquipmentReplaceInfoEntity();
							equipmentReplaceInfoEntity.setReportId(entity.getId());
							equipmentReplaceInfoEntity.setOrgName(dept.getDeptName());
							equipmentReplaceInfoEntity.setEquipmentName("雨量计");
							equipmentReplaceInfoEntity.setReplaceCount(replaceCount);
							weekOrgEquipmentReplaceService.save(equipmentReplaceInfoEntity);
						}
						replaceCount = orgEquipmentReplace.get("waterLevelSensor");
						if (replaceCount > 0) {
							WeekOrgEquipmentReplaceInfoEntity equipmentReplaceInfoEntity = new WeekOrgEquipmentReplaceInfoEntity();
							equipmentReplaceInfoEntity.setReportId(entity.getId());
							equipmentReplaceInfoEntity.setOrgName(dept.getDeptName());
							equipmentReplaceInfoEntity.setEquipmentName("水位计");
							equipmentReplaceInfoEntity.setReplaceCount(replaceCount);
							weekOrgEquipmentReplaceService.save(equipmentReplaceInfoEntity);
						}
						replaceCount = orgEquipmentReplace.get("groundLevelSensor");
						if (replaceCount > 0) {
							WeekOrgEquipmentReplaceInfoEntity equipmentReplaceInfoEntity = new WeekOrgEquipmentReplaceInfoEntity();
							equipmentReplaceInfoEntity.setReportId(entity.getId());
							equipmentReplaceInfoEntity.setOrgName(dept.getDeptName());
							equipmentReplaceInfoEntity.setEquipmentName("墒情计");
							equipmentReplaceInfoEntity.setReplaceCount(replaceCount);
							weekOrgEquipmentReplaceService.save(equipmentReplaceInfoEntity);
						}
						replaceCount = orgEquipmentReplace.get("bt");
						if (replaceCount > 0) {
							WeekOrgEquipmentReplaceInfoEntity equipmentReplaceInfoEntity = new WeekOrgEquipmentReplaceInfoEntity();
							equipmentReplaceInfoEntity.setReportId(entity.getId());
							equipmentReplaceInfoEntity.setOrgName(dept.getDeptName());
							equipmentReplaceInfoEntity.setEquipmentName("蓄电池");
							equipmentReplaceInfoEntity.setReplaceCount(replaceCount);
							weekOrgEquipmentReplaceService.save(equipmentReplaceInfoEntity);
						}
						replaceCount = orgEquipmentReplace.get("sun");
						if (replaceCount > 0) {
							WeekOrgEquipmentReplaceInfoEntity equipmentReplaceInfoEntity = new WeekOrgEquipmentReplaceInfoEntity();
							equipmentReplaceInfoEntity.setReportId(entity.getId());
							equipmentReplaceInfoEntity.setOrgName(dept.getDeptName());
							equipmentReplaceInfoEntity.setEquipmentName("太阳能板");
							equipmentReplaceInfoEntity.setReplaceCount(replaceCount);
							weekOrgEquipmentReplaceService.save(equipmentReplaceInfoEntity);
						}
						replaceCount = orgEquipmentReplace.get("net");
						if (replaceCount > 0) {
							WeekOrgEquipmentReplaceInfoEntity equipmentReplaceInfoEntity = new WeekOrgEquipmentReplaceInfoEntity();
							equipmentReplaceInfoEntity.setReportId(entity.getId());
							equipmentReplaceInfoEntity.setOrgName(dept.getDeptName());
							equipmentReplaceInfoEntity.setEquipmentName("物联网卡欠费");
							equipmentReplaceInfoEntity.setReplaceCount(replaceCount);
							weekOrgEquipmentReplaceService.save(equipmentReplaceInfoEntity);
						}
					}
				}
			} catch (Exception e) {
				log.error("{}", e.getMessage());
			} finally {
				log.info("周统计结束 {}", Func.formatDateTime(new Date()));
			}
			return 0;
		}
	}
}
