parent
d341d6f2fe
commit
5e37be8891
29 changed files with 557 additions and 280 deletions
@ -0,0 +1,53 @@ |
||||
package com.teaching.backend.controller.FavourInformation; |
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
||||
import com.teaching.backend.common.BaseResponse; |
||||
import com.teaching.backend.common.ErrorCode; |
||||
import com.teaching.backend.common.PageRequest; |
||||
import com.teaching.backend.common.ResultUtils; |
||||
import com.teaching.backend.exception.BusinessException; |
||||
import com.teaching.backend.model.dto.favour.resourceFavour.ResourceFavourAddRequest; |
||||
import com.teaching.backend.model.entity.CourseResources; |
||||
import com.teaching.backend.model.entity.favour.SeCourseFavour; |
||||
import com.teaching.backend.model.entity.favour.SeResourceFavour; |
||||
import com.teaching.backend.service.CourseResourcesService; |
||||
import com.teaching.backend.service.favour.SeResourceFavourService; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.util.List; |
||||
import java.util.stream.Collectors; |
||||
|
||||
/** |
||||
* ClassName: Get |
||||
* Package: com.teaching.backend.controller.FavourInformation |
||||
* Description: |
||||
* |
||||
* @Author 姜钧瀚 |
||||
* @Create 2024/6/13 18:15 |
||||
* @Version 1.0 |
||||
*/ |
||||
@RestController |
||||
public class FavourInformationController{ |
||||
|
||||
|
||||
@Resource |
||||
CourseResourcesService courseResourcesService; |
||||
|
||||
|
||||
|
||||
@GetMapping("/Favour/get") |
||||
public BaseResponse<Page<CourseResources>> getInformation2(@RequestParam List<Integer> resourceIdList , @RequestParam Integer type, PageRequest pageRequest) { |
||||
|
||||
int page=pageRequest.getCurrent(); |
||||
int pageSize=pageRequest.getPageSize(); |
||||
|
||||
Page<CourseResources> resourcesPage=courseResourcesService.getresourceRecords(type,resourceIdList,page,pageSize); |
||||
|
||||
return ResultUtils.success(resourcesPage); |
||||
|
||||
} |
||||
|
||||
} |
@ -0,0 +1,132 @@ |
||||
package com.teaching.backend.controller; |
||||
|
||||
import com.aliyun.oss.OSS; |
||||
import com.aliyun.oss.OSSClientBuilder; |
||||
import com.aliyun.oss.model.GetObjectRequest; |
||||
import com.aliyun.oss.model.OSSObject; |
||||
import com.teaching.backend.utils.WordUtil; |
||||
import org.apache.poi.openxml4j.opc.OPCPackage; |
||||
import org.apache.poi.xwpf.usermodel.XWPFDocument; |
||||
import org.apache.poi.xwpf.usermodel.XWPFParagraph; |
||||
import org.apache.poi.xwpf.usermodel.XWPFRun; |
||||
import org.apache.poi.xwpf.usermodel.XWPFTable; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.http.HttpStatus; |
||||
import org.springframework.http.MediaType; |
||||
import org.springframework.http.ResponseEntity; |
||||
import org.springframework.util.ResourceUtils; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.PostMapping; |
||||
import org.springframework.web.bind.annotation.RequestParam; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.*; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.nio.file.Files; |
||||
import java.util.*; |
||||
|
||||
@RestController |
||||
public class WordController2 { |
||||
@Value("${aliyun.oss.endpoint}") |
||||
private String endpoint; |
||||
|
||||
@Value("${aliyun.oss.accessKeyId}") |
||||
private String accessKeyId; |
||||
|
||||
@Value("${aliyun.oss.accessKeySecret}") |
||||
private String accessKeySecret; |
||||
|
||||
@Value("${aliyun.oss.bucketName}") |
||||
private String bucketName; |
||||
|
||||
@Value("${filename}") |
||||
private String templateFileName; // 模板文件名
|
||||
|
||||
@PostMapping("/Word/get") |
||||
public ResponseEntity<byte[]> createWord(@RequestParam("outputPath") String outputPath) { |
||||
String filename = "generatedWord.docx"; |
||||
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
||||
|
||||
InputStream templateStream = null; |
||||
//获取模版
|
||||
OSSObject ossObject = ossClient.getObject(new GetObjectRequest(bucketName, templateFileName)); |
||||
|
||||
templateStream = ossObject.getObjectContent(); |
||||
|
||||
WordUtil wordUtil = new WordUtil(); |
||||
wordUtil.setTemplateInputStream(templateStream); // 设置模板文件输入流
|
||||
|
||||
|
||||
Map<String, Object> dataMap = new HashMap<>(); |
||||
dataMap.put("username1", "姜钧瀚"); |
||||
|
||||
dataMap.put("img", "src/main/resources/static/3823.png"); |
||||
|
||||
|
||||
// 设置 Word 生成的输出目录
|
||||
wordUtil.setOutputDir(outputPath); |
||||
|
||||
String generatedFilePath = wordUtil.createWord(dataMap); |
||||
|
||||
if (generatedFilePath.equals("操作失败")) { |
||||
System.out.println("操作失败"); |
||||
return ResponseEntity.badRequest().body("操作失败".getBytes()); |
||||
} |
||||
|
||||
File file = new File(generatedFilePath); |
||||
byte[] fileContent; |
||||
try { |
||||
fileContent = Files.readAllBytes(file.toPath()); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
return ResponseEntity.badRequest().body("文件读取失败".getBytes()); |
||||
} |
||||
|
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); |
||||
headers.setContentDispositionFormData("attachment", filename); |
||||
|
||||
//返回文件内容
|
||||
return new ResponseEntity<>(fileContent, headers, HttpStatus.OK); |
||||
} |
||||
|
||||
@PostMapping("/Word/get2") |
||||
private void downWord(HttpServletResponse response) throws IOException { |
||||
|
||||
// 读取模板文件并创建XWPFDocument对象
|
||||
File rootFile = new File(ResourceUtils.getURL("classpath:").getPath()); |
||||
File templateFile = new File(rootFile, "/word_template/ceshi2.docx"); |
||||
|
||||
try (FileInputStream fileInputStream = new FileInputStream(templateFile)) { |
||||
XWPFDocument word = new XWPFDocument(fileInputStream); |
||||
|
||||
// 填充数据等后续处理代码
|
||||
|
||||
Map<String, String> params = new HashMap<>(); |
||||
params.put("username", "姜钧瀚"); |
||||
//处理正文
|
||||
|
||||
List<XWPFParagraph> paragraphs = word.getParagraphs(); |
||||
for (XWPFParagraph paragraph : paragraphs) { |
||||
List<XWPFRun> runs = paragraph.getRuns(); |
||||
for (XWPFRun run : runs) { |
||||
String text = run.getText(0); |
||||
for (String key : params.keySet()) { |
||||
if (text.contains(key)) { |
||||
run.setText(text.replaceAll(key, params.get(key)), 0); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
// 导出Word
|
||||
String fileName = "123123123.docx"; |
||||
response.setHeader("Content-Disposition", "attachment;filename=" + fileName); |
||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
||||
word.write(response.getOutputStream()); |
||||
} |
||||
} |
||||
} |
||||
|
@ -0,0 +1,19 @@ |
||||
package com.teaching.backend.mapper; |
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||
import com.teaching.backend.model.entity.ResourceRelationship; |
||||
import org.apache.ibatis.annotations.Mapper; |
||||
import org.apache.tomcat.websocket.BackgroundProcess; |
||||
|
||||
/** |
||||
* ClassName: ResourcesRelationshipMapper |
||||
* Package: com.teaching.backend.mapper |
||||
* Description: |
||||
* |
||||
* @Author 姜钧瀚 |
||||
* @Create 2024/6/12 21:07 |
||||
* @Version 1.0 |
||||
*/ |
||||
@Mapper |
||||
public interface ResourcesRelationshipMapper extends BaseMapper<ResourceRelationship> { |
||||
} |
@ -1,18 +0,0 @@ |
||||
package com.teaching.backend.mapper; |
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||
import com.teaching.backend.model.entity.User; |
||||
import org.apache.ibatis.annotations.Mapper; |
||||
|
||||
/** |
||||
* ClassName: UserMapper |
||||
* Package: com.teaching.backend.mapper |
||||
* Description: |
||||
* |
||||
* @Author 姜钧瀚 |
||||
* @Create 2024/6/3 17:47 |
||||
* @Version 1.0 |
||||
*/ |
||||
@Mapper |
||||
public interface UserMapper extends BaseMapper<User> { |
||||
} |
@ -0,0 +1,27 @@ |
||||
package com.teaching.backend.service; |
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
||||
import com.baomidou.mybatisplus.extension.service.IService; |
||||
import com.teaching.backend.model.entity.CourseResources; |
||||
|
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.IOException; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* ClassName: FileTableService |
||||
* Package: com.teaching.backend.service |
||||
* Description: |
||||
* |
||||
* @Author 姜钧瀚 |
||||
* @Create 2024/6/4 11:49 |
||||
* @Version 1.0 |
||||
*/ |
||||
public interface CourseResourcesService extends IService<CourseResources> { |
||||
void updateFile(Integer type, String fileName, String filePath); |
||||
|
||||
void download(String id, String path, HttpServletResponse response, String endpoint, String accessKeyId, String accessKeySecret, String bucketName) throws IOException; |
||||
|
||||
|
||||
Page<CourseResources> getresourceRecords(Integer type, List<Integer> resourceIdList, int page, int pageSize); |
||||
} |
@ -1,23 +0,0 @@ |
||||
package com.teaching.backend.service; |
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService; |
||||
import com.teaching.backend.model.entity.FileTable; |
||||
|
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.IOException; |
||||
|
||||
/** |
||||
* ClassName: FileTableService |
||||
* Package: com.teaching.backend.service |
||||
* Description: |
||||
* |
||||
* @Author 姜钧瀚 |
||||
* @Create 2024/6/4 11:49 |
||||
* @Version 1.0 |
||||
*/ |
||||
public interface FileTableService extends IService<FileTable> { |
||||
void updateFile(String courseId, String fileName, String filePath); |
||||
|
||||
|
||||
void download(String courseId, String path, HttpServletResponse response, String endpoint, String accessKeyId, String accessKeySecret, String bucketName) throws IOException; |
||||
} |
@ -0,0 +1,17 @@ |
||||
package com.teaching.backend.service; |
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService; |
||||
import com.teaching.backend.model.entity.ResourceRelationship; |
||||
|
||||
|
||||
/** |
||||
* ClassName: ResourcesRelationshipService |
||||
* Package: com.teaching.backend.service |
||||
* Description: |
||||
* |
||||
* @Author 姜钧瀚 |
||||
* @Create 2024/6/12 21:04 |
||||
* @Version 1.0 |
||||
*/ |
||||
public interface ResourcesRelationshipService extends IService<ResourceRelationship> { |
||||
} |
@ -1,16 +0,0 @@ |
||||
package com.teaching.backend.service; |
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService; |
||||
import com.teaching.backend.model.entity.User; |
||||
|
||||
/** |
||||
* ClassName: UserService |
||||
* Package: com.teaching.backend.service |
||||
* Description: |
||||
* |
||||
* @Author 姜钧瀚 |
||||
* @Create 2024/6/3 17:48 |
||||
* @Version 1.0 |
||||
*/ |
||||
public interface UserService extends IService<User> { |
||||
} |
@ -0,0 +1,121 @@ |
||||
package com.teaching.backend.service.impl; |
||||
|
||||
import com.aliyun.oss.OSS; |
||||
import com.aliyun.oss.OSSClientBuilder; |
||||
import com.aliyun.oss.model.OSSObject; |
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
|
||||
import com.teaching.backend.mapper.CourseResourcesMapper; |
||||
import com.teaching.backend.model.entity.CourseResources; |
||||
import com.teaching.backend.model.entity.ResourceRelationship; |
||||
import com.teaching.backend.service.CourseResourcesService; |
||||
import com.teaching.backend.service.ResourcesRelationshipService; |
||||
import org.apache.commons.io.FileUtils; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import javax.annotation.Resource; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* ClassName: FileTableImpl |
||||
* Package: com.teaching.backend.service.impl |
||||
* Description: |
||||
* |
||||
* @Author 姜钧瀚 |
||||
* @Create 2024/6/4 11:50 |
||||
* @Version 1.0 |
||||
*/ |
||||
@Service |
||||
public class CourseResourcesServiceImpl extends ServiceImpl<CourseResourcesMapper, CourseResources> implements CourseResourcesService { |
||||
@Resource |
||||
ResourcesRelationshipService resourcesRelationshipService; |
||||
@Override |
||||
public void updateFile(Integer type, String fileName, String filePath) { |
||||
|
||||
CourseResources courseResources = new CourseResources(); |
||||
|
||||
courseResources.setName(fileName); |
||||
courseResources.setPath(filePath); |
||||
courseResources.setType(type); |
||||
|
||||
this.save(courseResources); |
||||
|
||||
int resourceId=courseResources.getId(); |
||||
System.out.println(resourceId); |
||||
|
||||
ResourceRelationship resourcesRelationship=new ResourceRelationship(); |
||||
|
||||
resourcesRelationship.setResourceid(String.valueOf(resourceId)); |
||||
|
||||
resourcesRelationshipService.save(resourcesRelationship); |
||||
} |
||||
|
||||
@Override |
||||
public void download(String Id, String path, HttpServletResponse response, String endpoint, String accessKeyId, String accessKeySecret, String bucketName) throws IOException { |
||||
|
||||
|
||||
|
||||
CourseResources courseResources=getById(Id); |
||||
|
||||
|
||||
System.out.println(courseResources); |
||||
|
||||
|
||||
// 创建OSS客户端
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
||||
|
||||
String key = extractKeyFromFullPath(courseResources.getPath()); |
||||
OSSObject object = ossClient.getObject(bucketName, key); |
||||
|
||||
String fileName = courseResources.getName(); |
||||
|
||||
// 设置保存文件的路径
|
||||
String savePath = path + "/" + fileName; |
||||
|
||||
// 获取文件的输入流
|
||||
InputStream inputStream = object.getObjectContent(); |
||||
|
||||
// 设置响应头
|
||||
response.setContentType("application/octet-stream"); |
||||
response.setHeader("Content-Disposition", "attachment; filename=" + fileName); |
||||
|
||||
// 创建保存文件
|
||||
File saveFile = new File(savePath); |
||||
|
||||
// 将文件内容复制到保存文件中
|
||||
FileUtils.copyInputStreamToFile(inputStream, saveFile); |
||||
|
||||
// 关闭输入流和OSS客户端
|
||||
inputStream.close(); |
||||
ossClient.shutdown(); |
||||
} |
||||
|
||||
@Override |
||||
public Page<CourseResources> getresourceRecords(Integer type, List<Integer> resourceIdList, int page, int pageSize) { |
||||
Page <CourseResources> pageInfo=new Page<>(page,pageSize); |
||||
|
||||
LambdaQueryWrapper<CourseResources>lambdaQueryWrapper=new LambdaQueryWrapper<>(); |
||||
lambdaQueryWrapper.in(CourseResources::getId,resourceIdList); |
||||
lambdaQueryWrapper.eq(CourseResources::getType,type); |
||||
|
||||
return this.page(pageInfo,lambdaQueryWrapper); |
||||
|
||||
} |
||||
|
||||
|
||||
private String extractKeyFromFullPath(String filePath) { |
||||
// 提取存储桶名称
|
||||
String key = filePath.substring(filePath.indexOf("com") + 4, filePath.indexOf("?")); |
||||
return key; |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
|
@ -1,97 +0,0 @@ |
||||
package com.teaching.backend.service.impl; |
||||
|
||||
import com.aliyun.oss.OSS; |
||||
import com.aliyun.oss.OSSClientBuilder; |
||||
import com.aliyun.oss.model.OSSObject; |
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
import com.teaching.backend.mapper.FileTableMapper; |
||||
import com.teaching.backend.model.entity.FileTable; |
||||
import com.teaching.backend.service.FileTableService; |
||||
import org.apache.commons.io.FileUtils; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
|
||||
/** |
||||
* ClassName: FileTableImpl |
||||
* Package: com.teaching.backend.service.impl |
||||
* Description: |
||||
* |
||||
* @Author 姜钧瀚 |
||||
* @Create 2024/6/4 11:50 |
||||
* @Version 1.0 |
||||
*/ |
||||
@Service |
||||
public class FileTableServiceImpl extends ServiceImpl<FileTableMapper,FileTable> implements FileTableService { |
||||
@Override |
||||
public void updateFile(String courseId, String fileName, String filePath) { |
||||
System.out.println("执行了service中的方法"); |
||||
System.out.println(courseId); |
||||
System.out.println(fileName); |
||||
System.out.println(filePath); |
||||
|
||||
FileTable fileTable = new FileTable(); |
||||
|
||||
fileTable.setFileName(fileName); |
||||
fileTable.setFilePath(filePath); |
||||
// fileTable.setCourseId(courseId); // 设置主键值
|
||||
//
|
||||
// this.save(fileTable); // 使用save方法来添加新的记录
|
||||
|
||||
UpdateWrapper<FileTable> fileTableUpdateWrapper = new UpdateWrapper<>(); |
||||
fileTableUpdateWrapper.eq("course_id", courseId); |
||||
|
||||
this.update(fileTable, fileTableUpdateWrapper); |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void download(String courseId, String path, HttpServletResponse response, String endpoint, String accessKeyId, String accessKeySecret, String bucketName) throws IOException { |
||||
|
||||
|
||||
FileTable fileTable = this.getById(courseId); |
||||
System.out.println(fileTable); |
||||
|
||||
// 创建OSS客户端
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
||||
|
||||
String key = extractKeyFromFullPath(fileTable.getFilePath()); |
||||
OSSObject object = ossClient.getObject(bucketName, key); |
||||
|
||||
String fileName = fileTable.getFileName(); |
||||
|
||||
// 设置保存文件的路径
|
||||
String savePath = path + "/" + fileName; |
||||
|
||||
// 获取文件的输入流
|
||||
InputStream inputStream = object.getObjectContent(); |
||||
|
||||
// 设置响应头
|
||||
response.setContentType("application/octet-stream"); |
||||
response.setHeader("Content-Disposition", "attachment; filename=" + fileName); |
||||
|
||||
// 创建保存文件
|
||||
File saveFile = new File(savePath); |
||||
|
||||
// 将文件内容复制到保存文件中
|
||||
FileUtils.copyInputStreamToFile(inputStream, saveFile); |
||||
|
||||
// 关闭输入流和OSS客户端
|
||||
inputStream.close(); |
||||
ossClient.shutdown(); |
||||
} |
||||
|
||||
private String extractKeyFromFullPath(String filePath) { |
||||
// 提取存储桶名称
|
||||
String key = filePath.substring(filePath.indexOf("com") + 4, filePath.indexOf("?")); |
||||
return key; |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
|
@ -0,0 +1,21 @@ |
||||
package com.teaching.backend.service.impl; |
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
import com.teaching.backend.mapper.ResourcesRelationshipMapper; |
||||
import com.teaching.backend.model.entity.ResourceRelationship; |
||||
import com.teaching.backend.service.ResourcesRelationshipService; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
/** |
||||
* ClassName: ResourcesRelationshipServiceImpl |
||||
* Package: com.teaching.backend.service.impl |
||||
* Description: |
||||
* |
||||
* @Author 姜钧瀚 |
||||
* @Create 2024/6/12 21:05 |
||||
* @Version 1.0 |
||||
*/ |
||||
@Service |
||||
public class ResourcesRelationshipServiceImpl extends ServiceImpl<ResourcesRelationshipMapper, ResourceRelationship> implements ResourcesRelationshipService { |
||||
|
||||
} |
@ -1,24 +0,0 @@ |
||||
package com.teaching.backend.service.impl; |
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
import com.teaching.backend.mapper.UserMapper; |
||||
import com.teaching.backend.model.entity.User; |
||||
import com.teaching.backend.service.UserService; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
/** |
||||
* ClassName: UserServiceImpl |
||||
* Package: com.teaching.backend.service.impl |
||||
* Description: |
||||
* |
||||
* @Author 姜钧瀚 |
||||
* @Create 2024/6/3 17:48 |
||||
* @Version 1.0 |
||||
*/ |
||||
@Service |
||||
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { |
||||
|
||||
|
||||
|
||||
|
||||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,9 +1,14 @@ |
||||
package com.teaching; |
||||
|
||||
|
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
import java.util.*; |
||||
|
||||
|
||||
|
||||
@SpringBootTest |
||||
class TeachingBackendApplicationTests { |
||||
Map<String, List<String>> userLearningRecords = new HashMap<>(); |
||||
|
||||
|
||||
} |
||||
|
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue