package com.diagbot.facade; import java.io.InputStream; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.time.DateFormatUtils; import org.apache.poi.hssf.usermodel.HSSFDateUtil; 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.Qualifier; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.diagbot.dto.DiagnoseImportDTO; import com.diagbot.dto.DiagnosticAllExportDTO; import com.diagbot.dto.DiagnosticBasisExportDTO; import com.diagbot.dto.EquationDTO; import com.diagbot.dto.FeatureDTO; import com.diagbot.dto.RespDTO; import com.diagbot.entity.DiagnoseDetail; import com.diagbot.entity.DiagnoseQuestion; import com.diagbot.enums.DiagnoseFeatureTypeEnum; import com.diagbot.enums.DiagnoseTypeEnum; import com.diagbot.enums.IsDeleteEnum; import com.diagbot.enums.TermEnum; import com.diagbot.exception.CommonErrorCode; import com.diagbot.exception.CommonException; import com.diagbot.service.DiagnoseDetailService; import com.diagbot.service.DiagnoseQuestionService; import com.diagbot.util.ExportBeanExcelUtil; import com.diagbot.util.GsonUtil; import com.diagbot.util.StringUtil; import com.diagbot.vo.DiagnosticAllExportVO; import com.diagbot.vo.DiagnosticBasisExportVO; /** * @author wangfeng * @Description: TODO * @date 2019年8月8日 下午1:30:53 */ @Component public class DiagnoseImportFacade { @Autowired DiagnoseQuestionService diagnoseQuestionService; @Autowired @Qualifier("diagnoseDetailServiceImpl") DiagnoseDetailService diagnoseDetailService; @SuppressWarnings("null") public RespDTO importDiagnosticBasisAll(MultipartFile file, HttpServletRequest request) { List messages = new ArrayList<>(); InputStream inputStream = null; DiagnoseImportDTO diagnoseImportDTO = new DiagnoseImportDTO(); List featureList = new ArrayList(); List equationList = new ArrayList(); List assemblys = new ArrayList(); Workbook wb = null; try { if (!file.isEmpty()) { inputStream = file.getInputStream(); if (inputStream.available() > 512000) { messages.add("化验文件最大支持500KB!"); } else { String fileName = file.getOriginalFilename(); if (fileName.lastIndexOf(".") != -1) { String type = fileName.substring(fileName.lastIndexOf(".")); if (type.equals(".xls")) { wb = new HSSFWorkbook(inputStream); } else if (type.equals(".xlsx")) { wb = new XSSFWorkbook(inputStream); } if (wb != null) { Sheet sheet = wb.getSheetAt(0); diagnoseImportDTO.setDisName(sheet.getSheetName()); int count = 0; String name, code, standardWord, associatedWord, consequence; for (Row row : sheet) { count++; try { if (row != null) { name = getValue(row.getCell(0)).trim().replace(" ", ""); code = getValue(row.getCell(1)).trim().replace(" ", ""); standardWord = getValue(row.getCell(2)).trim(); associatedWord = getValue(row.getCell(3)).trim(); consequence = getValue(row.getCell(4)).trim(); } else { name = null; code = null; standardWord = null; associatedWord = null; consequence = null; } if (count == 1 && name.equals("类型")) { continue; } if (StringUtil.isEmpty(name)) { messages.add("第" + count + "行数据不规范,类型必填;"); continue; } FeatureDTO feature = new FeatureDTO(); EquationDTO equation = new EquationDTO(); if (code.equals("")) { equation.setType(name); equation.setAssociated(associatedWord); equationList.add(equation); } else { feature.setType(name); feature.setCode(code); feature.setResult(consequence); feature.setAssociated(associatedWord); feature.setStandard(standardWord); featureList.add(feature); } } catch (Exception e) { e.printStackTrace(); throw new CommonException(CommonErrorCode.NOT_EXISTS, e.toString()); // logger.error("",e); } } } else { // throw new // CommonException(CommonErrorCode.NOT_EXISTS, // "非excel文件无法解析!"); messages.add("非excel文件无法解析!"); } } else { // throw new CommonException(CommonErrorCode.NOT_EXISTS, // "未知文件无法解析!"); messages.add("未知文件无法解析!"); } } } else { // throw new CommonException(CommonErrorCode.NOT_EXISTS, // "无文件上传!"); messages.add("无文件上传!"); } diagnoseImportDTO.setEquation(equationList); diagnoseImportDTO.setFeature(featureList); diagnoseImportDTO.setMessages(messages); return RespDTO.onSuc(diagnoseImportDTO); } catch (Exception e) { e.printStackTrace(); throw new CommonException(CommonErrorCode.NOT_EXISTS, "化验excel文件解析出错!"); // return response.failure("化验excel文件解析出错!"); } finally { try { if (inputStream != null) { inputStream.close(); } if (wb != null) { wb.close(); } } catch (Exception e) { } } } private String getValue(Cell cell) { try { Object obj = null; switch (cell.getCellTypeEnum()) { case BOOLEAN: obj = cell.getBooleanCellValue(); break; case ERROR: obj = cell.getErrorCellValue(); break; case NUMERIC: if (HSSFDateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); obj = DateFormatUtils.format(date, "yyyy-MM-dd"); } else { obj = cell.getNumericCellValue(); DecimalFormat df = new DecimalFormat(); obj = df.format(obj); } // obj = cell.getNumericCellValue(); break; case STRING: obj = cell.getStringCellValue(); break; default: break; } return obj.toString(); } catch (Exception e) { return ""; } } public void exportDiagnosticBasis(DiagnosticBasisExportVO diaExportVO, HttpServletResponse response) { QueryWrapper diaQuery = new QueryWrapper<>(); diaQuery.like("dis_name", diaExportVO.getDisName()).like("question_name", diaExportVO.getQuestion()) .like("type", diaExportVO.getType()).eq("is_deleted", IsDeleteEnum.N.getKey()).orderByDesc("gmt_modified"); List datas = diagnoseQuestionService.list(diaQuery); GsonUtil.toJson(datas); System.out.println(GsonUtil.toJson(datas)); List listName = new ArrayList<>(); listName.add("归属诊断"); listName.add("标准术语*"); listName.add("类型*"); listName.add("术语同义词*(多个时用\",\"隔开,必须需要包含本体)"); listName.add("标准术语说明"); listName.add("性别"); listName.add("最小年龄"); listName.add("最大年龄"); List listId = new ArrayList<>(); listId.add("disName"); listId.add("term"); listId.add("type"); listId.add("termSynonym"); listId.add("TermNote"); listId.add("sex"); listId.add("minAge"); listId.add("maxAge"); List list = new ArrayList<>(); for (int i = 0; i < datas.size(); i++) { DiagnoseQuestion diagnose = datas.get(i); list.add(new DiagnosticBasisExportDTO(diagnose.getDisName(), diagnose.getQuestionName(), getType(diagnose.getType(), diagnose.getQuestionType()), diagnose.getQuestionName(), "", "通用", "0", "200")); } response.setContentType("text/html;charset=UTF-8"); ExportBeanExcelUtil ex = new ExportBeanExcelUtil(); ex.exportExcelNew("诊断依据问题词", listName, listId, list, response); } /** * @param diaAllExportVO * @param response */ public void exportDiagnosticAll(DiagnosticAllExportVO diaAllExportVO, HttpServletResponse response) { QueryWrapper diaQuery = new QueryWrapper<>(); diaQuery.eq("diagnose_id", diaAllExportVO.getDiagnoseId()) .eq("is_deleted", IsDeleteEnum.N.getKey()); List datas = diagnoseDetailService.list(diaQuery); System.out.println(GsonUtil.toJson(datas)); List listName = new ArrayList<>(); listName.add("类型"); listName.add("序号"); listName.add("标准词*"); listName.add("是否需要病史采集"); listName.add("关联词"); listName.add("结果"); List listId = new ArrayList<>(); listId.add("type"); listId.add("code"); listId.add("standard"); listId.add("historyTaking"); listId.add("relation"); listId.add("result"); String disName = (datas.size() > 0) ? datas.get(0).getDisName() : " "; List list = new ArrayList<>(); for (int i = 0; i < datas.size(); i++) { DiagnoseDetail diagnose = datas.get(i); disName = diagnose.getDisName(); list.add(new DiagnosticAllExportDTO(getDisType(diagnose.getType()), diagnose.getCode(), diagnose.getStandard(), "", getRelation(diagnose.getType(), diagnose), diagnose.getResult())); } response.setContentType("text/html;charset=UTF-8"); ExportBeanExcelUtil ex = new ExportBeanExcelUtil(); ex.exportExcelNew(disName, listName, listId, list, response); } /** * 导出类型转换 * * @param type * @param questionType * @return */ public String getType(Integer type, Integer questionType) { String typeName = ""; if (type == DiagnoseFeatureTypeEnum.Dis.getKey()) { typeName = TermEnum.getName(0); } else if (type == DiagnoseFeatureTypeEnum.Symptom.getKey()) { typeName = TermEnum.getName(1); } else if (type == DiagnoseFeatureTypeEnum.Vital.getKey()) { typeName = TermEnum.getName(2); } else if (type == DiagnoseFeatureTypeEnum.Lis.getKey()) { if (questionType == 2) { typeName = TermEnum.getName(3); } if (questionType == 3) { typeName = TermEnum.getName(4); } if (questionType == 4) { typeName = TermEnum.getName(5); } } else if (type == DiagnoseFeatureTypeEnum.Pacs.getKey()) { if (questionType == 2) { typeName = TermEnum.getName(6); } if (questionType == 3) { typeName = TermEnum.getName(7); } } else if (type == DiagnoseFeatureTypeEnum.Antidiastole.getKey()) { typeName = TermEnum.getName(8); } else if (type == DiagnoseFeatureTypeEnum.History.getKey()) { typeName = TermEnum.getName(8); } else if (type == DiagnoseFeatureTypeEnum.Cause.getKey()) { typeName = TermEnum.getName(9); } else if (type == DiagnoseFeatureTypeEnum.CourseOfDisease.getKey()) { typeName = TermEnum.getName(10); } else if (type == DiagnoseFeatureTypeEnum.Other.getKey()) { typeName = TermEnum.getName(10); } return typeName; } /** * 导出公式类型转换 */ public String getDisType(Integer type) { String typeName = ""; typeName = DiagnoseFeatureTypeEnum.getName(type); if (typeName == null) { typeName = DiagnoseTypeEnum.getName(type); } return typeName; } /** * 导出公式和标准词切换 */ public String getRelation(Integer type, DiagnoseDetail diagnose) { String typeName = ""; String relation = ""; typeName = DiagnoseFeatureTypeEnum.getName(type); relation = diagnose.getRelation(); if (typeName == null) { relation = diagnose.getFormula(); } return relation; } }