/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/18
 */
package org.springblade.modules.business.rtumanage.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.cache.DictBizCache;
import org.springblade.constant.BusinessConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.enums.OrderStatusEnum;
import org.springblade.modules.baseinfo.org.entity.DeptRegionEntity;
import org.springblade.modules.baseinfo.org.service.IDeptRegionService;
import org.springblade.modules.baseinfo.rtu.dto.RtuInfoDTO;
import org.springblade.modules.baseinfo.rtu.entity.RtuInfoEntity;
import org.springblade.modules.baseinfo.rtu.service.IRtuBaseInfoService;
import org.springblade.modules.baseinfo.rtu.vo.RtuInfoVO;
import org.springblade.modules.baseinfo.rtu.wrapper.RtuInfoWrapper;
import org.springblade.modules.business.check.order.dto.CheckOrderInfoDTO;
import org.springblade.modules.business.check.order.entity.CheckOrderInfoEntity;
import org.springblade.modules.business.check.order.service.IRtuCheckOrderService;
import org.springblade.modules.business.check.process.dto.OrderProcessInfoDTO;
import org.springblade.modules.business.check.process.entity.OrderProcessInfoEntity;
import org.springblade.modules.business.check.process.service.IRtuOrderProcessService;
import org.springblade.modules.business.check.process.vo.OrderProcessInfoVO;
import org.springblade.modules.business.equipment.inspection.report.dto.EquipmentInspectionReportDTO;
import org.springblade.modules.business.equipment.inspection.report.entity.EquipmentInspectionReportEntity;
import org.springblade.modules.business.equipment.inspection.report.service.IEquipmentInspectionReportService;
import org.springblade.modules.business.equipment.inspection.report.vo.EquipmentInspectionReportVO;
import org.springblade.modules.business.rtumanage.entity.EquipmentInfoExportEntity;
import org.springblade.modules.business.rtumanage.entity.RtuStatusEntity;
import org.springblade.modules.business.rtumanage.service.IRtuManageService;
import org.springblade.modules.business.rtumanage.vo.*;
import org.springblade.modules.business.rtumanage.wrapper.RtuManageWrapper;
import org.springblade.modules.business.warning.dto.RtuWarningInfoDTO;
import org.springblade.modules.business.warning.service.IRtuWarningService;
import org.springblade.modules.resource.builder.oss.OssBuilder;
import org.springblade.modules.system.entity.DictBiz;
import org.springblade.modules.system.service.IDictBizService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.*;

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

@Slf4j
@RestController
@RequestMapping("galaxy-business/rtu/manage")
@RequiredArgsConstructor
@Api(value = "测站设备管理工作台", tags = "测站设备管理工作台")
public class RtuManageController extends BladeController {

	private final IRtuManageService rtuManageService;
	private final IRtuBaseInfoService rtuBaseInfoService;
	private final IDictBizService dictBizService;
	private final IDeptRegionService deptRegionService;
	private final IRtuWarningService rtuWarningService;
	private final IRtuCheckOrderService rtuCheckOrderService;
	private final IRtuOrderProcessService rtuOrderProcessService;
	private final IEquipmentInspectionReportService equipmentInspectionReportService;

	@Resource
	private OssBuilder ossBuilder;

	@Value("${export-config.workdir}")
	private String exportConfigWorkDir;

	@Value("${export-config.equipment-info-template}")
	private String exportConfigTemplate;

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<RtuStatusVO>> list(Query query) {
		IPage<RtuStatusEntity> pages = rtuManageService.page(Condition.getPage(query));
		return R.data(RtuManageWrapper.build().pageVO(pages));
	}

