Procházet zdrojové kódy

优化获取首页信息接口

郭超 před 4 roky
rodič
revize
71ebac6375

+ 6 - 2
core/src/main/java/com/mooctest/crowd/domain/dao/CompetitionDao.java

@@ -2,6 +2,7 @@ package com.mooctest.crowd.domain.dao;
 
 import com.mooctest.crowd.domain.model.CompetitionPO;
 import org.springframework.data.domain.Pageable;
+import org.springframework.data.jpa.repository.Query;
 import org.springframework.data.repository.CrudRepository;
 
 import javax.transaction.Transactional;
@@ -13,10 +14,13 @@ public interface CompetitionDao extends CrudRepository<CompetitionPO, Long>{
 
     Optional<CompetitionPO> findByIdAndIsDeleted(Long id, int isNotDeleted);
 
-    List<CompetitionPO> findAll();
-
     List<CompetitionPO> findAll(Pageable pageable);
 
+    @Query(value = "SELECT * FROM competition WHERE C_IS_DELETED = 0 order by C_START_TIME desc LIMIT ?1", nativeQuery = true)
+    List<CompetitionPO> findAllByCount(int indexCompetitionCount);
+
+    @Query(value = "SELECT * FROM competition WHERE C_IS_DELETED = 0 order by C_START_TIME desc", nativeQuery = true)
+    List<CompetitionPO> findAll();
 
 //    @Query(value = "SELECT new com.mooctest.crowd.domain.domainobject.Competition(c.id,c.name,c.linkUrl,c.startTime,c.isDeleted) FROM CompetitionPO c where c.isDeleted=0 ORDER BY c.startTime DESC")
 //    List<Competition> findTop3(Pageable pageable);

+ 4 - 0
core/src/main/java/com/mooctest/crowd/domain/dao/ResidentAgencyDao.java

@@ -1,6 +1,7 @@
 package com.mooctest.crowd.domain.dao;
 
 import com.mooctest.crowd.domain.model.ResidentAgencyPO;
+import org.springframework.data.jpa.repository.Query;
 import org.springframework.data.repository.CrudRepository;
 
 import javax.transaction.Transactional;
@@ -15,4 +16,7 @@ public interface ResidentAgencyDao extends CrudRepository<ResidentAgencyPO, Long
     Optional<ResidentAgencyPO> findById(Long id);
 
     List<ResidentAgencyPO> findAll();
+
+    @Query(value = "SELECT * FROM resident_agency limit ?1", nativeQuery = true)
+	List<ResidentAgencyPO> findAllByCount(int showCount);
 }

+ 16 - 8
core/src/main/java/com/mooctest/crowd/domain/repository/CommonRepo.java

@@ -82,12 +82,14 @@ public class CommonRepo {
         return resourceDao.findAll().stream().map(resourcePO -> Converter.convert(Resource.class, resourcePO)).collect(Collectors.toList());
     }
 
