licailing пре 4 година
родитељ
комит
2640653da8

+ 11 - 0
src/main/java/com/izouma/wenlvju/domain/performance/Participant.java

@@ -2,6 +2,7 @@ package com.izouma.wenlvju.domain.performance;
 
 import com.alibaba.excel.annotation.ExcelProperty;
 import com.izouma.wenlvju.domain.BaseEntity;
+import com.izouma.wenlvju.dto.ProgrammeDTO;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.AllArgsConstructor;
@@ -41,4 +42,14 @@ public class Participant extends BaseEntity {
     private String certificate;
 
     private String phone;
+
+    public Participant(ProgrammeDTO dto) {
+        this.name = dto.getParticipantName();
+        this.sex = dto.getParticipantSex();
+        this.birthday = dto.getParticipantBirthday();
+        this.img = dto.getImg();
+        this.idNo = dto.getIdNo();
+        this.certificate = dto.getCertificate();
+        this.phone = dto.getParticipantPhone();
+    }
 }

+ 4 - 5
src/main/java/com/izouma/wenlvju/dto/ProgrammeDTO.java

@@ -3,13 +3,13 @@ package com.izouma.wenlvju.dto;
 import cn.hutool.core.bean.BeanUtil;
 import com.alibaba.excel.annotation.ExcelIgnore;
 import com.alibaba.excel.annotation.ExcelProperty;
-import com.alibaba.excel.annotation.format.DateTimeFormat;
 import com.izouma.wenlvju.annotations.EnumFormat;
 import com.izouma.wenlvju.domain.VideoObject;
 import com.izouma.wenlvju.domain.performance.Participant;
 import com.izouma.wenlvju.domain.performance.Programme;
 import com.izouma.wenlvju.enums.CompetitionGroup;
 import com.izouma.wenlvju.utils.excel.EnumExcelConverter;