	/**
	 * 多表联合查询自定义分页
	 */
	@GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "adCode", value = "行政区划编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuKind", value = "测站类型", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "warnKind", value = "异常类型", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入RtuInfoEntity")
	public R<IPage<RtuStatusVO>> page(@ApiIgnore RtuInfoDTO rtuInfoDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
			if (rtuInfoDTO.getAdCode() == null) {
				rtuInfoDTO.setAdCode(BusinessConstant.REGION_NM_ADCODE);
			}
		} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_COMPANY_SERVICE_PERSON.equals(user.getRoleName())) {
			if (rtuInfoDTO.getAdCode() == null) {
				LambdaQueryWrapper<DeptRegionEntity> deptRegionEntityLambdaQueryWrapper = Wrappers.<DeptRegionEntity>query().lambda();
				deptRegionEntityLambdaQueryWrapper.eq(DeptRegionEntity::getIsDeleted, 0);
				deptRegionEntityLambdaQueryWrapper.eq(DeptRegionEntity::getDeptId, Func.toLong(user.getDeptId()));
				DeptRegionEntity deptRegionEntity = deptRegionService.getOne(deptRegionEntityLambdaQueryWrapper);
				rtuInfoDTO.setAdCode(deptRegionEntity.getAdcd());
			}
		}
		if (rtuInfoDTO.getRtuKind() != null && "1".equals(rtuInfoDTO.getRtuKind())) {
			rtuInfoDTO.setIsRain(1);
		} else if (rtuInfoDTO.getRtuKind() != null && "2".equals(rtuInfoDTO.getRtuKind())) {
			rtuInfoDTO.setIsRiver(1);
			rtuInfoDTO.setIsRes(1);
		} else if (rtuInfoDTO.getRtuKind() != null && "3".equals(rtuInfoDTO.getRtuKind())) {
			rtuInfoDTO.setIsGround(1);
		} else if (rtuInfoDTO.getRtuKind() != null && "4".equals(rtuInfoDTO.getRtuKind())) {
			rtuInfoDTO.setIsVideo(1);
		}
		if (rtuInfoDTO.getWarnKind()!=null) {
			IPage<RtuStatusVO> pages = rtuManageService.selectWarnPage(Condition.getPage(query), rtuInfoDTO);
			List<RtuStatusVO> list = pages.getRecords();
			for (RtuStatusVO vo : list) {
				RtuManageWrapper.build().entityVO(vo);
			}
			return R.data(pages);
		}else{
			IPage<RtuStatusVO> pages = rtuManageService.selectPage(Condition.getPage(query), rtuInfoDTO);
			List<RtuStatusVO> list = pages.getRecords();
			for (RtuStatusVO vo : list) {
				RtuManageWrapper.build().entityVO(vo);
			}
			return R.data(pages);
		}
	}

	@GetMapping("/equipmentlist/export")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "adCode", value = "行政区划编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "adName", value = "行政区划名称", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入RtuInfoEntity")
	public R<Map<String, Object>> equipmentlistExport(@ApiIgnore RtuInfoDTO rtuInfoDTO) {
		Map<String, Object> data = new HashMap<>();
		data.put("status", 0);
		String templateFilePath = this.exportConfigWorkDir + File.separator + this.exportConfigTemplate;
		String outpath = this.exportConfigWorkDir + File.separator + Func.formatDate(new Date());
		File dir = new File(outpath);
		if (!dir.exists()) {
			if (!dir.mkdir()) {
				return R.fail("目录创建失败");
			}
		}
		String outFileName = Func.randomUUID() + ".xlsx";
		outpath += File.separator + outFileName;
		List<RtuInfoEntity> rtuInfoEntityList = rtuBaseInfoService.rtuList(rtuInfoDTO);
		List<EquipmentInfoExportEntity> list = new LinkedList<>();
		int index = 1;
		for (RtuInfoEntity rtuInfoEntity : rtuInfoEntityList) {
			EquipmentInfoExportEntity entity = new EquipmentInfoExportEntity();
			entity.setIndex(index);
			if (rtuInfoEntity.getAdCity() != null) {
				entity.setAdCity(rtuInfoEntity.getAdCity());
			} else {
				entity.setAdCity("");
			}
			if (rtuInfoEntity.getAdDist() != null) {
				entity.setAdDist(rtuInfoEntity.getAdDist());
			} else {
				entity.setAdDist("");
			}
			if (rtuInfoEntity.getRtuName() != null) {
				entity.setRtuName(rtuInfoEntity.getRtuName());
			} else {
				entity.setRtuName("");
			}
			entity.setRtuCode(rtuInfoEntity.getRtuCode());
			String rtuKindDesc = null;
			if (rtuInfoEntity.getIsRain() != null && rtuInfoEntity.getIsRes() != null && rtuInfoEntity.getIsRiver() != null && rtuInfoEntity.getIsGround() != null) {
				if (rtuInfoEntity.getIsRain() == 1 && (rtuInfoEntity.getIsRes() == 1 || rtuInfoEntity.getIsRiver() == 1)) {
					rtuKindDesc = "水位雨量一体站";
				} else if (rtuInfoEntity.getIsRain() == 1 && rtuInfoEntity.getIsGround() == 1) {
					rtuKindDesc = "雨量墒情一体站";
				} else if (rtuInfoEntity.getIsRain() == 1) {
					rtuKindDesc = "雨量站";
				} else if (rtuInfoEntity.getIsRes() == 1 || rtuInfoEntity.getIsRiver() == 1) {
					rtuKindDesc = "水位站";
				} else if (rtuInfoEntity.getIsGround() == 1) {
					rtuKindDesc = "墒情站";
				}
			}
			if (rtuKindDesc != null) {
				entity.setRtuKindDesc(rtuKindDesc);
			} else {
				entity.setRtuKindDesc("");
			}
			if (rtuInfoEntity.getLocationDesc() != null) {
				entity.setLocationDesc(rtuInfoEntity.getLocationDesc());
			} else {
				entity.setLocationDesc("");
			}
			if (rtuInfoEntity.getLng() != null) {
				entity.setLng(rtuInfoEntity.getLng());
			} else {
				entity.setLng("");
			}
			if (rtuInfoEntity.getLat() != null) {
				entity.setLat(rtuInfoEntity.getLat());
			} else {
				entity.setLat("");
			}
			if (rtuInfoEntity.getManageCompany() != null) {
				entity.setManageCompany(rtuInfoEntity.getManageCompany());
			} else {
				entity.setManageCompany("");
			}
			if (rtuInfoEntity.getRtuBrand() != null) {
				entity.setRtuBrand(rtuInfoEntity.getRtuBrand());
			} else {
				entity.setRtuBrand("");
			}
			if (rtuInfoEntity.getRtuModel() != null) {
				entity.setRtuModel(rtuInfoEntity.getRtuModel());
			} else {
				entity.setRtuModel("");
			}
			if (rtuInfoEntity.getRtuReplaceDate() != null) {
				entity.setRtuReplaceDate(Func.formatDate(rtuInfoEntity.getRtuReplaceDate()));
			} else {
				entity.setRtuReplaceDate("");
			}
			if (rtuInfoEntity.getRtuName() != null) {
				entity.setRtuName(rtuInfoEntity.getRtuName());
			} else {
				entity.setRtuName("");
			}
			if (rtuInfoEntity.getDatagramToSheng() != null) {
				if (rtuInfoEntity.getDatagramToSheng() == 1) {
					entity.setDatagramToSheng("是");
				} else {
					entity.setDatagramToSheng("否");
				}
			} else {
				entity.setDatagramToSheng("");
			}
			if (rtuInfoEntity.getDatagramToShi() != null) {
				if (rtuInfoEntity.getDatagramToShi() == 1) {
					entity.setDatagramToShi("是");
				} else {
					entity.setDatagramToShi("否");
				}
			} else {
				entity.setDatagramToShi("");
			}
			if (rtuInfoEntity.getDatagramToQu() != null) {
				if (rtuInfoEntity.getDatagramToQu() == 1) {
					entity.setDatagramToQu("是");
				} else {
					entity.setDatagramToQu("否");
				}
			} else {
				entity.setDatagramToQu("");
			}
			if (rtuInfoEntity.getRainSensorBrand() != null) {
				entity.setRainSensorBrand(rtuInfoEntity.getRainSensorBrand());
			} else {
				entity.setRainSensorBrand("");
			}
			if (rtuInfoEntity.getRainSensorModel() != null) {
				entity.setRainSensorModel(rtuInfoEntity.getRainSensorModel());
			} else {
				entity.setRainSensorModel("");
			}

			if (rtuInfoEntity.getRainSensorReplaceDate() != null) {
				entity.setRainSensorReplaceDate(Func.formatDate(rtuInfoEntity.getRainSensorReplaceDate()));
			} else {
				entity.setRainSensorReplaceDate("");
			}
			if (rtuInfoEntity.getWaterSensorType() != null) {
				DictBiz dictBiz = dictBizService.getById(rtuInfoEntity.getWaterSensorType());
				entity.setWaterSensorType(dictBiz.getDictValue());
			} else {
				entity.setWaterSensorType("");
			}
			if (rtuInfoEntity.getWaterSensorBrand() != null) {
				entity.setWaterSensorBrand(rtuInfoEntity.getWaterSensorBrand());
			} else {
				entity.setWaterSensorBrand("");
			}
			if (rtuInfoEntity.getWaterSensorModel() != null) {
				entity.setWaterSensorModel(rtuInfoEntity.getWaterSensorModel());
			} else {
				entity.setWaterSensorModel("");
			}
			if (rtuInfoEntity.getWaterSensorReplaceDate() != null) {
				entity.setWaterSensorReplaceDate(Func.formatDate(rtuInfoEntity.getWaterSensorReplaceDate()));
			} else {
				entity.setWaterSensorReplaceDate("");
			}


			if (rtuInfoEntity.getGroundSensorType() != null) {
				DictBiz dictBiz = dictBizService.getById(rtuInfoEntity.getGroundSensorType());
				entity.setGroundSensorType(dictBiz.getDictValue());
			} else {
				entity.setGroundSensorType("");
			}
			if (rtuInfoEntity.getGroundSensorBrand() != null) {
				entity.setGroundSensorBrand(rtuInfoEntity.getGroundSensorBrand());
			} else {
				entity.setGroundSensorBrand("");
			}
			if (rtuInfoEntity.getGroundSensorModel() != null) {
				entity.setGroundSensorModel(rtuInfoEntity.getGroundSensorModel());
			} else {
				entity.setGroundSensorModel("");
			}
			if (rtuInfoEntity.getGroundSensorReplaceDate() != null) {
				entity.setGroundSensorReplaceDate(Func.formatDate(rtuInfoEntity.getGroundSensorReplaceDate()));
			} else {
				entity.setGroundSensorReplaceDate("");
			}

			if (rtuInfoEntity.getBatteryModel() != null) {
				entity.setBatteryModel(rtuInfoEntity.getBatteryModel());
			} else {
				entity.setBatteryModel("");
			}
			if (rtuInfoEntity.getBatteryReplaceDate() != null) {
				entity.setBatteryReplaceDate(Func.formatDate(rtuInfoEntity.getBatteryReplaceDate()));
			} else {
				entity.setBatteryReplaceDate("");
			}

			if (rtuInfoEntity.getSunPowerModel() != null) {
				entity.setSunPowerModel(rtuInfoEntity.getSunPowerModel());
			} else {
				entity.setSunPowerModel("");
			}
			if (rtuInfoEntity.getNetworkSimId() != null) {
				entity.setNetworkSimId(rtuInfoEntity.getNetworkSimId());
			} else {
				entity.setNetworkSimId("");
			}
			if (rtuInfoEntity.getNetworkPayer() != null) {
				entity.setNetworkPayer(rtuInfoEntity.getNetworkPayer());
			} else {
				entity.setNetworkPayer("");
			}
			if (rtuInfoEntity.getNetworkPayEndDate() != null) {
				entity.setNetworkPayEndDate(Func.formatDate(rtuInfoEntity.getNetworkPayEndDate()));
			} else {
				entity.setNetworkPayEndDate("");
			}

			if (rtuInfoEntity.getIsSatellite() != null) {
				if (rtuInfoEntity.getIsSatellite() == 1) {
					entity.setIsSatellite("是");
				} else {
					entity.setIsSatellite("否");
				}
			} else {
				entity.setIsSatellite("");
			}
			if (rtuInfoEntity.getSatelliteModel() != null) {
				DictBiz dictBiz = dictBizService.getById(rtuInfoEntity.getGroundSensorType());
				entity.setSatelliteModel(dictBiz.getDictValue());
			} else {
				entity.setSatelliteModel("");
			}
			if (rtuInfoEntity.getRemark() != null) {
				entity.setRemark(rtuInfoEntity.getRemark());
			} else {
				entity.setRemark("");
			}
			list.add(entity);
			index += 1;
		}
		ExcelWriter excelWriter = EasyExcel.write(outpath).withTemplate(templateFilePath).excelType(ExcelTypeEnum.XLSX).build();
		WriteSheet writeSheet = EasyExcel.writerSheet("站点信息统计表").build();
		excelWriter.write(list, writeSheet);
		excelWriter.finish();
		try {
			FileInputStream fileInputStream = new FileInputStream(new File(outpath));
			BladeFile bladeFile = ossBuilder.template().putFile(outFileName, fileInputStream);
			String ossPath = bladeFile.getName();
			String p = bladeFile.getDomain();
			log.info(ossPath);
			log.info(p);
			data.put("name", ossPath);
			data.put("url", ossPath);
			data.put("filename", rtuInfoDTO.getAdName());
			data.put("status", 1);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return R.data(data);
	}

	/**
	 * 分页
	 */
	@GetMapping("/count")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "adCode", value = "行政区划编码", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "按行政区划统计测站信息")
	public R<Map<String, Object>> countInfo(@ApiIgnore RtuInfoDTO rtuInfoDTO) {
		if (rtuInfoDTO.getAdCode() == null) {
			return R.status(false);
		}
		Map<String, Object> data = new HashMap<>();
		//测站数量
		long rtuCount = rtuBaseInfoService.rtuCount(rtuInfoDTO.getAdCode());
		data.put("rtuCount", rtuCount);
		//预警数量
		RtuWarningInfoDTO rtuWarningInfoDTO = new RtuWarningInfoDTO();
		rtuWarningInfoDTO.setAdCode(rtuInfoDTO.getAdCode());
		long warningRtuCount = rtuWarningService.warningRtuCount(rtuWarningInfoDTO);
		data.put("warningRtuCount", warningRtuCount);
		return R.data(data);
	}


	/**
	 * 测站RTU更换历史
	 */
	@GetMapping("/rtu/replace/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "不分页")
	public R<List<RtuReplaceInfoVO>> rtuReplacelist(@ApiIgnore RtuInfoDTO rtuInfoDTO) {
		List<RtuReplaceInfoVO> replaceInfoVOList = new LinkedList<>();
		LambdaQueryWrapper<RtuInfoEntity> wrapper = Wrappers.<RtuInfoEntity>query().lambda();
		wrapper.eq(RtuInfoEntity::getIsDeleted, 0);
		wrapper.eq(RtuInfoEntity::getRtuCode, rtuInfoDTO.getRtuCode());
		RtuInfoEntity rtuInfoEntity = rtuBaseInfoService.getOne(wrapper);
		if (null != rtuInfoEntity) {
			LambdaQueryWrapper<CheckOrderInfoEntity> orderwrapper = Wrappers.<CheckOrderInfoEntity>query().lambda();
			orderwrapper.eq(CheckOrderInfoEntity::getIsDeleted, 0);
			orderwrapper.eq(CheckOrderInfoEntity::getRtuCode, rtuInfoDTO.getRtuCode());

			List<CheckOrderInfoEntity> checkOrderInfoEntityList = rtuCheckOrderService.list(orderwrapper);
			for (CheckOrderInfoEntity checkOrderInfoEntity : checkOrderInfoEntityList) {

				CheckOrderInfoDTO checkOrderInfoDTO = new CheckOrderInfoDTO();
				checkOrderInfoDTO.setIsDeleted(0);
				checkOrderInfoDTO.setId(checkOrderInfoEntity.getId());

				List<OrderProcessInfoVO> orderProcessInfoEntityList = rtuOrderProcessService.selectList(checkOrderInfoDTO);
				for (OrderProcessInfoVO orderProcessInfoEntity : orderProcessInfoEntityList) {
					DictBiz biz = DictBizCache.getById(orderProcessInfoEntity.getOrderStatusKey());
					if (biz.getDictKey().equals(OrderStatusEnum.STATUS_PROCESS.getCode())) {
						if (orderProcessInfoEntity.getRtuReplace() != null && orderProcessInfoEntity.getRtuReplace() == 1) {
							RtuReplaceInfoVO vo = new RtuReplaceInfoVO();
							vo.setOrderId(checkOrderInfoEntity.getId());
							vo.setRtuCode(rtuInfoDTO.getRtuCode());
							vo.setRtuBrand(orderProcessInfoEntity.getRtuBrand());
							vo.setRtuModel(orderProcessInfoEntity.getRtuModel());
							vo.setReplaceKind(1);
							vo.setReplaceDate(orderProcessInfoEntity.getCreateTime());
							vo.setReplaceDesc(orderProcessInfoEntity.getRtuDesc());
							replaceInfoVOList.add(vo);
						}
					}
				}
			}


			EquipmentInspectionReportDTO equipmentInspectionReportDTO = new EquipmentInspectionReportDTO();
			equipmentInspectionReportDTO.setIsDeleted(0);
			equipmentInspectionReportDTO.setRtuCode(rtuInfoDTO.getRtuCode());

			List<EquipmentInspectionReportVO> inspectionReportEntityList = equipmentInspectionReportService.selectList(equipmentInspectionReportDTO);
			for(EquipmentInspectionReportVO inspectionReportEntity:inspectionReportEntityList){
				if (inspectionReportEntity.getRtuReplace()!=null && inspectionReportEntity.getRtuReplace()==1){
					RtuReplaceInfoVO vo = new RtuReplaceInfoVO();
					vo.setInspectionId(inspectionReportEntity.getId());
					vo.setRtuCode(rtuInfoDTO.getRtuCode());
					vo.setRtuBrand(inspectionReportEntity.getRtuModelRemark());
					vo.setRtuModel(inspectionReportEntity.getRtuModelRemark());
					vo.setReplaceKind(2);
					vo.setReplaceDate(inspectionReportEntity.getCreateTime());
					//vo.setReplaceDesc(inspectionReportEntity.getRtuModelRemark());
					replaceInfoVOList.add(vo);
				}
			}
			replaceInfoVOList.sort(new Comparator<RtuReplaceInfoVO>() {
				@Override
				public int compare(RtuReplaceInfoVO o1, RtuReplaceInfoVO o2) {
					if (o1.getReplaceDate().after(o2.getReplaceDate())){
						return 1;
					}else if (o1.getReplaceDate().before(o2.getReplaceDate())){
						return -1;
					}
					return 0;
				}
			});
			if (replaceInfoVOList.size() == 0) {
				RtuReplaceInfoVO vo = new RtuReplaceInfoVO();
				vo.setRtuCode(rtuInfoDTO.getRtuCode());
				vo.setRtuBrand(rtuInfoEntity.getRtuBrand());
				vo.setRtuModel(rtuInfoEntity.getRtuModel());
				vo.setReplaceKind(0);
				vo.setReplaceDate(rtuInfoEntity.getRtuReplaceDate());
				replaceInfoVOList.add(vo);
			}
		}
		return R.data(replaceInfoVOList);
	}


	/**
	 * 测站雨量计更换历史
	 */
	@GetMapping("/rainsensor/replace/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "不分页")
	public R<List<RainSensorReplaceInfoVO>> rainSensorReplacelist(@ApiIgnore RtuInfoDTO rtuInfoDTO) {
		List<RainSensorReplaceInfoVO> replaceInfoVOList = new LinkedList<>();
		LambdaQueryWrapper<RtuInfoEntity> wrapper = Wrappers.<RtuInfoEntity>query().lambda();
		wrapper.eq(RtuInfoEntity::getIsDeleted, 0);
		wrapper.eq(RtuInfoEntity::getRtuCode, rtuInfoDTO.getRtuCode());
		RtuInfoEntity rtuInfoEntity = rtuBaseInfoService.getOne(wrapper);
		if (null != rtuInfoEntity) {
			LambdaQueryWrapper<CheckOrderInfoEntity> orderwrapper = Wrappers.<CheckOrderInfoEntity>query().lambda();
			orderwrapper.eq(CheckOrderInfoEntity::getIsDeleted, 0);
			orderwrapper.eq(CheckOrderInfoEntity::getRtuCode, rtuInfoDTO.getRtuCode());

			List<CheckOrderInfoEntity> checkOrderInfoEntityList = rtuCheckOrderService.list(orderwrapper);
			for (CheckOrderInfoEntity checkOrderInfoEntity : checkOrderInfoEntityList) {

				CheckOrderInfoDTO checkOrderInfoDTO = new CheckOrderInfoDTO();
				checkOrderInfoDTO.setIsDeleted(0);
				checkOrderInfoDTO.setId(checkOrderInfoEntity.getId());

				List<OrderProcessInfoVO> orderProcessInfoEntityList = rtuOrderProcessService.selectList(checkOrderInfoDTO);
				for (OrderProcessInfoVO orderProcessInfoEntity : orderProcessInfoEntityList) {
					DictBiz biz = DictBizCache.getById(orderProcessInfoEntity.getOrderStatusKey());
					if (biz.getDictKey().equals(OrderStatusEnum.STATUS_PROCESS.getCode())) {
						if (orderProcessInfoEntity.getRainSensorReplace() != null && orderProcessInfoEntity.getRainSensorReplace() == 1) {
							RainSensorReplaceInfoVO vo = new RainSensorReplaceInfoVO();
							vo.setRtuCode(rtuInfoDTO.getRtuCode());
							vo.setOrderId(checkOrderInfoEntity.getId());
							vo.setRainSensorBrand(orderProcessInfoEntity.getRainSensorBrand());
							vo.setRainSensorModel(orderProcessInfoEntity.getRainSensorModel());
							vo.setReplaceKind(1);
							vo.setReplaceDate(orderProcessInfoEntity.getCreateTime());
							vo.setReplaceDesc(orderProcessInfoEntity.getRainSensorDesc());
							replaceInfoVOList.add(vo);
						}
					}
				}
			}


			EquipmentInspectionReportDTO equipmentInspectionReportDTO = new EquipmentInspectionReportDTO();
			equipmentInspectionReportDTO.setIsDeleted(0);
			equipmentInspectionReportDTO.setRtuCode(rtuInfoDTO.getRtuCode());

			List<EquipmentInspectionReportVO> inspectionReportEntityList = equipmentInspectionReportService.selectList(equipmentInspectionReportDTO);
			for(EquipmentInspectionReportVO inspectionReportEntity:inspectionReportEntityList){
				if (inspectionReportEntity.getRainSensorReplace()!=null && inspectionReportEntity.getRainSensorReplace()==1){
					RainSensorReplaceInfoVO vo = new RainSensorReplaceInfoVO();
					vo.setRtuCode(rtuInfoDTO.getRtuCode());
					vo.setInspectionId(inspectionReportEntity.getId());
					vo.setRainSensorBrand(inspectionReportEntity.getRainSensorModelRemark());
					vo.setRainSensorModel(inspectionReportEntity.getRainSensorModelRemark());
					vo.setReplaceKind(2);
					vo.setReplaceDate(inspectionReportEntity.getCreateTime());
				//	vo.setReplaceDesc(inspectionReportEntity.getRainSensorModelRemark());
					replaceInfoVOList.add(vo);
				}
			}
			replaceInfoVOList.sort(new Comparator<RainSensorReplaceInfoVO>() {
				@Override
				public int compare(RainSensorReplaceInfoVO o1, RainSensorReplaceInfoVO o2) {
					if (o1.getReplaceDate().after(o2.getReplaceDate())){
						return 1;
					}else if (o1.getReplaceDate().before(o2.getReplaceDate())){
						return -1;
					}
					return 0;
				}
			});
			if (replaceInfoVOList.size() == 0) {
				RainSensorReplaceInfoVO vo = new RainSensorReplaceInfoVO();
				vo.setRtuCode(rtuInfoDTO.getRtuCode());
				vo.setRainSensorBrand(rtuInfoEntity.getRainSensorBrand());
				vo.setRainSensorModel(rtuInfoEntity.getRainSensorModel());
				vo.setReplaceKind(0);
				vo.setReplaceDate(rtuInfoEntity.getRainSensorReplaceDate());
				replaceInfoVOList.add(vo);
			}
		}
		return R.data(replaceInfoVOList);
	}


	/**
	 * 测站水位计更换历史
	 */
	@GetMapping("/watersensor/replace/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "不分页")
	public R<List<WaterSensorReplaceInfoVO>> waterSensorReplacelist(@ApiIgnore RtuInfoDTO rtuInfoDTO) {
		List<WaterSensorReplaceInfoVO> replaceInfoVOList = new LinkedList<>();
		LambdaQueryWrapper<RtuInfoEntity> wrapper = Wrappers.<RtuInfoEntity>query().lambda();
		wrapper.eq(RtuInfoEntity::getIsDeleted, 0);
		wrapper.eq(RtuInfoEntity::getRtuCode, rtuInfoDTO.getRtuCode());
		RtuInfoEntity rtuInfoEntity = rtuBaseInfoService.getOne(wrapper);
		if (null != rtuInfoEntity) {
			LambdaQueryWrapper<CheckOrderInfoEntity> orderwrapper = Wrappers.<CheckOrderInfoEntity>query().lambda();
			orderwrapper.eq(CheckOrderInfoEntity::getIsDeleted, 0);
			orderwrapper.eq(CheckOrderInfoEntity::getRtuCode, rtuInfoDTO.getRtuCode());

			List<CheckOrderInfoEntity> checkOrderInfoEntityList = rtuCheckOrderService.list(orderwrapper);
			for (CheckOrderInfoEntity checkOrderInfoEntity : checkOrderInfoEntityList) {

				CheckOrderInfoDTO checkOrderInfoDTO = new CheckOrderInfoDTO();
				checkOrderInfoDTO.setIsDeleted(0);
				checkOrderInfoDTO.setId(checkOrderInfoEntity.getId());

				List<OrderProcessInfoVO> orderProcessInfoEntityList = rtuOrderProcessService.selectList(checkOrderInfoDTO);
				for (OrderProcessInfoVO orderProcessInfoEntity : orderProcessInfoEntityList) {
					DictBiz biz = DictBizCache.getById(orderProcessInfoEntity.getOrderStatusKey());
					if (biz.getDictKey().equals(OrderStatusEnum.STATUS_PROCESS.getCode())) {
						if (orderProcessInfoEntity.getWaterSensorReplace() != null && orderProcessInfoEntity.getWaterSensorReplace() == 1) {
							WaterSensorReplaceInfoVO vo = new WaterSensorReplaceInfoVO();
							vo.setRtuCode(rtuInfoDTO.getRtuCode());
							vo.setOrderId(checkOrderInfoEntity.getId());
							vo.setWaterSensorBrand(orderProcessInfoEntity.getWaterSensorBrand());
							vo.setWaterSensorModel(orderProcessInfoEntity.getWaterSensorModel());
							vo.setReplaceKind(1);
							vo.setReplaceDate(orderProcessInfoEntity.getCreateTime());
							vo.setReplaceDesc(orderProcessInfoEntity.getWaterSensorDesc());
							replaceInfoVOList.add(vo);
						}
					}
				}
			}


			EquipmentInspectionReportDTO equipmentInspectionReportDTO = new EquipmentInspectionReportDTO();
			equipmentInspectionReportDTO.setIsDeleted(0);
			equipmentInspectionReportDTO.setRtuCode(rtuInfoDTO.getRtuCode());

			List<EquipmentInspectionReportVO> inspectionReportEntityList = equipmentInspectionReportService.selectList(equipmentInspectionReportDTO);
			for(EquipmentInspectionReportVO inspectionReportEntity:inspectionReportEntityList){
				if (inspectionReportEntity.getWaterSensorReplace()!=null && inspectionReportEntity.getWaterSensorReplace()==1){
					WaterSensorReplaceInfoVO vo = new WaterSensorReplaceInfoVO();
					vo.setRtuCode(rtuInfoDTO.getRtuCode());
					vo.setInspectionId(inspectionReportEntity.getId());
					vo.setWaterSensorBrand(inspectionReportEntity.getWaterSensorModelRemark());
					vo.setWaterSensorModel(inspectionReportEntity.getWaterSensorModelRemark());
					vo.setReplaceKind(2);
					vo.setReplaceDate(inspectionReportEntity.getCreateTime());
				//	vo.setReplaceDesc(inspectionReportEntity.getWaterSensorModelRemark());
					replaceInfoVOList.add(vo);
				}
			}
			replaceInfoVOList.sort(new Comparator<WaterSensorReplaceInfoVO>() {
				@Override
				public int compare(WaterSensorReplaceInfoVO o1, WaterSensorReplaceInfoVO o2) {
					if (o1.getReplaceDate().after(o2.getReplaceDate())){
						return 1;
					}else if (o1.getReplaceDate().before(o2.getReplaceDate())){
						return -1;
					}
					return 0;
				}
			});
			if (replaceInfoVOList.size() == 0) {
				WaterSensorReplaceInfoVO vo = new WaterSensorReplaceInfoVO();
				vo.setRtuCode(rtuInfoDTO.getRtuCode());
				vo.setWaterSensorBrand(rtuInfoEntity.getWaterSensorBrand());
				vo.setWaterSensorModel(rtuInfoEntity.getWaterSensorModel());
				vo.setReplaceKind(0);
				vo.setReplaceDate(rtuInfoEntity.getWaterSensorReplaceDate());
				replaceInfoVOList.add(vo);
			}
		}
		return R.data(replaceInfoVOList);
	}


	/**
	 * 测站墒情计更换历史
	 */
	@GetMapping("/groundsensor/replace/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "不分页")
	public R<List<GroundSensorReplaceInfoVO>> groundSensorReplacelist(@ApiIgnore RtuInfoDTO rtuInfoDTO) {
		List<GroundSensorReplaceInfoVO> replaceInfoVOList = new LinkedList<>();
		LambdaQueryWrapper<RtuInfoEntity> wrapper = Wrappers.<RtuInfoEntity>query().lambda();
		wrapper.eq(RtuInfoEntity::getIsDeleted, 0);
		wrapper.eq(RtuInfoEntity::getRtuCode, rtuInfoDTO.getRtuCode());
		RtuInfoEntity rtuInfoEntity = rtuBaseInfoService.getOne(wrapper);
		if (null != rtuInfoEntity) {
			LambdaQueryWrapper<CheckOrderInfoEntity> orderwrapper = Wrappers.<CheckOrderInfoEntity>query().lambda();
			orderwrapper.eq(CheckOrderInfoEntity::getIsDeleted, 0);
			orderwrapper.eq(CheckOrderInfoEntity::getRtuCode, rtuInfoDTO.getRtuCode());

			List<CheckOrderInfoEntity> checkOrderInfoEntityList = rtuCheckOrderService.list(orderwrapper);
			for (CheckOrderInfoEntity checkOrderInfoEntity : checkOrderInfoEntityList) {

				CheckOrderInfoDTO checkOrderInfoDTO = new CheckOrderInfoDTO();
				checkOrderInfoDTO.setIsDeleted(0);
				checkOrderInfoDTO.setId(checkOrderInfoEntity.getId());

				List<OrderProcessInfoVO> orderProcessInfoEntityList = rtuOrderProcessService.selectList(checkOrderInfoDTO);
				for (OrderProcessInfoVO orderProcessInfoEntity : orderProcessInfoEntityList) {
					DictBiz biz = DictBizCache.getById(orderProcessInfoEntity.getOrderStatusKey());
					if (biz.getDictKey().equals(OrderStatusEnum.STATUS_PROCESS.getCode())) {
						if (orderProcessInfoEntity.getGroundSensorReplace() != null && orderProcessInfoEntity.getGroundSensorReplace() == 1) {
							GroundSensorReplaceInfoVO vo = new GroundSensorReplaceInfoVO();
							vo.setRtuCode(rtuInfoDTO.getRtuCode());
							vo.setOrderId(checkOrderInfoEntity.getId());
							vo.setGroundSensorBrand(orderProcessInfoEntity.getGroundSensorBrand());
							vo.setGroundSensorModel(orderProcessInfoEntity.getGroundSensorModel());
							vo.setReplaceKind(1);
							vo.setReplaceDate(orderProcessInfoEntity.getCreateTime());
							vo.setReplaceDesc(orderProcessInfoEntity.getGroundSensorDesc());
							replaceInfoVOList.add(vo);
						}
					}
				}
			}


			EquipmentInspectionReportDTO equipmentInspectionReportDTO = new EquipmentInspectionReportDTO();
			equipmentInspectionReportDTO.setIsDeleted(0);
			equipmentInspectionReportDTO.setRtuCode(rtuInfoDTO.getRtuCode());

			List<EquipmentInspectionReportVO> inspectionReportEntityList = equipmentInspectionReportService.selectList(equipmentInspectionReportDTO);
			for(EquipmentInspectionReportVO inspectionReportEntity:inspectionReportEntityList){
				if (inspectionReportEntity.getGroundSensorReplace()!=null && inspectionReportEntity.getGroundSensorReplace()==1){
					GroundSensorReplaceInfoVO vo = new GroundSensorReplaceInfoVO();
					vo.setRtuCode(rtuInfoDTO.getRtuCode());
					vo.setInspectionId(inspectionReportEntity.getId());
					vo.setGroundSensorBrand(inspectionReportEntity.getGroundSensorModelRemark());
					vo.setGroundSensorModel(inspectionReportEntity.getGroundSensorModelRemark());
					vo.setReplaceKind(2);
					vo.setReplaceDate(inspectionReportEntity.getCreateTime());
					//vo.setReplaceDesc(inspectionReportEntity.getGroundSensorModelRemark());
					replaceInfoVOList.add(vo);
				}
			}
			replaceInfoVOList.sort(new Comparator<GroundSensorReplaceInfoVO>() {
				@Override
				public int compare(GroundSensorReplaceInfoVO o1, GroundSensorReplaceInfoVO o2) {
					if (o1.getReplaceDate().after(o2.getReplaceDate())){
						return 1;
					}else if (o1.getReplaceDate().before(o2.getReplaceDate())){
						return -1;
					}
					return 0;
				}
			});
			if (replaceInfoVOList.size() == 0) {
				GroundSensorReplaceInfoVO vo = new GroundSensorReplaceInfoVO();
				vo.setRtuCode(rtuInfoDTO.getRtuCode());
				vo.setGroundSensorBrand(rtuInfoEntity.getGroundSensorBrand());
				vo.setGroundSensorModel(rtuInfoEntity.getGroundSensorModel());
				vo.setReplaceKind(0);
				vo.setReplaceDate(rtuInfoEntity.getRtuReplaceDate());
				replaceInfoVOList.add(vo);
			}
		}
		return R.data(replaceInfoVOList);
	}

	/**
	 * 测站蓄电池更换历史
	 */
	@GetMapping("/battery/replace/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "不分页")
	public R<List<BatteryReplaceInfoVO>> batteryReplacelist(@ApiIgnore RtuInfoDTO rtuInfoDTO) {
		List<BatteryReplaceInfoVO> replaceInfoVOList = new LinkedList<>();
		LambdaQueryWrapper<RtuInfoEntity> wrapper = Wrappers.<RtuInfoEntity>query().lambda();
		wrapper.eq(RtuInfoEntity::getIsDeleted, 0);
		wrapper.eq(RtuInfoEntity::getRtuCode, rtuInfoDTO.getRtuCode());
		RtuInfoEntity rtuInfoEntity = rtuBaseInfoService.getOne(wrapper);
		if (null != rtuInfoEntity) {
			LambdaQueryWrapper<CheckOrderInfoEntity> orderwrapper = Wrappers.<CheckOrderInfoEntity>query().lambda();
			orderwrapper.eq(CheckOrderInfoEntity::getIsDeleted, 0);
			orderwrapper.eq(CheckOrderInfoEntity::getRtuCode, rtuInfoDTO.getRtuCode());

			List<CheckOrderInfoEntity> checkOrderInfoEntityList = rtuCheckOrderService.list(orderwrapper);
			for (CheckOrderInfoEntity checkOrderInfoEntity : checkOrderInfoEntityList) {

				CheckOrderInfoDTO checkOrderInfoDTO = new CheckOrderInfoDTO();
				checkOrderInfoDTO.setIsDeleted(0);
				checkOrderInfoDTO.setId(checkOrderInfoEntity.getId());

				List<OrderProcessInfoVO> orderProcessInfoEntityList = rtuOrderProcessService.selectList(checkOrderInfoDTO);
				for (OrderProcessInfoVO orderProcessInfoEntity : orderProcessInfoEntityList) {
					DictBiz biz = DictBizCache.getById(orderProcessInfoEntity.getOrderStatusKey());
					if (biz.getDictKey().equals(OrderStatusEnum.STATUS_PROCESS.getCode())) {
						if (orderProcessInfoEntity.getBatteryReplace() != null && orderProcessInfoEntity.getBatteryReplace() == 1) {
							BatteryReplaceInfoVO vo = new BatteryReplaceInfoVO();
							vo.setRtuCode(rtuInfoDTO.getRtuCode());
							vo.setOrderId(checkOrderInfoEntity.getId());
							//vo.setBatteryModel(orderProcessInfoEntity.getb());
							vo.setReplaceKind(1);
							vo.setReplaceDate(orderProcessInfoEntity.getCreateTime());
                            vo.setReplaceDesc(orderProcessInfoEntity.getPowerDesc());
							replaceInfoVOList.add(vo);
						}
					}
				}
			}


			EquipmentInspectionReportDTO equipmentInspectionReportDTO = new EquipmentInspectionReportDTO();
			equipmentInspectionReportDTO.setIsDeleted(0);
			equipmentInspectionReportDTO.setRtuCode(rtuInfoDTO.getRtuCode());

			List<EquipmentInspectionReportVO> inspectionReportEntityList = equipmentInspectionReportService.selectList(equipmentInspectionReportDTO);
			for(EquipmentInspectionReportVO inspectionReportEntity:inspectionReportEntityList){
				if (inspectionReportEntity.getBatteryReplace()!=null && inspectionReportEntity.getBatteryReplace()==1){
					BatteryReplaceInfoVO vo = new BatteryReplaceInfoVO();
					vo.setInspectionId(inspectionReportEntity.getId());
					vo.setRtuCode(rtuInfoDTO.getRtuCode());
					vo.setBatteryModel(inspectionReportEntity.getBatteryModelRemark());
					vo.setReplaceKind(2);
					vo.setReplaceDate(inspectionReportEntity.getCreateTime());
					//vo.setReplaceDesc(inspectionReportEntity.getBatteryModelRemark());
					replaceInfoVOList.add(vo);
				}
			}
			replaceInfoVOList.sort(new Comparator<BatteryReplaceInfoVO>() {
				@Override
				public int compare(BatteryReplaceInfoVO o1, BatteryReplaceInfoVO o2) {
					if (o1.getReplaceDate().after(o2.getReplaceDate())){
						return 1;
					}else if (o1.getReplaceDate().before(o2.getReplaceDate())){
						return -1;
					}
					return 0;
				}
			});
			if (replaceInfoVOList.size() == 0) {
				BatteryReplaceInfoVO vo = new BatteryReplaceInfoVO();
				vo.setRtuCode(rtuInfoDTO.getRtuCode());
				vo.setBatteryModel(rtuInfoEntity.getBatteryModel());
				vo.setReplaceKind(0);
				vo.setReplaceDate(rtuInfoEntity.getRtuReplaceDate());
				replaceInfoVOList.add(vo);
			}
		}
		return R.data(replaceInfoVOList);
	}
}
