直接上代码
/**
* 药品映射excel导入
*
* @param file
* @param title
* @return
*/
@SuppressWarnings("resource")
public String medicineExcelUpLoad(MultipartFile file, final String[] title, final String orgId) {
final SysUserDto sysUser = JwtUtil.getCurrentSysUser();
Map<String, Object> map = new HashMap<>();
InputStream inputStream = null;
Sheet sht0 = null;
try {
sds.deleteMedicine(orgId);
inputStream = file.getInputStream();
Workbook wb0 = new XSSFWorkbook(inputStream);
sht0 = wb0.getSheetAt(0);
} catch (IOException e2) {
e2.printStackTrace();
}
int totalCount = sht0.getLastRowNum();
int failerCount = 0;
final CountDownLatch totalCountLatch = new CountDownLatch(totalCount);// 总数
try {
for (final Row r : sht0) {
if (r.getRowNum() < 1) {
continue;
}
Future<Integer> future = es.submit(new MedicineThread(sysUser, totalCountLatch, orgId, r, title));
Integer errCount = future.get();
failerCount += errCount;
}
totalCountLatch.await();
long successCount = totalCount - failerCount;
map.put("successCount", successCount);
map.put("failerCount", failerCount);
} catch (Exception e) {
logger.error(e.getMessage());
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
return JSONObject.toJSONString(map);
}
class MedicineThread implements Callable<Integer> {
private SysUserDto sysUser;
private CountDownLatch totalCountLatch;
private String orgId;
private Row r;
private String[] title;
public MedicineThread(SysUserDto sysUser, CountDownLatch totalCountLatch, String orgId, Row r, String[] title) {
this.sysUser = sysUser;
this.totalCountLatch = totalCountLatch;
this.orgId = orgId;
this.r = r;
this.title = title;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Integer call() throws Exception {
Integer errorCount = 0;
Map mp = new HashMap<String, String>();
for (int j = 0; j < title.length; j++) {
Cell hc = r.getCell(j);
if (hc != null) {
hc.setCellType(CellType.STRING);
mp.put(title[j], hc.getStringCellValue());
}
}
MedMedicineMapping medMedicineMapping = null;
MedMedicineMappingId medMedicineMappingId = null;
try {
medMedicineMapping = (MedMedicineMapping) MapUtil.convertMap(MedMedicineMapping.class, mp);
medMedicineMappingId = (MedMedicineMappingId) MapUtil.convertMap(MedMedicineMappingId.class, mp);
medMedicineMappingId.setOrgId(orgId);
medMedicineMapping.setId(medMedicineMappingId);
medMedicineMappingDao.save(medMedicineMapping);
// 导入Excel,保存审核记录
saveAudit(sysUser, orgId, "MED_MEDICINE_MAPPING", medMedicineMapping.getId().getCode(), "0");
} catch (Exception e) {
e.printStackTrace();
try {
MedMedicineMappingErr medMedicineMappingErr = (MedMedicineMappingErr) MapUtil.convertMap(MedMedicineMappingErr.class, mp);
medMedicineMappingErr.setOrgId(medMedicineMapping.getId().getOrgId());
medMedicineMappingErr.setCode(medMedicineMapping.getId().getCode());
medMedicineMappingErr.setCollectNo(medMedicineMapping.getCollectNo());
medMedicineMappingErr.setName(medMedicineMapping.getName());
medMedicineMappingErr.setType(medMedicineMapping.getType());
medMedicineMappingErr.setTypeCode(medMedicineMapping.getTypeCode());
medMedicineMappingErr.setPharmType(medMedicineMapping.getPharmType());
medMedicineMappingErr.setPharmTypeCode(medMedicineMapping.getPharmTypeCode());
medMedicineMappingErr.setGeneralNameCode(medMedicineMapping.getGeneralNameCode());
medMedicineMappingErr.setFormName(medMedicineMapping.getFormName());
medMedicineMappingErr.setFormCode(medMedicineMapping.getFormCode());
medMedicineMappingErr.setSpec(medMedicineMapping.getSpec());
medMedicineMappingErr.setVerndor(medMedicineMapping.getVerndor());
medMedicineMappingErr.setVerndorCode(medMedicineMapping.getId().getVerndorCode());
medMedicineMappingErr.setApprovalNumber(medMedicineMapping.getApprovalNumber());
medMedicineMappingErr.setId(com.gohealth.ums.util.CommonUtils.get32UUID());
medMedicineMappingErrDao.save(medMedicineMappingErr);
} catch (Exception e1) {
logger.error(e1.getMessage(), e1);
e1.printStackTrace();
}
errorCount++;
} finally {
totalCountLatch.countDown();
}
return errorCount;
}
}