|
@@ -1,18 +1,36 @@
|
|
|
package com.rongwei.bscommon.system.service.impl;
|
|
|
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
import com.rongwei.bscommon.system.excelImportListener.UserPointsRegistrationListener;
|
|
|
import com.rongwei.bscommon.system.service.ExcelImportService;
|
|
|
+import com.rongwei.bscommon.system.utils.QHSEUtils;
|
|
|
+import com.rongwei.bsentity.domain.QhseContractWorkersDo;
|
|
|
import com.rongwei.bsentity.domain.QhsePointsDetailsUserDo;
|
|
|
+import com.rongwei.bsentity.domain.QhsePointsRecordUserDo;
|
|
|
import com.rongwei.bsentity.dto.UserPointsRegistrationImportDto;
|
|
|
+import com.rongwei.rwadmincommon.system.vo.SysUserVo;
|
|
|
+import com.rongwei.rwcommon.base.BaseDo;
|
|
|
import com.rongwei.rwcommon.base.R;
|
|
|
+import com.rongwei.rwcommon.utils.SecurityUtil;
|
|
|
import com.rongwei.rwcommon.utils.StringUtils;
|
|
|
import com.rongwei.rwcommonentity.commonservers.domain.SysFileItemDo;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.rongwei.rwcommon.utils.UtilsChecks.parameterCheck;
|
|
|
|
|
@@ -27,29 +45,129 @@ public class ExcelImportServiceImpl implements ExcelImportService {
|
|
|
@Autowired
|
|
|
private QHSEFileItemServiceImpl qhseFileItemServiceImpl;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private QhseContractWorkersServiceImpl qhseContractWorkersService;
|
|
|
+ @Autowired
|
|
|
+ private QhsePointsDetailsUserServiceImpl qhsePointsDetailsUserService;
|
|
|
+ @Autowired
|
|
|
+ private QhsePointsRecordUserServiceImpl qhsePointsRecordUserService;
|
|
|
+ @Autowired
|
|
|
+ private PointServiceImpl pointServiceImpl;
|
|
|
+
|
|
|
@Override
|
|
|
- public R userPointsRegistration(String fileId) {
|
|
|
- File file = fileCheck(fileId);
|
|
|
+ public R userPointsRegistration(MultipartFile file) {
|
|
|
+ Path tempPath;
|
|
|
+ try {
|
|
|
+ tempPath = Files.createTempFile(file.getOriginalFilename(), null);
|
|
|
+ file.transferTo(tempPath);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ File tempFile = tempPath.toFile();
|
|
|
UserPointsRegistrationListener userPointsRegistrationListener = new UserPointsRegistrationListener();
|
|
|
- EasyExcel.read(file, UserPointsRegistrationImportDto.class, userPointsRegistrationListener).sheet().doRead();
|
|
|
+ EasyExcel.read(tempFile, UserPointsRegistrationImportDto.class, userPointsRegistrationListener).sheet(0).doRead();
|
|
|
// 解析后的数据
|
|
|
- List<QhsePointsDetailsUserDo> parsedData = userPointsRegistrationListener.getData();
|
|
|
- return null;
|
|
|
+ List<QhsePointsDetailsUserDo> analysisDataList = userPointsRegistrationListener.getData();
|
|
|
+ List<String> personCodes = analysisDataList.stream().map(QhsePointsDetailsUserDo::getPersoncode).collect(Collectors.toList());
|
|
|
+
|
|
|
+
|
|
|
+ List<QhseContractWorkersDo> qhseContractWorkersDos = qhseContractWorkersService
|
|
|
+ .list(new LambdaQueryWrapper<QhseContractWorkersDo>()
|
|
|
+ .select(QhseContractWorkersDo::getId, QhseContractWorkersDo::getWorknumber,
|
|
|
+ QhseContractWorkersDo::getWorkertype, QhseContractWorkersDo::getWorkername)
|
|
|
+ .eq(BaseDo::getDeleted, 0)
|
|
|
+ .in(QhseContractWorkersDo::getWorknumber, personCodes));
|
|
|
+ // 人员工号校验
|
|
|
+ checkPersonCode(qhseContractWorkersDos, personCodes);
|
|
|
+
|
|
|
+ LambdaQueryWrapper<QhsePointsRecordUserDo> searchWrapper = new LambdaQueryWrapper<>();
|
|
|
+ // 校验对应年份是否有该员工的积分信息
|
|
|
+ Map<Integer, List<String>> yearAndPersonCode = analysisDataList.stream().collect(Collectors.groupingBy(QhsePointsDetailsUserDo::getPointsyear, Collectors.mapping(QhsePointsDetailsUserDo::getPersoncode, Collectors.toList())));
|
|
|
+ searchWrapper.eq(BaseDo::getDeleted, "0");
|
|
|
+ yearAndPersonCode.forEach((k, v) -> {
|
|
|
+ searchWrapper.or(wrapper -> wrapper.eq(QhsePointsRecordUserDo::getPointyear, k).in(QhsePointsRecordUserDo::getHolderaccount, v));
|
|
|
+ });
|
|
|
+ List<QhsePointsRecordUserDo> mainTableData = qhsePointsRecordUserService.list(searchWrapper);
|
|
|
+ List<String> collect = mainTableData.parallelStream().map(data -> data.getPointyear() + data.getHolderaccount()).collect(Collectors.toList());
|
|
|
+ String errorMsg = analysisDataList.parallelStream().filter(data -> !collect.contains(data.getPointsyear() + data.getPointscode()))
|
|
|
+ .map(data -> data.getPointsyear() + data.getPointscode()).collect(Collectors.joining(";"));
|
|
|
+ if (StringUtils.isNotBlank(errorMsg)) {
|
|
|
+ throw new RuntimeException("以下用户在对应年份没有想对应的积分记录信息" + errorMsg);
|
|
|
+ }
|
|
|
+ //校验数据是否在数据库中存在
|
|
|
+ checkDataRepeat(analysisDataList, qhseContractWorkersDos);
|
|
|
+
|
|
|
+ dataProcessingBeforeSaving(analysisDataList, qhseContractWorkersDos, mainTableData);
|
|
|
+ // 删除临时文件
|
|
|
+ tempFile.delete();
|
|
|
+ return R.ok();
|
|
|
}
|
|
|
|
|
|
+ public void dataProcessingBeforeSaving(List<QhsePointsDetailsUserDo> analysisDataList, List<QhseContractWorkersDo> qhseContractWorkersDos, List<QhsePointsRecordUserDo> mainTableData) {
|
|
|
+ QhseContractWorkersDo qhseContractWorkersDo;
|
|
|
+ SysUserVo currentUser = QHSEUtils.getCurrentUser();
|
|
|
+ QhsePointsRecordUserDo qhsePointsRecordUserDo;
|
|
|
+ // 增加基础数据
|
|
|
+ for (QhsePointsDetailsUserDo analysisData : analysisDataList) {
|
|
|
+ qhseContractWorkersDo = qhseContractWorkersDos.stream().filter(data -> data.getWorknumber().equals(analysisData.getPersoncode())).findFirst().orElse(null);
|
|
|
+ qhsePointsRecordUserDo = mainTableData.stream().filter(data -> data.getPointyear().equals(analysisData.getPointsyear()) && data.getHolderaccount().equals(analysisData.getPersoncode()))
|
|
|
+ .findFirst().orElse(null);
|
|
|
+ analysisData.setId(SecurityUtil.getUUID());
|
|
|
+ QHSEUtils.initModelGeneralParameters(analysisData, currentUser);
|
|
|
+ if (qhsePointsRecordUserDo != null) {
|
|
|
+ analysisData.setMainid(qhsePointsRecordUserDo.getId());
|
|
|
+ }
|
|
|
+ if (qhseContractWorkersDo != null) {
|
|
|
+ analysisData.setPersonname(qhseContractWorkersDo.getWorkername());
|
|
|
+ analysisData.setPersonid(qhseContractWorkersDo.getId());
|
|
|
+ analysisData.setPersontype(qhseContractWorkersDo.getWorkertype());
|
|
|
+ analysisData.setDepartmentid(qhseContractWorkersDo.getDepartmentid());
|
|
|
+ analysisData.setDepartmentname(qhseContractWorkersDo.getDepartmentname());
|
|
|
+ analysisData.setSubcontractorid(qhseContractWorkersDo.getContractorid());
|
|
|
+ analysisData.setSubcontractorname(qhseContractWorkersDo.getContractor());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 数据存表
|
|
|
+ for (List<QhsePointsDetailsUserDo> qhsePointsDetailsUserDos : Lists.partition(analysisDataList, 1000)) {
|
|
|
+ qhsePointsDetailsUserService.getBaseMapper().insertBatchSomeColumn(qhsePointsDetailsUserDos);
|
|
|
+ }
|
|
|
|
|
|
+ // 更新用户积分信息
|
|
|
+ pointServiceImpl.userPointEegister(analysisDataList.stream().map(QhsePointsDetailsUserDo::getId).collect(Collectors.toList()));
|
|
|
+ }
|
|
|
|
|
|
- public File fileCheck(String fileId) {
|
|
|
- parameterCheck(() -> StringUtils.isBlank(fileId), "请上传文件", "文件ID为空");
|
|
|
- SysFileItemDo sysFileItemDo = qhseFileItemServiceImpl.getById(fileId);
|
|
|
- parameterCheck(() -> sysFileItemDo == null, "文件解析有误!请联系管理员", "无法根据文件ID:{},获取到文件信息", fileId);
|
|
|
+ /**
|
|
|
+ * 校验检验数据是否重复
|
|
|
+ */
|
|
|
+ public void checkDataRepeat(List<QhsePointsDetailsUserDo> analysisDataList, List<QhseContractWorkersDo> qhseContractWorkersDos) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ List<Integer> years = analysisDataList.stream().map(QhsePointsDetailsUserDo::getPointsyear).collect(Collectors.toList());
|
|
|
+ List<String> personId = qhseContractWorkersDos.stream().map(QhseContractWorkersDo::getId).collect(Collectors.toList());
|
|
|
+ // 数据库中的数据
|
|
|
+ List<String> dataBaseDatas = qhsePointsDetailsUserService.getBaseMapper().checkExistsData(years, personId);
|
|
|
+ // excel 中的数据
|
|
|
+ List<String> checkExistsData = analysisDataList.stream().map(data -> data.getPersoncode() + data.getDescription() + sdf.format(data.getHappentime()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ String errorMsg = dataBaseDatas.parallelStream().filter(checkExistsData::contains).collect(Collectors.joining(";"));
|
|
|
+ if (StringUtils.isNotBlank(errorMsg)) {
|
|
|
+ throw new RuntimeException("excel中以下数据" + errorMsg + "已存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- String realPath = sysFileItemDo.getFullpath();
|
|
|
- parameterCheck(() -> StringUtils.isBlank(realPath), "文件解析有误!请联系管理员", "根据文件ID:{},无法获取到文件路径", fileId);
|
|
|
- File file = new File(realPath);
|
|
|
- parameterCheck(() -> !file.exists(), "文件解析有误!请联系管理员", "根据文件地址:{},无法获取文件", realPath);
|
|
|
- return file;
|
|
|
|
|
|
+ /**
|
|
|
+ * 员工工号校验
|
|
|
+ *
|
|
|
+ * @param qhseContractWorkersDos
|
|
|
+ * @param personCodes
|
|
|
+ */
|
|
|
+ public void checkPersonCode(List<QhseContractWorkersDo> qhseContractWorkersDos, List<String> personCodes) {
|
|
|
+ List<String> dataBaseWorkNum = qhseContractWorkersDos.stream().map(QhseContractWorkersDo::getWorknumber).collect(Collectors.toList());
|
|
|
+ String noExistsPersonCode = personCodes.parallelStream().filter(personCode -> !dataBaseWorkNum.contains(personCode)).collect(Collectors.joining(";"));
|
|
|
+ if (StringUtils.isNotBlank(noExistsPersonCode)) {
|
|
|
+ throw new RuntimeException("excel中以下员工工号不存在" + noExistsPersonCode);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+
|
|
|
}
|