|
@@ -0,0 +1,107 @@
|
|
|
|
+package com.hssx.bms.service.impl;
|
|
|
|
+
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.github.pagehelper.PageHelper;
|
|
|
|
+import com.github.pagehelper.PageInfo;
|
|
|
|
+import com.hssx.bms.entity.Book;
|
|
|
|
+import com.hssx.bms.entity.InstitutionalInformation;
|
|
|
|
+import com.hssx.bms.mapper.BookMapper;
|
|
|
|
+import com.hssx.bms.service.BookService;
|
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.hssx.bms.until.HttpRespMsg;
|
|
|
|
+import com.hssx.bms.until.PageUtil;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.UUID;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * <p>
|
|
|
|
+ * 服务实现类
|
|
|
|
+ * </p>
|
|
|
|
+ *
|
|
|
|
+ * @author 吴涛涛
|
|
|
|
+ * @since 2019-10-16
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {
|
|
|
|
+ @Resource
|
|
|
|
+ private BookMapper bookMapper;
|
|
|
|
+ @Value("${upload.path}")
|
|
|
|
+ private String path;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public HttpRespMsg addOrUpdateBook(Book book, MultipartFile file) {
|
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
|
+ String fileName = "";
|
|
|
|
+ if (null != file) {
|
|
|
|
+ fileName = uploadFile(file, path);
|
|
|
|
+ }
|
|
|
|
+ if (null != book.getId()) {
|
|
|
|
+ //添加操作
|
|
|
|
+ if (!"".equals(fileName)) {
|
|
|
|
+ book.setCover(fileName);
|
|
|
|
+ }
|
|
|
|
+ bookMapper.insert(book);
|
|
|
|
+ } else {
|
|
|
|
+ //添加操作
|
|
|
|
+ if (!"".equals(fileName)) {
|
|
|
|
+ book.setCover(fileName);
|
|
|
|
+ }
|
|
|
|
+ bookMapper.insert(book);
|
|
|
|
+ }
|
|
|
|
+ msg.data = book;
|
|
|
|
+ return msg;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public HttpRespMsg getList(PageUtil page) {
|
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
|
+ if(null == page.getPageNum() && null == page.getPageSize()){
|
|
|
|
+ page = new PageUtil();
|
|
|
|
+ }
|
|
|
|
+ PageHelper.startPage(page.getPageNum(),page.getPageSize());
|
|
|
|
+ List<Book> institutionalInformations = bookMapper.selectList(new QueryWrapper<Book>());
|
|
|
|
+ PageInfo<Book> pageInfo = new PageInfo<>(institutionalInformations);
|
|
|
|
+ msg.data = pageInfo;
|
|
|
|
+ return msg;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private String uploadFile(MultipartFile file, String path) {
|
|
|
|
+ String afterUploadFileName = "";
|
|
|
|
+ if (file != null) {
|
|
|
|
+ File dir = null;
|
|
|
|
+ dir = new File(path);
|
|
|
|
+ // D://dolphin/upload 文件上传后所存储的位置,部署到服务器上时配置服务器地址即可
|
|
|
|
+ if (!dir.exists()) {
|
|
|
|
+ dir.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ String fileName = "";
|
|
|
|
+ if (file != null) {
|
|
|
|
+ fileName = file.getOriginalFilename();
|
|
|
|
+ System.out.println("上传文件名称" + file.getName() + ", dir = " + dir.getAbsolutePath());
|
|
|
|
+ int pos = fileName.lastIndexOf(".");
|
|
|
|
+ String rand = UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
|
+ String sufix = fileName.substring(pos);
|
|
|
|
+ fileName = rand + sufix;
|
|
|
|
+ afterUploadFileName = "/upload/" + fileName;
|
|
|
|
+ File saveFile = new File(dir, fileName);
|
|
|
|
+ try {
|
|
|
|
+ saveFile.createNewFile();
|
|
|
|
+ file.transferTo(saveFile);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return afterUploadFileName;
|
|
|
|
+ }
|
|
|
|
+}
|