Commit 95d59f83 authored by 李秀明's avatar 李秀明

fix(JCS)值班排版:并发导致数据错乱

parent e2d64218
......@@ -883,4 +883,19 @@ public class DateUtils {
Date end = calendar.getTime();
return end;
}
/**
* 获取指定月份的最后一天
* @param monthDate yyyy-MM
* @return yyyy-MM-dd
*/
public static String getLastDayOfMonth(String monthDate) {
String[] split = monthDate.split("-");
int year = Integer.parseInt(split[0]);
int month = Integer.parseInt(split[1]);
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
int actualMaximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
return year + "-" + month + "-" + actualMaximum;
}
}
......@@ -93,7 +93,7 @@ public class DutyCarServiceImpl extends DutyCommonServiceImpl implements IDutyCa
});
// 修改操作动态表单的方法,修改人陈浩 -------------end 2021-09-28
// 生成变更日志
createDutyPersonShiftLog(dutyCarDto, false, ActionStatus.METHOD_UPDATE.getCode());
createDutyPersonShiftLog(dutyCarDto, false, ActionStatus.METHOD_ADD.getCode());
// 3.返回保存后的数据
return dutyCarDto;
......@@ -432,49 +432,42 @@ public class DutyCarServiceImpl extends DutyCommonServiceImpl implements IDutyCa
}
@SuppressWarnings("Duplicates")
public void createDutyPersonShiftLog(DutyCarDto dutyCarDto, boolean isInit, String actionCode) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
List<DutyPersonShiftDto> dutyPersonShift = dutyCarDto.getDutyShift();
final String groupCode = "dutyCar";
// 新建/更新排班时处理跨月数据
if (!isInit && StringUtils.isNotBlank(actionCode)) {
public synchronized void createDutyPersonShiftLog(DutyCarDto dutyCarDto, boolean isInit, String actionCode) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
List<DutyPersonShiftDto> dutyPersonShift = dutyCarDto.getDutyShift();
final String groupCode = "dutyCar";
// 更新排班时处理
if (!isInit && Objects.equals(actionCode, "update")) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
if (Objects.nonNull(dutyPersonShift) && !dutyPersonShift.isEmpty()) {
// 拿到排班月份 yyyy-MM
String dutyDate = dateFormat.format(dutyPersonShift.get(0).getDutyDate());
dutyDate = dutyDate.substring(0, 7);
String month = dateFormat.format(dutyPersonShift.get(0).getDutyDate()).substring(0, 7);
String startDate = month + "-01";
String endDate = DateUtils.getLastDayOfMonth(month);
String today = dateFormat.format(new Date());
if (today.substring(0, 7).equals(dutyDate)) {
dutyDate = today;
} else {
dutyDate += "-01";
try {
startDate = isAfterToday(dateFormat.parse(startDate)) ? startDate : today;
} catch (ParseException e) {
throw new RuntimeException(e);
}
dutyPersonShiftLogMapper.delete(
Wrappers.<DutyPersonShiftLog>lambdaQuery()
.eq(DutyPersonShiftLog::getGroupCode, groupCode)
.eq(DutyPersonShiftLog::getDutyUser, dutyCarDto.getUserName())
.ge(DutyPersonShiftLog::getDutyDate, dutyDate)
.ge(DutyPersonShiftLog::getDutyDate, dateFormat.format(new Date()))
.eq(DutyPersonShiftLog::getDutyUserId, dutyCarDto.getUserId())
.ge(DutyPersonShiftLog::getDutyDate, startDate)
.le(DutyPersonShiftLog::getDutyDate, endDate)
.eq(DutyPersonShiftLog::getIsDelete, false)
);
}
dutyPersonShift.clear();
try {
List<Map<String, Object>> list = list(null, dateFormat.format(new Date()), "9999-12-12");
list = list.stream().filter(map -> Objects.equals(map.get("userName"), dutyCarDto.getUserName())).collect(Collectors.toList());
for (Map<String, Object> map : list) {
DutyCarDto dutyCarDto = (DutyCarDto) Bean.mapToBean(map, DutyCarDto.class);
List<DutyPersonShiftDto> dutyShifts = dutyCarDto.getDutyShift().stream().filter(v -> isAfterToday(v.getDutyDate())).collect(Collectors.toList());
dutyPersonShift.addAll(dutyShifts);
}
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
});
}
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
Set<String> dutyDates = dutyPersonShift.stream().map(v -> dateFormat.format(v.getDutyDate())).collect(Collectors.toSet());
if (dutyDates.isEmpty()) {
return;
......@@ -483,7 +476,7 @@ public class DutyCarServiceImpl extends DutyCommonServiceImpl implements IDutyCa
Wrappers.<DutyPersonShiftLog>lambdaQuery()
.in(DutyPersonShiftLog::getDutyDate, dutyDates)
.eq(DutyPersonShiftLog::getGroupCode, groupCode)
.eq(DutyPersonShiftLog::getDutyUser, dutyCarDto.getUserName())
.eq(DutyPersonShiftLog::getDutyUserId, dutyCarDto.getUserId())
.eq(DutyPersonShiftLog::getIsDelete, false)
).stream().collect(Collectors.groupingBy(DutyPersonShiftLog::getDutyDate));
......@@ -534,7 +527,7 @@ public class DutyCarServiceImpl extends DutyCommonServiceImpl implements IDutyCa
Wrappers.<DutyPersonShiftLog>lambdaUpdate()
.eq(DutyPersonShiftLog::getDutyDate, dutyPersonShiftDto.getDutyDate())
.eq(DutyPersonShiftLog::getGroupCode, groupCode)
.eq(DutyPersonShiftLog::getDutyUser, dutyCarDto.getUserName())
.eq(DutyPersonShiftLog::getDutyUserId, dutyCarDto.getUserId())
.eq(DutyPersonShiftLog::getIsDelete, false)
);
}
......
......@@ -534,50 +534,42 @@ public Object BuildScheduleDetails(String dutyDay, Long shiftId, String postType
}
@SuppressWarnings("Duplicates")
public void createDutyPersonShiftLog(DutyPersonDto dutyPersonDto, boolean isInit, String actionCode) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
List<DutyPersonShiftDto> dutyPersonShift = dutyPersonDto.getDutyShift();
final String groupCode = "dutyPerson";
// 新建/更新排班时处理跨月数据
if (!isInit && StringUtils.isNotBlank(actionCode)) {
public synchronized void createDutyPersonShiftLog(DutyPersonDto dutyPersonDto, boolean isInit, String actionCode) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
List<DutyPersonShiftDto> dutyPersonShift = dutyPersonDto.getDutyShift();
final String groupCode = "dutyPerson";
// 更新排班时处理
if (!isInit && Objects.equals(actionCode, "update")) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
if (Objects.nonNull(dutyPersonShift) && !dutyPersonShift.isEmpty()) {
// 拿到排班月份 yyyy-MM
String dutyDate = dateFormat.format(dutyPersonShift.get(0).getDutyDate());
dutyDate = dutyDate.substring(0, 7);
String month = dateFormat.format(dutyPersonShift.get(0).getDutyDate()).substring(0, 7);
String startDate = month + "-01";
String endDate = DateUtils.getLastDayOfMonth(month);
String today = dateFormat.format(new Date());
if (today.substring(0, 7).equals(dutyDate)) {
dutyDate = today;
} else {
dutyDate += "-01";
try {
startDate = isAfterToday(dateFormat.parse(startDate)) ? startDate : today;
} catch (ParseException e) {
throw new RuntimeException(e);
}
dutyPersonShiftLogMapper.delete(
Wrappers.<DutyPersonShiftLog>lambdaQuery()
.eq(DutyPersonShiftLog::getGroupCode, groupCode)
.eq(DutyPersonShiftLog::getDutyUser, dutyPersonDto.getUserName())
.ge(DutyPersonShiftLog::getDutyDate, dutyDate)
.ge(DutyPersonShiftLog::getDutyDate, dateFormat.format(new Date()))
.eq(DutyPersonShiftLog::getDutyUserId, dutyPersonDto.getUserId())
.ge(DutyPersonShiftLog::getDutyDate, startDate)
.le(DutyPersonShiftLog::getDutyDate, endDate)
.eq(DutyPersonShiftLog::getIsDelete, false)
);
}
dutyPersonShift.clear();
try {
List<Map<String, Object>> list = list(null, dateFormat.format(new Date()), "9999-12-12");
list = list.stream().filter(map -> Objects.equals(map.get("userName"), dutyPersonDto.getUserName())).collect(Collectors.toList());
for (Map<String, Object> map : list) {
DutyCarDto dutyCarDto = (DutyCarDto) Bean.mapToBean(map, DutyCarDto.class);
List<DutyPersonShiftDto> dutyShifts = dutyCarDto.getDutyShift().stream().filter(v -> isAfterToday(v.getDutyDate())).collect(Collectors.toList());
dutyPersonShift.addAll(dutyShifts);
}
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
});
}
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
Set<String> dutyDates = dutyPersonShift.stream().map(v -> dateFormat.format(v.getDutyDate())).collect(Collectors.toSet());
if (dutyDates.isEmpty()) {
return;
......@@ -586,7 +578,7 @@ public Object BuildScheduleDetails(String dutyDay, Long shiftId, String postType
Wrappers.<DutyPersonShiftLog>lambdaQuery()
.in(DutyPersonShiftLog::getDutyDate, dutyDates)
.eq(DutyPersonShiftLog::getGroupCode, groupCode)
.eq(DutyPersonShiftLog::getDutyUser, dutyPersonDto.getUserName())
.eq(DutyPersonShiftLog::getDutyUserId, dutyPersonDto.getUserId())
.eq(DutyPersonShiftLog::getIsDelete, false)
).stream().collect(Collectors.groupingBy(DutyPersonShiftLog::getDutyDate));
......@@ -634,12 +626,11 @@ public Object BuildScheduleDetails(String dutyDay, Long shiftId, String postType
Wrappers.<DutyPersonShiftLog>lambdaUpdate()
.eq(DutyPersonShiftLog::getDutyDate, dutyPersonShiftDto.getDutyDate())
.eq(DutyPersonShiftLog::getGroupCode, groupCode)
.eq(DutyPersonShiftLog::getDutyUser, dutyPersonDto.getUserName())
.eq(DutyPersonShiftLog::getDutyUserId, dutyPersonDto.getUserId())
.eq(DutyPersonShiftLog::getIsDelete, false)
);
}
}
;
}
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment