/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/23
 */
package org.springblade.modules.business.equipment.inspection.report.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.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.constant.BusinessConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
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.ConcurrentDateFormat;
import org.springblade.core.tool.utils.Func;

import org.springblade.modules.baseinfo.org.entity.DeptRegionEntity;
import org.springblade.modules.baseinfo.org.service.IDeptRegionService;
import org.springblade.modules.baseinfo.rtu.entity.RtuInfoEntity;
import org.springblade.modules.baseinfo.rtu.service.IRtuBaseInfoService;
import org.springblade.modules.business.equipment.inspection.base.entity.EquipmentInspectionInfoEntity;
import org.springblade.modules.business.equipment.inspection.base.service.IEquipmentInspectionService;
import org.springblade.modules.business.equipment.inspection.plan.entity.EquipmentInspectionPlanEntity;
import org.springblade.modules.business.equipment.inspection.plan.service.IEquipmentInspectionPlanService;
import org.springblade.modules.business.equipment.inspection.report.dto.EquipmentInspectionReportDTO;
import org.springblade.modules.business.equipment.inspection.report.entity.EquipmentInspectionExportEntity;
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.equipment.inspection.report.wrapper.EquipmentInspectionReportWrapper;
import org.springblade.modules.resource.builder.oss.OssBuilder;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
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/23
 * Title:设备巡检填报管理模块
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/equipment/inspection/report")
@RequiredArgsConstructor
@Slf4j
@Api(value = "设备巡检填报管理模块", tags = "设备巡检填报管理模块")
public class EquipmentInspectionReportController extends BladeController {
	private final IEquipmentInspectionReportService reportService;
	private final IEquipmentInspectionService inspectionService;
	private final IEquipmentInspectionPlanService planService;
	private final IDeptRegionService deptRegionService;
	private final IRtuBaseInfoService rtuBaseInfoService;

	@Resource
	private OssBuilder ossBuilder;

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

