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

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

parent e2d64218
...@@ -883,4 +883,19 @@ public class DateUtils { ...@@ -883,4 +883,19 @@ public class DateUtils {
Date end = calendar.getTime(); Date end = calendar.getTime();
return end; 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 ...@@ -93,7 +93,7 @@ public class DutyCarServiceImpl extends DutyCommonServiceImpl implements IDutyCa
}); });
// 修改操作动态表单的方法,修改人陈浩 -------------end 2021-09-28 // 修改操作动态表单的方法,修改人陈浩 -------------end 2021-09-28
// 生成变更日志 // 生成变更日志
createDutyPersonShiftLog(dutyCarDto, false, ActionStatus.METHOD_UPDATE.getCode()); createDutyPersonShiftLog(dutyCarDto, false, ActionStatus.METHOD_ADD.getCode());
// 3.返回保存后的数据 // 3.返回保存后的数据
return dutyCarDto; return dutyCarDto;
...@@ -432,49 +432,42 @@ public class DutyCarServiceImpl extends DutyCommonServiceImpl implements IDutyCa ...@@ -432,49 +432,42 @@ public class DutyCarServiceImpl extends DutyCommonServiceImpl implements IDutyCa
} }
@SuppressWarnings("Duplicates") @SuppressWarnings("Duplicates")
public void createDutyPersonShiftLog(DutyCarDto dutyCarDto, boolean isInit, String actionCode) { public synchronized void createDutyPersonShiftLog(DutyCarDto dutyCarDto, boolean isInit, String actionCode) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
List<DutyPersonShiftDto> dutyPersonShift = dutyCarDto.getDutyShift(); List<DutyPersonShiftDto> dutyPersonShift = dutyCarDto.getDutyShift();
final String groupCode = "dutyCar"; final String groupCode = "dutyCar";
// 新建/更新排班时处理跨月数据 // 更新排班时处理
if (!isInit && StringUtils.isNotBlank(actionCode)) { if (!isInit && Objects.equals(actionCode, "update")) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
if (Objects.nonNull(dutyPersonShift) && !dutyPersonShift.isEmpty()) { if (Objects.nonNull(dutyPersonShift) && !dutyPersonShift.isEmpty()) {
// 拿到排班月份 yyyy-MM String month = dateFormat.format(dutyPersonShift.get(0).getDutyDate()).substring(0, 7);
String dutyDate = dateFormat.format(dutyPersonShift.get(0).getDutyDate()); String startDate = month + "-01";
dutyDate = dutyDate.substring(0, 7); String endDate = DateUtils.getLastDayOfMonth(month);
String today = dateFormat.format(new Date()); String today = dateFormat.format(new Date());
if (today.substring(0, 7).equals(dutyDate)) { try {
dutyDate = today; startDate = isAfterToday(dateFormat.parse(startDate)) ? startDate : today;
} else { } catch (ParseException e) {
dutyDate += "-01"; throw new RuntimeException(e);
} }
dutyPersonShiftLogMapper.delete( dutyPersonShiftLogMapper.delete(
Wrappers.<DutyPersonShiftLog>lambdaQuery() Wrappers.<DutyPersonShiftLog>lambdaQuery()
.eq(DutyPersonShiftLog::getGroupCode, groupCode) .eq(DutyPersonShiftLog::getGroupCode, groupCode)
.eq(DutyPersonShiftLog::getDutyUser, dutyCarDto.getUserName()) .eq(DutyPersonShiftLog::getDutyUserId, dutyCarDto.getUserId())
.ge(DutyPersonShiftLog::getDutyDate, dutyDate) .ge(DutyPersonShiftLog::getDutyDate, startDate)
.ge(DutyPersonShiftLog::getDutyDate, dateFormat.format(new Date())) .le(DutyPersonShiftLog::getDutyDate, endDate)
.eq(DutyPersonShiftLog::getIsDelete, false) .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()); Set<String> dutyDates = dutyPersonShift.stream().map(v -> dateFormat.format(v.getDutyDate())).collect(Collectors.toSet());
if (dutyDates.isEmpty()) { if (dutyDates.isEmpty()) {
return; return;
...@@ -483,7 +476,7 @@ public class DutyCarServiceImpl extends DutyCommonServiceImpl implements IDutyCa ...@@ -483,7 +476,7 @@ public class DutyCarServiceImpl extends DutyCommonServiceImpl implements IDutyCa
Wrappers.<DutyPersonShiftLog>lambdaQuery() Wrappers.<DutyPersonShiftLog>lambdaQuery()
.in(DutyPersonShiftLog::getDutyDate, dutyDates) .in(DutyPersonShiftLog::getDutyDate, dutyDates)
.eq(DutyPersonShiftLog::getGroupCode, groupCode) .eq(DutyPersonShiftLog::getGroupCode, groupCode)
.eq(DutyPersonShiftLog::getDutyUser, dutyCarDto.getUserName()) .eq(DutyPersonShiftLog::getDutyUserId, dutyCarDto.getUserId())
.eq(DutyPersonShiftLog::getIsDelete, false) .eq(DutyPersonShiftLog::getIsDelete, false)
).stream().collect(Collectors.groupingBy(DutyPersonShiftLog::getDutyDate)); ).stream().collect(Collectors.groupingBy(DutyPersonShiftLog::getDutyDate));
...@@ -534,7 +527,7 @@ public class DutyCarServiceImpl extends DutyCommonServiceImpl implements IDutyCa ...@@ -534,7 +527,7 @@ public class DutyCarServiceImpl extends DutyCommonServiceImpl implements IDutyCa
Wrappers.<DutyPersonShiftLog>lambdaUpdate() Wrappers.<DutyPersonShiftLog>lambdaUpdate()
.eq(DutyPersonShiftLog::getDutyDate, dutyPersonShiftDto.getDutyDate()) .eq(DutyPersonShiftLog::getDutyDate, dutyPersonShiftDto.getDutyDate())
.eq(DutyPersonShiftLog::getGroupCode, groupCode) .eq(DutyPersonShiftLog::getGroupCode, groupCode)
.eq(DutyPersonShiftLog::getDutyUser, dutyCarDto.getUserName()) .eq(DutyPersonShiftLog::getDutyUserId, dutyCarDto.getUserId())
.eq(DutyPersonShiftLog::getIsDelete, false) .eq(DutyPersonShiftLog::getIsDelete, false)
); );
} }
......
...@@ -534,50 +534,42 @@ public Object BuildScheduleDetails(String dutyDay, Long shiftId, String postType ...@@ -534,50 +534,42 @@ public Object BuildScheduleDetails(String dutyDay, Long shiftId, String postType
} }
@SuppressWarnings("Duplicates") @SuppressWarnings("Duplicates")
public void createDutyPersonShiftLog(DutyPersonDto dutyPersonDto, boolean isInit, String actionCode) { public synchronized void createDutyPersonShiftLog(DutyPersonDto dutyPersonDto, boolean isInit, String actionCode) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
List<DutyPersonShiftDto> dutyPersonShift = dutyPersonDto.getDutyShift(); List<DutyPersonShiftDto> dutyPersonShift = dutyPersonDto.getDutyShift();
final String groupCode = "dutyPerson"; final String groupCode = "dutyPerson";
// 新建/更新排班时处理跨月数据 // 更新排班时处理
if (!isInit && StringUtils.isNotBlank(actionCode)) { if (!isInit && Objects.equals(actionCode, "update")) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
if (Objects.nonNull(dutyPersonShift) && !dutyPersonShift.isEmpty()) { if (Objects.nonNull(dutyPersonShift) && !dutyPersonShift.isEmpty()) {
// 拿到排班月份 yyyy-MM String month = dateFormat.format(dutyPersonShift.get(0).getDutyDate()).substring(0, 7);
String dutyDate = dateFormat.format(dutyPersonShift.get(0).getDutyDate()); String startDate = month + "-01";
dutyDate = dutyDate.substring(0, 7); String endDate = DateUtils.getLastDayOfMonth(month);
String today = dateFormat.format(new Date()); String today = dateFormat.format(new Date());
if (today.substring(0, 7).equals(dutyDate)) { try {
dutyDate = today; startDate = isAfterToday(dateFormat.parse(startDate)) ? startDate : today;
} else { } catch (ParseException e) {
dutyDate += "-01"; throw new RuntimeException(e);
} }
dutyPersonShiftLogMapper.delete( dutyPersonShiftLogMapper.delete(
Wrappers.<DutyPersonShiftLog>lambdaQuery() Wrappers.<DutyPersonShiftLog>lambdaQuery()
.eq(DutyPersonShiftLog::getGroupCode, groupCode) .eq(DutyPersonShiftLog::getGroupCode, groupCode)
.eq(DutyPersonShiftLog::getDutyUser, dutyPersonDto.getUserName()) .eq(DutyPersonShiftLog::getDutyUserId, dutyPersonDto.getUserId())
.ge(DutyPersonShiftLog::getDutyDate, dutyDate) .ge(DutyPersonShiftLog::getDutyDate, startDate)
.ge(DutyPersonShiftLog::getDutyDate, dateFormat.format(new Date())) .le(DutyPersonShiftLog::getDutyDate, endDate)
.eq(DutyPersonShiftLog::getIsDelete, false) .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()); Set<String> dutyDates = dutyPersonShift.stream().map(v -> dateFormat.format(v.getDutyDate())).collect(Collectors.toSet());
if (dutyDates.isEmpty()) { if (dutyDates.isEmpty()) {
return; return;
...@@ -586,7 +578,7 @@ public Object BuildScheduleDetails(String dutyDay, Long shiftId, String postType ...@@ -586,7 +578,7 @@ public Object BuildScheduleDetails(String dutyDay, Long shiftId, String postType
Wrappers.<DutyPersonShiftLog>lambdaQuery() Wrappers.<DutyPersonShiftLog>lambdaQuery()
.in(DutyPersonShiftLog::getDutyDate, dutyDates) .in(DutyPersonShiftLog::getDutyDate, dutyDates)
.eq(DutyPersonShiftLog::getGroupCode, groupCode) .eq(DutyPersonShiftLog::getGroupCode, groupCode)
.eq(DutyPersonShiftLog::getDutyUser, dutyPersonDto.getUserName()) .eq(DutyPersonShiftLog::getDutyUserId, dutyPersonDto.getUserId())
.eq(DutyPersonShiftLog::getIsDelete, false) .eq(DutyPersonShiftLog::getIsDelete, false)
).stream().collect(Collectors.groupingBy(DutyPersonShiftLog::getDutyDate)); ).stream().collect(Collectors.groupingBy(DutyPersonShiftLog::getDutyDate));
...@@ -634,12 +626,11 @@ public Object BuildScheduleDetails(String dutyDay, Long shiftId, String postType ...@@ -634,12 +626,11 @@ public Object BuildScheduleDetails(String dutyDay, Long shiftId, String postType
Wrappers.<DutyPersonShiftLog>lambdaUpdate() Wrappers.<DutyPersonShiftLog>lambdaUpdate()
.eq(DutyPersonShiftLog::getDutyDate, dutyPersonShiftDto.getDutyDate()) .eq(DutyPersonShiftLog::getDutyDate, dutyPersonShiftDto.getDutyDate())
.eq(DutyPersonShiftLog::getGroupCode, groupCode) .eq(DutyPersonShiftLog::getGroupCode, groupCode)
.eq(DutyPersonShiftLog::getDutyUser, dutyPersonDto.getUserName()) .eq(DutyPersonShiftLog::getDutyUserId, dutyPersonDto.getUserId())
.eq(DutyPersonShiftLog::getIsDelete, false) .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