自我记录用。只提供思路,代码不能直接复制 一·SERVICE接口: public ResponseResult<PageResult<BusInspectPlanVO>> selectSecurityPlanPage(BusInspectPlanPageVO busInspectPlanPageVO) { // 分页参数 Page<BusInspectPlanVO> pageParam = new Page<>(busInspectPlanPageVO.getPage(), busInspectPlanPageVO.getRows()); //分页查询 IPage<BusInspectPlanVO> pageModel = busInspectionPlanMapper.selectSecurityPlanPage(pageParam, busInspectPlanPageVO); Date date = new Date(); List<BusInspectPlanVO> busInspectPlanVOList = new ArrayList<>(); for (BusInspectPlanVO busInspectionPlan : pageModel.getRecords()) { if (date.after(busInspectionPlan.getEndDate())) { busInspectionPlan.setStatus("已失效"); } else if (date.before(busInspectionPlan.getStartDate())) { busInspectionPlan.setStatus("未生效"); } else { busInspectionPlan.setStatus("已生效"); } busInspectPlanVOList.add(busInspectionPlan); } //统一返回类型 将返回的数据存入PageResult PageResult<BusInspectPlanVO> pageResult = new PageResult<>(pageParam, pageModel.getRecords()); return ResultUtil.getResponseResult(pageResult); } /** * 功能描述:新增/修改计划 * * @param * @return */ public ResponseResult<String> addPlan(BusAddPlanVO busAddPlanVO) throws ParseException { //映射转换 BusInspectionPlan busInspectionPlan = BusInspectPlanConvert.INSTANCE.toBusInspectPlanModel(busAddPlanVO); //获取当前时间 Date date = new Date(); if (busInspectionPlan.getCreateTime() == null) { busInspectionPlan.setCreateTime(date); busInspectionPlan.setUpdateUserTime(date); } busInspectionPlan.setUpdateUserTime(date); //根据计划名字查询是否存在计划 //BusSelectPlanVO busInspectionPlanOne = busInspectionPlanMapper.selectSecurityPlan(busAddPlanVO.getPlanName(),busAddPlanVO.getPlanType()); BusSelectPlanVO busInspectionPlanOne = busInspectionPlanMapper.selectSecurityPlan(busAddPlanVO.getId()); //初始化planId Long planId = Long.valueOf(0); if (busInspectionPlanOne == null) { //不存在就插入计划表 busInspectionPlan.setIsDelete(0); save(busInspectionPlan); planId = busInspectionPlan.getId(); } else { //获取planId planId = busInspectionPlanOne.getId(); //存在就插入id,根据id修改 busInspectionPlan.setId(busInspectionPlanOne.getId()); updateById(busInspectionPlan); //软删除任务和排期 for (BusSelectPlanTaskVO busSelectPlanTaskVO : busInspectionPlanOne.getBusSelectPlanTaskVOList()) { //获取任务id Long taskId = busSelectPlanTaskVO.getId(); //软删除排期和点位关联表 List<Long> taskItemIdList = busInspectionTaskItemMapper.selectTaskItemId(taskId); for (Long taskItemId : taskItemIdList) { busInspectionTaskPointRelMapper.updateTaskPointRelDelete(taskItemId); } //软删除原有任务排期 busInspectionTaskItemMapper.updateTaskItemDelete(taskId); //软删除原有任务 busInspectionTaskMapper.updateTaskDelete(taskId); } //软删除点位 busInspectionPlanPointRelMapper.updatePointDelete(planId); } //根据planId插入busInspectionPlanPointRel表 List<BusInspectionPlanPointRel> busInspectionPlanPointRelList = BusInspectPlanConvert.INSTANCE.toBusInspectionPlanPointRelListModel(busAddPlanVO.getBusAddPlanPointRelVOList());//映射 for (BusInspectionPlanPointRel busInspectionPlanPointRel : busInspectionPlanPointRelList) { busInspectionPlanPointRel.setPlanId(planId); busInspectionPlanPointRel.setIsDelete(0);//设置插入状态为正常 //busInspectionPlanPointRel.setInsertId(busInspectionPlanPointRel.getInsertId()); busInspectionPlanPointRelService.save(busInspectionPlanPointRel); } //根据planId插入task表 List<BusInspectionTask> busInspectionTaskList = BusInspectPlanConvert.INSTANCE.toBusInspectionTaskListModel(busAddPlanVO.getBusAddPlanTaskVOList());//映射 for (BusInspectionTask busInspectionTask : busInspectionTaskList) { busInspectionTask.setPlanId(planId); busInspectionTask.setIsDelete(0);//设置插入状态为正常 //清空id busInspectionTask.setId(null); busInspectionTask.setStatus(0);//设置初始任务状态为未开始 //插入task表 busInspectionTaskService.save(busInspectionTask); //插入task下的关联表 //重新进行任务排期,并插入排期表,排期与点位关联表 BusInspectionTaskItem busInspectionTaskItem = BusInspectPlanConvert.INSTANCE.BUS_INSPECTION_TASK_ITEM(busInspectionTask);//映射转换 //获取任务周期,来调用不同的接口 if (busAddPlanVO.getCycleType().equals("MONTH")) { int cycleNum = 30; //日期拿有效日期 System.out.println("获取月份成功"); insertByMonth(busInspectionTask.getDayList(), busAddPlanVO.getStartDate(), busAddPlanVO.getEndDate(), busInspectionTaskItem, busAddPlanVO.getBusAddPlanPointRelVOList()); System.out.println("插入月份成功"); } else if (busAddPlanVO.getCycleType().equals("WEEK")) { int cycleNum = 7; //日期拿有效日期 insertByWeek(busInspectionTask.getDayList(), busAddPlanVO.getStartDate(), busAddPlanVO.getEndDate(), busInspectionTaskItem, busAddPlanVO.getBusAddPlanPointRelVOList()); } else if (busAddPlanVO.getCycleType().equals("DAY")) { int cycleNum = 1; //日期拿自定义日期 insertByDay(cycleNum, busInspectionTask.getStartDate(), busInspectionTask.getEndDate(), busInspectionTaskItem, busAddPlanVO.getBusAddPlanPointRelVOList()); } else { return ResultUtil.getResponseResult(ResponseResultCode.FAIL, "新增修改失败"); } } return ResultUtil.getResponseResult("新增成功"); } /** * 功能描述:按月month拆分时间并插入任务排期,date取有效期(有效期要大于当前时间) * * @param * @return */ public void insertByMonth(String dayInMonthList, Date startDate, Date endDate, BusInspectionTaskItem busInspectionTaskItem, List<BusAddPlanPointRelVO> busAddPlanPointRelVOList) { List<String> daylist = Arrays.asList(dayInMonthList.split(",")); for (String dayInMonth : daylist) { //格式标准化,以下不要也行 /*Date dayInMonthParse = new SimpleDateFormat("yyyy-MM-dd").parse(dayInMonth); Long dayInMonthLong = dayInMonthParse.getTime(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//这个是你要转成后的时间的格式 dayInMonth = sdf.format(new Date(dayInMonthLong));*/ //正文: System.out.println("开始时间为:" + startDate.toString()); System.out.println("结束时间为:" + endDate.toString()); System.out.println("结束时间为:" + dayInMonth); //获取有效期的年月份 Calendar startCal = Calendar.getInstance(); startCal.setTime(startDate); int startYear = startCal.get(Calendar.YEAR); int startMonth = startCal.get(Calendar.MONTH) + 1; Calendar endCal = Calendar.getInstance(); endCal.setTime(endDate); int endMonth = endCal.get(Calendar.MONTH) + 1; int endYear = endCal.get(Calendar.YEAR); //获取间隔月份 int monthDiffer = (endYear - startYear) * 12 + endMonth - startMonth; //每个月循环插入一次,当月也可以 for (int i = 0; i <= monthDiffer; i++) { //设定需插入的时间 int inserTYear = startYear + (startMonth + i) / 12; int inserTMonth = (startMonth + i) % 12; Calendar insertCal = Calendar.getInstance(); insertCal.set(Calendar.YEAR, inserTYear); insertCal.set(Calendar.MONTH, inserTMonth - 1);//Calendar.MONTH起始为0,要减1 insertCal.set(Calendar.DATE, Integer.parseInt(dayInMonth)); // 时分秒毫秒设置为0,用不着,避免影响比较 insertCal.set(Calendar.HOUR_OF_DAY, 0); insertCal.set(Calendar.MINUTE, 0); insertCal.set(Calendar.SECOND, 0); insertCal.set(Calendar.MILLISECOND, 0); System.out.println("待比对时间为:" + insertCal.getTime().toString()); //判断插入日期是否在有效期内 if ((insertCal.before(endCal)||insertCal.equals(endCal)) && (insertCal.after(startCal)||insertCal.equals(startCal))) { System.out.println("插入时间为:" + insertCal.getTime().toString()); //插入排期表 busInspectionTaskItem.setStartDate(insertCal.getTime()); busInspectionTaskItem.setEndDate(insertCal.getTime());//起止日期为同一天 /*insertCal.add(Calendar.DATE, 1); busInspectionTaskItem.setEndDate(insertCal.getTime());*/ busInspectionTaskItem.setId(null);//消除多余的id busInspectionTaskItem.setStatus(0);//设置初始任务状态为未开始 busInspectionTaskItemMapper.insert(busInspectionTaskItem); //获取排期Id,点位和摄像头,插入其关联表 busInspectionTaskItem.getId(); for (BusAddPlanPointRelVO busAddPlanPointRelVO : busAddPlanPointRelVOList) { BusInspectionTaskPointRel busInspectionTaskPointRel = new BusInspectionTaskPointRel(); busInspectionTaskPointRel.setPointId(busAddPlanPointRelVO.getPointId()); busInspectionTaskPointRel.setCameraId(busAddPlanPointRelVO.getCameraId()); busInspectionTaskPointRel.setTaskItemId(busInspectionTaskItem.getId()); busInspectionTaskPointRel.setStatus(0); busInspectionTaskPointRel.setIsDelete(0); busInspectionTaskPointRelMapper.insert(busInspectionTaskPointRel); } } } } } /** * 功能描述:按周week拆分时间并插入任务排期,date取有效期(有效期要大于当前时间) * * @param * @return */ public void insertByWeek(String dayInWeekList, Date startDate, Date endDate, BusInspectionTaskItem busInspectionTaskItem, List<BusAddPlanPointRelVO> busAddPlanPointRelVOList) throws ParseException { List<String> daylist = Arrays.asList(dayInWeekList.split(",")); for (String dayInWeek : daylist) { System.out.println("开始时间为:" + startDate.toString()); System.out.println("结束时间为:" + endDate.toString()); //获取有效期的年月份 Calendar startCal = Calendar.getInstance(); startCal.setTime(startDate); int startWeekDay = startCal.get(Calendar.DAY_OF_WEEK) - 1;//获取起始日为周几,起始日为周日,所以减1 Calendar endCal = Calendar.getInstance(); endCal.setTime(endDate); //获取间隔星期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//这个是你要转成后的时间的格式 String endTime = sdf.format(endDate); String startTime = sdf.format(startDate); int daysBetweenNum = daysBetween(startTime, endTime);//调用接口 int weeksBetweenNum = 0;//初始化 weeksBetweenNum = daysBetweenNum / 7 + 1;//此处逻辑很关键,两种情况都是如此加1 //获取第一次插入的时间 Date insertStartDate = startCal.getTime();//有效期第一天 Calendar insertCal = Calendar.getInstance(); insertCal.setTime(insertStartDate); insertCal.add(Calendar.DATE, Integer.parseInt(dayInWeek) - startWeekDay);//需要插入第一天 //每个星期循环插入一次 for (int i = 0; i < weeksBetweenNum; i++) { //设定需插入的时间 System.out.println("待比对开始时间为:" + insertCal.getTime().toString()); System.out.println("待比对结束时间为:" + endCal.getTime().toString()); //判断插入日期是否在有效期内 if ((insertCal.before(endCal)||insertCal.equals(endCal)) && (insertCal.after(startCal)||insertCal.equals(startCal))) { System.out.println("插入时间为:" + insertCal.getTime().toString()); //插入排期表 busInspectionTaskItem.setStartDate(insertCal.getTime()); busInspectionTaskItem.setEndDate(insertCal.getTime());//起止日期为同一天 /* insertCal.add(Calendar.DATE, 1); busInspectionTaskItem.setEndDate(insertCal.getTime());*/ busInspectionTaskItem.setId(null);//消除多余的id busInspectionTaskItem.setStatus(0);//设置初始任务状态为未开始 busInspectionTaskItemMapper.insert(busInspectionTaskItem); //获取排期Id,点位和摄像头,插入其关联表 busInspectionTaskItem.getId(); for (BusAddPlanPointRelVO busAddPlanPointRelVO : busAddPlanPointRelVOList) { BusInspectionTaskPointRel busInspectionTaskPointRel = new BusInspectionTaskPointRel(); busInspectionTaskPointRel.setPointId(busAddPlanPointRelVO.getPointId()); busInspectionTaskPointRel.setCameraId(busAddPlanPointRelVO.getCameraId()); busInspectionTaskPointRel.setTaskItemId(busInspectionTaskItem.getId()); busInspectionTaskPointRel.setStatus(0); busInspectionTaskPointRel.setIsDelete(0); busInspectionTaskPointRelMapper.insert(busInspectionTaskPointRel); } } insertCal.add(Calendar.DATE, 7); System.out.println("下一次验证的插入时间:"+insertCal.getTime()); } } } /** * 功能描述:按日拆分时间并插入任务排期,date取自定义日期区间 * * @param * @return */ public void insertByDay(int cycleNum, Date startDate, Date endDate, BusInspectionTaskItem busInspectionTaskItem, List<BusAddPlanPointRelVO> busAddPlanPointRelVOList) throws ParseException { //取endtime(String),作为最终时间 //System.out.println("统计的时间段为:从"+startTime+"到"+endTime); /*Date endParse = new SimpleDateFormat("yyyy-MM-dd").parse(endTime); Long endLong = endParse.getTime();*/ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//这个是你要转成后的时间的格式 //endTime = sdf.format(new Date(endLong)); String endTime = sdf.format(endDate); String startTime = sdf.format(startDate); //System.out.println("结束时间是:"+endTime); int daysBetweenNum = daysBetween(startTime, endTime); //起止日期相差要加一天,比如起止为同一天 int cycleForNum = daysBetweenNum / cycleNum + 1; //int cycleForNum = daysBetweenNum % cycleNum == 0 ? (daysBetweenNum / cycleNum) : (daysBetweenNum / cycleNum) + 1; System.out.println("两日期间相隔的天数为:" + daysBetweenNum); System.out.println("周期选择是:" + cycleNum + "天一周期, 则切割出来的周期存在个数:" + cycleForNum); //设置当前时间 Long calculationLong = startDate.getTime(); for (int i = 1; i <= cycleForNum; i++) { String startStr = sdf.format(new Date(calculationLong)); Date startStrParse = new SimpleDateFormat("yyyy-MM-dd").parse(startStr); Long startStrLong = startStrParse.getTime(); Long cycleLong = cycleNum * 86400000L; calculationLong = startStrLong + cycleLong; String endStr = endTime; //判断当前时间是否小于最终时间 if (calculationLong <= endDate.getTime()) { endStr = sdf.format(new Date(calculationLong)); } System.out.println("起始:" + startStr + "结束:" + endStr); //String转换为Date Date startDateSet = new SimpleDateFormat("yyyy-MM-dd").parse(startStr); Date endDateSet = new SimpleDateFormat("yyyy-MM-dd").parse(endStr); busInspectionTaskItem.setStartDate(startDateSet); //busInspectionTaskItem.setEndDate(endDateSet); //按日划分,结束日期设为当天 busInspectionTaskItem.setEndDate(startDateSet); //插入排期表 busInspectionTaskItem.setId(null);//消除多余的id busInspectionTaskItem.setStatus(0);//设置初始任务状态为未开始 busInspectionTaskItemMapper.insert(busInspectionTaskItem); //获取排期Id,点位和摄像头,插入其关联表 busInspectionTaskItem.getId(); for (BusAddPlanPointRelVO busAddPlanPointRelVO : busAddPlanPointRelVOList) { BusInspectionTaskPointRel busInspectionTaskPointRel = new BusInspectionTaskPointRel(); busInspectionTaskPointRel.setPointId(busAddPlanPointRelVO.getPointId()); busInspectionTaskPointRel.setCameraId(busAddPlanPointRelVO.getCameraId()); busInspectionTaskPointRel.setTaskItemId(busInspectionTaskItem.getId()); busInspectionTaskPointRel.setStatus(0); busInspectionTaskPointRel.setIsDelete(0); busInspectionTaskPointRelMapper.insert(busInspectionTaskPointRel); } } } public static int daysBetween(String smdate, String bdate) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(smdate)); long time1 = cal.getTimeInMillis(); cal.setTime(sdf.parse(bdate)); long time2 = cal.getTimeInMillis(); long between_days = (time2 - time1) / (1000 * 3600 * 24); return Integer.parseInt(String.valueOf(between_days)); }
二· convert映射接口
package cn.cptec.bus.convert; import cn.cptec.bus.entity.BusInspectionPlan; import cn.cptec.bus.entity.BusInspectionPlanPointRel; import cn.cptec.bus.entity.BusInspectionTask; import cn.cptec.bus.entity.BusInspectionTaskItem; import cn.cptec.bus.vo.busInspectPlanVo.add.BusAddPlanPointRelVO; import cn.cptec.bus.vo.busInspectPlanVo.add.BusAddPlanTaskVO; import cn.cptec.bus.vo.busInspectPlanVo.add.BusAddPlanVO; import cn.cptec.bus.vo.busInspectPlanVo.select.BusSelectPlanPointRel; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import org.mapstruct.factory.Mappers; import java.util.List; /** * @author: * @date: 2021/12/2 17:32 */ @Mapper(componentModel = "spring") public interface BusInspectPlanConvert { BusInspectPlanConvert INSTANCE = Mappers.getMapper(BusInspectPlanConvert.class); /*@Mapping(source = "createAt", target = "createAt", dateFormat = "yyyy-MM-dd") @Mapping(source = "updateAt", target = "updateAt", dateFormat = "yyyy-MM-dd") MonAbnormalEventExcelVO toExcelVoConver(MonAbnormalEvent selectList); @IterableMapping(elementTargetType = MonAbnormalEventExcelVO.class) List<MonAbnormalEventExcelVO> toExcelVoConvertList(List<MonAbnormalEvent> selectList);*/ //不特别标注的话,id映射不过来 @Mapping(source = "id", target = "id") BusInspectionPlan toBusInspectPlanModel(BusAddPlanVO busAddPlanVO); List<BusInspectionTask> toBusInspectionTaskListModel(List<BusAddPlanTaskVO> busAddPlanTaskVOList); List<BusInspectionPlanPointRel> toBusInspectionPlanPointRelListModel(List<BusAddPlanPointRelVO> busAddPlanPointRelVOList); @Mapping(source = "id", target = "taskId") @Mapping(source = "id", target = "id", ignore = true) BusInspectionTaskItem BUS_INSPECTION_TASK_ITEM(BusInspectionTask busInspectionTask); }