|
|
@@ -4,8 +4,8 @@ import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.convert.Convert;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.core.util.ZipUtil;
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
-import com.alibaba.excel.ExcelReader;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.izouma.wenlvju.domain.ArtType;
|
|
|
import com.izouma.wenlvju.domain.GradingOrganization;
|
|
|
@@ -25,21 +25,24 @@ import com.izouma.wenlvju.repo.OrganizationRepo;
|
|
|
import com.izouma.wenlvju.repo.SettingRepo;
|
|
|
import com.izouma.wenlvju.repo.performance.*;
|
|
|
import com.izouma.wenlvju.service.ArtTypeService;
|
|
|
+import com.izouma.wenlvju.service.storage.StorageService;
|
|
|
+import com.izouma.wenlvju.utils.FileUtils;
|
|
|
import com.izouma.wenlvju.utils.JpaUtils;
|
|
|
-import com.izouma.wenlvju.utils.excel.ExcelListener;
|
|
|
-import com.izouma.wenlvju.utils.excel.ExcelUtils;
|
|
|
import com.izouma.wenlvju.utils.excel.UploadDataListener;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
+import org.apache.commons.io.FilenameUtils;
|
|
|
+import org.apache.commons.lang3.RandomStringUtils;
|
|
|
+import org.apache.poi.util.TempFile;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageImpl;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
import javax.transaction.Transactional;
|
|
|
-import java.io.ByteArrayInputStream;
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
+import java.io.*;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -59,6 +62,7 @@ public class ProgrammeService {
|
|
|
private ArrangeRepo arrangeRepo;
|
|
|
private ProgrammeScoreRepo programmeScoreRepo;
|
|
|
private ArrangeJudgeRepo arrangeJudgeRepo;
|
|
|
+ private StorageService storageService;
|
|
|
|
|
|
|
|
|
public Page<Programme> all(PageQuery pageQuery) {
|
|
|
@@ -158,32 +162,46 @@ public class ProgrammeService {
|
|
|
.stream()
|
|
|
.collect(Collectors.groupingBy(Participant::getProgrammeId));
|
|
|
|
|
|
- return programmes.stream().map(programme -> {
|
|
|
+ List<ProgrammeDTO> dtos = new ArrayList<>();
|
|
|
+ programmes.forEach(programme -> {
|
|
|
List<Participant> participants = participantMap.get(programme.getId());
|
|
|
- ProgrammeDTO programmeDTO = toDTO(programme, performanceMap.get(programme.getPerformanceId()),
|
|
|
- organizationMap.get(programme.getOrganizationId()),
|
|
|
- gradingOrganizationMap.get(programme.getGradingOrganizationId()),
|
|
|
- artTypeMap.get(programme.getSpecialtyId()),
|
|
|
- settingMap.get(programme.getLevelSettingId()));
|
|
|
- participants.forEach(programmeDTO::setParticipant);
|
|
|
- return programmeDTO;
|
|
|
- }).collect(Collectors.toList());
|
|
|
+ if (CollUtil.isEmpty(participants)) {
|
|
|
+ ProgrammeDTO programmeDTO = toDTO(programme, performanceMap.get(programme.getPerformanceId()),
|
|
|
+ organizationMap.get(programme.getOrganizationId()),
|
|
|
+ gradingOrganizationMap.get(programme.getGradingOrganizationId()),
|
|
|
+ artTypeMap.get(programme.getSpecialtyId()),
|
|
|
+ settingMap.get(programme.getLevelSettingId()));
|
|
|
+ dtos.add(programmeDTO);
|
|
|
+ } else {
|
|
|
+ participants.forEach(participant -> {
|
|
|
+ ProgrammeDTO programmeDTO = toDTO(programme, performanceMap.get(programme.getPerformanceId()),
|
|
|
+ organizationMap.get(programme.getOrganizationId()),
|
|
|
+ gradingOrganizationMap.get(programme.getGradingOrganizationId()),
|
|
|
+ artTypeMap.get(programme.getSpecialtyId()),
|
|
|
+ settingMap.get(programme.getLevelSettingId()));
|
|
|
+ programmeDTO.setParticipant(participant);
|
|
|
+ dtos.add(programmeDTO);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return dtos;
|
|
|
}
|
|
|
|
|
|
@Transactional(rollbackOn = Exception.class)
|
|
|
- public void upload(InputStream is, Long userId) throws IOException {
|
|
|
+ public void upload(MultipartFile file, Long userId) throws IOException {
|
|
|
Organization organization = organizationRepo.findByUserId(userId).orElseThrow(new BusinessException("无记录"));
|
|
|
|
|
|
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
+ File destDir = TempFile.createTempDirectory("import");
|
|
|
+ ZipUtil.unzip(file.getInputStream(), destDir, StandardCharsets.UTF_8);
|
|
|
+// FileUtils.unzip(file.getInputStream(), destDir);
|
|
|
|
|
|
- byte[] buffer = new byte[1024];
|
|
|
- int len;
|
|
|
- while ((len = is.read(buffer)) > -1) {
|
|
|
- baos.write(buffer, 0, len);
|
|
|
- }
|
|
|
- baos.flush();
|
|
|
+ Map<String, File> map = FileUtils.findExcel(destDir);
|
|
|
+ if (map == null) return;
|
|
|
|
|
|
- InputStream indicatorStream = new ByteArrayInputStream(baos.toByteArray());
|
|
|
+ File xlsxFile = map.get("file");
|
|
|
+ destDir = map.get("destDir");
|
|
|
+
|
|
|
+ InputStream indicatorStream = new FileInputStream(xlsxFile);
|
|
|
UploadDataListener<ProgrammeDTO> listener = new UploadDataListener<>();
|
|
|
List<ProgrammeDTO> dtos = EasyExcel.read(indicatorStream, ProgrammeDTO.class, listener).sheet().doReadSync();
|
|
|
|
|
|
@@ -204,23 +222,49 @@ public class ProgrammeService {
|
|
|
.collect(Collectors.toMap(ArtType::getName, ArtType::getId));
|
|
|
|
|
|
|
|
|
- List<Programme> programmes = new ArrayList<>();
|
|
|
- dtos.forEach(dto -> {
|
|
|
- Programme programme = new Programme(dto);
|
|
|
- programme.setOrganizationId(organization.getId());
|
|
|
- programme.setGradingOrganizationId(gradeMap.get(dto.getGradingOrganization()));
|
|
|
- programme.setPerformanceId(performanceMap.get(dto.getPerformance()));
|
|
|
- programme.setSpecialtyId(artTypeMap.get(dto.getSpecialty()));
|
|
|
- if (dto.getCompetitionGroup().equals(CompetitionGroup.SINGLE)) {
|
|
|
- programme.setLevelSettingId(mapMap.get(3).get(dto.getLevel()));
|
|
|
- } else if (dto.getCompetitionGroup().equals(CompetitionGroup.COLLECTIVE)) {
|
|
|
- programme.setLevelSettingId(mapMap.get(4).get(dto.getLevel()));
|
|
|
+ List<Participant> participants = new ArrayList<>();
|
|
|
+ Long pid = null;
|
|
|
+ for (ProgrammeDTO dto : dtos) {
|
|
|
+ if (dto.getName() != null && dto.getSpecialty() != null) {
|
|
|
+ Programme programme = new Programme(dto);
|
|
|
+ programme.setOrganizationId(organization.getId());
|
|
|
+ programme.setGradingOrganizationId(gradeMap.get(dto.getGradingOrganization()));
|
|
|
+ programme.setPerformanceId(performanceMap.get(dto.getPerformance()));
|
|
|
+ programme.setSpecialtyId(artTypeMap.get(dto.getSpecialty()));
|
|
|
+ if (dto.getCompetitionGroup().equals(CompetitionGroup.SINGLE)) {
|
|
|
+ programme.setLevelSettingId(mapMap.get(3).get(dto.getLevel()));
|
|
|
+ } else if (dto.getCompetitionGroup().equals(CompetitionGroup.COLLECTIVE)) {
|
|
|
+ programme.setLevelSettingId(mapMap.get(4).get(dto.getLevel()));
|
|
|
+ }
|
|
|
+ pid = programmeRepo.save(programme).getId();
|
|
|
}
|
|
|
|
|
|
- programmes.add(programme);
|
|
|
- });
|
|
|
+ Participant participant = new Participant(dto);
|
|
|
+ participant.setProgrammeId(pid);
|
|
|
+ participants.add(participant);
|
|
|
|
|
|
- programmeRepo.saveAll(programmes);
|
|
|
+ if (dto.getImg() != null) {
|
|
|
+ File uploadFile = new File(destDir, dto.getImg());
|
|
|
+ if (uploadFile.exists()) {
|
|
|
+ participant.setImg(this.saveImg(uploadFile));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (dto.getCertificate() != null) {
|
|
|
+ File uploadFile = new File(destDir, dto.getCertificate());
|
|
|
+ if (uploadFile.exists()) {
|
|
|
+ participant.setCertificate(this.saveImg(uploadFile));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ participantRepo.saveAll(participants);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String saveImg(File uploadFile) throws FileNotFoundException {
|
|
|
+ String path = "image/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
|
|
|
+ + RandomStringUtils.randomAlphabetic(8)
|
|
|
+ + "." + FilenameUtils.getExtension(uploadFile.getName());
|
|
|
+ return storageService.uploadFromInputStream(new FileInputStream(uploadFile), path);
|
|
|
}
|
|
|
|
|
|
@Transactional(rollbackOn = Exception.class)
|