提交 5bcc993e authored 作者: 朱旭欣's avatar 朱旭欣

第二次提交

上级 331a7d7e
package com.example.tyweekly.config;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import java.util.ArrayList;
import java.util.List;
/**
* @author dengdiyi
* 分页工具类
*/
public class PageUtil {
/**
* @param page 当前页数,默认0为第一页
* @param size 分页大小
* 获取分页后每一页的分页信息
*/
public static <T> Page<T> getPerPage(Integer page, Integer size, List<T> list, Pageable pageable) {
//若List的size小于分页大小(只有一页)
if (list.size() <= size&&page==0) {
return new PageImpl<>(list, pageable, list.size());
} else {
//计算分了几页
double d = Math.ceil(list.size() / size.doubleValue());
int value = new Double(d).intValue();
//当前页数超出了最大页数
if (page >= value) {
return new PageImpl<>(new ArrayList<>(),pageable,list.size());
} else {
//当前页的第一条记录的索引值
int index = page * size;
//当前页为最后一页
if (page == value - 1) {
return new PageImpl<>(list.subList(index, list.size()), pageable, list.size());
}
return new PageImpl<>(list.subList(index, index + size), pageable, list.size());
}
}
}
}
...@@ -35,7 +35,7 @@ public class WebMvcConfig { ...@@ -35,7 +35,7 @@ public class WebMvcConfig {
@Override @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/file/**") registry.addResourceHandler("/file/**")
.addResourceLocations("file:" + "C:\\Intel\\" +File.separator); .addResourceLocations("file:" + "/root/projects/weekly/save" +File.separator);
} }
......
...@@ -20,7 +20,6 @@ public class UserController { ...@@ -20,7 +20,6 @@ public class UserController {
@Autowired @Autowired
private WeeklyUserService weeklyUserService; private WeeklyUserService weeklyUserService;
@PostMapping("/saveUser") @PostMapping("/saveUser")
@ApiOperation(value = "新增用户") @ApiOperation(value = "新增用户")
public WeeklyUser saveOne(@RequestBody WeeklyUser weeklyUser){ public WeeklyUser saveOne(@RequestBody WeeklyUser weeklyUser){
......
package com.example.tyweekly.controller; package com.example.tyweekly.controller;
import com.example.tyweekly.entity.PagePojo;
import com.example.tyweekly.entity.ReturnPojo; import com.example.tyweekly.entity.ReturnPojo;
import com.example.tyweekly.entity.WeeklyCondition; import com.example.tyweekly.entity.WeeklyCondition;
import com.example.tyweekly.entity.WeeklyFile;
import com.example.tyweekly.service.WeeklyFileService; import com.example.tyweekly.service.WeeklyFileService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -47,8 +46,8 @@ public class WeeklyController { ...@@ -47,8 +46,8 @@ public class WeeklyController {
@PostMapping("/findAllWeekly") @PostMapping("/findAllWeekly")
@ApiOperation(value = "周报情况动态查询") @ApiOperation(value = "周报情况动态查询")
public PagePojo findAllWeekly(@RequestBody WeeklyCondition weeklyCondition){ public ResponseEntity findAllWeekly(@RequestBody WeeklyCondition weeklyCondition){
return weeklyFileService.findAllWeekly(weeklyCondition); return ResponseEntity.ok(weeklyFileService.findAllWeekly(weeklyCondition));
} }
...@@ -64,17 +63,4 @@ public class WeeklyController { ...@@ -64,17 +63,4 @@ public class WeeklyController {
} }
...@@ -3,9 +3,17 @@ package com.example.tyweekly.dao; ...@@ -3,9 +3,17 @@ package com.example.tyweekly.dao;
import com.example.tyweekly.entity.WeeklyFile; import com.example.tyweekly.entity.WeeklyFile;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import java.util.List;
/** /**
* @author tykj * @author tykj
*/ */
public interface WeeklyFileDao extends JpaRepository<WeeklyFile,Integer>, JpaSpecificationExecutor<WeeklyFile> { public interface WeeklyFileDao extends JpaRepository<WeeklyFile,Integer>, JpaSpecificationExecutor<WeeklyFile> {
/**
* 根据文件名进行模糊查询
* @param name 文件名
* @return 实体的集合
*/
List<WeeklyFile> findAllByWeeklyNameLike(String name);
} }
...@@ -2,10 +2,11 @@ package com.example.tyweekly.dao; ...@@ -2,10 +2,11 @@ package com.example.tyweekly.dao;
import com.example.tyweekly.entity.WeeklyUser; import com.example.tyweekly.entity.WeeklyUser;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
/** /**
* @author tykj * @author tykj
*/ */
public interface WeeklyUserDao extends JpaRepository<WeeklyUser,Integer> { public interface WeeklyUserDao extends JpaRepository<WeeklyUser,Integer> , JpaSpecificationExecutor<WeeklyUser> {
} }
...@@ -24,6 +24,9 @@ public class WeeklyCondition { ...@@ -24,6 +24,9 @@ public class WeeklyCondition {
@ApiModelProperty(value = "请求数量",example = "1") @ApiModelProperty(value = "请求数量",example = "1")
private Integer pageSize; private Integer pageSize;
@ApiModelProperty(value = "状态",example = "ture") @ApiModelProperty(value = "状态",example ="{未上交0 已提交1 全部2}")
private Boolean state; @NotNull(message = "状态不能为空")
private Integer status;
} }
...@@ -40,7 +40,4 @@ public class WeeklyFile { ...@@ -40,7 +40,4 @@ public class WeeklyFile {
@ApiModelProperty(value = "文件保存路径",example = "文件地址") @ApiModelProperty(value = "文件保存路径",example = "文件地址")
private String uploadAddress; private String uploadAddress;
@ApiModelProperty(value = "状态",example = "ture")
private Boolean state;
} }
...@@ -6,10 +6,8 @@ import lombok.AllArgsConstructor; ...@@ -6,10 +6,8 @@ import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import javax.persistence.Entity; import javax.persistence.*;
import javax.persistence.GeneratedValue; import java.util.List;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/** /**
* @author tykj * @author tykj
...@@ -31,4 +29,9 @@ public class WeeklyUser { ...@@ -31,4 +29,9 @@ public class WeeklyUser {
private String userName; private String userName;
@ApiModelProperty(value = "文件",example = "小明")
@Transient
private List<WeeklyFile> files;
} }
package com.example.tyweekly.service; package com.example.tyweekly.service;
import com.example.tyweekly.entity.PagePojo; import com.example.tyweekly.entity.*;
import com.example.tyweekly.entity.ReturnPojo; import org.springframework.data.domain.Page;
import com.example.tyweekly.entity.WeeklyCondition;
import com.example.tyweekly.entity.WeeklyFile;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
...@@ -35,7 +33,7 @@ public interface WeeklyFileService { ...@@ -35,7 +33,7 @@ public interface WeeklyFileService {
* @param weeklyCondition * @param weeklyCondition
* @return * @return
*/ */
PagePojo findAllWeekly(WeeklyCondition weeklyCondition); Page<WeeklyUser> findAllWeekly(WeeklyCondition weeklyCondition);
/** /**
......
package com.example.tyweekly.service.impl; package com.example.tyweekly.service.impl;
import com.example.tyweekly.config.PageUtil;
import com.example.tyweekly.config.TimedTask; import com.example.tyweekly.config.TimedTask;
import com.example.tyweekly.dao.WeeklyFileDao; import com.example.tyweekly.dao.WeeklyFileDao;
import com.example.tyweekly.dao.WeeklyUserDao; import com.example.tyweekly.dao.WeeklyUserDao;
...@@ -7,8 +8,10 @@ import com.example.tyweekly.entity.*; ...@@ -7,8 +8,10 @@ import com.example.tyweekly.entity.*;
import com.example.tyweekly.service.WeeklyFileService; import com.example.tyweekly.service.WeeklyFileService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -23,6 +26,8 @@ import java.io.FileOutputStream; ...@@ -23,6 +26,8 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.*; import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/** /**
* @author tykj * @author tykj
...@@ -37,6 +42,12 @@ public class WeeklyFileServiceImpl implements WeeklyFileService { ...@@ -37,6 +42,12 @@ public class WeeklyFileServiceImpl implements WeeklyFileService {
@Autowired @Autowired
private WeeklyUserDao weeklyUserDao; private WeeklyUserDao weeklyUserDao;
@Value("${file.path}")
private String download;
@Value("${file.path1}")
private String save;
@Override @Override
public WeeklyFile uploadDatum(MultipartFile file) throws IOException { public WeeklyFile uploadDatum(MultipartFile file) throws IOException {
//MultipartFile 接收前端传过来的文件 //MultipartFile 接收前端传过来的文件
...@@ -51,7 +62,7 @@ public class WeeklyFileServiceImpl implements WeeklyFileService { ...@@ -51,7 +62,7 @@ public class WeeklyFileServiceImpl implements WeeklyFileService {
} }
} }
//指定上传路径 //指定上传路径
String path="C:\\Intel\\周报文件夹\\"; String path=save;
//拼接成为新文件的路径 //拼接成为新文件的路径
String filePath=path+oldName; String filePath=path+oldName;
//创建新文件对象 指定文件路径为拼接好的路径 //创建新文件对象 指定文件路径为拼接好的路径
...@@ -63,7 +74,7 @@ public class WeeklyFileServiceImpl implements WeeklyFileService { ...@@ -63,7 +74,7 @@ public class WeeklyFileServiceImpl implements WeeklyFileService {
weeklyFile1.setUploadAddress(filePath); weeklyFile1.setUploadAddress(filePath);
weeklyFile1.setWeeklyName(oldName); weeklyFile1.setWeeklyName(oldName);
weeklyFile1.setUploadTime(LocalDateTime.now()); weeklyFile1.setUploadTime(LocalDateTime.now());
weeklyFile1.setState(false);
return weeklyFileDao.save(weeklyFile1); return weeklyFileDao.save(weeklyFile1);
} }
...@@ -73,49 +84,53 @@ public class WeeklyFileServiceImpl implements WeeklyFileService { ...@@ -73,49 +84,53 @@ public class WeeklyFileServiceImpl implements WeeklyFileService {
} }
@Override @Override
public PagePojo findAllWeekly(WeeklyCondition weeklyCondition) { public Page<WeeklyUser> findAllWeekly(WeeklyCondition weeklyCondition) {
//根据传入的数据,进行分页数据查询 //根据传入的数据,进行分页数据查询
PageRequest of1 = PageRequest.of(weeklyCondition.getPage()-1, weeklyCondition.getPageSize()); Pageable of1 = PageRequest.of(weeklyCondition.getPage(), weeklyCondition.getPageSize());
Map<String, WeeklyUser> userMap = weeklyUserDao.findAll().stream().collect(Collectors.toMap(WeeklyUser::getUserName, Function.identity()));
List<String> userNames = new ArrayList<>(userMap.keySet());
//动态查询构造器 //动态查询构造器
Specification<WeeklyFile> queryCondition = new Specification<WeeklyFile>(){ List<WeeklyFile> weeklyFileList = weeklyFileDao.findAll();
@Override List<WeeklyUser> userList = new ArrayList<>();
public Predicate toPredicate(Root<WeeklyFile> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) { int a = 0;
List<Predicate> predicateList = new ArrayList<>(); //查询已上交
if (weeklyCondition.getState()!=null){ userNames.forEach(s -> {
predicateList.add(criteriaBuilder.equal(root.get("state"),weeklyCondition.getState())); WeeklyUser weeklyUser = userMap.get(s);
List<WeeklyFile> weeklyFiles = new ArrayList<>();
weeklyFileList.forEach(weeklyFile -> {
if (weeklyFile.getWeeklyName().contains(s)){
//如果存在
weeklyFiles.add(weeklyFile);
} }
return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()])); });
weeklyUser.setFiles(weeklyFiles);
userList.add(weeklyUser);
});
//做处理
//1 上交的
Integer status = weeklyCondition.getStatus();
List<WeeklyUser> userList1 = new ArrayList<>();
switch(status){
case 0 :
//未上交
userList1 = userList.stream().filter(weeklyUser -> weeklyUser.getFiles().size() == 0).collect(Collectors.toList());
break; //可选
case 1 :
//提交
userList1 = userList.stream().filter(weeklyUser -> weeklyUser.getFiles().size() != 0).collect(Collectors.toList());
break; //可选
//你可以有任意数量的case语句
case 2 :
//提交
userList1 = userList;
break; //可选
default : //可选
userList1 = userList;
//userList
} }
}; //做物理分页
Page<WeeklyFile> all = weeklyFileDao.findAll(queryCondition, of1); Page<WeeklyUser> perPage = PageUtil.getPerPage(of1.getPageNumber()-1, of1.getPageSize(), userList1, of1);
List<WeeklyFile> content = all.getContent(); return perPage;
List<WeeklyFileVO> weeklyFileVOList = new ArrayList<>();
List<WeeklyUser> userList = weeklyUserDao.findAll();
for (WeeklyUser weeklyUser : userList) {
String userName = weeklyUser.getUserName();
WeeklyFileVO weeklyFileVO =new WeeklyFileVO();
weeklyFileVO.setUserName(userName);
weeklyFileVO.setState(false);
for (WeeklyFile weeklyFile : content) {
if (weeklyFile.getWeeklyName().contains(userName)){
weeklyFileVO.setState(true);
WeeklyFile weeklyFile1 = weeklyFileDao.findById(weeklyFile.getId()).get();
weeklyFile1.setState(true);
weeklyFileDao.save(weeklyFile1);
weeklyFileVO.setUploadTime(weeklyFile.getUploadTime());
weeklyFileVO.setWeeklyName(weeklyFile.getWeeklyName());
}
}
weeklyFileVOList.add(weeklyFileVO);
}
PagePojo pagePojo =new PagePojo();
pagePojo.setPage(weeklyCondition.getPage());
pagePojo.setPageSize(weeklyCondition.getPageSize());
pagePojo.setTotal(all.getTotalElements());
pagePojo.setWeeklyFileVOList(weeklyFileVOList);
return pagePojo;
} }
@Override @Override
...@@ -125,7 +140,7 @@ public class WeeklyFileServiceImpl implements WeeklyFileService { ...@@ -125,7 +140,7 @@ public class WeeklyFileServiceImpl implements WeeklyFileService {
for (WeeklyFile weeklyFile : all) { for (WeeklyFile weeklyFile : all) {
fileList.add(new File(weeklyFile.getUploadAddress())); fileList.add(new File(weeklyFile.getUploadAddress()));
} }
FileOutputStream fos2 = new FileOutputStream(new File("C:\\Intel\\周报文件夹\\ty-weekly.zip")); FileOutputStream fos2 = new FileOutputStream(new File(download));
TimedTask.toZip(fileList,fos2); TimedTask.toZip(fileList,fos2);
} }
......
server.port=8088 server.port=8096
# mysql jdbc # mysql jdbc
spring.datasource.url=jdbc:mysql://localhost:3306/user?serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false spring.datasource.url=jdbc:mysql://192.168.100.249:3306/weekly?serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root spring.datasource.username=root
spring.datasource.password=123456 spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.format_sql=true
file.path=/root/projects/weekly/download
file.path1=/root/projects/weekly/save
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论