提交 3adb7570 作者: 925993793@qq.com

导入导出逻辑优化调整-遗漏提交

上级 74dfeb42
......@@ -16,7 +16,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
......@@ -216,20 +215,6 @@ public class ThinktankBasicInfoController {
return bytes;
}
/* @PostMapping("/batchExport")
public byte[] batchExport(@RequestBody ThinktankBasicInfoListVo thinktankBasicInfoListVo,HttpServletRequest request, HttpServletResponse response){
log.info("导出:{}", thinktankBasicInfoListVo);
// Integer pageNo = thinktankBasicInfoListVo.getPageNo();
// Integer pageSize= thinktankBasicInfoListVo.getPageSize();
// if(pageNo < 1)
// pageNo = 1;
// if(pageSize < 0)
// pageSize = 10;
// Integer office = pageSize * (pageNo - 1);
// thinktankBasicInfoListVo.setOffset(office);
return thinktankBasicInfoService.batchExport(thinktankBasicInfoListVo,request,response);
}*/
@GetMapping("/listBaseData")
public Result<?> getListBaseData(@RequestParam(name = "id", required = true) String id,
@RequestParam(name = "pageNo", required = true) Integer pageNo,
......
......@@ -3,9 +3,7 @@ package com.zzsn.thinktank.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.zzsn.thinktank.entity.ThinktankBasicInfo;
import com.zzsn.thinktank.vo.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
......@@ -29,10 +27,6 @@ public interface ThinktankBasicInfoService extends IService<ThinktankBasicInfo>
void downloadTemplate(HttpServletResponse response);
Result batchImport(MultipartFile multipartFile);
byte[] batchExport(ThinktankBasicInfoListVo thinktankBasicInfoListVo, HttpServletRequest request, HttpServletResponse response);
Result<?> getListBaseData(String id, Integer pageNo, Integer pageSize, Integer office);
/**
......
package com.zzsn.thinktank.service.impl;
import cn.hutool.core.date.DateTime;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
......@@ -11,13 +9,13 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzsn.thinktank.entity.*;
import com.zzsn.thinktank.mapper.ThinktankBasicInfoMapper;
import com.zzsn.thinktank.service.*;
import com.zzsn.thinktank.util.*;
import com.zzsn.thinktank.util.EsDateUtil;
import com.zzsn.thinktank.util.EsUtil;
import com.zzsn.thinktank.util.StringUtil;
import com.zzsn.thinktank.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.Functions;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
......@@ -27,10 +25,8 @@ 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.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
......@@ -384,345 +380,6 @@ public class ThinktankBasicInfoServiceImpl extends ServiceImpl<ThinktankBasicInf
}
@Override
public Result batchImport(MultipartFile multipartFile) {
int index = multipartFile.getOriginalFilename().lastIndexOf(".");
String fileSuffix = multipartFile.getOriginalFilename().substring(index + 1);
if ("xls".equals(fileSuffix) || "xlsx".equals(fileSuffix)) {
//文件校验
if (!this.checkExcel(multipartFile)) {
log.error("excel格式不对");
return Result.error("模版有误,请使用正确的模板!");
}
String msg = doExcel(multipartFile, fileSuffix);
return Result.OK(msg);
} else {
return Result.error("不支持的文件类型!");
}
}
private boolean checkExcel(MultipartFile multipartFile) {
Workbook workbook = ExcelExportUtil.getWorkbook(multipartFile);
if (workbook != null) {
List<String> list = ExcelExportUtil.getSheetTitles(workbook);
return list != null
&& list.contains("序号")
&& list.contains("智库机构编码")
&& list.contains("中文全称")
&& list.contains("中文简称")
&& list.contains("英文全称")
&& list.contains("英文简称")
&& list.contains("原文全称")
&& list.contains("原文简称")
&& list.contains("官网")
&& list.contains("所属国家")
&& list.contains("标签")
&& list.contains("简介")
&& list.contains("成立时间")
&& list.contains("是否收费(1-是;0-否)")
&& list.contains("地址")
&& list.contains("创办单位/所属单位")
&& list.contains("属性")
;
}
return false;
}
private String doExcel(MultipartFile multipartFile, String fileSuffix) {
List<List<String>> excelLists = null;
try {
byte[] fileData = multipartFile.getBytes();
excelLists = ExcelExportUtil.readExcel(new ByteArrayInputStream(fileData), 1, 17);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
String msg = "";
int i = 0;//记录成功数量
int j = 0;//记录失败数量
if (excelLists != null && excelLists.size() > 0) {
//处理数据
List<ThinktankBasicInfo> infoList = new ArrayList<>();
for (List<String> list : excelLists) {
ThinktankBasicInfo thinktankBasicInfo = new ThinktankBasicInfo();
if (StringUtils.isNotEmpty(list.get(0))) thinktankBasicInfo.setId(list.get(0));
if (StringUtils.isNotEmpty(list.get(1))) thinktankBasicInfo.setCodeId(list.get(1));
if (StringUtils.isNotEmpty(list.get(2))) thinktankBasicInfo.setChineseWhole(list.get(2));
if (StringUtils.isNotEmpty(list.get(3))) thinktankBasicInfo.setChineseSimple(list.get(3));
if (StringUtils.isNotEmpty(list.get(4))) thinktankBasicInfo.setEnglishWhole(list.get(4));
if (StringUtils.isNotEmpty(list.get(5))) thinktankBasicInfo.setEnglishSimple(list.get(5));
if (StringUtils.isNotEmpty(list.get(6))) thinktankBasicInfo.setOriginalWhole(list.get(6));
if (StringUtils.isNotEmpty(list.get(7))) thinktankBasicInfo.setOriginalSimple(list.get(7));
if (StringUtils.isNotEmpty(list.get(8))) thinktankBasicInfo.setOfficialWebsite(list.get(8));
if (StringUtils.isNotEmpty(list.get(9))) thinktankBasicInfo.setBelongCountry(list.get(9));
if (StringUtils.isNotEmpty(list.get(10))) thinktankBasicInfo.setTagName(list.get(10));
if (StringUtils.isNotEmpty(list.get(11))) thinktankBasicInfo.setBiographicalNotes(list.get(11));
if (StringUtils.isNotEmpty(list.get(12))) thinktankBasicInfo.setEstablishTime(list.get(12));
//是否收费
if (StringUtils.isNotEmpty(list.get(13))) {
String chargeStr = list.get(13);
try {
Integer charge = Integer.valueOf(chargeStr);
thinktankBasicInfo.setCharge(charge);
} catch (Exception e) {
thinktankBasicInfo.setCharge(-1);
log.error("是否收费转换失败:{},{}", list.get(1), list.get(13));
}
}
if (StringUtils.isNotEmpty(list.get(14))) thinktankBasicInfo.setAddress(list.get(14));
if (StringUtils.isNotEmpty(list.get(15))) thinktankBasicInfo.setBelongUnit(list.get(15));
if (StringUtils.isNotEmpty(list.get(15))) thinktankBasicInfo.setAttribute(list.get(16));
infoList.add(thinktankBasicInfo);
}
//国家列表
List<ThinktankCategoryStructure> listCategoryStructure = leaderCategoryService.list();
Map<String, String> mapCategoryStructure = new HashMap<>();
for (ThinktankCategoryStructure thinktankCategoryStructure : listCategoryStructure) {
mapCategoryStructure.put(thinktankCategoryStructure.getTypeName(), thinktankCategoryStructure.getId());
}
//标签列表
Map<String, String> mapTag = new HashMap<>();//标签名称列表
Result result = thinktankTagService.getTagList(null, null);
List<SysLabelVo> list = (List<SysLabelVo>) result.getResult();
for (SysLabelVo labelVo : list) {
mapTag.put(labelVo.getName(), labelVo.getId());
}
StringBuffer sb = new StringBuffer();
for (ThinktankBasicInfo thinktankBasicInfo : infoList) {
boolean b = true;
//序号
String number = thinktankBasicInfo.getId();
String codeId = thinktankBasicInfo.getCodeId();
if (codeId == null || codeId.length() == 0) {
thinktankBasicInfo.setCodeId(generatorIdService.getIdNo());
thinktankBasicInfo.setId(Long.toString(generatorIdService.getOrderId()));
thinktankBasicInfo.setCreateTime(new Date());
thinktankBasicInfo.setUpdateTime(new Date());
} else {
LambdaQueryWrapper<ThinktankBasicInfo> lambdaQuery = Wrappers.lambdaQuery();
lambdaQuery.eq(ThinktankBasicInfo::getCodeId, codeId);
List<ThinktankBasicInfo> listBasicInfo = thinktankBasicInfoMapper.selectList(lambdaQuery);
if (listBasicInfo.size() > 0) {
thinktankBasicInfo.setId(listBasicInfo.get(0).getId());
thinktankBasicInfo.setUpdateTime(new Date());
} else {
thinktankBasicInfo.setId(Long.toString(generatorIdService.getOrderId()));
thinktankBasicInfo.setCreateTime(new Date());
thinktankBasicInfo.setUpdateTime(new Date());
}
}
//国家
String belongCountry = thinktankBasicInfo.getBelongCountry();
String belongCountryId = mapCategoryStructure.get(belongCountry);
if (StringUtils.isEmpty(belongCountryId)) {
b = false;
sb.append("序号为'" + number + "'的国家’" + belongCountry + "‘数据库没有,导入失败;\n");
} else {
thinktankBasicInfo.setBelongCountryId(belongCountryId);
}
//标签
String tagName = thinktankBasicInfo.getTagName();
if (tagName != null && tagName.length() > 0) {
String[] tagNameArr = tagName.split(",");
List<String> tagIdList = new ArrayList<>();
for (String s : tagNameArr) {
String tagId = mapTag.get(s);
if (StringUtils.isEmpty(tagId)) {
b = false;
sb.append("序号为'" + number + "'的标签'" + s + "'数据库没有,导入失败;\n");
} else {
tagIdList.add(tagId);
}
}
String tagId = String.join(",", tagIdList);
thinktankBasicInfo.setTagId(tagId);
}
//所属单位
String belongUnit = thinktankBasicInfo.getBelongUnit();
if (belongUnit != null && belongUnit.length() > 0) {
List<SysBaseEnterprise> listEnter = enterpriseService.getListByName(belongUnit);
if (listEnter.size() > 0) {
SysBaseEnterprise enter = listEnter.get(0);
String belongUnitCode = enter.getSocialCreditCode();
thinktankBasicInfo.setBelongUnitCode(belongUnitCode);
} else {
b = false;
sb.append("序号为'" + number + "'的创办单位/所属单位'" + belongUnit + "'数据库没有,导入失败;\n");
}
}
//成立时间
String establishTime = thinktankBasicInfo.getEstablishTime();
if (!DateUtil.isDate(establishTime)) {
b = false;
sb.append("序号为'" + number + "'的成立时间'" + belongUnit + "'格式错误,导入失败;\n");
}
if (b) {
this.saveOrUpdate(thinktankBasicInfo);
thinktankTagService.addLabel(thinktankBasicInfo);
i++;
} else {
j++;
}
}
sb.append("请重新修改后再次上传。");
msg = "导入完成!\n"
+ "导入成功数量:" + i + "\n"
+ "导入失败数量:" + j + "\n"
+ "其中:\n"
+ sb.toString();
}
if (j > 0) {
return msg;
}
return "true";
}
@Override
public byte[] batchExport(ThinktankBasicInfoListVo thinktankBasicInfoListVo, HttpServletRequest request, HttpServletResponse response) {
List<ThinktankBasicInfoExportVo> list = this.getExportList(thinktankBasicInfoListVo);
log.info("{}", list.size());
int i = 1;
for (ThinktankBasicInfoExportVo thinktankBasicInfoVo : list) {
thinktankBasicInfoVo.setInfoSourceId(String.valueOf(i++));
}
try {
ExcelWriter writer = ExcelUtil.getWriter();
writer.addHeaderAlias("infoSourceId", "序号");
writer.addHeaderAlias("id", "智库机构ID");
writer.addHeaderAlias("codeId", "智库机构编码");
writer.addHeaderAlias("chineseWhole", "中文全称");
writer.addHeaderAlias("chineseSimple", "中文简称");
writer.addHeaderAlias("englishWhole", "英文全称");
writer.addHeaderAlias("englishSimple", "英文简称");
writer.addHeaderAlias("originalWhole", "原文全称");
writer.addHeaderAlias("originalSimple", "原文简称");
writer.addHeaderAlias("officialWebsite", "官网");
writer.addHeaderAlias("belongCountry", "所属国家");
writer.addHeaderAlias("tagName", "标签");
writer.addHeaderAlias("biographicalNotes", "简介");
writer.addHeaderAlias("establishTime", "成立时间");
writer.addHeaderAlias("charge", "是否收费(1-是;0-否)");
writer.addHeaderAlias("address", "地址");
writer.addHeaderAlias("belongUnit", "创办单位/所属单位");
writer.addHeaderAlias("collectionCount", "信息采集数量");
writer.addHeaderAlias("infoSourceCount", "关联信息源数量");
writer.addHeaderAlias("infoSourceCode", "关联信息源编码");
writer.addHeaderAlias("webSiteName", "关联信息源名称");
writer.addHeaderAlias("siteName", "栏目名称");
writer.addHeaderAlias("siteUri", "网址");
writer.addHeaderAlias("status", "启用状态(1-启用;0-禁用)");
writer.addHeaderAlias("count", "信息源采集数量");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// 一次性写出内容,使用默认样式,强制输出标题
writer.write(list, true);
//out为OutputStream,需要写出到的目标流
writer.flush(outputStream);
// 关闭writer,释放内存
writer.close();
return outputStream.toByteArray();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public List<ThinktankBasicInfoExportVo> getExportList(ThinktankBasicInfoListVo thinktankBasicInfoListVo) {
thinktankBasicInfoListVo.setColumn(StringUtil.humpToLine(thinktankBasicInfoListVo.getColumn()));
//查询列表
List<ThinktankBasicInfoExportVo> list = thinktankBasicInfoMapper.getExportList(thinktankBasicInfoListVo);
if (list.size() == 0) {
return list;
}
//信息源
List<String> groupIds = new ArrayList<>();
for (ThinktankBasicInfoExportVo thinktankBasicInfoVo : list) {
groupIds.add(thinktankBasicInfoVo.getId());
}
List<InfoSourceGroupMap> infoSourceGroupMapList = infoSourceGroupMapService.getSourceGroupMap(groupIds);
List<InfoSourceGroupCountVo> countVoList = new ArrayList<>();
List<String> sidList = new ArrayList<>();
for (InfoSourceGroupMap infoSourceGroupMap : infoSourceGroupMapList) {
InfoSourceGroupCountVo infoSourceGroupCountVo = new InfoSourceGroupCountVo();
BeanUtils.copyProperties(infoSourceGroupMap, infoSourceGroupCountVo);
countVoList.add(infoSourceGroupCountVo);
sidList.add(infoSourceGroupMap.getSourceId());
}
//查询es
BoolQueryBuilder boolQuerylist = QueryBuilders.boolQuery();
boolQuerylist.must(QueryBuilders.termsQuery("sid", sidList));
//boolQuerylist.filter(QueryBuilders.rangeQuery("publishDate").gte("2024-05-18T00:00:01"));
Map<String, Long> mapGroup = null;
try {
mapGroup = esUtil.groupBy("basedata", "sid", boolQuerylist, 100);
} catch (IOException e) {
log.error("ES查询失败");
e.printStackTrace();
}
if (mapGroup != null && mapGroup.size() > 0) {
for (InfoSourceGroupCountVo infoSourceGroupCountVo : countVoList) {
String sourceId = infoSourceGroupCountVo.getSourceId();
Long countInfo = mapGroup.get(sourceId);
if (countInfo == null) {
countInfo = 0L;
}
infoSourceGroupCountVo.setInfoCount(Math.toIntExact(countInfo));
}
}
//统计关联信息源数量
Map<String, List<InfoSourceGroupCountVo>> studentsByGrade = countVoList.stream()
.collect(Collectors.groupingBy(InfoSourceGroupCountVo::getGroupId));
for (ThinktankBasicInfoExportVo thinktankBasicInfoVo : list) {
String infoSourceId = thinktankBasicInfoVo.getInfoSourceId();
Long count = mapGroup.get(infoSourceId);
if (count == null) {
thinktankBasicInfoVo.setCount(0);
} else {
thinktankBasicInfoVo.setCount(Math.toIntExact(count));
}
String id = thinktankBasicInfoVo.getId();
List<InfoSourceGroupCountVo> listVo = studentsByGrade.get(id);
if (listVo == null) {
thinktankBasicInfoVo.setCollectionCount(0);
thinktankBasicInfoVo.setInfoSourceCount(0);
} else {
thinktankBasicInfoVo.setInfoSourceCount(listVo.size());
int sum = 0;
for (InfoSourceGroupCountVo infoSourceGroupCountVo : listVo) {
sum += infoSourceGroupCountVo.getInfoCount();
}
thinktankBasicInfoVo.setCollectionCount(sum);
}
}
return list;
}
@Override
public Result<?> getListBaseData(String id, Integer pageNo, Integer pageSize, Integer office) {
List<InfoSourceGroupMap> infoSourceGroupMapList = infoSourceGroupMapService.getSourceGroupMap(Arrays.asList(id));
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论