LisExcelResFacade.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package com.diagbot.facade;
  2. import java.io.InputStream;
  3. import java.text.DecimalFormat;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.stream.Collectors;
  9. import javax.servlet.http.HttpServletRequest;
  10. import org.apache.commons.lang.time.DateFormatUtils;
  11. import org.apache.poi.hssf.usermodel.HSSFDateUtil;
  12. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  13. import org.apache.poi.ss.usermodel.Cell;
  14. import org.apache.poi.ss.usermodel.Row;
  15. import org.apache.poi.ss.usermodel.Sheet;
  16. import org.apache.poi.ss.usermodel.Workbook;
  17. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  18. import org.springframework.stereotype.Component;
  19. import org.springframework.web.multipart.MultipartFile;
  20. import com.diagbot.dto.RespDTO;
  21. import com.diagbot.exception.CommonErrorCode;
  22. import com.diagbot.exception.CommonException;
  23. import com.diagbot.util.StringUtil;
  24. import com.diagbot.vo.LisExcelResVO;
  25. import com.diagbot.vo.LisExcelWrapperVO;
  26. import com.diagbot.vo.LitAssayVO;
  27. /**
  28. *
  29. * @author wangfeng
  30. * @Description: 化验导入
  31. * @date 2018年11月29日 上午10:16:35
  32. */
  33. @Component
  34. public class LisExcelResFacade {
  35. public RespDTO<LitAssayVO> lisExcelAnalysis(MultipartFile file, HttpServletRequest request) {
  36. List<String> messages = new ArrayList<>();
  37. List<LisExcelWrapperVO> lisExcelWrapperList = new ArrayList<>();
  38. InputStream inputStream = null;
  39. Workbook wb = null;
  40. try {
  41. if (!file.isEmpty()) {
  42. inputStream = file.getInputStream();
  43. if (inputStream.available() > 512000) {
  44. messages.add("化验文件最大支持500KB!");
  45. } else {
  46. String fileName = file.getOriginalFilename();
  47. if (fileName.lastIndexOf(".") != -1) {
  48. String type = fileName.substring(fileName.lastIndexOf("."));
  49. if (type.equals(".xls")) {
  50. wb = new HSSFWorkbook(inputStream);
  51. } else if (type.equals(".xlsx")) {
  52. wb = new XSSFWorkbook(inputStream);
  53. }
  54. if (wb != null) {
  55. Sheet sheet = wb.getSheetAt(0);
  56. int count = 0;
  57. String mealName, itemName, unit, value, max, min, time;
  58. for (Row row : sheet) {
  59. count++;
  60. try {
  61. if (row != null) {
  62. mealName = getValue(row.getCell(0)).trim().replace(" ", "");
  63. itemName = getValue(row.getCell(1)).trim().replace(" ", "");
  64. unit = getValue(row.getCell(2)).trim();
  65. value = getValue(row.getCell(3)).trim();
  66. max = getValue(row.getCell(4)).trim();
  67. min = getValue(row.getCell(5)).trim();
  68. time = getValue(row.getCell(6)).trim();
  69. type = getValue(row.getCell(7)).trim();
  70. } else {
  71. mealName = null;
  72. itemName = null;
  73. unit = null;
  74. value = null;
  75. max = null;
  76. min = null;
  77. time = null;
  78. type = null;
  79. }
  80. if (StringUtil.isEmpty(mealName) && StringUtil.isEmpty(itemName)
  81. && StringUtil.isEmpty(value)) {
  82. continue;
  83. }
  84. if (count == 1 && mealName.equals("套餐名")) {
  85. continue;
  86. }
  87. if (StringUtil.isEmpty(mealName) || StringUtil.isEmpty(itemName)
  88. || StringUtil.isEmpty(value)) {
  89. // throw new
  90. // CommonException(CommonErrorCode.NOT_EXISTS,
  91. // "第"+count+"行数据不规范,套餐名、项目名、结果三项必填;");
  92. messages.add("第" + count + "行数据不规范,套餐名、项目名、结果三项必填;");
  93. continue;
  94. }
  95. LisExcelWrapperVO lisExcelWrapper = new LisExcelWrapperVO();
  96. lisExcelWrapper.setMealName(mealName);
  97. lisExcelWrapper.setItemName(itemName);
  98. lisExcelWrapper.setUnit(unit);
  99. lisExcelWrapper.setValue(value);
  100. lisExcelWrapper.setMax(max);
  101. lisExcelWrapper.setMin(min);
  102. lisExcelWrapper.setTime(time);
  103. lisExcelWrapper.setType(type);
  104. lisExcelWrapperList.add(lisExcelWrapper);
  105. } catch (Exception e) {
  106. e.printStackTrace();
  107. throw new CommonException(CommonErrorCode.NOT_EXISTS, e.toString());
  108. // logger.error("",e);
  109. }
  110. }
  111. } else {
  112. // throw new
  113. // CommonException(CommonErrorCode.NOT_EXISTS,
  114. // "非excel文件无法解析!");
  115. messages.add("非excel文件无法解析!");
  116. }
  117. } else {
  118. // throw new CommonException(CommonErrorCode.NOT_EXISTS,
  119. // "未知文件无法解析!");
  120. messages.add("未知文件无法解析!");
  121. }
  122. }
  123. } else {
  124. // throw new CommonException(CommonErrorCode.NOT_EXISTS,
  125. // "无文件上传!");
  126. messages.add("无文件上传!");
  127. }
  128. Map<String, List<LisExcelWrapperVO>> lixExMap = lisExcelWrapperList.stream()
  129. .collect(Collectors.groupingBy(LisExcelWrapperVO::getMealName));
  130. List<LisExcelResVO> LisExcelReslist = new ArrayList<LisExcelResVO>();
  131. for (String str : lixExMap.keySet()) {
  132. LisExcelResVO lisExcelResVO = new LisExcelResVO();
  133. lisExcelResVO.setMenus(str);
  134. lisExcelResVO.setLisExcelItem(lixExMap.get(str));
  135. LisExcelReslist.add(lisExcelResVO);
  136. }
  137. LitAssayVO litAssay = new LitAssayVO();
  138. litAssay.setMessages(messages);
  139. litAssay.setLisExcelRes(LisExcelReslist);
  140. return RespDTO.onSuc(litAssay);
  141. } catch (Exception e) {
  142. e.printStackTrace();
  143. throw new CommonException(CommonErrorCode.NOT_EXISTS, "化验excel文件解析出错!");
  144. // return response.failure("化验excel文件解析出错!");
  145. } finally {
  146. try {
  147. if (inputStream != null) {
  148. inputStream.close();
  149. }
  150. if (wb != null) {
  151. wb.close();
  152. }
  153. } catch (Exception e) {
  154. }
  155. }
  156. }
  157. private String getValue(Cell cell) {
  158. try {
  159. Object obj = null;
  160. switch (cell.getCellTypeEnum()) {
  161. case BOOLEAN:
  162. obj = cell.getBooleanCellValue();
  163. break;
  164. case ERROR:
  165. obj = cell.getErrorCellValue();
  166. break;
  167. case NUMERIC:
  168. if (HSSFDateUtil.isCellDateFormatted(cell)) {
  169. Date date = cell.getDateCellValue();
  170. obj = DateFormatUtils.format(date, "yyyy-MM-dd");
  171. } else {
  172. obj = cell.getNumericCellValue();
  173. DecimalFormat df = new DecimalFormat("0");
  174. obj = df.format(obj);
  175. }
  176. // obj = cell.getNumericCellValue();
  177. break;
  178. case STRING:
  179. obj = cell.getStringCellValue();
  180. break;
  181. default:
  182. break;
  183. }
  184. return obj.toString();
  185. } catch (Exception e) {
  186. return "";
  187. }
  188. }
  189. }