diff --git a/kn-common/src/main/java/org/springblade/common/utils/ExcelCellUtil.java b/kn-common/src/main/java/org/springblade/common/utils/ExcelCellUtil.java new file mode 100644 index 0000000..3edf91d --- /dev/null +++ b/kn-common/src/main/java/org/springblade/common/utils/ExcelCellUtil.java @@ -0,0 +1,52 @@ + +package org.springblade.common.utils; + +import org.apache.poi.ss.usermodel.BorderStyle; +import org.apache.poi.ss.usermodel.HorizontalAlignment; +import org.apache.poi.ss.usermodel.VerticalAlignment; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.ss.util.RegionUtil; +import org.apache.poi.xssf.usermodel.XSSFCellStyle; +import org.apache.poi.xssf.usermodel.XSSFFont; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +public class ExcelCellUtil { + public ExcelCellUtil() { + } + + public static String strHandle(Object obj) { + return obj == null ? "" : obj.toString().trim(); + } + + public static XSSFCellStyle cellBorder(XSSFWorkbook workbook) { + XSSFCellStyle borderStyle = workbook.createCellStyle(); + borderStyle.setBorderBottom(BorderStyle.THIN); + borderStyle.setBorderLeft(BorderStyle.THIN); + borderStyle.setBorderTop(BorderStyle.THIN); + borderStyle.setBorderRight(BorderStyle.THIN); + return borderStyle; + } + + public static XSSFCellStyle cellFontAndBorder(XSSFWorkbook wb, String fontName, short fontSize) { + XSSFCellStyle fontAndBorderStyle = wb.createCellStyle(); + XSSFFont font = wb.createFont(); + font.setFontName(fontName); + font.setFontHeightInPoints(fontSize); + fontAndBorderStyle.setFont(font); + fontAndBorderStyle.setBorderBottom(BorderStyle.THIN); + fontAndBorderStyle.setBorderLeft(BorderStyle.THIN); + fontAndBorderStyle.setBorderTop(BorderStyle.THIN); + fontAndBorderStyle.setBorderRight(BorderStyle.THIN); + fontAndBorderStyle.setAlignment(HorizontalAlignment.CENTER); + fontAndBorderStyle.setVerticalAlignment(VerticalAlignment.CENTER); + return fontAndBorderStyle; + } + + public static void setRegionBorder(CellRangeAddress region, XSSFSheet sheet) { + RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet); + RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet); + RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet); + RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet); + } +} diff --git a/kn-service-api/biz-vordm-api/src/main/java/com/kening/vordm/entity/News.java b/kn-service-api/biz-vordm-api/src/main/java/com/kening/vordm/entity/News.java index 9b71af2..63b5e29 100644 --- a/kn-service-api/biz-vordm-api/src/main/java/com/kening/vordm/entity/News.java +++ b/kn-service-api/biz-vordm-api/src/main/java/com/kening/vordm/entity/News.java @@ -83,6 +83,11 @@ public class News implements Serializable { @ExcelIgnore private Date reviewTime; + /** + * 审核时间 + */ + private String Time; + @ExcelIgnore @TableField(exist = false) private static final long serialVersionUID = 1L; diff --git a/kn-service/biz-vordm/pom.xml b/kn-service/biz-vordm/pom.xml index e41a563..4533b47 100644 --- a/kn-service/biz-vordm/pom.xml +++ b/kn-service/biz-vordm/pom.xml @@ -98,6 +98,21 @@ org.springframework.boot spring-boot-starter-thymeleaf + + org.apache.poi + poi + 4.0.0 + + + org.apache.poi + poi-ooxml + 4.0.0 + + + org.apache.poi + poi-ooxml-schemas + 4.0.0 + diff --git a/kn-service/biz-vordm/src/main/java/com/kening/vordm/controller/NewsController.java b/kn-service/biz-vordm/src/main/java/com/kening/vordm/controller/NewsController.java index 8f4fcd2..4b63263 100644 --- a/kn-service/biz-vordm/src/main/java/com/kening/vordm/controller/NewsController.java +++ b/kn-service/biz-vordm/src/main/java/com/kening/vordm/controller/NewsController.java @@ -16,6 +16,7 @@ import org.springblade.core.mp.support.Condition; import org.springblade.core.mp.support.Query; import org.springblade.core.tool.api.R; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -87,4 +88,14 @@ public class NewsController { Boolean flag=newsService.update(updateWrapper); return R.data(flag); } + + /** + * 导入供应商模板 + * @param file + * @return + */ + @PostMapping({"/importTemplate"}) + public R fileExcelUpload(@RequestParam("fileData") MultipartFile file,@RequestParam("type") Integer type,@RequestParam("disasterId") Long disasterId) { + return R.data(newsService.importTemplate(file,type,disasterId)); + } } diff --git a/kn-service/biz-vordm/src/main/java/com/kening/vordm/mapper/NewsMapper.xml b/kn-service/biz-vordm/src/main/java/com/kening/vordm/mapper/NewsMapper.xml index 273c67d..c1da9ff 100644 --- a/kn-service/biz-vordm/src/main/java/com/kening/vordm/mapper/NewsMapper.xml +++ b/kn-service/biz-vordm/src/main/java/com/kening/vordm/mapper/NewsMapper.xml @@ -15,12 +15,13 @@ + id,title,content, link,type,disaster_id, create_time,status,manager_id, - review_time + review_time,time diff --git a/kn-service/biz-vordm/src/main/java/com/kening/vordm/service/NewsService.java b/kn-service/biz-vordm/src/main/java/com/kening/vordm/service/NewsService.java index 6055b45..b3298f0 100644 --- a/kn-service/biz-vordm/src/main/java/com/kening/vordm/service/NewsService.java +++ b/kn-service/biz-vordm/src/main/java/com/kening/vordm/service/NewsService.java @@ -3,6 +3,8 @@ package com.kening.vordm.service; import com.baomidou.mybatisplus.extension.service.IService; import com.kening.vordm.entity.News; +import org.springblade.core.tool.api.R; +import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -22,4 +24,7 @@ public interface NewsService extends IService { * @param type */ void export(Long disasterId,Integer type, HttpServletRequest request, HttpServletResponse response); + + R importTemplate(MultipartFile file, Integer type, Long disasterId); + } diff --git a/kn-service/biz-vordm/src/main/java/com/kening/vordm/service/impl/NewsServiceImpl.java b/kn-service/biz-vordm/src/main/java/com/kening/vordm/service/impl/NewsServiceImpl.java index c8ab79b..aab7d56 100644 --- a/kn-service/biz-vordm/src/main/java/com/kening/vordm/service/impl/NewsServiceImpl.java +++ b/kn-service/biz-vordm/src/main/java/com/kening/vordm/service/impl/NewsServiceImpl.java @@ -1,72 +1,210 @@ package com.kening.vordm.service.impl; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.exceptions.ExceptionUtil; +import cn.hutool.poi.excel.ExcelReader; +import cn.hutool.poi.excel.ExcelUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.kening.vordm.entity.Hotspot; import com.kening.vordm.entity.News; +import com.kening.vordm.entity.SocialMedia; import com.kening.vordm.mapper.NewsMapper; +import com.kening.vordm.mapper.SocialMediaMapper; import com.kening.vordm.service.HotspotService; import com.kening.vordm.service.NewsService; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springblade.common.utils.ExcelCellUtil; +import org.springblade.core.tool.api.R; import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.List; /** -* @author G1393 -* @description 针对表【news(新闻咨询数据表)】的数据库操作Service实现 -* @createDate 2023-04-04 15:54:31 -*/ + * @author G1393 + * @description 针对表【news(新闻咨询数据表)】的数据库操作Service实现 + * @createDate 2023-04-04 15:54:31 + */ @Slf4j @Service @AllArgsConstructor public class NewsServiceImpl extends ServiceImpl -implements NewsService { + implements NewsService { private HotspotService hotspotService; + private SocialMediaMapper socialMediaMapper; + /** * 新闻、热点导出功能 + * * @param disasterId * @param request * @param response * @param type */ @Override - public void export(Long disasterId,Integer type, HttpServletRequest request, HttpServletResponse response) { - try{ + public void export(Long disasterId, Integer type, HttpServletRequest request, HttpServletResponse response) { + try { String fileName = ""; - if(type.equals(1) || type.equals(0)){ + if (type.equals(1) || type.equals(0)) { fileName = type.equals(1) ? "English News" : "Chinese News"; //英文的新闻 或 中文的新闻 - List newsList = list(Wrappers.lambdaQuery().eq(News::getDisasterId,disasterId).eq(News::getStatus,1).eq(News::getType,type)); + List newsList = list(Wrappers.lambdaQuery().eq(News::getDisasterId, disasterId).eq(News::getStatus, 1).eq(News::getType, type)); extracted(response, fileName); - EasyExcel.write(response.getOutputStream(),News.class).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("news").doWrite(newsList); - }else if(type.equals(2)){ + EasyExcel.write(response.getOutputStream(), News.class).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("news").doWrite(newsList); + } else if (type.equals(2)) { //热点数据 fileName = "Hotspot Area"; - List hotspotList = hotspotService.list(Wrappers.lambdaQuery().eq(Hotspot::getDisasterId,disasterId).eq(Hotspot::getStatus,1)); + List hotspotList = hotspotService.list(Wrappers.lambdaQuery().eq(Hotspot::getDisasterId, disasterId).eq(Hotspot::getStatus, 1)); extracted(response, fileName); - EasyExcel.write(response.getOutputStream(),Hotspot.class).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("hotspot").doWrite(hotspotList); + EasyExcel.write(response.getOutputStream(), Hotspot.class).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("hotspot").doWrite(hotspotList); } - }catch (IOException e){ - log.error("导出IO异常:{}",e.getMessage()); + } catch (IOException e) { + log.error("导出IO异常:{}", e.getMessage()); ExceptionUtil.stacktraceToString(e); - }catch (Exception e){ - log.error("导出发生异常:{}",e.getMessage()); + } catch (Exception e) { + log.error("导出发生异常:{}", e.getMessage()); ExceptionUtil.stacktraceToString(e); } } + @Override + public R importTemplate(MultipartFile file, Integer typeData, Long disasterId) { + List content; + try { + ExcelReader reader = ExcelUtil.getReader(file.getInputStream()); + content = reader.read(); + } catch (Exception var30) { + return R.fail("上传文件格式解析错误"); + } + if (CollectionUtil.isEmpty(content)) { + return R.fail("文件内容为空!"); + } + if (typeData < 1) { + for (int i = 0; i < content.size(); ++i) { + List row = (List) content.get(i); + if (i != 0) { + if (CollUtil.isNotEmpty(row)) { + String title = ExcelCellUtil.strHandle(row.get(0)); + + String contentNew = ExcelCellUtil.strHandle(row.get(1)); + + String link = ExcelCellUtil.strHandle(row.get(2)); + + String time = ExcelCellUtil.strHandle(row.get(3)); + + News news = new News(); + news.setTitle(title); + news.setContent(contentNew); + news.setLink(link); + news.setTime(time); + news.setCreateTime(new Date()); + news.setDisasterId(disasterId); + news.setType(typeData); + news.setStatus(1); + this.baseMapper.insert(news); + } + } + } + + } else { + + for (int i = 0; i < content.size(); ++i) { + List row = (List) content.get(i); + if (i != 0) { + if (CollUtil.isNotEmpty(row)) { + String link = ExcelCellUtil.strHandle(row.get(0)); + + String time = ExcelCellUtil.strHandle(row.get(1)); + + String contentNew = ExcelCellUtil.strHandle(row.get(2)); + + String userName = ExcelCellUtil.strHandle(row.get(3)); + String favorite_count = ExcelCellUtil.strHandle(row.get(4)); + String reply_count = ExcelCellUtil.strHandle(row.get(5)); + String visit_count = ExcelCellUtil.strHandle(row.get(6)); + String retweet_count = ExcelCellUtil.strHandle(row.get(7)); + String geo = ExcelCellUtil.strHandle(row.get(7)); + String coordinates = ExcelCellUtil.strHandle(row.get(7)); + String place = ExcelCellUtil.strHandle(row.get(7)); + String contributors = ExcelCellUtil.strHandle(row.get(7)); + String topic = ExcelCellUtil.strHandle(row.get(7)); + + SocialMedia socialMedia = new SocialMedia(); + socialMedia.setCreateTime(new Date()); + socialMedia.setDisasterId(disasterId); + if (typeData == 2) { + socialMedia.setType(0); + } else if (typeData == 3) { + socialMedia.setType(1); + } + socialMedia.setLink(link); + if (time != null) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Date dateStr = null; + try { + dateStr = sdf.parse(time); + } catch (ParseException e) { + return R.fail("时间格式错误"); + } + socialMedia.setTime(dateStr); + } + socialMedia.setContent(contentNew); + if (userName != null) { + socialMedia.setUserId(userName); + } + if (favorite_count != null) { + socialMedia.setFavoriteCount(Integer.parseInt(favorite_count)); + } + if (reply_count != null) { + socialMedia.setReplyCount(Integer.parseInt(reply_count)); + } + if (visit_count != null) { + socialMedia.setVisitCount(Integer.parseInt(visit_count)); + } + if (retweet_count != null) { + socialMedia.setRetweetCount(Integer.parseInt(retweet_count)); + } + if (geo != null) { + socialMedia.setGeo(geo); + } + if (coordinates != null) { + socialMedia.setCoordinates(coordinates); + } + if (place != null) { + socialMedia.setPlace(place); + } + if (contributors != null) { + socialMedia.setContributors(contributors); + } + if (topic != null) { + + } + socialMedia.setTopic(topic); + socialMedia.setStatus(1); + socialMediaMapper.insert(socialMedia); + } + } + } + } + return R.data("上传成功!"); + } + /** * 导出设置 + * * @param response * @param fileName * @throws IOException @@ -77,6 +215,6 @@ implements NewsService { // 设置字符编码 response.setCharacterEncoding("utf-8"); // 设置响应头 - response.setHeader("Content-disposition", "attachment;filename="+ fileName +".xlsx"); + response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); } } diff --git a/kn-service/biz-vordm/src/main/resources/application.yml b/kn-service/biz-vordm/src/main/resources/application.yml index 62e9388..84a6b8f 100644 --- a/kn-service/biz-vordm/src/main/resources/application.yml +++ b/kn-service/biz-vordm/src/main/resources/application.yml @@ -14,4 +14,4 @@ swagger: mail: home: - link: http://192.168.31.77:8888 + link: http://39.98.35.99:6400 diff --git a/pom.xml b/pom.xml index 84a2088..475d4fd 100644 --- a/pom.xml +++ b/pom.xml @@ -26,7 +26,7 @@ Harbor12345 iot 1.4.13 - 5.8.16 + 5.0.0 3.2.1