提交 8a4e7209 作者: yanxin

专家数据导入(不完善,本地执行使用)

上级 8110a6e9
package com.zzsn.leaderbase.controller; package com.zzsn.leaderbase.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzsn.leaderbase.entity.CharacterBasicInfo; import com.zzsn.leaderbase.entity.CharacterBasicInfo;
import com.zzsn.leaderbase.service.CharacterBasicInfoService; import com.zzsn.leaderbase.service.CharacterBasicInfoService;
import com.zzsn.leaderbase.service.IGeneratorIdService;
import com.zzsn.leaderbase.util.ExcelUtil;
import com.zzsn.leaderbase.util.SortUtil; import com.zzsn.leaderbase.util.SortUtil;
import com.zzsn.leaderbase.vo.BasicInfoListVo; import com.zzsn.leaderbase.vo.BasicInfoListVo;
import com.zzsn.leaderbase.vo.Result; import com.zzsn.leaderbase.vo.Result;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.io.IOException;
import java.util.Map; import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
@Slf4j @Slf4j
@RestController @RestController
...@@ -24,7 +35,10 @@ public class CharacterBasicInfoController { ...@@ -24,7 +35,10 @@ public class CharacterBasicInfoController {
@Autowired @Autowired
CharacterBasicInfoService characterBasicInfoService; CharacterBasicInfoService characterBasicInfoService;
@Autowired
IGeneratorIdService generatorIdService;
private static Map<String, String> importFieldMap;
/** /**
* 资讯抽取人物信息分页查询 * 资讯抽取人物信息分页查询
* 人物审核列表 * 人物审核列表
...@@ -180,5 +194,167 @@ public class CharacterBasicInfoController { ...@@ -180,5 +194,167 @@ public class CharacterBasicInfoController {
IPage page = characterBasicInfoService.getList(basicInfoListVo); IPage page = characterBasicInfoService.getList(basicInfoListVo);
return Result.OK(page); return Result.OK(page);
} }
@GetMapping("/importExcel")
public void importExcel(String[] args) {
//本地文件路径
String filePath = "E:\\数能软件\\克虏宝\\人物服务\\专家导入数据2025-01-08.xlsx";
addSummaryLocalExcel(filePath,"2");
}
public void addSummaryLocalExcel(String filePath, String category) {
List<String> sheetNameList = new ArrayList<>();
Workbook workbook = null;
InputStream in = null;
try {
// 获取输入流
in = Files.newInputStream(Paths.get(filePath));
// 判断excel版本
if (ExcelUtil.judegExcelEdition(filePath)) {
workbook = new XSSFWorkbook(in);
} else {
workbook = new HSSFWorkbook(in);
}
Iterator<Sheet> it = workbook.sheetIterator();
while (it.hasNext()) {
Sheet sheetTmp = it.next();
String sheetName = sheetTmp.getSheetName();
sheetNameList.add(sheetName);
}
for (String sheetName : sheetNameList) {
Sheet sheet = workbook.getSheet(sheetName);
//获取标题行
Row titleRow = sheet.getRow(0);
List<CharacterBasicInfo> list = new ArrayList<>();
for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
// 循环获取工作表的每一行
Row sheetRow = sheet.getRow(i);
if (sheetRow == null) {
continue;
}
Map<String, Object> beanMap = new HashMap<>();
for (Cell cell : titleRow) {
String titleName = cell.getStringCellValue();
if(importFieldMap.containsKey(titleName)){
try {
String stringCellValue = sheetRow.getCell(cell.getColumnIndex()).getStringCellValue();
if(StringUtils.isNotEmpty(stringCellValue)){
beanMap.put(importFieldMap.get(titleName), stringCellValue);
}
}catch (Exception e){
log.debug("数据导入excel解析{}行{}字段为空",cell.getColumnIndex(),titleName);
}
}
}
System.out.println(beanMap);
list.add(BeanUtil.mapToBean(beanMap, CharacterBasicInfo.class, true,CopyOptions.create().setIgnoreError(true)));
}
//需要新增的数据列表
List<CharacterBasicInfo> saveList = new ArrayList<>();
//需要更新的数据列表
List<CharacterBasicInfo> updateList = new ArrayList<>();
for (CharacterBasicInfo basicInfo : list) {
if(basicInfo == null || StringUtils.isEmpty(basicInfo.getName()) || StringUtils.isEmpty(basicInfo.getDepartment())){
log.info("数据为空,跳过");
continue;
}
basicInfo.setCategory(category);
if(StringUtils.isNotEmpty(basicInfo.getBirthday())){
basicInfo.setBirthday(basicInfo.getBirthday().replaceAll("[./]","-"));
}
if(StringUtils.isNotEmpty(basicInfo.getTechnicalDate())){
basicInfo.setTechnicalDate(basicInfo.getTechnicalDate().replaceAll("[./]","-"));
}
if(StringUtils.isNotEmpty(basicInfo.getWorkDate())){
basicInfo.setWorkDate(basicInfo.getWorkDate().replaceAll("[./]","-"));
}
//检查是否已有数据
CharacterBasicInfo oldBasicInfo = null;
if(StringUtils.isNotEmpty(basicInfo.getUid())){
oldBasicInfo = characterBasicInfoService.getBasicInfoByUid(basicInfo.getUid());
}else{
oldBasicInfo = characterBasicInfoService.getByNameAndByDepartment(category, basicInfo.getName(), basicInfo.getDepartment());
}
if(oldBasicInfo != null){
//合并两个对象
oldBasicInfo.setSex(basicInfo.getSex());
oldBasicInfo.setBirthday(basicInfo.getBirthday());
oldBasicInfo.setNativePlace(basicInfo.getNativePlace());
oldBasicInfo.setSchool(basicInfo.getSchool());
oldBasicInfo.setSchoolSpeciality(basicInfo.getSchoolSpeciality());
oldBasicInfo.setEducation(basicInfo.getEducation());
oldBasicInfo.setDepart(basicInfo.getDepart());
oldBasicInfo.setDuty(basicInfo.getDuty());
oldBasicInfo.setTakeOfficeTime(basicInfo.getTakeOfficeTime());
oldBasicInfo.setTakeOfficeTimeEnd(basicInfo.getTakeOfficeTimeEnd());
oldBasicInfo.setExpertType(basicInfo.getExpertType());
oldBasicInfo.setResearchField(basicInfo.getResearchField());
oldBasicInfo.setSpeciality(basicInfo.getSpeciality());
oldBasicInfo.setTalentPlanning(basicInfo.getTalentPlanning());
oldBasicInfo.setTechnicalTitles(basicInfo.getTechnicalTitles());
oldBasicInfo.setTechnicalDate(basicInfo.getTechnicalDate());
oldBasicInfo.setWorkDate(basicInfo.getWorkDate());
oldBasicInfo.setUpdateTime(new Date());
updateList.add(oldBasicInfo);
}else {
String id = Long.toString(generatorIdService.getOrderId());
basicInfo.setId(id);
basicInfo.setCreateTime(new Date());
String uid = generatorIdService.getIdNo();
basicInfo.setUid(uid);
basicInfo.setMainEntry(1);
saveList.add(basicInfo);
}
}
if(!saveList.isEmpty()){
characterBasicInfoService.saveBatch(saveList);
log.info("新增{}条数据",saveList.size());
}
if(!updateList.isEmpty()){
characterBasicInfoService.updateBatchById(updateList);
log.info("更新{}条数据",updateList.size());
}
}
} catch (Exception e) {
log.info("导入专家信息异常:{}",e.getMessage(), e);
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (workbook != null) {
workbook.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
static {
importFieldMap = new HashMap<>();
importFieldMap.put("人物id", "uid");
importFieldMap.put("姓名", "name");
importFieldMap.put("性别", "sex");
importFieldMap.put("出生年月", "birthday");
importFieldMap.put("籍贯", "nativePlace");
importFieldMap.put("毕业院校", "school");
importFieldMap.put("毕业专业", "schoolSpeciality");
importFieldMap.put("学历", "education");
importFieldMap.put("单位名称", "department");
importFieldMap.put("所在部门", "depart");
importFieldMap.put("职务", "duty");
importFieldMap.put("任职开始时间", "takeOfficeTime");
importFieldMap.put("任职结束时间", "takeOfficeTimeEnd");
importFieldMap.put("顶尖专家/领军专家", "expertType");
importFieldMap.put("研究领域", "researchField");
importFieldMap.put("专业方向", "speciality");
importFieldMap.put("已入选的人才计划", "talentPlanning");
importFieldMap.put("专业技术职称", "technicalTitles");
importFieldMap.put("专业技术职称取得时间", "technicalDate");
importFieldMap.put("参加工作时间", "workDate");
}
} }
...@@ -55,4 +55,8 @@ public interface CharacterBasicInfoService extends IService<CharacterBasicInfo> ...@@ -55,4 +55,8 @@ public interface CharacterBasicInfoService extends IService<CharacterBasicInfo>
List<Map<String, String>> getDepartment(String socialCreditCode, String department, String keyword); List<Map<String, String>> getDepartment(String socialCreditCode, String department, String keyword);
List<CharacterBasicInfo> getAllList(BasicInfoListVo basicInfoListVo); List<CharacterBasicInfo> getAllList(BasicInfoListVo basicInfoListVo);
CharacterBasicInfo getBasicInfoByUid(String uid);
CharacterBasicInfo getByNameAndByDepartment(String category, String name, String department);
} }
...@@ -249,4 +249,24 @@ public class CharacterBasicInfoServiceImpl extends ServiceImpl<CharacterBasicInf ...@@ -249,4 +249,24 @@ public class CharacterBasicInfoServiceImpl extends ServiceImpl<CharacterBasicInf
List<CharacterBasicInfo> listNew =setListValues(list,basicInfoListVo); List<CharacterBasicInfo> listNew =setListValues(list,basicInfoListVo);
return listNew; return listNew;
} }
@Override
public CharacterBasicInfo getBasicInfoByUid(String uid) {
LambdaQueryWrapper<CharacterBasicInfo> queryWrapper = Wrappers.lambdaQuery();
queryWrapper.eq(CharacterBasicInfo::getUid,uid);
return baseMapper.selectOne(queryWrapper);
}
@Override
public CharacterBasicInfo getByNameAndByDepartment(String category, String name, String department) {
LambdaQueryWrapper<CharacterBasicInfo> queryWrapper = Wrappers.lambdaQuery();
queryWrapper.eq(CharacterBasicInfo::getCategory,category);
queryWrapper.eq(CharacterBasicInfo::getName,name);
queryWrapper.eq(CharacterBasicInfo::getDepartment,department);
List<CharacterBasicInfo> characterBasicInfos = baseMapper.selectList(queryWrapper);
if(characterBasicInfos!=null && characterBasicInfos.size()>0){
return characterBasicInfos.get(0);
}
return null;
}
} }
package com.zzsn.leaderbase.util;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import java.text.NumberFormat;
import java.util.ArrayList;
public class ExcelUtil {
public static ArrayList<ArrayList<String>> analysis(Sheet sheet) {
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(false);
ArrayList<ArrayList<String>> row = new ArrayList<>();
for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
// 循环获取工作表的每一行
Row sheetRow = sheet.getRow(i);
if(sheetRow == null){
continue;
}
// 循环获取每一列
ArrayList<String> cell = new ArrayList<>();
for (int j = 0; j < sheetRow.getLastCellNum(); j++) { //
// 将每一个单元格的值装入列集合
if (sheetRow.getCell(j) != null) {
try {
cell.add(sheetRow.getCell(j).getStringCellValue().trim());
}catch (Exception e){
cell.add(String.valueOf(sheetRow.getCell(j).getNumericCellValue()));
}
} else {
cell.add("");
}
}
// 将装有每一列的集合装入大集合
row.add(cell);
}
return row;
}
public static boolean judegExcelEdition(String fileName) {
String regex = "^.+\\.(?i)(xls)$";
return !fileName.matches(regex);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论