1.基础数据-往来单位信息导入修改为导入json文件

master
MrZhai 4 years ago
parent b27b0ff294
commit d3f12d66ee

@ -1,10 +1,10 @@
package com.glxp.api.admin.controller.basic;
import cn.hutool.core.io.IoUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.glxp.api.admin.constant.BasicProcessStatus;
import com.glxp.api.admin.entity.basic.BasicThirdSysEntity;
import com.glxp.api.admin.entity.basic.CorpImportDetailEntity;
import com.glxp.api.admin.entity.basic.CorpImportLogEntity;
import com.glxp.api.admin.entity.basic.*;
import com.glxp.api.admin.req.basic.FilterUdiIpLogRequest;
import com.glxp.api.admin.req.basic.PostCorpsRequest;
import com.glxp.api.admin.req.inout.DeleteRequest;
@ -17,21 +17,19 @@ import com.glxp.api.admin.util.CustomUtil;
import com.glxp.api.common.enums.ResultEnum;
import com.glxp.api.common.res.BaseResponse;
import com.glxp.api.common.util.ResultVOUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Slf4j
@RestController
public class CorpImportLogController {
@Resource
@ -113,8 +111,6 @@ public class CorpImportLogController {
//往来单位信息文件导入
@PostMapping("/udiwms/corps/importLog/upload")
public BaseResponse uploadProducts(@RequestParam("file") List<MultipartFile> files, @RequestParam("thirdSys") String thirdSys) {
BasicThirdSysEntity basicThirdSysEntity = basicThirdSysService.selectMainThrSys();
String filePath = "D:\\udiwms\\filePath\\";
File createFile = new File(filePath);
if (!createFile.exists()) {
@ -126,207 +122,30 @@ public class CorpImportLogController {
return ResultVOUtils.error(500, "上传第" + (i++) + "个文件失败");
}
String fileName = file.getOriginalFilename();
//解析数据
try {
InputStream inputStream = file.getInputStream();
Workbook workbook = null;
String filetype = fileName.substring(fileName.lastIndexOf("."));
if (".xls".equals(filetype)) {
workbook = new HSSFWorkbook(inputStream);
} else if (".xlsx".equals(filetype)) {
workbook = new XSSFWorkbook(inputStream);
String fileType = fileName.substring(fileName.lastIndexOf("."));
if (".json".equals(fileType.toLowerCase())) {
//FileUtil.readString(file.getInputStream(), Charset.forName("UTF-8"));
String jsonData = IoUtil.read(file.getInputStream()).toString();
List<BasicUnitMaintainEntity> basicUnitMaintainEntities = JSONUtil.toList(jsonData, BasicUnitMaintainEntity.class);
//导入日志
CorpImportLogEntity corpImportLogEntity = new CorpImportLogEntity();
String genKey = CustomUtil.getId();
corpImportLogEntity.setGenKey(genKey);
corpImportLogEntity.setFromType("文件导入");
corpImportLogEntity.setStatus(BasicProcessStatus.UDIINFO_IMPORT_UNPROCESS);
corpImportLogEntity.setUpdateTime(new Date(System.currentTimeMillis()));
corpImportLogService.insertImportLog(corpImportLogEntity);
basicCorpImportService.importJsonData(basicUnitMaintainEntities);
corpImportLogEntity.setStatus(BasicProcessStatus.UDIINFO_IMPORT_SUCCESS);
corpImportLogService.updateImportLog(corpImportLogEntity);
} else {
return ResultVOUtils.error(500, "请上传excel文件");
}
Sheet sheet = null;
Row row = null;
CorpImportLogEntity corpImportLogEntity = new CorpImportLogEntity();
String genKey = CustomUtil.getId();
corpImportLogEntity.setGenKey(genKey);
corpImportLogEntity.setFromType("文件导入");
corpImportLogEntity.setStatus(BasicProcessStatus.UDIINFO_IMPORT_UNPROCESS);
corpImportLogEntity.setUpdateTime(new Date(System.currentTimeMillis()));
corpImportLogService.insertImportLog(corpImportLogEntity);
List<CorpImportDetailEntity> corpImportDetailEntities = new ArrayList<>();
for (int j = 0; j < 1; j++) {
sheet = workbook.getSheetAt(j);
if (sheet == null) {
continue;
}
// 滤过第一行标题
row = sheet.getRow(0);
if ((row.getCell(0) == null || row.getCell(1) == null || row.getCell(2) == null
|| row.getCell(3) == null || row.getCell(4) == null || row.getCell(5) == null|| row.getCell(6) == null) ||
(!"往来单位ID".equals(row.getCell(0).getStringCellValue())
|| !"往来单位名称".equals(row.getCell(1).getStringCellValue())
|| !"往来单位拼音简写".equals(row.getCell(2).getStringCellValue())
|| !"地址".equals(row.getCell(3).getStringCellValue())
|| !"联系人".equals(row.getCell(4).getStringCellValue())
|| !"联系电话".equals(row.getCell(5).getStringCellValue())
|| !"社会信用号".equals(row.getCell(6).getStringCellValue())
|| !"往来单位状态".equals(row.getCell(7).getStringCellValue())
|| !"往来单位类型".equals(row.getCell(8).getStringCellValue())
|| !"thirdId".equals(row.getCell(9).getStringCellValue()))) {
return ResultVOUtils.error(500, "文件格式错误!");
}
for (int k = sheet.getFirstRowNum()+1; k <= sheet.getLastRowNum(); k++) {
row = sheet.getRow(k);
if (row == null || row.getRowNum() <= 0) {
continue;
}
if (filterEmpty(row)) {
continue;
}
CorpImportDetailEntity corpImportDetailEntity = new CorpImportDetailEntity();
Cell cell1 = row.getCell(0);
if (cell1 != null) {
cell1.setCellType(CellType.STRING);
corpImportDetailEntity.setErpId(cell1.getStringCellValue());
}
Cell cell2 = row.getCell(1);
if (cell2 != null) {
cell2.setCellType(CellType.STRING);
corpImportDetailEntity.setName(cell2.getStringCellValue());
}
Cell cell3 = row.getCell(2);
if (cell3 != null) {
cell3.setCellType(CellType.STRING);
corpImportDetailEntity.setSpell(cell3.getStringCellValue());
}
Cell cell4 = row.getCell(3);
if (cell4 != null) {
cell4.setCellType(CellType.STRING);
corpImportDetailEntity.setAddr(cell4.getStringCellValue());
}
Cell cell5 = row.getCell(4);
if (cell5 != null) {
cell5.setCellType(CellType.STRING);
corpImportDetailEntity.setContact(cell5.getStringCellValue());
}
Cell cell6 = row.getCell(5);
if (cell6 != null) {
cell6.setCellType(CellType.STRING);
corpImportDetailEntity.setMobile(cell6.getStringCellValue());
}
Cell cell7 = row.getCell(6);
if (cell7 != null) {
cell7.setCellType(CellType.STRING);
corpImportDetailEntity.setCreditNo(cell7.getStringCellValue());
}
Cell cell8 = row.getCell(7);
if (cell8 != null) {
cell8.setCellType(CellType.STRING);
corpImportDetailEntity.setStatus(cell8.getStringCellValue());
}
Cell cell9 = row.getCell(8);
if (cell9 != null) {
cell9.setCellType(CellType.STRING);
corpImportDetailEntity.setType(cell9.getStringCellValue());
}
Cell cell10 = row.getCell(9);
if (cell10 != null) {
cell10.setCellType(CellType.STRING);
corpImportDetailEntity.setThirdId(cell10.getStringCellValue());
}
Cell cell11 = row.getCell(10);
if (cell11 != null) {
cell11.setCellType(CellType.STRING);
corpImportDetailEntity.setThirdName(cell11.getStringCellValue());
}
Cell cell12 = row.getCell(11);
if (cell12 != null) {
cell12.setCellType(CellType.STRING);
corpImportDetailEntity.setThirdId1(cell12.getStringCellValue());
}
Cell cell13 = row.getCell(12);
if (cell13 != null) {
cell13.setCellType(CellType.STRING);
corpImportDetailEntity.setThirdName1(cell13.getStringCellValue());
}
Cell cell14 = row.getCell(13);
if (cell14 != null) {
cell14.setCellType(CellType.STRING);
corpImportDetailEntity.setThirdId2(cell14.getStringCellValue());
}
Cell cell15 = row.getCell(14);
if (cell15 != null) {
cell15.setCellType(CellType.STRING);
corpImportDetailEntity.setThirdName2(cell15.getStringCellValue());
}
Cell cell16 = row.getCell(15);
if (cell16 != null) {
cell16.setCellType(CellType.STRING);
corpImportDetailEntity.setThirdId3(cell16.getStringCellValue());
}
Cell cell17 = row.getCell(16);
if (cell17 != null) {
cell17.setCellType(CellType.STRING);
corpImportDetailEntity.setThirdName3(cell17.getStringCellValue());
}
Cell cell18 = row.getCell(17);
if (cell18 != null) {
cell18.setCellType(CellType.STRING);
corpImportDetailEntity.setThirdId4(cell18.getStringCellValue());
}
Cell cell19 = row.getCell(18);
if (cell19 != null) {
cell19.setCellType(CellType.STRING);
corpImportDetailEntity.setThirdName4(cell19.getStringCellValue());
}
if (basicThirdSysEntity != null) {
if ("thirdId".equals(basicThirdSysEntity.getThirdId())) {
corpImportDetailEntity.setThirdId(corpImportDetailEntity.getErpId());
corpImportDetailEntity.setThirdName(corpImportDetailEntity.getName());
} else if ("thirdId1".equals(basicThirdSysEntity.getThirdId())) {
corpImportDetailEntity.setThirdId1(corpImportDetailEntity.getErpId());
corpImportDetailEntity.setThirdName1(corpImportDetailEntity.getName());
} else if ("thirdId2".equals(basicThirdSysEntity.getThirdId())) {
corpImportDetailEntity.setThirdId2(corpImportDetailEntity.getErpId());
corpImportDetailEntity.setThirdName2(corpImportDetailEntity.getName());
} else if ("thirdId3".equals(basicThirdSysEntity.getThirdId())) {
corpImportDetailEntity.setThirdId3(corpImportDetailEntity.getErpId());
corpImportDetailEntity.setThirdName3(corpImportDetailEntity.getName());
} else if ("thirdId4".equals(basicThirdSysEntity.getThirdId())) {
corpImportDetailEntity.setThirdId4(corpImportDetailEntity.getErpId());
corpImportDetailEntity.setThirdName4(corpImportDetailEntity.getName());
}
} else {
corpImportDetailEntity.setThirdId(corpImportDetailEntity.getErpId());
corpImportDetailEntity.setThirdName(corpImportDetailEntity.getName());
}
corpImportDetailEntity.setGenKeyFk(genKey);
corpImportDetailEntity.setImportStatus(BasicProcessStatus.UDIINFO_IMPORT_CODE_UNPROCESS);
corpImportDetailEntity.setUpdateTime(new Date(System.currentTimeMillis()));
corpImportDetailEntities.add(corpImportDetailEntity);
}
return ResultVOUtils.error(500, "请上传json文件");
}
corpImportDetailService.insertCorpImports(corpImportDetailEntities);
corpImportLogEntity.setStatus(BasicProcessStatus.UDIINFO_IMPORT_UNPROCESS);
corpImportLogService.updateImportLog(corpImportLogEntity);
workbook.close();
basicCorpImportService.importCorps(genKey);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
log.error("导入往来单位信息错误,文件名为:{}", fileName);
}
}
return ResultVOUtils.success("上传成功");

@ -1,6 +1,8 @@
package com.glxp.api.admin.thread;
import cn.hutool.core.collection.CollUtil;
import com.glxp.api.admin.constant.BasicProcessStatus;
import com.glxp.api.admin.dao.basic.BasicUnitMaintainDao;
import com.glxp.api.admin.entity.basic.BasicUnitMaintainEntity;
import com.glxp.api.admin.entity.basic.CorpImportDetailEntity;
import com.glxp.api.admin.entity.basic.CorpImportLogEntity;
@ -9,6 +11,11 @@ import com.glxp.api.admin.req.basic.FilterUdiIpLogRequest;
import com.glxp.api.admin.service.basic.BasicUnitMaintainService;
import com.glxp.api.admin.service.basic.CorpImportDetailService;
import com.glxp.api.admin.service.basic.CorpImportLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.TransactionIsolationLevel;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@ -16,6 +23,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Slf4j
@Service
public class BasicCorpImportService {
@ -25,6 +33,8 @@ public class BasicCorpImportService {
CorpImportDetailService corpImportDetailService;
@Resource
BasicUnitMaintainService basicUnitMaintainService;
@Resource
SqlSessionFactory sqlSessionFactory;
@Async
public void importCorps(String genKey) {
@ -60,4 +70,24 @@ public class BasicCorpImportService {
}
/**
*
*
* @param basicUnitMaintainEntities
*/
public void importJsonData(List<BasicUnitMaintainEntity> basicUnitMaintainEntities) {
if (CollUtil.isNotEmpty(basicUnitMaintainEntities)) {
try {
SqlSession batchSession = sqlSessionFactory.openSession(ExecutorType.BATCH, TransactionIsolationLevel.READ_COMMITTED);
BasicUnitMaintainDao mapper = batchSession.getMapper(BasicUnitMaintainDao.class);
for (BasicUnitMaintainEntity basicUnitMaintainEntity : basicUnitMaintainEntities) {
mapper.updateById(basicUnitMaintainEntity);
}
batchSession.commit();
log.info("导入数据成功,导入数量为:{} 条", basicUnitMaintainEntities.size());
} catch (Exception e) {
log.error("导入往来单位信息异常", e);
}
}
}
}

@ -0,0 +1,51 @@
package com.glxp.api.admin.thread;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.json.JSONUtil;
import com.glxp.api.admin.constant.BasicProcessStatus;
import com.glxp.api.admin.entity.basic.BasicUnitMaintainEntity;
import com.glxp.api.admin.entity.basic.CorpExportLogEntity;
import com.glxp.api.admin.req.basic.BasicUnitMaintainFilterRequest;
import com.glxp.api.admin.req.basic.CorpsExportRequest;
import com.glxp.api.admin.service.basic.BasicUnitMaintainService;
import com.glxp.api.admin.service.basic.CorpExportLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class BasicGenJsonService {
@Resource
CorpExportLogService corpExportLogService;
@Resource
BasicUnitMaintainService basicUnitMaintainService;
public void exportCorp(String genKey, CorpsExportRequest corpsExportRequest) {
CorpExportLogEntity corpExportLogEntity = corpExportLogService.selectByGenKey(genKey);
List<BasicUnitMaintainEntity> exportData = new ArrayList<>();
if (corpsExportRequest.getIds() != null && corpsExportRequest.getIds().size() > 0) {
List<Integer> ids = corpsExportRequest.getIds();
ids.forEach(id -> {
BasicUnitMaintainFilterRequest request = new BasicUnitMaintainFilterRequest();
request.setId(id);
List<BasicUnitMaintainEntity> basicUnitMaintainEntities = basicUnitMaintainService.filterList(request);
exportData.addAll(basicUnitMaintainEntities);
});
} else { //一键导出
BasicUnitMaintainFilterRequest filterRequest = new BasicUnitMaintainFilterRequest();
BeanUtils.copyProperties(corpsExportRequest, filterRequest);
filterRequest.setPage(null);
List<BasicUnitMaintainEntity> basicUnitMaintainEntities = basicUnitMaintainService.filterList(filterRequest);
exportData.addAll(basicUnitMaintainEntities);
}
FileWriter writer = new FileWriter(corpExportLogEntity.getFilePath());
writer.write(JSONUtil.toJsonStr(exportData));
corpExportLogEntity.setStatus(BasicProcessStatus.UDIINFO_IMPORT_SUCCESS);
corpExportLogService.updateCorpExportLog(corpExportLogEntity);
}
}
Loading…
Cancel
Save