-    public List<Competition> getAllCompetition() {
-        return competitionDao.findAll().stream()
-                .map(competitionPO -> Converter.convert(Competition.class, competitionPO))
-                .filter(competition -> competition.getIsDeleted() == DeletedStatus.isNotDeleted)
-                .sorted(Comparator.comparing(Competition::getStartTime).reversed())
-                .collect(Collectors.toList());
+    public List<Competition> getAllCompetition(int indexCompetitionCount) {
+        List<CompetitionPO> competitionPOS;
+        if(indexCompetitionCount != 0){
+            competitionPOS = competitionDao.findAllByCount(indexCompetitionCount);
+        }else{
+            competitionPOS = competitionDao.findAll();
+        }
+        return competitionPOS.stream().map(competitionPO -> Converter.convert(Competition.class, competitionPO)).collect(Collectors.toList());
     }
 
     public Page<Competition> findAllCompetition(Pageable pageable, String keyword) {
@@ -120,8 +122,14 @@ public class CommonRepo {
         return partnerDao.findAll().stream().map(partnerPO -> Converter.convert(Partner.class, partnerPO)).collect(Collectors.toList());
     }
 
-    public List<ResidentAgency> getAllResidentAgency() {
-        return residentAgencyDao.findAll().stream().map(residentAgencyPO -> Converter.convert(ResidentAgency.class, residentAgencyPO)).collect(Collectors.toList());
+    public List<ResidentAgency> getAllResidentAgency(int showCount) {
+        List<ResidentAgencyPO> agencyPOS;
+        if(showCount != 0){
+            agencyPOS = residentAgencyDao.findAllByCount(showCount);
+        }else{
+            agencyPOS = residentAgencyDao.findAll();
+        }
+        return agencyPOS.stream().map(residentAgencyPO -> Converter.convert(ResidentAgency.class, residentAgencyPO)).collect(Collectors.toList());
     }
 
     public ApplicationType getApplicationTypeByAppCode(String appCode) {

+ 1 - 0
site/src/main/java/com/mooctest/crowd/site/data/dto/IndexInfoDTO.java

@@ -24,4 +24,5 @@ public class IndexInfoDTO implements Serializable {
     private List<EvaluationAgencyVO> residentAgencyList;
     private List<PartnerVO> partnerList;
     private List<CrowdTaskVO> taskList;
+    private List<CrowdTestProjectVO> projectList;
 }

+ 55 - 162
site/src/main/java/com/mooctest/crowd/site/mediator/impl/WebMediatorImpl.java

@@ -19,6 +19,7 @@ import com.mooctest.crowd.site.data.dto.*;
 import com.mooctest.crowd.site.data.enums.RoleType;
 import com.mooctest.crowd.site.data.vo.*;
 import com.mooctest.crowd.site.mediator.ViewMediator;
+import com.mooctest.crowd.site.service.CrowdTaskService;
 import com.mooctest.crowd.site.service.ThemeStatusService;
 import com.mooctest.crowd.site.service.ThemeStatusTaskService;
 import com.mooctest.crowd.site.service.impl.AuthCheckServiceImpl;
@@ -47,6 +48,9 @@ public class WebMediatorImpl implements ViewMediator {
     private TestTypeDao testTypeDao;
 
     @Autowired
+    private CrowdTaskService crowdTaskService;
+
+    @Autowired
     private CrowdTestProjectRepo projectRepo;
 
     @Autowired
@@ -139,6 +143,33 @@ public class WebMediatorImpl implements ViewMediator {
     @Value("${feature.client.oss}")
     private Boolean usingOss;
 
+    @Value("${index.count.project}")
+    private String indexProjectCount;
+
+    @Value("${index.count.task}")
+    private String indexTaskCount;
+
+    @Value("${index.count.residentAgency}")
+    private String indexResidentAgencyCount;
+
+    @Value("${index.count.competition}")
+    private String indexCompetitionCount;
+
+    @Value("${index.count.crowd}")
+    private String indexCrowdCount;
+
+    @Value("${index.count.agency}")
+    private String indexAgencyCount;
+
+    @Value("${index.count.user}")
+    private String indexUserCount;
+
+    @Value("${index.count.tool}")
+    private String indexToolCount;
+
+    @Value("${index.count.resource}")
+    private String indexResourceCount;
+
     @Override
     public List<CrowdTestProjectVO> crowdTestProjects() {
         List<CrowdTestProjectVO> resultList = new ArrayList<>();
@@ -302,133 +333,51 @@ public class WebMediatorImpl implements ViewMediator {
     @Override
     public IndexInfoDTO renderIndexInfosCache() {
         IndexInfoDTO indexInfoDTO = new IndexInfoDTO();
-        int top = 3;
         // 获取大赛信息
-        List<Competition> competitionList = commonRepo.getAllCompetition();
-        List<Competition> competitionLists = new ArrayList<>();
-        if (competitionList.size() > top) {
-            for (int i = 0; i < top; i++) {
-                competitionLists.add(competitionList.get(i));
-            }
-        } else {
-            competitionLists = competitionList;
-        }
-
-        List<CompetitionVO> competitionVOS = competitionLists.stream().map(competition -> {
-            CompetitionVO competitionVO = new CompetitionVO(competition);
-            return competitionVO;
-        }).sorted(Comparator.comparing(CompetitionVO::getStartTime).reversed()).collect(Collectors.toList());
-
+        List<CompetitionVO> competitionVOS = commonRepo.getAllCompetition(Integer.parseInt(indexCompetitionCount)).stream().map(CompetitionVO::new).collect(Collectors.toList());
+        // 获取领域划分
+        List<FieldVO> fieldVOS = commonRepo.getAllFieldType().stream().map(FieldVO::new).collect(Collectors.toList());
         // 获取服务类型
-        List<TestType> allTestType = commonRepo.getAllTestType();
-        List<TestTypeVO> testTypeVOS = allTestType.stream().map(testType -> {
-            TestTypeVO testTypeVO = new TestTypeVO(testType);
-            return testTypeVO;
-        }).collect(Collectors.toList());
-
+        List<TestTypeVO> testTypeVOS = commonRepo.getAllTestType().stream().map(TestTypeVO::new).collect(Collectors.toList());
         // 获取应用类型
         List<ApplicationTypeVO> applicationTypeVOS = commonRepo.getAllApplicationType().stream().map(ApplicationTypeVO::new).collect(Collectors.toList());
-//        List<ApplicationType> allApplicationType = commonRepo.getAllApplicationType();
-//        List<ApplicationTypeVO> applicationTypeVOS = allApplicationType.stream().map(applicationType -> {
-//            ApplicationTypeVO applicationTypeVO = new ApplicationTypeVO(applicationType);
-//            List<TestType> testTypeList = commonRepo.getTestTypeListByAppCode(applicationType.getCode());
-//            List<TestTypeVO> typeVOS = testTypeList.stream().map(testType -> {
-//                TestTypeVO testTypeVO = new TestTypeVO(testType);
-//                return testTypeVO;
-//            }).collect(Collectors.toList());
-//            applicationTypeVO.setTestTypeList(typeVOS);
-//            return applicationTypeVO;
-//        }).collect(Collectors.toList());
-
-        // 获取领域划分
-        List<Field> fieldList = commonRepo.getAllFieldType();
-        List<FieldVO> fieldVOS = fieldList.stream().map(field -> {
-            FieldVO fieldVO = new FieldVO(field);
-            return fieldVO;
-        }).collect(Collectors.toList());
-
         // 获取入驻品牌机构
-        List<ResidentAgency> allResidentAgency = commonRepo.getAllResidentAgency();
-        List<EvaluationAgencyVO> agencyVOS = new ArrayList<>();
-        List<EvaluationAgencyVO> agencyVOList = allResidentAgency.stream().map(residentAgency -> {
+        List<ResidentAgency> allResidentAgency = commonRepo.getAllResidentAgency(Integer.parseInt(indexResidentAgencyCount));
+        // 获取资源
+//        List<ResourceVO> resourceVOS = commonRepo.getAllResource().stream().map(ResourceVO::new).limit(Integer.parseInt(indexResourceCount)).collect(Collectors.toList());
+
+        // 获取工具
+        List<ToolVO> toolVOS = toolRepo.getAllTools().stream().map(tool -> new ToolVO(tool)).limit(Integer.parseInt(indexToolCount)).collect(Collectors.toList());
+        List<EvaluationAgencyVO> agencyVOS = allResidentAgency.stream().map(residentAgency -> {
             EvaluationAgency agency = evaluationAgencyRepo.findAgencyById(residentAgency.getAgencyId());
             EvaluationAgencyVO evalutionAgencyVO = new EvaluationAgencyVO(agency);
             return evalutionAgencyVO;
         }).collect(Collectors.toList());
-        if (agencyVOList.size() > 12) {
-            for (int i = 0; i < 12; i++) {
-                agencyVOS.add(agencyVOList.get(i));
-            }
-        } else {
-            agencyVOS = agencyVOList;
-        }
-
-        // 获取热门任务
-        List<CrowdTaskVO> taskVOList = taskDao.findMoreHotTasks().stream().map(crowdTestTaskPO -> {
-            CrowdTestTask task = new CrowdTestTask();
-            BeanUtils.copyProperties(crowdTestTaskPO, task);
-            return new CrowdTaskVO(task);
-        }).collect(Collectors.toList());
-
-        List<CrowdTaskVO> taskVOS = new ArrayList<>();
-        if (taskVOList.size() > top) {
-            for (int i = 0; i < top; i++) {
-                taskVOS.add(taskVOList.get(i));
-            }
-        } else {
-            taskVOS = taskVOList;
-        }
-
-        // 获取合作机构
-//        List<Partner> partnerList = commonRepo.getAllPartner();
-//        List<PartnerVO> partnerVOS = partnerList.stream().map(partner -> {
-//            PartnerVO partnerVO = new PartnerVO(partner);
-//            return partnerVO;
-//        }).collect(Collectors.toList());
 
         indexInfoDTO.setTestTypeList(testTypeVOS);
         indexInfoDTO.setApplicationTypeList(applicationTypeVOS);
         indexInfoDTO.setFieldList(fieldVOS);
         indexInfoDTO.setCompetitionList(competitionVOS);
         indexInfoDTO.setResidentAgencyList(agencyVOS);
-//        indexInfoDTO.setPartnerList(partnerVOS);
-        indexInfoDTO.setTaskList(taskVOS);
+//        indexInfoDTO.setResourceList(resourceVOS);
+        indexInfoDTO.setToolList(toolVOS);
         return indexInfoDTO;
     }
 
     @Override
     public IndexInfoDTO renderIndexInfos() {
         IndexInfoDTO indexInfoDTO = new IndexInfoDTO();
-        Pageable pageable = PageRequest.of(0, 3);
-        int top = 3;
-        List<ApplicationTypeVO> applicationTypeRank = getApplicationTypeRankVOS(pageable);
-        List<ApplicationTypeVO> applicationTypeRanks = new ArrayList<>();
-        if (applicationTypeRanks.size() > top) {
-            for (int i = 0; i < top; i++) {
-                applicationTypeRanks.add(applicationTypeRank.get(i));
-            }
-        } else {
-            applicationTypeRanks = applicationTypeRank;
-        }
+        Pageable pageable = PageRequest.of(0, Integer.parseInt(indexCrowdCount));
+        // 获取热门项目
+        List<CrowdTestProjectVO> projectVOS = projectRepo.findHotProjectCardLimitCount(Integer.parseInt(indexProjectCount)).stream().map(CrowdTestProjectVO::new).collect(Collectors.toList());
+        // 获取热门任务
+        List<CrowdTaskVO> taskVOS = crowdTaskService.findMoreHotTasksList("", Integer.parseInt(indexTaskCount));
+        // 获取热门众测
+        List<ApplicationTypeVO> applicationTypeRanks = getApplicationTypeRankVOS(pageable).stream().limit(Integer.parseInt(indexCrowdCount)).collect(Collectors.toList());
 
         //获取机构排名
-//        String agencyName = agencyDao.findById(Long.parseLong(agencyId)).get().getEvaluationAgencyName();
-//        List<EvaluationAgencyVO> agencyRank = taskToUserDao.findTotalCountOfUser().stream().map(rankInfo -> {
-//            EvaluationAgencyPO agency = agencyDao.findByUserId(rankInfo.getEntityId());
-//            if (agency != null && !agency.getEvaluationAgencyName().equals(agencyName)) {
-//                EvaluationAgencyVO agencyVO = new EvaluationAgencyVO();
-//                agencyVO.setEvaluationAgencyName(agency.getEvaluationAgencyName());
-//                agencyVO.setAgencyPhoto(agency.getAgencyPhoto());
-//                agencyVO.setTaskCount(rankInfo.getCount());
-//                agencyVO.setId(agency.getId());
-//                agencyVO.setUserId(agency.getUserId());
-//                return agencyVO;
-//            }
-//            return null;
-//        }).filter(Objects::nonNull).collect(Collectors.toList());
-
         List<EvaluationAgencyVO> agencyRanks = userTaskCountDao.findByType(RoleType.AGENCY.getId())
-                .stream().sorted(Comparator.comparing(UserTaskCountPO::getCount).reversed()).limit(top).collect(Collectors.toList())
+                .stream().sorted(Comparator.comparing(UserTaskCountPO::getCount).reversed()).limit(Integer.parseInt(indexAgencyCount)).collect(Collectors.toList())
                 .stream().map(userTaskCountPO -> {
                     EvaluationAgencyPO agency = agencyDao.findByUserId(userTaskCountPO.getUserId());
                     EvaluationAgencyVO agencyVO = new EvaluationAgencyVO();
@@ -440,29 +389,9 @@ public class WebMediatorImpl implements ViewMediator {
                     return agencyVO;
                 }).collect(Collectors.toList());
 
-//        List<EvaluationAgencyVO> agencyRanks = new ArrayList<>();
-//        if (agencyRank.size() > top) {
-//            for (int i = 0; i < top; i++) {
-//                agencyRanks.add(agencyRank.get(i));
-//            }
-//        } else {
-//            agencyRanks = agencyRank;
-//        }
-
         //获取众测人员排名
-//        List<UserVO> userRank = taskToUserDao.findTotalCountOfUser().stream().map(rankInfo -> {
-//            EvaluationAgencyPO agency = agencyDao.findByUserId(rankInfo.getEntityId());
-//            if (agency != null && agency.getEvaluationAgencyName().equals(agencyName)) {
-//                User user = userRepo.getByID(agency.getUserId());
-//                UserVO userVO = new UserVO(user);
-//                userVO.setTaskCount(rankInfo.getCount());
-//                return userVO;
-//            }
-//            return null;
-//        }).filter(Objects::nonNull).collect(Collectors.toList());
-
         List<UserVO> userRanks = userTaskCountDao.findByType(RoleType.EVALUATION_USER.getId())
-                .stream().sorted(Comparator.comparing(UserTaskCountPO::getCount).reversed()).limit(top).collect(Collectors.toList())
+                .stream().sorted(Comparator.comparing(UserTaskCountPO::getCount).reversed()).limit(Integer.parseInt(indexUserCount)).collect(Collectors.toList())
                 .stream().map(userTaskCountPO -> {
                     User user = userRepo.getByIDJustInfo(userTaskCountPO.getUserId());
                     UserVO userVO = new UserVO(user);
@@ -470,47 +399,11 @@ public class WebMediatorImpl implements ViewMediator {
                     return userVO;
                 }).collect(Collectors.toList());
 
-//        List<UserVO> userRanks = new ArrayList<>();
-//        if (userRank.size() > top) {
-//            for (int i = 0; i < top; i++) {
-//                userRanks.add(userRank.get(i));
-//            }
-//        } else {
-//            userRanks = userRank;
-//        }
-
-        // 获取资源
-        List<Resource> resourceList = commonRepo.getAllResource();
-        List<ResourceVO> resourceVOS = new ArrayList<>();
-        List<ResourceVO> resourceVOList = resourceList.stream().map(resource -> {
-            ResourceVO resourceVO = new ResourceVO(resource);
-            return resourceVO;
-        }).collect(Collectors.toList());
-        if (resourceVOList.size() > 12) {
-            for (int i = 0; i < 12; i++) {
-                resourceVOS.add(resourceVOList.get(i));
-            }
-        } else {
-            resourceVOS = resourceVOList;
-        }
-
-        // 获取工具
-        List<Tool> toolList = toolRepo.getAllTools();
-        List<ToolVO> toolVOS = new ArrayList<>();
-        List<ToolVO> toolVOList = toolList.stream().map(tool -> new ToolVO(tool)).collect(Collectors.toList());
-        if (toolVOList.size() > 12) {
-            for (int i = 0; i < 12; i++) {
-                toolVOS.add(toolVOList.get(i));
-            }
-        } else {
-            toolVOS = toolVOList;
-        }
-
+        indexInfoDTO.setTaskList(taskVOS);
+        indexInfoDTO.setProjectList(projectVOS);
         indexInfoDTO.setApplicationTypeRank(applicationTypeRanks);
         indexInfoDTO.setAgencyRank(agencyRanks);
         indexInfoDTO.setUserRank(userRanks);
-        indexInfoDTO.setResourceList(resourceVOS);
-        indexInfoDTO.setToolList(toolVOS);
         return indexInfoDTO;
     }
 

+ 9 - 10
site/src/main/java/com/mooctest/crowd/site/service/impl/OSSUploadServiceImpl.java

@@ -1,8 +1,6 @@
 package com.mooctest.crowd.site.service.impl;
 
 import com.mooctest.crowd.site.constants.UploadType;
-import com.mooctest.crowd.site.data.dto.FileUploadDTO;
-import com.mooctest.crowd.site.data.dto.FileUploadRequestDTO;
 import com.mooctest.crowd.site.service.FileService;
 import com.mooctest.crowd.site.service.UploadService;
 import lombok.extern.slf4j.Slf4j;
@@ -88,14 +86,15 @@ public class OSSUploadServiceImpl implements UploadService {
     }
 
     private String doUpload(String fileName, MultipartFile file, int uploadType){
-//        if(uploadType == UploadType.SIMPLE_UPLOAD){
+        if(uploadType == UploadType.SIMPLE_UPLOAD){
 //            return fileService.uploadFile(fileName, file);
-//        }else{
-//            System.out.println("分片上传");
-//            return fileService.sliceUploadFile(fileName, file);
-//        }
-        FileUploadRequestDTO fileUploadRequestDTO = new FileUploadRequestDTO(file);
-        FileUploadDTO upload = fileService.sliceUpload(fileUploadRequestDTO);
-        return "";
+            return fileService.sliceUploadFile(fileName, file);
+        }else{
+            System.out.println("分片上传");
+            return fileService.sliceUploadFile(fileName, file);
+        }
+//        FileUploadRequestDTO fileUploadRequestDTO = new FileUploadRequestDTO(file);
+//        FileUploadDTO upload = fileService.sliceUpload(fileUploadRequestDTO);
+//        return "";
     }
 }

+ 22 - 9
site/src/main/resources/application.yml

@@ -42,6 +42,19 @@ green:
   agency:
     id: 99231
 
+index:
+  count:
+    project: 3
+    task: 3
+    competition: 3
+    crowd: 3
+    agency: 3
+    user: 3
+    tool: 12
+    resource: 12
+    residentAgency: 12
+
+
 square:
   index:
     count: 6
@@ -140,20 +153,20 @@ user:
   service:
     baseUrl: http://59.42.10.53:8081
 
-#file:
-#  save:
-#    path: /var/www/
-
 file:
   save:
-    path: /Users/guochao/Desktop/project/data/cofortest/
+    path: /var/www/
+feature:
+  client:
+    oss: true
+
+#file:
+#  save:
+#    path: /Users/guochao/Desktop/project/data/cofortest/
 
 #feature:
 #  client:
-#    oss: true
-feature:
-  client:
-    oss: false
+#    oss: false
 
 website:
   domain: mooctest.net