|
@@ -0,0 +1,175 @@
|
|
|
+package com.management.platform.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.management.platform.entity.Attendance;
|
|
|
+import com.management.platform.entity.CustomerInfo;
|
|
|
+import com.management.platform.entity.User;
|
|
|
+import com.management.platform.mapper.AttendanceMapper;
|
|
|
+import com.management.platform.service.AttendanceService;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.management.platform.service.UserService;
|
|
|
+import com.management.platform.util.ExcelUtil;
|
|
|
+import com.management.platform.util.HttpRespMsg;
|
|
|
+import com.management.platform.util.MessageUtils;
|
|
|
+import org.apache.poi.EncryptedDocumentException;
|
|
|
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFCell;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.text.DateFormat;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author Seyason
|
|
|
+ * @since 2025-06-16
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance> implements AttendanceService {
|
|
|
+ @Resource
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private AttendanceService attendanceService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg importAttendanceData(String month, MultipartFile file, HttpServletRequest request) {
|
|
|
+ HttpRespMsg msg=new HttpRespMsg();
|
|
|
+ try(InputStream inputStream= file.getInputStream()) {
|
|
|
+ //然后解析表格
|
|
|
+ Workbook workbook = WorkbookFactory.create(inputStream);
|
|
|
+// Integer companyId=userService.getById(request.getHeader("token")).getCompanyId();
|
|
|
+ //获取公司所有员工
|
|
|
+// List<User> userList = userService.list(new QueryWrapper<User>().eq("company_id", companyId));
|
|
|
+
|
|
|
+ //获取对应月份的考勤记录
|
|
|
+ List<Attendance> attendanceList = attendanceService.list(new QueryWrapper<Attendance>().eq("month", month));
|
|
|
+
|
|
|
+ Sheet sheet = workbook.getSheetAt(0);
|
|
|
+ //由于第一行需要指明列对应的标题
|
|
|
+ int rowNum = sheet.getLastRowNum();
|
|
|
+ if (rowNum == 0) {
|
|
|
+ //msg.setError("请填写客户数据");
|
|
|
+ msg.setError("请填写考勤数据");
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ List<String> nameList=new ArrayList<>();
|
|
|
+ int dataCount = 0;
|
|
|
+ ArrayList<Attendance> list = new ArrayList<>();
|
|
|
+ for (int rowIndex = 1; rowIndex <= rowNum; rowIndex++) {
|
|
|
+ Row row = sheet.getRow(rowIndex);
|
|
|
+ if (row == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (ExcelUtil.isRowEmpty(row)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ for (int i = 0; i < 8; i++) {
|
|
|
+ Cell cell = row.getCell(0);
|
|
|
+ if (cell!=null) {
|
|
|
+ cell.setCellType(CellType.STRING);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Attendance attendance = new Attendance();
|
|
|
+ attendance.setMonth(month);
|
|
|
+ Cell numCell = row.getCell(2);
|
|
|
+ if (numCell!=null&&!StringUtils.isEmpty(numCell.getStringCellValue())) {
|
|
|
+ String num = numCell.getStringCellValue();
|
|
|
+ String trim = num.trim();
|
|
|
+ attendance.setJobNumber("LEW"+trim);
|
|
|
+ }else {
|
|
|
+ attendance.setJobNumber("");
|
|
|
+ }
|
|
|
+
|
|
|
+ Cell nameCell = row.getCell(3);
|
|
|
+ if (nameCell!=null&&!StringUtils.isEmpty(nameCell.getStringCellValue())) {
|
|
|
+ attendance.setName(nameCell.getStringCellValue());
|
|
|
+ }else {
|
|
|
+ attendance.setName("");
|
|
|
+ }
|
|
|
+
|
|
|
+ Cell deptCell = row.getCell(4);
|
|
|
+ if (deptCell!=null&&!StringUtils.isEmpty(deptCell.getStringCellValue())) {
|
|
|
+ attendance.setDeptName(deptCell.getStringCellValue());
|
|
|
+ }else {
|
|
|
+ attendance.setDeptName("");
|
|
|
+ }
|
|
|
+
|
|
|
+ Cell timeCell = row.getCell(5);
|
|
|
+ if (timeCell!=null&&!StringUtils.isEmpty(timeCell.getStringCellValue())) {
|
|
|
+ String stringCellValue = timeCell.getStringCellValue();
|
|
|
+ int index = stringCellValue.indexOf("星");
|
|
|
+ String timeValue = stringCellValue.substring(0,index-1);
|
|
|
+ String weekValue = stringCellValue.substring(index);
|
|
|
+
|
|
|
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date date = simpleDateFormat.parse(timeValue);
|
|
|
+ attendance.setClockTime(date);
|
|
|
+ attendance.setWeek(weekValue);
|
|
|
+
|
|
|
+ }else {
|
|
|
+ attendance.setClockTime(null);
|
|
|
+ attendance.setWeek("");
|
|
|
+ }
|
|
|
+
|
|
|
+ Cell addressCell = row.getCell(6);
|
|
|
+ if (addressCell!=null&&!StringUtils.isEmpty(addressCell.getStringCellValue())) {
|
|
|
+ attendance.setClockAddress(addressCell.getStringCellValue());
|
|
|
+ }else {
|
|
|
+ attendance.setDeptName("");
|
|
|
+ }
|
|
|
+ long count = attendanceList.stream().filter(a -> a != null
|
|
|
+ && a.getMonth().equals(attendance.getMonth())
|
|
|
+ && a.getJobNumber().equals(attendance.getJobNumber())
|
|
|
+ && a.getClockTime().equals(attendance.getClockTime())
|
|
|
+ && a.getClockAddress().equals(attendance.getClockAddress()))
|
|
|
+ .count();
|
|
|
+ if (count>0) {
|
|
|
+ continue;
|
|
|
+ }else {
|
|
|
+ dataCount++;
|
|
|
+ list.add(attendance);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ attendanceService.saveBatch(list);
|
|
|
+ msg.data=dataCount;
|
|
|
+ return msg;
|
|
|
+ } catch (IOException e){
|
|
|
+ e.printStackTrace();
|
|
|
+ //msg.setError("文件处理出错");
|
|
|
+ msg.setError(MessageUtils.message("file.error"));
|
|
|
+ return msg;
|
|
|
+ } catch (InvalidFormatException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ //msg.setError("文件格式错误,如果安装了加密软件需要先解密再上传");
|
|
|
+ msg.setError(MessageUtils.message("file.FormatErrorAndDecrypt"));
|
|
|
+ return msg;
|
|
|
+ } catch (EncryptedDocumentException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ //msg.setError("文件加密状态,需要先解除加密状态再上传");
|
|
|
+ msg.setError(MessageUtils.message("file.encryption"));
|
|
|
+ return msg;
|
|
|
+ } catch (ParseException e) {
|
|
|
+ msg.setError("日期解析有问题");
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|