Commit 1c065301 by 丁伟峰

多工程师、批量添加日历

1 parent 727c0e9b
...@@ -25,40 +25,46 @@ public class DateUtils { ...@@ -25,40 +25,46 @@ public class DateUtils {
return DayOfWeek.of(dayOfWeek).getDisplayName(TextStyle.SHORT, Locale.CHINESE); return DayOfWeek.of(dayOfWeek).getDisplayName(TextStyle.SHORT, Locale.CHINESE);
} }
public static String formatDateTime(LocalDateTime d, String format){ public static String formatDateTime(LocalDateTime d, String format) {
return d.format(DateTimeFormatter.ofPattern(format)); return d.format(DateTimeFormatter.ofPattern(format));
} }
public static String formatDateTime(LocalDateTime d){ public static String formatDateTime(LocalDateTime d) {
return formatDateTime(d, PATTERN_DATETIME); return formatDateTime(d, PATTERN_DATETIME);
} }
public static String formatDate(LocalDate d, String format){ public static String formatDate(LocalDate d, String format) {
return d.format(DateTimeFormatter.ofPattern(format)); return d.format(DateTimeFormatter.ofPattern(format));
} }
public static String formatDate(LocalDate d){ public static String formatDate(LocalDate d) {
return formatDate(d, PATTERN_DATE); return formatDate(d, PATTERN_DATE);
} }
public static long getAge(String birth){ public static long getAge(String birth) {
return ChronoUnit.YEARS.between(LocalDate.parse(birth), LocalDate.now()); return ChronoUnit.YEARS.between(LocalDate.parse(birth), LocalDate.now());
} }
public static LocalDate localDateFromStr(String dateStr){ public static LocalDate localDateFromStr(String dateStr) {
return localDateFromStr(dateStr, "yyyy-MM-dd"); return localDateFromStr(dateStr, "yyyy-MM-dd");
} }
public static LocalDate localDateFromStr(String str, String pattern){ public static LocalDate localDateFromStr(String str, String pattern) {
if (str.length() > pattern.length()) {
str = str.substring(0, pattern.length());
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return LocalDate.parse(str, formatter); return LocalDate.parse(str, formatter);
} }
public static LocalDateTime localDateTimeFromStr(String str){ public static LocalDateTime localDateTimeFromStr(String str) {
return localDateTimeFromStr(str, "yyyy-MM-dd HH:mm:ss"); return localDateTimeFromStr(str, "yyyy-MM-dd HH:mm:ss");
} }
public static LocalDateTime localDateTimeFromStr(String str, String pattern){ public static LocalDateTime localDateTimeFromStr(String str, String pattern) {
if (str.length() > pattern.length()) {
str = str.substring(0, pattern.length());
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return LocalDateTime.parse(str, formatter); return LocalDateTime.parse(str, formatter);
} }
......
...@@ -46,7 +46,6 @@ public class EngineerCalendarController { ...@@ -46,7 +46,6 @@ public class EngineerCalendarController {
@GetMapping("/engineer/calendar/plan/detail") @GetMapping("/engineer/calendar/plan/detail")
public Result<?> getCalendarPlanDetail(@Validated CalendarDetailDTO.Request reqDTO) { public Result<?> getCalendarPlanDetail(@Validated CalendarDetailDTO.Request reqDTO) {
log.info("==== request ===> {}", request);
Result<?> res = null; Result<?> res = null;
try { try {
res = engineerCalendarService.getPlanDetail(reqDTO); res = engineerCalendarService.getPlanDetail(reqDTO);
......
...@@ -10,4 +10,6 @@ public interface PubParamsDao extends JpaRepository<PubParamsEntity, Integer> { ...@@ -10,4 +10,6 @@ public interface PubParamsDao extends JpaRepository<PubParamsEntity, Integer> {
List<PubParamsEntity> findByBiztype(String biztype); List<PubParamsEntity> findByBiztype(String biztype);
List<PubParamsEntity> findByCatalogAndBiztype(String cagtalog, String biztype); List<PubParamsEntity> findByCatalogAndBiztype(String cagtalog, String biztype);
PubParamsEntity getByCatalogAndBiztypeAndPkey(String catalog, String biztype, String pkey);
} }
...@@ -16,7 +16,7 @@ public class CalendarBatAddDTO { ...@@ -16,7 +16,7 @@ public class CalendarBatAddDTO {
/** /**
* 是否全天 * 是否全天
*/ */
private long isAllday; private Boolean isAllday;
/** /**
* 备注 * 备注
*/ */
......
...@@ -38,13 +38,13 @@ public class CapacityEngineerCalendarEntity { ...@@ -38,13 +38,13 @@ public class CapacityEngineerCalendarEntity {
private String reason; private String reason;
@Column(nullable = false) @Column(nullable = false)
private String memo; private String memo = "";
@Column(name = "create_time", nullable = false, updatable = false, columnDefinition = "timestamp default current_timestamp") @Column(name = "create_time", nullable = false, updatable = false, columnDefinition = "timestamp default current_timestamp")
private Timestamp createTime; private LocalDateTime createTime = LocalDateTime.now();
@Column(name = "update_time", nullable = false, columnDefinition = "timestamp default current_timestamp on update current_timestamp") @Column(name = "update_time", nullable = false, columnDefinition = "timestamp default current_timestamp on update current_timestamp")
private Timestamp updateTime; private LocalDateTime updateTime = LocalDateTime.now();
// 其他字段和关联关系的定义 // 其他字段和关联关系的定义
// ... // ...
......
package com.dituhui.pea.order.service.impl; package com.dituhui.pea.order.service.impl;
import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.IdUtil;
import com.dituhui.pea.common.Result; import com.dituhui.pea.common.Result;
import com.dituhui.pea.order.common.DateUtils; import com.dituhui.pea.order.common.DateUtils;
import com.dituhui.pea.order.common.EngineerUtils; import com.dituhui.pea.order.common.EngineerUtils;
import com.dituhui.pea.order.dao.*; import com.dituhui.pea.order.dao.*;
import com.dituhui.pea.order.dto.*; import com.dituhui.pea.order.dto.*;
import com.dituhui.pea.order.entity.CapacityEngineerCalendarEntity; import com.dituhui.pea.order.entity.*;
import com.dituhui.pea.order.entity.EngineerInfoEntity;
import com.dituhui.pea.order.entity.OrgTeamEntity;
import com.dituhui.pea.order.entity.PubParamsEntity;
import com.dituhui.pea.order.service.EngineerCalendarService; import com.dituhui.pea.order.service.EngineerCalendarService;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.Duration; import java.time.*;
import java.time.LocalDate; import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -36,19 +33,18 @@ public class EngineerCalendarServiceImpl implements EngineerCalendarService { ...@@ -36,19 +33,18 @@ public class EngineerCalendarServiceImpl implements EngineerCalendarService {
@Autowired @Autowired
private EngineerUtils engineerUtil; private EngineerUtils engineerUtil;
@Autowired @Autowired
private OrgTeamEngineerDao orgTeamEngineerDao; private OrgTeamEngineerDao orgTeamEngineerDao;
@Autowired @Autowired
private CapacityEngineerCalendarDao capacityEngineerCalendarDao; private CapacityEngineerCalendarDao capacityEngineerCalendarDao;
@Autowired @Autowired
private OrgTeamDao orgTeamDao; private OrgTeamDao orgTeamDao;
@Autowired @Autowired
private PubParamsDao pubParamsDao; private PubParamsDao pubParamsDao;
@Autowired @Autowired
private EngineerInfoDao engineerInfoDao; private EngineerInfoDao engineerInfoDao;
@Autowired
private EngineerBusinessDao engineerBusinessDao;
@Override @Override
public Result<?> getEngineersCalendar(EngineerCalendarDTO.Request req) { public Result<?> getEngineersCalendar(EngineerCalendarDTO.Request req) {
...@@ -150,13 +146,135 @@ public class EngineerCalendarServiceImpl implements EngineerCalendarService { ...@@ -150,13 +146,135 @@ public class EngineerCalendarServiceImpl implements EngineerCalendarService {
@Override @Override
public Result<?> batchAddPlan(CalendarBatAddDTO.Request reqDTO) { public Result<?> batchAddPlan(CalendarBatAddDTO.Request reqDTO) {
if (reqDTO.getIsAllday() == 1) { LocalDate beginDate = DateUtils.localDateFromStr(reqDTO.getStart());
// 如果是全天只需要填写日期yyyy-MM-dd;否则填写日期+时间 yyyy-MM-dd HH:mm LocalDate endDate = DateUtils.localDateFromStr(reqDTO.getEnd());
if (endDate.isBefore(beginDate)) {
return Result.failed("结束时间不能早于开始时间");
}
if (!reqDTO.getRepeatType().isEmpty() && !"不重复".equals(reqDTO.getRepeatType())) {
if (reqDTO.getRepeatEndDate().isEmpty()) {
return Result.failed("重复的日历配置,请设定结束日期");
}
LocalDate repeatEndDate = DateUtils.localDateFromStr(reqDTO.getRepeatEndDate());
if (repeatEndDate.isBefore(endDate)) {
return Result.failed("重复结束日期,早于日历结束日期");
}
}
for (String egineerCode : reqDTO.getEngineerCodes()) {
addEngineerPlan(egineerCode, reqDTO.getType(), reqDTO.getIsAllday(), reqDTO.getStart(), reqDTO.getEnd(), reqDTO.getRemark(), reqDTO.getRepeatType(), reqDTO.getRepeatEndDate());
} }
return null; return null;
} }
private void addEngineerPlan(String engineerCode, String type, Boolean isAllday, String beginTime, String endTime, String remark, String repeatType, String repeatEndDate) {
if (isAllday) {
EngineerBusinessEntity config = engineerBusinessDao.getByEngineerCode(engineerCode);
beginTime = String.format("%s %s:00", beginTime, config.getWorkOn());
endTime = String.format("%s %s:00", endTime, config.getWorkOff());
}
if ("不重复".equals(repeatType)) {
addOneEngineerPlan(engineerCode, type, beginTime, endTime, remark);
} else {
LocalDate beginDate = DateUtils.localDateFromStr(beginTime);
LocalDate endDate = DateUtils.localDateFromStr(repeatEndDate);
LocalDate current = beginDate;
while (!current.isAfter(endDate)) {
// 开始时间,结束时间平移
int days = Period.between(beginDate, current).getDays();
beginTime = DateUtils.formatDateTime(DateUtils.localDateTimeFromStr(beginTime).plusDays(days));
endTime = DateUtils.formatDateTime(DateUtils.localDateTimeFromStr(endTime).plusDays(days));
addOneEngineerPlan(engineerCode, type, beginTime, endTime, remark);
current = getNextDate(current, repeatType);
}
}
}
private LocalDate getNextDate(LocalDate current, String repeatType) {
switch (repeatType) {
case "每天":
current = current.plusDays(1);
break;
case "每周":
current = current.plusWeeks(1);
break;
case "每月":
current = current.plusMonths(1);
break;
case "每3个月":
current = current.plusMonths(3);
break;
case "每6个月":
current = current.plusMonths(6);
break;
case "每年":
current = current.plusYears(1);
break;
default:
throw new RuntimeException("不支持的重复类型!");
}
return current;
}
private void addOneEngineerPlan(String engineerCode, String type, String beginTime, String endTime, String remark) {
// 一个工程师、一条配置处理;可能一条会跨多天,需要分别补充处理
LocalDate beginDate = DateUtils.localDateFromStr(beginTime);
LocalDate endDate = DateUtils.localDateFromStr(endTime);
String planId = IdUtil.getSnowflake().nextIdStr().substring(0, 10);
if (beginDate.isEqual(endDate)) {
// 设定的是同一天,简单处理
CapacityEngineerCalendarEntity e = new CapacityEngineerCalendarEntity();
e.setCreateTime(LocalDateTime.now());
e.setUpdateTime(LocalDateTime.now());
e.setEngineerCode(engineerCode);
e.setPlanId(planId);
e.setType(type);
e.setReason(getNameForType(type));
e.setStartTime(DateUtils.localDateTimeFromStr(beginTime));
e.setEndTime(DateUtils.localDateTimeFromStr(endTime));
e.setWorkday(DateUtils.formatDate(beginDate));
e.setMemo(remark);
capacityEngineerCalendarDao.save(e);
return;
}
// 跨天处理
LocalDate current = beginDate;
EngineerBusinessEntity config = engineerBusinessDao.getByEngineerCode(engineerCode);
while (!current.isAfter(endDate)) {
CapacityEngineerCalendarEntity e = new CapacityEngineerCalendarEntity();
e.setCreateTime(LocalDateTime.now());
e.setUpdateTime(LocalDateTime.now());
e.setEngineerCode(engineerCode);
e.setPlanId(String.format("%s-%s", planId, current.format(DateTimeFormatter.ofPattern("MMdd"))));
e.setType(type);
e.setReason(getNameForType(type));
if (current.isEqual(beginDate)) {
// 第一天直接用传进来的时间
e.setStartTime(DateUtils.localDateTimeFromStr(beginTime));
} else {
// 如果是跨日,开始时间需要从 xxxx 08:00:00 这样开始
String btime = String.format("%s %s:00", DateUtils.formatDate(current), config.getWorkOn());
e.setStartTime(DateUtils.localDateTimeFromStr(btime));
}
if (current.isEqual(endDate)) {
// 最后一天,直接用传进来的时间
e.setEndTime(DateUtils.localDateTimeFromStr(endTime));
} else {
// 如果不是最后一天,结束时间应该是当天下班时间
String etime = String.format("%s %s:00", DateUtils.formatDate(current), config.getWorkOff());
e.setEndTime(DateUtils.localDateTimeFromStr(etime));
}
e.setWorkday(DateUtils.formatDate(current));
e.setMemo(remark);
capacityEngineerCalendarDao.save(e);
// 按日迭代
current = current.plusDays(1);
}
}
@Override @Override
public Result<?> queryEngineerPlanNum(CalendarQueryNumDTO.Request reqDTO) { public Result<?> queryEngineerPlanNum(CalendarQueryNumDTO.Request reqDTO) {
if (reqDTO.getEngineerCodes().isEmpty()) { if (reqDTO.getEngineerCodes().isEmpty()) {
...@@ -253,4 +371,11 @@ public class EngineerCalendarServiceImpl implements EngineerCalendarService { ...@@ -253,4 +371,11 @@ public class EngineerCalendarServiceImpl implements EngineerCalendarService {
} }
return calendar; return calendar;
} }
public String getNameForType(String type) {
// todo 需要缓存
List<PubParamsEntity> entities = pubParamsDao.findByCatalogAndBiztype("CalendarType", "CalendarType");
Map<String, String> mapping = entities.stream().collect(Collectors.toMap(PubParamsEntity::getPkey, PubParamsEntity::getPlabel));
return mapping.get(type);
}
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!