|
|
@@ -5,22 +5,27 @@ import com.alibaba.excel.EasyExcel;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.google.protobuf.ServiceException;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
import lombok.SneakyThrows;
|
|
|
import org.sky.core.mp.base.BaseServiceImpl;
|
|
|
+import org.sky.core.secure.utils.SecureUtil;
|
|
|
import org.sky.core.tool.utils.CollectionUtil;
|
|
|
+import org.sky.core.tool.utils.Func;
|
|
|
import org.sky.scientific.excel.AttendanceMonthlyDataListener;
|
|
|
import org.sky.scientific.excel.AttendanceMonthlyExcel;
|
|
|
import org.sky.scientific.excel.DailyAttendance;
|
|
|
import org.sky.scientific.mapper.AttendanceMapper;
|
|
|
+import org.sky.scientific.mapper.TechnicianMapper;
|
|
|
import org.sky.scientific.pojo.entity.AttendanceEntity;
|
|
|
import org.sky.scientific.service.IAttendanceService;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
|
|
import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
* 考勤管理 服务实现类
|
|
|
@@ -29,8 +34,11 @@ import java.util.List;
|
|
|
* @since 2025-06-29
|
|
|
*/
|
|
|
@Service
|
|
|
+@AllArgsConstructor
|
|
|
public class AttendanceServiceImpl extends BaseServiceImpl<AttendanceMapper, AttendanceEntity> implements IAttendanceService {
|
|
|
|
|
|
+ private final TechnicianMapper technicianMapper;
|
|
|
+
|
|
|
@Override
|
|
|
public boolean submit(AttendanceEntity attendance) {
|
|
|
int affectNum = 0;
|
|
|
@@ -50,6 +58,7 @@ public class AttendanceServiceImpl extends BaseServiceImpl<AttendanceMapper, Att
|
|
|
}
|
|
|
|
|
|
@SneakyThrows
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
@Override
|
|
|
public void importAttendance(MultipartFile file, String yearAndMonth) {
|
|
|
AttendanceMonthlyDataListener listener = new AttendanceMonthlyDataListener();
|
|
|
@@ -63,31 +72,60 @@ public class AttendanceServiceImpl extends BaseServiceImpl<AttendanceMapper, Att
|
|
|
if (CollectionUtil.isEmpty(records)) {
|
|
|
throw new ServiceException("未能从模板中读取数据,请检查模板格式是否正确");
|
|
|
}
|
|
|
- List<Long> technicianIds = Arrays.asList(2L, 3L, 5L);
|
|
|
|
|
|
- List<AttendanceEntity> attendanceEntities = new ArrayList<>();
|
|
|
- int index = 0;
|
|
|
+ //需要新增的,以便后续批量新增
|
|
|
+ List<AttendanceEntity> insertList = new ArrayList<>();
|
|
|
+ //需要更新的,以便后续批量修改
|
|
|
+ List<AttendanceEntity> updateList = new ArrayList<>();
|
|
|
+
|
|
|
+ int batchCount = 20;
|
|
|
+
|
|
|
+ List<AttendanceEntity> dbList = baseMapper.selectByYearAndMonth(yearAndMonth);
|
|
|
for (AttendanceMonthlyExcel record : records) {
|
|
|
//保存
|
|
|
-// technicianId 2,3,5,先写死吧
|
|
|
-
|
|
|
+ Long technicianId = technicianMapper.selectPrimaryKeyByNumberOrIdentityCard(record.getNumber(), record.getIdentityCard());
|
|
|
+ if (technicianId == null) {
|
|
|
+ throw new ServiceException("序号为[" + record.getSerialNumber() + "]的工号和者身份证都不存在");
|
|
|
+ }
|
|
|
+// List<AttendanceEntity> dataByTechnicianMonthly = dbList.stream().filter(e -> e.getTechnicianId().equals(technicianId) && Objects.equals(e.getWorkDate().substring(0, 6), yearAndMonth)).toList();
|
|
|
+ List<AttendanceEntity> dataByTechnicianMonthly = dbList.stream().filter(e -> e.getTechnicianId().equals(technicianId)).toList();
|
|
|
for (DailyAttendance dailyAttendance : record.getDailyAttendances()) {
|
|
|
- index++;
|
|
|
- AttendanceEntity entity = new AttendanceEntity();
|
|
|
- entity.setTechnicianId(technicianIds.get(records.indexOf(record)));
|
|
|
- entity.setWorkDate(dailyAttendance.getDate());
|
|
|
- entity.setWorkDuration(dailyAttendance.getHours());
|
|
|
- attendanceEntities.add(entity);
|
|
|
- if(index%20 == 0){
|
|
|
- boolean saveBatchResult = this.saveBatch(attendanceEntities);
|
|
|
- if(saveBatchResult){
|
|
|
- attendanceEntities.clear();
|
|
|
- }
|
|
|
+
|
|
|
+ List<Long> primaryKeyList = dataByTechnicianMonthly.stream().filter(e -> Objects.equals(e.getWorkDate(), dailyAttendance.getDate())).map(AttendanceEntity::getId).toList();
|
|
|
+ Long primaryKey = CollectionUtil.isEmpty(primaryKeyList) ? null : primaryKeyList.get(0);
|
|
|
+// baseMapper.selectPrimaryKeyByTechnicianIdAndWorkDate(technicianId, dailyAttendance.getDate());
|
|
|
+ if (primaryKey == null) {
|
|
|
+ AttendanceEntity entity = new AttendanceEntity();
|
|
|
+ entity.setTechnicianId(technicianId);
|
|
|
+ entity.setWorkDate(dailyAttendance.getDate());
|
|
|
+ entity.setWorkDuration(dailyAttendance.getHours());
|
|
|
+ entity.setTenantId(SecureUtil.getTenantId());
|
|
|
+ entity.setCreateUser(SecureUtil.getUserId());
|
|
|
+ entity.setCreateDept(Func.toLong(SecureUtil.getDeptId()));
|
|
|
+ insertList.add(entity);
|
|
|
+ } else {
|
|
|
+ AttendanceEntity entity = new AttendanceEntity();
|
|
|
+ entity.setId(primaryKey);
|
|
|
+ entity.setWorkDuration(dailyAttendance.getHours());
|
|
|
+ updateList.add(entity);
|
|
|
}
|
|
|
+ if (insertList.size() % batchCount == 0) {
|
|
|
+ int insertCount = baseMapper.batchSave(insertList);
|
|
|
+ insertList.clear();
|
|
|
+ }
|
|
|
+// if (updateList.size() % batchCount == 0) {
|
|
|
+// int updateCount = baseMapper.batchUpdate(updateList);
|
|
|
+// if (updateCount == batchCount) {
|
|
|
+// updateList.clear();
|
|
|
+// }
|
|
|
+// }
|
|
|
}
|
|
|
- if(index%20>0){
|
|
|
- this.saveBatch(attendanceEntities);
|
|
|
- }
|
|
|
}
|
|
|
+ if (!insertList.isEmpty()) {
|
|
|
+ baseMapper.batchSave(insertList);
|
|
|
+ }
|
|
|
+// if (!updateList.isEmpty()) {
|
|
|
+// baseMapper.batchUpdate(updateList);
|
|
|
+// }
|
|
|
}
|
|
|
}
|