/**
* 导入数据 先将文件上传到服务器,在开启线程进行执行插入
*/
@RequestMapping(value = "/newUpload", method = { RequestMethod.POST })
@Transactional
public @ResponseBody JSONObject newUpload(@RequestParam(value = "file", required = false) MultipartFile file,HttpServletResponse response, HttpServletRequest request) {
JSONObject json = new JSONObject();
String type = file.getOriginalFilename();
File filePath = null;
try {
String filename = file.getOriginalFilename();
String uuid = UUID.randomUUID().toString();
filePath = new File("C:/qzqdCompareDate");
if (!filePath.exists()) {
filePath.mkdir();
}
String desPath = filePath + File.separator + uuid;
File savefile = new File(desPath);
file.transferTo(savefile);
Compare compare=new Compare();
compare.setTaskName(filename);
compare.setInsTime(new Date());
compare.setRecId(UuidOpt.getUuidAsString32());
compare.setStates("0");//正在查询
compareService.mergeObject(compare);//生成任务
String recId=compare.getRecId();
//使用多线程技术,提高程序的效率 有一个客户端上传文件,就开启一个线程,完成文件的上传
new Thread(new Runnable() {//
@SneakyThrows
@Override
public void run() {
List<String[]> xlsList = FileUtil.getXlsList(desPath);
//根据模板第二行隐藏字段取到对应匹配字段
String[] xlsTrFill = new String[2];
Compare compareDb=compareService.getObjectById(recId);
if(xlsList.size()>0){
for (int i = 0; i < xlsList.size(); i++) {
String[] xlsTr1 = (String[]) xlsList.get(i);
if(0==i){//获取姓名、性别、出生日期
continue;
}else{
String baseCode=xlsTr1[0];
String itemName1=xlsTr1[1];
Map<String, Object> searchColumn=new HashMap<String, Object>();
searchColumn.put("itemName", itemName1);
//查询数据库中的相似名称的权力事项数据
JSONArray jsonArrayObjects = compareService.queryAllItemByitemName( searchColumn, null);
if(jsonArrayObjects.size()>0){
for(int k=0;k<jsonArrayObjects.size();k++){
CompareInfo compareInfo=new CompareInfo();
compareInfo.setRecId(UuidOpt.getUuidAsString32());
compareInfo.setTaskId(compareDb.getRecId());
compareInfo.setItemName1(itemName1);//Excel导入的事项名称
compareInfo.setItemName2(String.valueOf(jsonArrayObjects.getJSONObject(k).get("itemName")));///数据库中查询出来的事项名称
compareInfo.setBaseCode(String.valueOf(jsonArrayObjects.getJSONObject(k).get("baseCode")));
compareInfoService.mergeObject(compareInfo);
}
}else{//如果没有查询到数据,则把当前这个事项导入到数据库中
CompareInfo compareInfo=new CompareInfo();
compareInfo.setRecId(UuidOpt.getUuidAsString32());
compareInfo.setTaskId(compareDb.getRecId());
compareInfo.setItemName1(itemName1);//Excel导入的事项名称
compareInfoService.mergeObject(compareInfo);
}
}
}
compareDb.setEndTime(new Date());
compareDb.setStates("1");
compareService.mergeObject(compareDb);//生成任务
}else{
json.put("msg", "导入失败!");
json.put("code", "500");
}
}
}).start();
} catch (Exception e1) {
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
json.put("msg", "导入失败!");
json.put("code", "500");
e1.printStackTrace();
}
json.put("msg", "导入成功!");
json.put("code", "0");
return json;
}
/**
* 获取服务器上的文件
*/
public static List<String[]> getXlsList(String desPath) {
List<String[]> xlsList = new ArrayList<String[]>();
try {
//根据绝对路径获取服务器文件
File cfile = new File(desPath);
// 从excel中读取数据
InputStream is = new FileInputStream(cfile);
//这里用BufferedInputStream再包装一层,可解决:mark/reset not supported问题
BufferedInputStream bis = new BufferedInputStream(is);
ExcelTypeEnum excelType = null;
if (POIFSFileSystem.hasPOIFSHeader(bis)) {
//2003及以下
excelType = ExcelTypeEnum.HSSF;
}
if (POIXMLDocument.hasOOXMLHeader(bis)) {
//2007及以上
excelType = ExcelTypeEnum.XSSF;
}
if (excelType == null) {
throw new IllegalArgumentException("请使用2007及以上版本或2003以下excel版本");
}
xlsList = ExcelUtil.loadDataFromExcel(bis, excelType, 0, 0, 20, 0, 5000);
} catch (IOException e) {
e.printStackTrace();
}
return xlsList;
}