Beispiel #1
0
  /**
   * @Title: HopIncBlh.java @Description: TODO(导入药品)
   *
   * @param res
   * @return:void
   * @author zhouxin
   * @date 2014年6月10日 下午2:37:46
   * @version V1.0
   * @throws IOException
   */
  @SuppressWarnings("unchecked")
  public void upload(BusinessRequest res) throws IOException {

    HopIncDto dto = super.getDto(HopIncDto.class, res);
    dto.setOpFlg("1");
    // 生成随机文件名
    String newFileName = UUID.randomUUID().toString();
    // 获取文件存储路径
    String storageFileName = ServletActionContext.getServletContext().getRealPath("/uploadtmps");
    // 判断文件存储路径是否存在,若不存在则自动新建
    File document = new File(storageFileName);
    if (!document.exists()) {
      document.mkdir();
    }

    File dstFile = new File(storageFileName, newFileName);
    com.dhcc.framework.util.FileUtils.copyFile(dto.getUpload(), dstFile, BaseConstants.BUFFER_SIZE);

    //
    SysImpModelDto SysImpModelDto = new SysImpModelDto();
    SysImpModelDto.setImpModel(new ImpModel());
    SysImpModelDto.getImpModel().setType("INC");
    List<ImpModel> listImpModels = sysImpModelService.getModelList(SysImpModelDto);
    Map<Integer, String> modelMap = new HashMap<Integer, String>();
    for (int i = 0; i < listImpModels.size(); i++) {
      modelMap.put(
          Integer.valueOf(listImpModels.get(i).getSeq().toString()),
          listImpModels.get(i).getCode());
    }
    // 读取excel
    try {
      List<HopInc> hopIncs = new ArrayList<HopInc>();
      // 读取Excel文件
      Workbook workbook = null;
      Sheet sheet = null;
      Row row = null;
      Cell cell = null;

      String prefix =
          dto.getUploadFileName().substring(dto.getUploadFileName().lastIndexOf(".") + 1);
      if (prefix.equals("xls")) {
        workbook =
            new HSSFWorkbook(new FileInputStream(storageFileName + File.separator + newFileName));
      } else if (prefix.equals("xlsx")) {
        workbook =
            new XSSFWorkbook(new FileInputStream(storageFileName + File.separator + newFileName));
      } else {
        dto.setOpFlg("-1");
        dto.setMsg("<br>文件类型错误:");
        WebContextHolder.getContext().getResponse().getWriter().write(JsonUtils.toJson(dto));
        return;
      }
      sheet = workbook.getSheetAt(0);

      // 明细
      for (int numRows = 1; numRows <= sheet.getLastRowNum(); numRows++) {

        row = sheet.getRow(numRows);

        HopInc hopInc = new HopInc();
        for (int numCells = 0; numCells <= row.getLastCellNum(); numCells++) {
          cell = row.getCell(numCells);
          String colNameString = modelMap.get(numCells);
          if (StringUtils.isNullOrEmpty(colNameString)) {
            colNameString = " ";
          }
          ;
          switch (colNameString) {
            case "HOSPINC_CODE":
              if (cell != null) {
                hopInc.setIncCode(cell.toString());
              }
              break;
            case "HOSPINC_NAME":
              if (cell != null) {
                hopInc.setIncName(cell.toString());
              }
              break;
            case "HOSPINC_SPEC":
              if (cell != null) {
                hopInc.setIncSpec(cell.toString());
              }
              break;
            case "HOSPINC_PUOM":
              if (cell != null) {
                hopInc.setIncUomname(cell.toString());
              }
              break;
            case "HOSPINC_RP":
              if (cell != null) {
                cell.setCellType(Cell.CELL_TYPE_NUMERIC);
                hopInc.setIncRp((float) (cell.getNumericCellValue()));
              }
              break;
            case "HOSPINC_MANF":
              if (cell != null) {
                if (hopManfService.getIdByName(cell.toString()) == null) {
                  HopManf manf = new HopManf();
                  manf.setManfName(cell.toString());
                  manf.setManfHisid(
                      WebContextHolder.getContext().getVisit().getUserInfo().getHopId());
                  commonService.saveOrUpdate(manf);
                  hopInc.setIncManfid(manf.getHopManfId());
                } else {
                  hopInc.setIncManfid(hopManfService.getIdByName(cell.toString()));
                }
              }
              break;
            case "HOSPINC_CAT":
              if (cell != null) {
                hopInc.setIncCat(cell.toString());
              }
              break;
            case "HOSPINC_ALIAS":
              if (cell != null) {
                hopInc.setIncAliaS(cell.toString());
              }
              break;
            case "HOSPINC_PUOMCODE":
              if (cell != null) {
                hopInc.setIncUomcode(cell.toString());
              }
              break;
            case "HOSPINC_SP":
              if (cell != null) {
                cell.setCellType(Cell.CELL_TYPE_NUMERIC);
                hopInc.setIncSp((float) (cell.getNumericCellValue()));
              }
              break;
          }
        }
        // 验证数据的完整性
        if (org.apache.commons.lang.StringUtils.isBlank(hopInc.getIncCode())) {
          dto.setOpFlg("-1");
          dto.setMsg("<br>" + "第" + numRows + "行医院商品代码不能为空!");
          continue;
        } else {
          if (dto.getOpFlg().equals("1")) {
            DetachedCriteria criteria = DetachedCriteria.forClass(HopInc.class);
            criteria.add(Restrictions.eq("incCode", hopInc.getIncCode()));
            criteria.add(
                Restrictions.eq(
                    "hopHopId", Long.valueOf(super.getLoginInfo().get("HOSPID").toString())));
            List<HopInc> hopIncsIds = commonService.findByDetachedCriteria(criteria);
            if (hopIncsIds.size() > 0) {
              hopInc.setIncHospid(hopIncsIds.get(0).getIncHospid());
              hopInc.setIncId(hopIncsIds.get(0).getIncId());
            }
          }
        }
        hopIncs.add(hopInc);
      }
      if (dto.getOpFlg().equals("1")) {
        dto.setHopIncs(hopIncs);
        hopIncService.saveInc(dto);
      }
      workbook = null;
      FileUtils.forceDelete(dstFile);

    } catch (Exception e) {
      e.printStackTrace();
      dto.setOpFlg("-1");
      dto.setMsg("<br>" + dto.getMsg() + "<br>" + e.getMessage());
    } finally {
      super.writeJSON(dto);
      FileUtils.forceDelete(dstFile);
    }
  }