	@Value("${export-config.inspection-remainingproblems-template}")
	private String inspectionRemainingProblemsTemplate;


	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "adCode", value = "行政区划编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "inspectionYear", value = "巡检年度", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<List<EquipmentInspectionReportVO>> list(@ApiIgnore EquipmentInspectionReportDTO inspectionReportDTO) {
		if (inspectionReportDTO.getInspectionYear() != null) {
			Date year = Func.parseDate(inspectionReportDTO.getInspectionYear(), "yyyy-MM-dd");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(year);
			calendar.set(Calendar.MONTH, 0);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			inspectionReportDTO.setInspectionYearStartDate(calendar.getTime());
			calendar.add(Calendar.YEAR, 1);
			inspectionReportDTO.setInspectionYearEndDate(calendar.getTime());
		} else {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.set(Calendar.MONTH, 0);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			inspectionReportDTO.setInspectionYearStartDate(calendar.getTime());
			calendar.add(Calendar.YEAR, 1);
			inspectionReportDTO.setInspectionYearEndDate(calendar.getTime());
		}
		List<EquipmentInspectionReportVO> list = reportService.selectList(inspectionReportDTO);
		for (EquipmentInspectionReportVO vo : list) {
			EquipmentInspectionReportWrapper.build().entityVO(vo);
		}
		return R.data(list);
	}

	/**
	 * 分页
	 */
	@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 = "inspectionYear", value = "巡检年度", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rainSeasonKind", value = "汛期类型", paramType = "query", dataType = "int"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<EquipmentInspectionReportVO>> page(@ApiIgnore EquipmentInspectionReportDTO inspectionReportDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
			if (inspectionReportDTO.getAdCode() == null) {
				inspectionReportDTO.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 (inspectionReportDTO.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);
				inspectionReportDTO.setAdCode(deptRegionEntity.getAdcd());
			}
		}
		if (inspectionReportDTO.getInspectionYear() != null) {
			Date year = Func.parseDate(inspectionReportDTO.getInspectionYear(), "yyyy-MM-dd");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(year);
			calendar.set(Calendar.MONTH, 0);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			inspectionReportDTO.setInspectionYearStartDate(calendar.getTime());
			calendar.add(Calendar.YEAR, 1);
			inspectionReportDTO.setInspectionYearEndDate(calendar.getTime());
		} else {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.set(Calendar.MONTH, 0);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			inspectionReportDTO.setInspectionYearStartDate(calendar.getTime());
			calendar.add(Calendar.YEAR, 1);
			inspectionReportDTO.setInspectionYearEndDate(calendar.getTime());
		}
		IPage<EquipmentInspectionReportVO> pages = reportService.selectPage(Condition.getPage(query), inspectionReportDTO);
		List<EquipmentInspectionReportVO> list = pages.getRecords();
		for (EquipmentInspectionReportVO vo : list) {
			EquipmentInspectionReportWrapper.build().entityVO(vo);
		}
		return R.data(pages);
	}

	/**
	 * 查询我填报的巡检信息
	 */
	@GetMapping("/my/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<EquipmentInspectionReportVO>> myPage(@ApiIgnore EquipmentInspectionReportDTO inspectionReportDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		inspectionReportDTO.setCreateUser(user.getUserId());
		IPage<EquipmentInspectionReportVO> pages = reportService.selectPage(Condition.getPage(query), inspectionReportDTO);
		List<EquipmentInspectionReportVO> list = pages.getRecords();
		for (EquipmentInspectionReportVO vo : list) {
			EquipmentInspectionReportWrapper.build().entityVO(vo);
		}
		return R.data(pages);
	}

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入实体类EquipmentInspectionReportDTO")
	public R<EquipmentInspectionReportVO> detail(EquipmentInspectionReportDTO inspectionReportDTO) {
		EquipmentInspectionReportEntity detail = reportService.getById(inspectionReportDTO.getId());
		EquipmentInspectionReportVO vo = EquipmentInspectionReportWrapper.build().entityVO(detail);
		return R.data(vo);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入实体类EquipmentInspectionReportEntity ")
	public R save(@RequestBody EquipmentInspectionReportEntity inspectionReportEntity) {
		BladeUser user = AuthUtil.getUser();
		Calendar dt = Calendar.getInstance();
		dt.setTime(new Date());
		Calendar calendarStart = Calendar.getInstance();
		Calendar calendarEnd = Calendar.getInstance();
		inspectionReportEntity.setRainSeasonKind(0);
		String rainSeasonText = "";
		//处理汛期类别
		LambdaQueryWrapper<EquipmentInspectionPlanEntity> wrapper = Wrappers.<EquipmentInspectionPlanEntity>query().lambda();
		wrapper.eq(EquipmentInspectionPlanEntity::getIsDeleted, 0);
		wrapper.orderByDesc(EquipmentInspectionPlanEntity::getCreateTime);
		List<EquipmentInspectionPlanEntity> list = planService.list(wrapper);
		if (null != list && list.size() > 0) {
			EquipmentInspectionPlanEntity entity = list.get(0);
			//汛前上报
			calendarStart.setTime(entity.getBeforeRainSeasonStartTime());
			calendarEnd.setTime(entity.getBeforeRainSeasonEndTime());
			rainSeasonText += "汛前巡检日期:" + Func.formatDate(calendarStart.getTime()) + "-" + Func.formatDate(calendarEnd.getTime());
			if ((dt.getTime().getTime() / 1000 == calendarStart.getTime().getTime() / 1000 || dt.after(calendarStart)) && dt.before(calendarEnd)) {
				inspectionReportEntity.setRainSeasonKind(1);
			}
			//汛中第一次上报
			calendarStart.setTime(entity.getRainSeasonFirstStartTime());
			calendarEnd.setTime(entity.getRainSeasonFirstEndTime());
			rainSeasonText += ",汛中（一）巡检日期:" + Func.formatDate(calendarStart.getTime()) + "-" + Func.formatDate(calendarEnd.getTime());
			if ((dt.getTime().getTime() / 1000 == calendarStart.getTime().getTime() / 1000 || dt.after(calendarStart)) && dt.before(calendarEnd)) {
				inspectionReportEntity.setRainSeasonKind(2);
			}
			//汛中第二次上报
			calendarStart.setTime(entity.getRainSeasonSecondStartTime());
			calendarEnd.setTime(entity.getRainSeasonSecondEndTime());
			rainSeasonText += ",汛中（二）巡检日期:" + Func.formatDate(calendarStart.getTime()) + "-" + Func.formatDate(calendarEnd.getTime());
			if ((dt.getTime().getTime() / 1000 == calendarStart.getTime().getTime() / 1000 || dt.after(calendarStart)) && dt.before(calendarEnd)) {
				inspectionReportEntity.setRainSeasonKind(3);
			}
		} else {
			//汛前上报
			calendarStart.setTime(dt.getTime());
			calendarStart.set(Calendar.MONTH, 4 - 1);
			calendarStart.set(Calendar.DAY_OF_MONTH, 1);
			calendarStart.set(Calendar.HOUR_OF_DAY, 0);
			calendarStart.set(Calendar.MINUTE, 0);
			calendarStart.set(Calendar.SECOND, 0);
			calendarEnd.setTime(dt.getTime());
			calendarEnd.set(Calendar.MONTH, 6 - 1);
			calendarEnd.set(Calendar.DAY_OF_MONTH, 1);
			calendarEnd.set(Calendar.HOUR_OF_DAY, 0);
			calendarEnd.set(Calendar.MINUTE, 0);
			calendarEnd.set(Calendar.SECOND, 0);
			rainSeasonText += "汛前巡检日期:" + Func.formatDate(calendarStart.getTime()) + "-" + Func.formatDate(calendarEnd.getTime());
			if ((dt.getTime().getTime() / 1000 == calendarStart.getTime().getTime() / 1000 || dt.after(calendarStart)) && dt.before(calendarEnd)) {
				inspectionReportEntity.setRainSeasonKind(1);
			}
			//汛中第一次上报
			calendarStart.setTime(dt.getTime());
			calendarStart.set(Calendar.MONTH, 6 - 1);
			calendarStart.set(Calendar.DAY_OF_MONTH, 1);
			calendarStart.set(Calendar.HOUR_OF_DAY, 0);
			calendarStart.set(Calendar.MINUTE, 0);
			calendarStart.set(Calendar.SECOND, 0);
			calendarEnd.setTime(dt.getTime());
			calendarEnd.set(Calendar.MONTH, 8 - 1);
			calendarEnd.set(Calendar.DAY_OF_MONTH, 15);
			calendarEnd.set(Calendar.HOUR_OF_DAY, 0);
			calendarEnd.set(Calendar.MINUTE, 0);
			calendarEnd.set(Calendar.SECOND, 0);
			rainSeasonText += ",汛中（一）巡检日期:" + Func.formatDate(calendarStart.getTime()) + "-" + Func.formatDate(calendarEnd.getTime());
			if ((dt.getTime().getTime() / 1000 == calendarStart.getTime().getTime() / 1000 || dt.after(calendarStart)) && dt.before(calendarEnd)) {
				inspectionReportEntity.setRainSeasonKind(2);
			}
			//汛中第二次上报
			calendarStart.setTime(dt.getTime());
			calendarStart.set(Calendar.MONTH, 8 - 1);
			calendarStart.set(Calendar.DAY_OF_MONTH, 15);
			calendarStart.set(Calendar.HOUR_OF_DAY, 0);
			calendarStart.set(Calendar.MINUTE, 0);
			calendarStart.set(Calendar.SECOND, 0);
			calendarEnd.setTime(dt.getTime());
			calendarEnd.set(Calendar.MONTH, 10 - 1);
			calendarEnd.set(Calendar.DAY_OF_MONTH, 1);
			calendarEnd.set(Calendar.HOUR_OF_DAY, 0);
			calendarEnd.set(Calendar.MINUTE, 0);
			calendarEnd.set(Calendar.SECOND, 0);
			rainSeasonText += ",汛中（二）巡检日期:" + Func.formatDate(calendarStart.getTime()) + "-" + Func.formatDate(calendarEnd.getTime());
			if ((dt.getTime().getTime() / 1000 == calendarStart.getTime().getTime() / 1000 || dt.after(calendarStart)) && dt.before(calendarEnd)) {
				inspectionReportEntity.setRainSeasonKind(3);
			}
		}
		if (inspectionReportEntity.getRainSeasonKind() == 0) {
			Map<String, Object> d = new HashMap<>();
			d.put("code", 1);
			d.put("msg", "当前不在汛期填报期(" + rainSeasonText + ")，请确认！");
			return R.data(d);
		}
		//处理遗留问题
		String remainQuestion = inspectionReportEntity.getRemainQuestion();
		if (remainQuestion != null && remainQuestion.length() > 0) {
			inspectionReportEntity.setRemainQuestionState(1);
		}
		EquipmentInspectionInfoEntity infoEntity = new EquipmentInspectionInfoEntity();
		infoEntity.setInspectionDate(new Date());
		//查找是否有主表记录
		calendarStart = Calendar.getInstance();
		calendarStart.setTime(dt.getTime());
		calendarEnd = Calendar.getInstance();
		calendarEnd.setTime(dt.getTime());
		calendarStart.set(Calendar.MONTH, 0);
		calendarStart.set(Calendar.DAY_OF_MONTH, 1);
		calendarEnd.add(Calendar.YEAR, 1);
		calendarEnd.set(Calendar.MONTH, 0);
		calendarEnd.set(Calendar.DAY_OF_MONTH, 1);
		LambdaQueryWrapper<EquipmentInspectionInfoEntity> infowrapper = Wrappers.<EquipmentInspectionInfoEntity>query().lambda();
		infowrapper.eq(EquipmentInspectionInfoEntity::getIsDeleted, 0);
		infowrapper.eq(EquipmentInspectionInfoEntity::getRtuCode, inspectionReportEntity.getRtuCode());
		infowrapper.ge(EquipmentInspectionInfoEntity::getInspectionDate, Func.formatDate(calendarStart.getTime()));
		infowrapper.lt(EquipmentInspectionInfoEntity::getInspectionDate, Func.formatDate(calendarEnd.getTime()));
		List<EquipmentInspectionInfoEntity> infoEntityList = inspectionService.list(infowrapper);
		if (null != infoEntityList && infoEntityList.size() == 1) {
			infoEntity = infoEntityList.get(0);
		} else {
			infoEntity.setRtuCode(inspectionReportEntity.getRtuCode());
		}
		infoEntity.setInspectionDate(dt.getTime());
		//写入填报子表，判断当前汛期是否已经填报
		if (1 == inspectionReportEntity.getRainSeasonKind()) {
			if (infoEntity.getBeforeRainSeasonStatus() != null && infoEntity.getBeforeRainSeasonStatus() == 1) {
				Map<String, Object> d = new HashMap<>();
				d.put("code", 1);
				d.put("msg", "汛前巡检已填报，请勿重复上报！");
				return R.data(d);
			}
		} else if (2 == inspectionReportEntity.getRainSeasonKind()) {
			if (infoEntity.getRainSeasonFirstStatus() != null && infoEntity.getRainSeasonFirstStatus() == 1) {
				Map<String, Object> d = new HashMap<>();
				d.put("code", 1);
				d.put("msg", "汛中（一）巡检已填报，请勿重复上报！");
				return R.data(d);
			}
		} else if (3 == inspectionReportEntity.getRainSeasonKind()) {
			if (infoEntity.getRainSeasonSecondStatus() != null && infoEntity.getRainSeasonSecondStatus() == 1) {
				Map<String, Object> d = new HashMap<>();
				d.put("code", 1);
				d.put("msg", "汛中（二）巡检已填报，请勿重复上报！");
				return R.data(d);
			}
		}
		reportService.save(inspectionReportEntity);
		if (1 == inspectionReportEntity.getRainSeasonKind()) {
			infoEntity.setBeforeRainSeasonReportId(inspectionReportEntity.getId());
			infoEntity.setBeforeRainSeasonStatus(1);
			infoEntity.setBeforeRainSeasonReportTime(infoEntity.getInspectionDate());
			infoEntity.setBeforeRainSeasonReportUser(user.getUserId());
		} else if (2 == inspectionReportEntity.getRainSeasonKind()) {
			infoEntity.setRainSeasonFirstReportId(inspectionReportEntity.getId());
			infoEntity.setRainSeasonFirstStatus(1);
			infoEntity.setRainSeasonFirstReportTime(infoEntity.getInspectionDate());
			infoEntity.setRainSeasonFirstReportUser(user.getUserId());
		} else if (3 == inspectionReportEntity.getRainSeasonKind()) {
			infoEntity.setRainSeasonSecondReportId(inspectionReportEntity.getId());
			infoEntity.setRainSeasonSecondStatus(1);
			infoEntity.setRainSeasonSecondReportTime(infoEntity.getInspectionDate());
			infoEntity.setRainSeasonSecondReportUser(user.getUserId());
		}
		inspectionService.saveOrUpdate(infoEntity);

		EquipmentInspectionReportEntity reportEntity = reportService.getById(inspectionReportEntity.getId());
		reportEntity.setInspectionId(infoEntity.getId());
		reportService.updateById(reportEntity);

		//更新更换信息
		LambdaQueryWrapper<RtuInfoEntity> rtuWrapper = Wrappers.<RtuInfoEntity>query().lambda();
		rtuWrapper.eq(RtuInfoEntity::getIsDeleted, 0);
		rtuWrapper.eq(RtuInfoEntity::getRtuCode, infoEntity.getRtuCode());
		RtuInfoEntity rtuInfoEntity = rtuBaseInfoService.getOne(rtuWrapper);
		if (inspectionReportEntity.getRtuReplace() != null && inspectionReportEntity.getRtuReplace() == 1) {
			rtuInfoEntity.setRtuReplaceDate(inspectionReportEntity.getCreateTime());
			rtuInfoEntity.setRtuModel(inspectionReportEntity.getRtuModelRemark());
		}
		if (inspectionReportEntity.getRainSensorReplace() != null && inspectionReportEntity.getRainSensorReplace() == 1) {
			rtuInfoEntity.setRainSensorReplaceDate(inspectionReportEntity.getCreateTime());
			rtuInfoEntity.setRainSensorModel(inspectionReportEntity.getRainSensorModelRemark());
		}
		if (inspectionReportEntity.getWaterSensorReplace() != null && inspectionReportEntity.getWaterSensorReplace() == 1) {
			rtuInfoEntity.setWaterSensorReplaceDate(inspectionReportEntity.getCreateTime());
			rtuInfoEntity.setWaterSensorModel(inspectionReportEntity.getWaterSensorModelRemark());
		}
		if (inspectionReportEntity.getGroundSensorReplace() != null && inspectionReportEntity.getGroundSensorReplace() == 1) {
			rtuInfoEntity.setGroundSensorReplaceDate(inspectionReportEntity.getCreateTime());
			rtuInfoEntity.setGroundSensorModel(inspectionReportEntity.getGroundSensorModelRemark());
		}
		if (inspectionReportEntity.getBatteryReplace() != null && inspectionReportEntity.getBatteryReplace() == 1) {
			rtuInfoEntity.setBatteryReplaceDate(inspectionReportEntity.getCreateTime());
			rtuInfoEntity.setBatteryModel(inspectionReportEntity.getBatteryModelRemark());
		}

		rtuInfoEntity.setNetworkSimId(inspectionReportEntity.getNetworkSimId());
		rtuInfoEntity.setNetworkPayer(inspectionReportEntity.getNetworkPayer());

		rtuBaseInfoService.updateById(rtuInfoEntity);
		Map<String, Object> d = new HashMap<>();
		d.put("code", 0);
		return R.data(d);

	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入reportEntity")
	public R update(@RequestBody EquipmentInspectionReportEntity reportEntity) {
		//处理遗留问题
		String remainQuestion = reportEntity.getRemainQuestion();
		if (remainQuestion != null && remainQuestion.length() > 0) {
			reportEntity.setRemainQuestionState(1);
		}
		return R.status(reportService.updateById(reportEntity));
	}

	/**
	 * 修改
	 */
	@PostMapping("/nonetworksignal/photo/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入orderProcessInfoEntity")
	public R nonetworksignalPhotoUpdate(@RequestBody EquipmentInspectionReportEntity reportEntity) {
		EquipmentInspectionReportEntity entity = reportService.getById(reportEntity.getId());
		entity.setSiteCheckPhotos(reportEntity.getSiteCheckPhotos());
		entity.setRainSiteCheckPhotos(reportEntity.getRainSiteCheckPhotos());
		entity.setWaterSiteCheckPhotos(reportEntity.getWaterSiteCheckPhotos());
		entity.setGroundSiteCheckPhotos(reportEntity.getGroundSiteCheckPhotos());
		entity.setOtherEquipmentCheckPhotos(reportEntity.getOtherEquipmentCheckPhotos());
		entity.setRtuReplacePhotos(reportEntity.getRtuReplacePhotos());
		entity.setRainSensorReplacePhotos(reportEntity.getRainSensorReplacePhotos());
		entity.setWaterSensorReplacePhotos(reportEntity.getWaterSensorReplacePhotos());
		entity.setGroundSensorReplacePhotos(reportEntity.getGroundSensorReplacePhotos());
		entity.setBatteryReplacePhotos(reportEntity.getBatteryReplacePhotos());
		entity.setBatteryControllerReplacePhotos(reportEntity.getBatteryControllerReplacePhotos());
		entity.setOtherEquipmentReplacePhotos(reportEntity.getOtherEquipmentReplacePhotos());
		entity.setSunPowerReplacePhotos(reportEntity.getSunPowerReplacePhotos());
		entity.setInspectionWorkPhotos(reportEntity.getInspectionWorkPhotos());
		return R.status(reportService.updateById(entity));
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合") @RequestParam String ids) {
		boolean temp = reportService.deleteLogic(Func.toLongList(ids));
		return R.status(temp);
	}

	/**
	 * 查询当天的巡检填报信息
	 */
	@GetMapping("/today/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "查询")
	public R<List<EquipmentInspectionReportVO>> todayList(@ApiIgnore EquipmentInspectionReportDTO inspectionReportDTO) {
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
			inspectionReportDTO.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())) {
			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);
			inspectionReportDTO.setAdCode(deptRegionEntity.getAdcd());
		}

		Date date = new Date();
		String dt = Func.formatDateTime(date);
		inspectionReportDTO.setCreateTime(Func.parse(dt, ConcurrentDateFormat.of("yyyy-MM-dd")));
		List<EquipmentInspectionReportVO> list = reportService.selectTodayReportList(inspectionReportDTO);
		for (EquipmentInspectionReportVO vo : list) {
			EquipmentInspectionReportWrapper.build().entityVO(vo);
		}
		return R.data(list);
	}

	/**
	 * 查询当天的巡检填报信息
	 */
	@GetMapping("/today/page")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "查询")
	public R<IPage<EquipmentInspectionReportVO>> todayPage(@ApiIgnore EquipmentInspectionReportDTO inspectionReportDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
			inspectionReportDTO.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())) {
			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);
			inspectionReportDTO.setAdCode(deptRegionEntity.getAdcd());
		}

		Date date = new Date();
		String dt = Func.formatDateTime(date);
		inspectionReportDTO.setCreateTime(Func.parse(dt, ConcurrentDateFormat.of("yyyy-MM-dd")));
		IPage<EquipmentInspectionReportVO> page = reportService.selectTodayReportPage(Condition.getPage(query),inspectionReportDTO);

		List<EquipmentInspectionReportVO> list = page.getRecords();
		for (EquipmentInspectionReportVO vo : list) {
			EquipmentInspectionReportWrapper.build().entityVO(vo);
		}
		return R.data(page);
	}


	@GetMapping("/count")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "adCode", value = "行政区划编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "inspectionYear", value = "巡检年度", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rainSeasonKind", value = "汛期类型", paramType = "query", dataType = "int"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "已巡检上报设备数量")
	public R<Map<String, Object>> inspectionReportCount(@ApiIgnore EquipmentInspectionReportDTO inspectionReportDTO) {
		if (inspectionReportDTO.getAdCode() == null) {
			return R.status(false);
		}
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
			if (inspectionReportDTO.getAdCode() == null) {
				inspectionReportDTO.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 (inspectionReportDTO.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);
				inspectionReportDTO.setAdCode(deptRegionEntity.getAdcd());
			}
		}
		if (inspectionReportDTO.getInspectionYear() != null) {
			Date year = Func.parseDate(inspectionReportDTO.getInspectionYear(), "yyyy-MM-dd");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(year);
			calendar.set(Calendar.MONTH, 0);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			inspectionReportDTO.setInspectionYearStartDate(calendar.getTime());
			calendar.add(Calendar.YEAR, 1);
			inspectionReportDTO.setInspectionYearEndDate(calendar.getTime());
		} else {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.set(Calendar.MONTH, 0);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			inspectionReportDTO.setInspectionYearStartDate(calendar.getTime());
			calendar.add(Calendar.YEAR, 1);
			inspectionReportDTO.setInspectionYearEndDate(calendar.getTime());
		}
		Map<String, Object> data = new HashMap<>();
		long count = reportService.equipmentInspectionReportCount(inspectionReportDTO);
		data.put("inspectionCount", count);
		return R.data(data);
	}

	/**
	 * 遗留问题信息列表,分页
	 */
	@GetMapping("/remainingproblems/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "inspectionYear", value = "巡检年度", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rainSeasonKind", value = "汛期类型", paramType = "query", dataType = "int"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "遗留问题信息列表")
	public R<IPage<EquipmentInspectionReportVO>> remainingproblemsPage(@ApiIgnore EquipmentInspectionReportDTO inspectionReportDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
			inspectionReportDTO.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())) {
			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);
			inspectionReportDTO.setAdCode(deptRegionEntity.getAdcd());
		}
		if (inspectionReportDTO.getInspectionYear() != null) {
			Date year = Func.parseDate(inspectionReportDTO.getInspectionYear(), "yyyy-MM-dd");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(year);
			calendar.set(Calendar.MONTH, 0);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			inspectionReportDTO.setInspectionYearStartDate(calendar.getTime());
			calendar.add(Calendar.YEAR, 1);
			inspectionReportDTO.setInspectionYearEndDate(calendar.getTime());
		} else {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.set(Calendar.MONTH, 0);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			inspectionReportDTO.setInspectionYearStartDate(calendar.getTime());
			calendar.add(Calendar.YEAR, 1);
			inspectionReportDTO.setInspectionYearEndDate(calendar.getTime());
		}
		IPage<EquipmentInspectionReportVO> pages = reportService.selectRemainingproblemsPage(Condition.getPage(query), inspectionReportDTO);
		return R.data(pages);
	}


	/**
	 * 遗留问题关闭
	 *
	 * @param reportEntity
	 * @return
	 */
	@PostMapping("/remainingproblems/close")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "工单ID", paramType = "query", dataType = "long"),
		@ApiImplicitParam(name = "remainQuestionResolution", value = "处理意见", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "遗留问题关闭操作", notes = "传入实体类EquipmentInspectionReportEntity")
	public R remainingProblemsClose(@RequestBody EquipmentInspectionReportEntity reportEntity) {
		if (reportEntity.getId() == null) {
			return R.fail("参数错误!");
		}
		EquipmentInspectionReportEntity entity = reportService.getById(reportEntity.getId());
		if (null == entity) {
			return R.status(false);
		}
		entity.setRemainQuestionState(2);
		entity.setRemainQuestionResolution(reportEntity.getRemainQuestionResolution());
		reportService.updateById(entity);
		return R.success("操作成功！");
	}

	@GetMapping("/monthremainingproblems/export")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "当前汛期巡检遗留问题导出", notes = "")
	public R<Map<String, Object>> monthRemainingProblemsExport() {
		EquipmentInspectionReportDTO inspectionReportDTO = new EquipmentInspectionReportDTO();
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
			inspectionReportDTO.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())) {
			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);
			inspectionReportDTO.setAdCode(deptRegionEntity.getAdcd());
		}
		//文件标题
		String title = "";
		//当前时间
		Calendar dt = Calendar.getInstance();
		dt.setTime(new Date());
		//处理汛期类别
		LambdaQueryWrapper<EquipmentInspectionPlanEntity> wrapper = Wrappers.<EquipmentInspectionPlanEntity>query().lambda();
		wrapper.eq(EquipmentInspectionPlanEntity::getIsDeleted, 0);
		wrapper.orderByDesc(EquipmentInspectionPlanEntity::getCreateTime);
		EquipmentInspectionPlanEntity planEntity = new EquipmentInspectionPlanEntity();
		List<EquipmentInspectionPlanEntity> planEntityList = planService.list(wrapper);
		if (null != planEntityList && planEntityList.size() > 0) {
			EquipmentInspectionPlanEntity entity = planEntityList.get(0);
			planEntity.setBeforeRainSeasonStartTime(entity.getBeforeRainSeasonStartTime());
			planEntity.setBeforeRainSeasonEndTime(entity.getBeforeRainSeasonEndTime());
			planEntity.setRainSeasonFirstStartTime(entity.getRainSeasonFirstStartTime());
			planEntity.setRainSeasonFirstEndTime(entity.getRainSeasonFirstEndTime());
			planEntity.setRainSeasonSecondStartTime(entity.getRainSeasonSecondStartTime());
			planEntity.setRainSeasonSecondEndTime(entity.getRainSeasonSecondEndTime());
		} else {
			Calendar calendarStart = Calendar.getInstance();
			Calendar calendarEnd = Calendar.getInstance();
			//汛前上报
			calendarStart.setTime(new Date());
			calendarStart.set(Calendar.MONTH, 4 - 1);
			calendarStart.set(Calendar.DAY_OF_MONTH, 1);
			calendarStart.set(Calendar.HOUR_OF_DAY, 0);
			calendarStart.set(Calendar.MINUTE, 0);
			calendarStart.set(Calendar.SECOND, 0);
			calendarEnd.setTime(new Date());
			calendarEnd.set(Calendar.MONTH, 6 - 1);
			calendarEnd.set(Calendar.DAY_OF_MONTH, 1);
			calendarEnd.set(Calendar.HOUR_OF_DAY, 0);
			calendarEnd.set(Calendar.MINUTE, 0);
			calendarEnd.set(Calendar.SECOND, 0);
			planEntity.setBeforeRainSeasonStartTime(calendarStart.getTime());
			planEntity.setBeforeRainSeasonEndTime(calendarEnd.getTime());
			//汛中第一次上报
			calendarStart.setTime(new Date());
			calendarStart.set(Calendar.MONTH, 6 - 1);
			calendarStart.set(Calendar.DAY_OF_MONTH, 1);
			calendarStart.set(Calendar.HOUR_OF_DAY, 0);
			calendarStart.set(Calendar.MINUTE, 0);
			calendarStart.set(Calendar.SECOND, 0);
			calendarEnd.setTime(new Date());
			calendarEnd.set(Calendar.MONTH, 8 - 1);
			calendarEnd.set(Calendar.DAY_OF_MONTH, 15);
			calendarEnd.set(Calendar.HOUR_OF_DAY, 0);
			calendarEnd.set(Calendar.MINUTE, 0);
			calendarEnd.set(Calendar.SECOND, 0);
			planEntity.setRainSeasonFirstStartTime(calendarStart.getTime());
			planEntity.setRainSeasonFirstEndTime(calendarEnd.getTime());
			//汛中第二次上报
			calendarStart.setTime(new Date());
			calendarStart.set(Calendar.MONTH, 8 - 1);
			calendarStart.set(Calendar.DAY_OF_MONTH, 15);
			calendarStart.set(Calendar.HOUR_OF_DAY, 0);
			calendarStart.set(Calendar.MINUTE, 0);
			calendarStart.set(Calendar.SECOND, 0);
			calendarEnd.setTime(new Date());
			calendarEnd.set(Calendar.MONTH, 10 - 1);
			calendarEnd.set(Calendar.DAY_OF_MONTH, 1);
			calendarEnd.set(Calendar.HOUR_OF_DAY, 0);
			calendarEnd.set(Calendar.MINUTE, 0);
			calendarEnd.set(Calendar.SECOND, 0);
			planEntity.setRainSeasonSecondStartTime(calendarStart.getTime());
			planEntity.setRainSeasonSecondEndTime(calendarEnd.getTime());
		}
		Calendar calendarStart = Calendar.getInstance();
		calendarStart.setTime(planEntity.getBeforeRainSeasonStartTime());
		Calendar calendarEnd = Calendar.getInstance();
		//汛前上报
		calendarEnd.setTime(planEntity.getBeforeRainSeasonEndTime());
		calendarEnd.add(Calendar.DAY_OF_MONTH, -1);
		title = (calendarStart.get(Calendar.MONTH) + 1) + "月" + calendarStart.get(Calendar.DAY_OF_MONTH) + "日" + "-" + (calendarEnd.get(Calendar.MONTH) + 1) + "月" + calendarEnd.get(Calendar.DAY_OF_MONTH) + "日";
		calendarEnd.setTime(planEntity.getRainSeasonFirstStartTime());
		if ((dt.getTime().getTime() / 1000 == calendarStart.getTime().getTime() / 1000 || dt.after(calendarStart)) && dt.before(calendarEnd)) {
			inspectionReportDTO.setRainSeasonKind(1);
		} else {
			//汛中第一次上报
			calendarStart.setTime(planEntity.getRainSeasonFirstStartTime());
			calendarEnd.setTime(planEntity.getRainSeasonFirstEndTime());
			calendarEnd.add(Calendar.DAY_OF_MONTH, -1);
			title = (calendarStart.get(Calendar.MONTH) + 1) + "月" + calendarStart.get(Calendar.DAY_OF_MONTH) + "日" + "-" + (calendarEnd.get(Calendar.MONTH) + 1) + "月" + calendarEnd.get(Calendar.DAY_OF_MONTH) + "日";
			calendarEnd.setTime(planEntity.getRainSeasonFirstEndTime());
			if ((dt.getTime().getTime() / 1000 == calendarStart.getTime().getTime() / 1000 || dt.after(calendarStart)) && dt.before(calendarEnd)) {
				inspectionReportDTO.setRainSeasonKind(2);
			} else {
				//汛中第二次上报
				calendarStart.setTime(planEntity.getRainSeasonSecondStartTime());
				calendarEnd.setTime(planEntity.getRainSeasonSecondEndTime());
				calendarEnd.add(Calendar.DAY_OF_MONTH, -1);
				title = (calendarStart.get(Calendar.MONTH) + 1) + "月" + calendarStart.get(Calendar.DAY_OF_MONTH) + "日" + "-" + (calendarEnd.get(Calendar.MONTH) + 1) + "月" + calendarEnd.get(Calendar.DAY_OF_MONTH) + "日";
				calendarEnd.setTime(dt.getTime());
				calendarEnd.add(Calendar.YEAR, 1);
				calendarEnd.set(Calendar.MONTH, 0);
				calendarEnd.set(Calendar.DAY_OF_MONTH, 1);
				calendarEnd.set(Calendar.HOUR_OF_DAY, 0);
				calendarEnd.set(Calendar.MINUTE, 0);
				calendarEnd.set(Calendar.SECOND, 0);
				if ((dt.getTime().getTime() / 1000 == calendarStart.getTime().getTime() / 1000 || dt.after(calendarStart)) && dt.before(calendarEnd)) {
					inspectionReportDTO.setRainSeasonKind(3);
				}
			}
		}

		//生成数据
		List<EquipmentInspectionReportVO> reportEntityList = reportService.selectRainSeasonRemainingproblemsList(inspectionReportDTO);
		List<EquipmentInspectionExportEntity> list = new LinkedList<>();
		if (null != reportEntityList && reportEntityList.size() > 0) {
			int index = 1;
			for (EquipmentInspectionReportVO entity : reportEntityList) {
				EquipmentInspectionExportEntity exportEntity = new EquipmentInspectionExportEntity();
				exportEntity.setIndex(index);
				if (entity.getAdCity() != null) {
					exportEntity.setAdCity(entity.getAdCity());
				} else {
					exportEntity.setAdCity("");
				}
				if (entity.getAdDist() != null) {
					exportEntity.setAdDist(entity.getAdDist());
				} else {
					exportEntity.setAdDist("");
				}
				if (entity.getRtuName() != null) {
					exportEntity.setRtuName(entity.getRtuName());
				} else {
					exportEntity.setRtuName("");
				}
				exportEntity.setRtuCode(entity.getRtuCode());
				String rtuKindDesc = "";
				if (entity.getIsRain() != null && entity.getIsRain() == 1) {
					rtuKindDesc += "雨量站";
				}
				if ((entity.getIsRiver() != null && entity.getIsRiver() == 1) || (entity.getIsRes() != null && entity.getIsRes() == 1)) {
					if (rtuKindDesc.length() > 0) {
						rtuKindDesc += "/";
					}
					rtuKindDesc += "水位站";
				}
				if (entity.getIsGround() != null && entity.getIsGround() == 1) {
					if (rtuKindDesc.length() > 0) {
						rtuKindDesc += "/";
					}
					rtuKindDesc += "墒情站";
				}
				if (entity.getIsCamera() != null && entity.getIsCamera() == 1) {
					if (rtuKindDesc.length() > 0) {
						rtuKindDesc += "/";
					}
					rtuKindDesc += "视频站";
				}
				exportEntity.setRtuKindDesc(rtuKindDesc);
				if (entity.getLocationDesc() != null) {
					exportEntity.setLocationDesc(entity.getLocationDesc());
				} else {
					exportEntity.setLocationDesc("");
				}
				if (entity.getLng() != null) {
					exportEntity.setLng(entity.getLng());
				} else {
					exportEntity.setLng("");
				}
				if (entity.getLat() != null) {
					exportEntity.setLat(entity.getLat());
				} else {
					exportEntity.setLat("");
				}
				if (entity.getManageCompany() != null) {
					exportEntity.setManageCompany(entity.getManageCompany());
				} else {
					exportEntity.setManageCompany("");
				}
				if (entity.getServicePersonName() != null) {
					exportEntity.setServicePersonName(entity.getServicePersonName());
				} else {
					exportEntity.setServicePersonName("");
				}
				if (entity.getCreateTime() != null) {
					exportEntity.setCreateTime(Func.formatDate(entity.getCreateTime()));
				} else {
					exportEntity.setCreateTime("");
				}
				if (entity.getRemainQuestion() != null) {
					exportEntity.setRemainQuestion(entity.getRemainQuestion());
				} else {
					exportEntity.setRemainQuestion("");
				}
				if (entity.getRemainQuestionState() != null) {
					if (entity.getRemainQuestionState() == 1) {
						exportEntity.setRemainQuestionState("待处理");
					} else if (entity.getRemainQuestionState() == 2) {
						exportEntity.setRemainQuestionState("已处理完成");
					} else {
						exportEntity.setRemainQuestionState("");
					}
				} else {
					exportEntity.setRemainQuestionState("");
				}
				if (entity.getRemainQuestionResolution() != null) {
					exportEntity.setRemainQuestionResolution(entity.getRemainQuestionResolution());
				} else {
					exportEntity.setRemainQuestionResolution("");
				}
				list.add(exportEntity);
				index += 1;
			}
		}
		Map<String, Object> data = new HashMap<>();
		data.put("status", 0);
		String templateFilePath = this.workDir + File.separator + this.inspectionRemainingProblemsTemplate;
		String outpath = this.workDir + 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;
		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", "汛期(" + title + ")巡检遗留问题表");
			data.put("status", 1);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return R.data(data);
	}


	@GetMapping("/uncloseremainingproblems/export")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "当前汛期未关闭遗留问题导出", notes = "")
	public R<Map<String, Object>> uncloseRemainingProblemsExport() {
		EquipmentInspectionReportDTO inspectionReportDTO = new EquipmentInspectionReportDTO();
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
			inspectionReportDTO.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())) {
			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);
			inspectionReportDTO.setAdCode(deptRegionEntity.getAdcd());
		}
		//文件标题
		String title = "";
		//当前时间
		Calendar dt = Calendar.getInstance();
		dt.setTime(new Date());
		//处理汛期类别
		LambdaQueryWrapper<EquipmentInspectionPlanEntity> wrapper = Wrappers.<EquipmentInspectionPlanEntity>query().lambda();
		wrapper.eq(EquipmentInspectionPlanEntity::getIsDeleted, 0);
		wrapper.orderByDesc(EquipmentInspectionPlanEntity::getCreateTime);
		EquipmentInspectionPlanEntity planEntity = new EquipmentInspectionPlanEntity();
		List<EquipmentInspectionPlanEntity> planEntityList = planService.list(wrapper);
		if (null != planEntityList && planEntityList.size() > 0) {
			EquipmentInspectionPlanEntity entity = planEntityList.get(0);
			planEntity.setBeforeRainSeasonStartTime(entity.getBeforeRainSeasonStartTime());
			planEntity.setBeforeRainSeasonEndTime(entity.getBeforeRainSeasonEndTime());
			planEntity.setRainSeasonFirstStartTime(entity.getRainSeasonFirstStartTime());
			planEntity.setRainSeasonFirstEndTime(entity.getRainSeasonFirstEndTime());
			planEntity.setRainSeasonSecondStartTime(entity.getRainSeasonSecondStartTime());
			planEntity.setRainSeasonSecondEndTime(entity.getRainSeasonSecondEndTime());
		} else {
			Calendar calendarStart = Calendar.getInstance();
			Calendar calendarEnd = Calendar.getInstance();
			//汛前上报
			calendarStart.setTime(new Date());
			calendarStart.set(Calendar.MONTH, 4 - 1);
			calendarStart.set(Calendar.DAY_OF_MONTH, 1);
			calendarStart.set(Calendar.HOUR_OF_DAY, 0);
			calendarStart.set(Calendar.MINUTE, 0);
			calendarStart.set(Calendar.SECOND, 0);
			calendarEnd.setTime(new Date());
			calendarEnd.set(Calendar.MONTH, 6 - 1);
			calendarEnd.set(Calendar.DAY_OF_MONTH, 1);
			calendarEnd.set(Calendar.HOUR_OF_DAY, 0);
			calendarEnd.set(Calendar.MINUTE, 0);
			calendarEnd.set(Calendar.SECOND, 0);
			planEntity.setBeforeRainSeasonStartTime(calendarStart.getTime());
			planEntity.setBeforeRainSeasonEndTime(calendarEnd.getTime());
			//汛中第一次上报
			calendarStart.setTime(new Date());
			calendarStart.set(Calendar.MONTH, 6 - 1);
			calendarStart.set(Calendar.DAY_OF_MONTH, 1);
			calendarStart.set(Calendar.HOUR_OF_DAY, 0);
			calendarStart.set(Calendar.MINUTE, 0);
			calendarStart.set(Calendar.SECOND, 0);
			calendarEnd.setTime(new Date());
			calendarEnd.set(Calendar.MONTH, 8 - 1);
			calendarEnd.set(Calendar.DAY_OF_MONTH, 15);
			calendarEnd.set(Calendar.HOUR_OF_DAY, 0);
			calendarEnd.set(Calendar.MINUTE, 0);
			calendarEnd.set(Calendar.SECOND, 0);
			planEntity.setRainSeasonFirstStartTime(calendarStart.getTime());
			planEntity.setRainSeasonFirstEndTime(calendarEnd.getTime());
			//汛中第二次上报
			calendarStart.setTime(new Date());
			calendarStart.set(Calendar.MONTH, 8 - 1);
			calendarStart.set(Calendar.DAY_OF_MONTH, 15);
			calendarStart.set(Calendar.HOUR_OF_DAY, 0);
			calendarStart.set(Calendar.MINUTE, 0);
			calendarStart.set(Calendar.SECOND, 0);
			calendarEnd.setTime(new Date());
			calendarEnd.set(Calendar.MONTH, 10 - 1);
			calendarEnd.set(Calendar.DAY_OF_MONTH, 1);
			calendarEnd.set(Calendar.HOUR_OF_DAY, 0);
			calendarEnd.set(Calendar.MINUTE, 0);
			calendarEnd.set(Calendar.SECOND, 0);
			planEntity.setRainSeasonSecondStartTime(calendarStart.getTime());
			planEntity.setRainSeasonSecondEndTime(calendarEnd.getTime());
		}
		Calendar calendarStart = Calendar.getInstance();
		calendarStart.setTime(planEntity.getBeforeRainSeasonStartTime());
		Calendar calendarEnd = Calendar.getInstance();
		//汛前上报
		calendarEnd.setTime(planEntity.getBeforeRainSeasonEndTime());
		calendarEnd.add(Calendar.DAY_OF_MONTH, -1);
		title = (calendarStart.get(Calendar.MONTH) + 1) + "月" + calendarStart.get(Calendar.DAY_OF_MONTH) + "日" + "-" + (calendarEnd.get(Calendar.MONTH) + 1) + "月" + calendarEnd.get(Calendar.DAY_OF_MONTH) + "日";
		calendarEnd.setTime(planEntity.getRainSeasonFirstStartTime());
		if ((dt.getTime().getTime() / 1000 == calendarStart.getTime().getTime() / 1000 || dt.after(calendarStart)) && dt.before(calendarEnd)) {
			inspectionReportDTO.setRainSeasonKind(1);
		} else {
			//汛中第一次上报
			calendarStart.setTime(planEntity.getRainSeasonFirstStartTime());
			calendarEnd.setTime(planEntity.getRainSeasonFirstEndTime());
			calendarEnd.add(Calendar.DAY_OF_MONTH, -1);
			title = (calendarStart.get(Calendar.MONTH) + 1) + "月" + calendarStart.get(Calendar.DAY_OF_MONTH) + "日" + "-" + (calendarEnd.get(Calendar.MONTH) + 1) + "月" + calendarEnd.get(Calendar.DAY_OF_MONTH) + "日";
			calendarEnd.setTime(planEntity.getRainSeasonFirstEndTime());
			if ((dt.getTime().getTime() / 1000 == calendarStart.getTime().getTime() / 1000 || dt.after(calendarStart)) && dt.before(calendarEnd)) {
				inspectionReportDTO.setRainSeasonKind(2);
			} else {
				//汛中第二次上报
				calendarStart.setTime(planEntity.getRainSeasonSecondStartTime());
				calendarEnd.setTime(planEntity.getRainSeasonSecondEndTime());
				calendarEnd.add(Calendar.DAY_OF_MONTH, -1);
				title = (calendarStart.get(Calendar.MONTH) + 1) + "月" + calendarStart.get(Calendar.DAY_OF_MONTH) + "日" + "-" + (calendarEnd.get(Calendar.MONTH) + 1) + "月" + calendarEnd.get(Calendar.DAY_OF_MONTH) + "日";
				calendarEnd.setTime(dt.getTime());
				calendarEnd.add(Calendar.YEAR, 1);
				calendarEnd.set(Calendar.MONTH, 0);
				calendarEnd.set(Calendar.DAY_OF_MONTH, 1);
				calendarEnd.set(Calendar.HOUR_OF_DAY, 0);
				calendarEnd.set(Calendar.MINUTE, 0);
				calendarEnd.set(Calendar.SECOND, 0);
				if ((dt.getTime().getTime() / 1000 == calendarStart.getTime().getTime() / 1000 || dt.after(calendarStart)) && dt.before(calendarEnd)) {
					inspectionReportDTO.setRainSeasonKind(3);
				}
			}
		}
		//生成数据
		List<EquipmentInspectionReportVO> reportEntityList = reportService.selectRainSeasonUncolseRemainingproblemsList(inspectionReportDTO);
		List<EquipmentInspectionExportEntity> list = new LinkedList<>();
		if (null != reportEntityList && reportEntityList.size() > 0) {
			int index = 1;
			for (EquipmentInspectionReportVO entity : reportEntityList) {
				EquipmentInspectionExportEntity exportEntity = new EquipmentInspectionExportEntity();
				exportEntity.setIndex(index);
				if (entity.getAdCity() != null) {
					exportEntity.setAdCity(entity.getAdCity());
				} else {
					exportEntity.setAdCity("");
				}
				if (entity.getAdDist() != null) {
					exportEntity.setAdDist(entity.getAdDist());
				} else {
					exportEntity.setAdDist("");
				}
				if (entity.getRtuName() != null) {
					exportEntity.setRtuName(entity.getRtuName());
				} else {
					exportEntity.setRtuName("");
				}
				exportEntity.setRtuCode(entity.getRtuCode());
				String rtuKindDesc = "";
				if (entity.getIsRain() != null && entity.getIsRain() == 1) {
					rtuKindDesc += "雨量站";
				}
				if ((entity.getIsRiver() != null && entity.getIsRiver() == 1) || (entity.getIsRes() != null && entity.getIsRes() == 1)) {
					if (rtuKindDesc.length() > 0) {
						rtuKindDesc += "/";
					}
					rtuKindDesc += "水位站";
				}
				if (entity.getIsGround() != null && entity.getIsGround() == 1) {
					if (rtuKindDesc.length() > 0) {
						rtuKindDesc += "/";
					}
					rtuKindDesc += "墒情站";
				}
				if (entity.getIsCamera() != null && entity.getIsCamera() == 1) {
					if (rtuKindDesc.length() > 0) {
						rtuKindDesc += "/";
					}
					rtuKindDesc += "视频站";
				}
				exportEntity.setRtuKindDesc(rtuKindDesc);
				if (entity.getLocationDesc() != null) {
					exportEntity.setLocationDesc(entity.getLocationDesc());
				} else {
					exportEntity.setLocationDesc("");
				}
				if (entity.getLng() != null) {
					exportEntity.setLng(entity.getLng());
				} else {
					exportEntity.setLng("");
				}
				if (entity.getLat() != null) {
					exportEntity.setLat(entity.getLat());
				} else {
					exportEntity.setLat("");
				}
				if (entity.getManageCompany() != null) {
					exportEntity.setManageCompany(entity.getManageCompany());
				} else {
					exportEntity.setManageCompany("");
				}
				if (entity.getServicePersonName() != null) {
					exportEntity.setServicePersonName(entity.getServicePersonName());
				} else {
					exportEntity.setServicePersonName("");
				}
				if (entity.getCreateTime() != null) {
					exportEntity.setCreateTime(Func.formatDate(entity.getCreateTime()));
				} else {
					exportEntity.setCreateTime("");
				}
				if (entity.getRemainQuestion() != null) {
					exportEntity.setRemainQuestion(entity.getRemainQuestion());
				} else {
					exportEntity.setRemainQuestion("");
				}
				if (entity.getRemainQuestionState() != null) {
					if (entity.getRemainQuestionState() == 1) {
						exportEntity.setRemainQuestionState("待处理");
					} else if (entity.getRemainQuestionState() == 2) {
						exportEntity.setRemainQuestionState("已处理完成");
					} else {
						exportEntity.setRemainQuestionState("");
					}
				} else {
					exportEntity.setRemainQuestionState("");
				}
				if (entity.getRemainQuestionResolution() != null) {
					exportEntity.setRemainQuestionResolution(entity.getRemainQuestionResolution());
				} else {
					exportEntity.setRemainQuestionResolution("");
				}
				list.add(exportEntity);
				index += 1;
			}
		}
		Map<String, Object> data = new HashMap<>();
		data.put("status", 0);
		String templateFilePath = this.workDir + File.separator + this.inspectionRemainingProblemsTemplate;
		String outpath = this.workDir + 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;
		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", "汛期(" + title + ")巡检遗留问题表");
			data.put("status", 1);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return R.data(data);
	}


	/**
	 * 巡检上报-无信号上报信息列表,分页
	 */
	@GetMapping("/nonetworksignal/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtuName", value = "测站名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rtuCode", value = "测站编码", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "inspectionYear", value = "巡检年度", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "rainSeasonKind", value = "汛期类型", paramType = "query", dataType = "int"),
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "无信号上报信息列表")
	public R<IPage<EquipmentInspectionReportVO>> nonetworksignalReportPage(@ApiIgnore EquipmentInspectionReportDTO inspectionReportDTO, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
			inspectionReportDTO.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())) {
			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);
			inspectionReportDTO.setAdCode(deptRegionEntity.getAdcd());
		}
		if (inspectionReportDTO.getInspectionYear() != null) {
			Date year = Func.parseDate(inspectionReportDTO.getInspectionYear(), "yyyy-MM-dd");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(year);
			calendar.set(Calendar.MONTH, 0);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			inspectionReportDTO.setInspectionYearStartDate(calendar.getTime());
			calendar.add(Calendar.YEAR, 1);
			inspectionReportDTO.setInspectionYearEndDate(calendar.getTime());
		} else {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.set(Calendar.MONTH, 0);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			inspectionReportDTO.setInspectionYearStartDate(calendar.getTime());
			calendar.add(Calendar.YEAR, 1);
			inspectionReportDTO.setInspectionYearEndDate(calendar.getTime());
		}
		IPage<EquipmentInspectionReportVO> pages = reportService.selectNoNetworkSignalPage(Condition.getPage(query), inspectionReportDTO);
		return R.data(pages);
	}

	/**
	 * 巡检上报-无信号上报图片补交关闭
	 *
	 * @param reportEntity
	 * @return
	 */
	@PostMapping("/nonetworksignal/close")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "记录ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "图片补交关闭操作", notes = "传入实体类EquipmentInspectionReportEntity")
	public R noNetworkSignalPhotoSubmitClose(@RequestBody EquipmentInspectionReportEntity reportEntity) {
		if (reportEntity.getId() == null) {
			return R.fail("参数错误!");
		}
		EquipmentInspectionReportEntity entity = reportService.getById(reportEntity.getId());
		if (null == entity) {
			return R.status(false);
		}
		entity.setPhotoSupplementSubmitStatus(1);

		reportService.updateById(entity);
		return R.success("操作成功！");
	}

	@ApiLog("图片提交")
	@PostMapping(value = "/nonetworksignal/putPhoto")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "图片提交", notes = "")
	public R putFile(MultipartFile file) {
		BladeFile bladeFile = ossBuilder.template().putFile(file);
		return R.data(bladeFile);
	}
}
