|
@@ -0,0 +1,137 @@
|
|
|
+package com.benyun.service.impl;
|
|
|
+
|
|
|
+
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import com.benyun.boot.core.vo.Result;
|
|
|
+import com.benyun.dao.EventAppendixDao;
|
|
|
+import com.benyun.entity.EventAppendix;
|
|
|
+import com.benyun.service.EventAppendixService;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.beetl.sql.core.engine.PageQuery;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 事件附件表(EventAppendix)表服务实现
|
|
|
+ *
|
|
|
+ * @author makejava
|
|
|
+ * create on 2023-03-27 08:55:05
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor(onConstructor_ = {@Autowired})
|
|
|
+public class EventAppendixServiceImpl implements EventAppendixService {
|
|
|
+
|
|
|
+ private final EventAppendixDao eventAppendixDao;
|
|
|
+ /**
|
|
|
+ * 附件目录前缀
|
|
|
+ */
|
|
|
+ @Value("${event.appendix.prefixPath}")
|
|
|
+ private String prefixPath;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public EventAppendix queryById(Integer id) {
|
|
|
+ return eventAppendixDao.single(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<EventAppendix> queryAll() {
|
|
|
+ return eventAppendixDao.all();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void insert(EventAppendix eventAppendix) {
|
|
|
+ eventAppendixDao.insert(eventAppendix, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据主键更新记录
|
|
|
+ *
|
|
|
+ * @param eventAppendix 待更新的记录.包含主键在内,如果其他列的值为空则不更新
|
|
|
+ * @return 成功返回true,失败返回false
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public boolean updateById(EventAppendix eventAppendix) {
|
|
|
+ return eventAppendixDao.updateTemplateById(eventAppendix) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据主键删除记录
|
|
|
+ *
|
|
|
+ * @param id 主键
|
|
|
+ * @return 成功返回true,反之返回false
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public boolean deleteById(Integer id) {
|
|
|
+ return eventAppendixDao.deleteById(id) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页查询
|
|
|
+ *
|
|
|
+ * @param pageQuery 封装分页条件的分页对象
|
|
|
+ * @return pageQuery 作为一个引用,将查询结果封装进去
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Result<List<EventAppendix>> queryByPage(PageQuery<EventAppendix> pageQuery) {
|
|
|
+ eventAppendixDao.queryByPage(pageQuery);
|
|
|
+ Result<List<EventAppendix>> result = Result.success();
|
|
|
+ result.setData(pageQuery.getList());
|
|
|
+ result.setCount((int) pageQuery.getTotalRow());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean saveAppendix(MultipartFile file, String id) {
|
|
|
+ String filename = file.getOriginalFilename();
|
|
|
+ try {
|
|
|
+ // 创建File对象,自动识别相对或绝对路径,相对路径将自动从ClassPath下寻找
|
|
|
+ File fileNew = FileUtil.file(prefixPath + "/" + id + "/" + filename);
|
|
|
+ if (!FileUtil.exist(fileNew)) {
|
|
|
+ // 创建文件
|
|
|
+ FileUtil.touch(fileNew);
|
|
|
+
|
|
|
+ EventAppendix eventAppendix = new EventAppendix();
|
|
|
+ eventAppendix.setEventId(id);
|
|
|
+ eventAppendix.setName(filename);
|
|
|
+ eventAppendixDao.insert(eventAppendix, true);
|
|
|
+ }
|
|
|
+ // 写入数据
|
|
|
+ FileUtil.writeBytes(file.getBytes(), fileNew);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public byte[] simpleAppendix(HttpServletResponse response, EventAppendix eventAppendix) {
|
|
|
+ File fileNew = FileUtil.file(prefixPath + "/" + eventAppendix.getEventId() + "/" + eventAppendix.getName());
|
|
|
+ if (FileUtil.exist(fileNew)) {
|
|
|
+ return FileUtil.readBytes(fileNew);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<EventAppendix> queryListByEventId(String eventId) {
|
|
|
+ return eventAppendixDao.createLambdaQuery().andEq(EventAppendix::getEventId, eventId).select();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean deleteFile(EventAppendix eventAppendix) {
|
|
|
+ File fileNew = FileUtil.file(prefixPath + "/" + eventAppendix.getEventId() + "/" + eventAppendix.getName());
|
|
|
+ if (FileUtil.exist(fileNew)) {
|
|
|
+ return FileUtil.del(fileNew);
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|