+import com.izouma.wenlvju.utils.excel.LocalDateConverter;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.AllArgsConstructor;
@@ -80,7 +80,7 @@ public class ProgrammeDTO {
     @ExcelIgnore
     private int status;
 
-    @ApiModelProperty(value = "照片")
+    @ExcelProperty(value = "照片")
     private String img;
 
     @ExcelProperty(value = "参赛人员姓名")
@@ -89,8 +89,7 @@ public class ProgrammeDTO {
     @ExcelProperty(value = "参赛人员性别")
     private String participantSex;
 
-    @DateTimeFormat("yyyy-MM-dd")
-    @ExcelProperty(value = "参赛人员出生年月")
+    @ExcelProperty(value = "参赛人员出生年月", converter = LocalDateConverter.class)
     private LocalDate participantBirthday;
 
     @ExcelProperty(value = "参赛人员联系方式")
@@ -100,7 +99,7 @@ public class ProgrammeDTO {
     @ApiModelProperty(value = "证件号码")
     private String idNo;
 
-    @ApiModelProperty(value = "证书")
+    @ExcelProperty(value = "证书")
     private String certificate;
 
     public ProgrammeDTO(Programme programme) {

+ 83 - 39
src/main/java/com/izouma/wenlvju/service/performance/ProgrammeService.java

@@ -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)

+ 18 - 0
src/main/java/com/izouma/wenlvju/utils/FileUtils.java

@@ -9,6 +9,8 @@ import java.nio.file.attribute.PosixFileAttributeView;
 import java.nio.file.attribute.PosixFileAttributes;
 import java.nio.file.attribute.PosixFilePermission;
 import java.nio.file.attribute.PosixFilePermissions;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Set;
 
 public class FileUtils {
@@ -201,4 +203,20 @@ public class FileUtils {
     }
 
 
+    public static Map<String, File> findExcel(File dir) {
+        if (!(dir.exists() && dir.isDirectory())) return null;
+        for (File file : dir.listFiles()) {
+            String name = file.getName().toLowerCase();
+            if ((name.endsWith(".xlsx") || name.endsWith(".xls")) && !name.startsWith(".") && !file.isHidden()) {
+                Map<String, File> fileMap = new HashMap<>();
+                fileMap.put("file", file);
+                fileMap.put("destDir", dir);
+                return fileMap;
+            } else if (file.isDirectory() && !file.isHidden()) {
+                return findExcel(file);
+            }
+        }
+        return null;
+    }
+
 }

+ 1 - 3
src/main/java/com/izouma/wenlvju/web/performance/ProgrammeController.java

@@ -75,10 +75,8 @@ public class ProgrammeController extends BaseController {
 
     @PostMapping("/upload")
     public void uploadFile(@RequestParam("file") MultipartFile file) {
-        InputStream is;
         try {
-            is = file.getInputStream();
-            programmeService.upload(is, SecurityUtils.getAuthenticatedUser().getId());
+            programmeService.upload(file, SecurityUtils.getAuthenticatedUser().getId());
         } catch (IOException e) {
             log.error("上传失败", e);
             throw new BusinessException("上传失败", e.getMessage());

+ 1 - 0
src/main/vue/src/views/performance/ArrangeList.vue

@@ -323,6 +323,7 @@ export default {
                         });
                     });
                     this.form.performanceId = res.content[0].id;
+                    this.getData();
                     this.$http
                         .post(
                             '/performanceSchedule/all',

+ 1 - 1
src/main/vue/src/views/performance/PerformanceList.vue

@@ -43,7 +43,7 @@
                         style="font-size: 13px"
                         :underline="false"
                         type="success"
-                        v-else-if="currentTime < row.endDate"
+                        v-else-if="currentTime <= row.endDate"
                         >报名中</el-link
                     >
                 </template>

+ 5 - 3
src/main/vue/src/views/performance/ProgrammeList.vue

@@ -380,6 +380,8 @@ export default {
                             value: item.id
                         });
                     });
+                    this.performanceId = res.content[0].id;
+                    this.getData();
                 }
             })
             .catch(e => {
@@ -428,12 +430,12 @@ export default {
             if (this.form.organizationId) {
                 data.query.organizationId = this.form.organizationId;
             }
-            if (this.performanceId) {
-                data.query.performanceId = this.performanceId;
-            }
             if (this.form.specialtyId) {
                 data.query.code = this.getCode(this.form.specialtyId);
             }
+            if (this.performanceId) {
+                data.query.performanceId = this.performanceId;
+            }
             return data;
         },
         toggleMultipleMode(multipleMode) {

+ 11 - 6
src/main/vue/src/views/performance/ProgrammeOrgList.vue

@@ -128,7 +128,7 @@
                     </el-col>
                 </el-row>
                 <el-form-item>
-                    <el-button @click="getData" type="primary" icon="el-icon-search">搜索 </el-button>
+                    <el-button @click="canAdd" type="primary" icon="el-icon-search">搜索 </el-button>
                     <el-button @click="addRow" type="primary" icon="el-icon-plus" v-if="add">添加 </el-button>
                     <el-upload
                         :action="uploadUrl"
@@ -378,7 +378,7 @@ export default {
                     this.performances = res.content;
                     if (!this.$route.query.pid) {
                         this.performanceId = res.content[0].id;
-                        this.canAdd(this.performanceId);
+                        this.canAdd();
                     }
                 }
             })
@@ -504,6 +504,9 @@ export default {
             if (this.instructor) {
                 data.query.instructor = this.instructor;
             }
+            if (this.performanceId) {
+                data.query.performanceId = this.performanceId;
+            }
             this.$axios
                 .get('/programme/excel', {
                     responseType: 'blob',
@@ -613,14 +616,16 @@ export default {
                 type: 'warning'
             });
         },
-        canAdd(id) {
+        canAdd() {
             let data = this.performances.find(item => {
-                item.id == id;
-                return item;
+                return item.id == this.performanceId;
             });
-            if (data.endDate > this.currentTime) {
+            if (data.endDate >= this.currentTime) {
                 this.add = true;
+            } else {
+                this.add = false;
             }
+            this.getData();
         }
     }
 };