悠然哈哈哈

导航

上传文件检测有错误返回

    @RequestMapping("/import")
    @ResponseBody
    public Map<String, Object> importItem(@RequestParam("fileName") MultipartFile uploadFile,
            HttpServletRequest request, HttpServletResponse response,Long warehouseId){
        if(warehouseId==null||warehouseId.longValue()<=0){
            return APIUtil.toMap("0200", false,"仓库id不能为空!");
        }
        if (uploadFile.isEmpty()) {
            return APIUtil.toMap("0200", false,"上传文件不能为空!");
        }
        String fileName = uploadFile.getOriginalFilename();
        if (!fileName.endsWith("xlsx") && !fileName.endsWith("xls")) {
            return APIUtil.toMap("0200", false,"上传文件错误");
        }
        
        InputStream inputStream = null;
        try {
            inputStream = uploadFile.getInputStream();
            boolean isExcel2003 = true;
            if (fileName.endsWith("xlsx")) {
                isExcel2003 = false;
            }
            List<Map<String, Object>> list=ReadExcelUtils.readLocationItem(inputStream, isExcel2003);
            if(list==null||list.size()==0){
                return APIUtil.toMap("0200", false,"上传文件数据为空");
            }
            int res=0;
            
            List<StorageStock> stocks=new ArrayList<>();
            
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> mapObj=list.get(i);
                String barCode=mapObj.get("barCode")==null?"0":mapObj.get("barCode").toString();
                String locationName=mapObj.get("locationName")==null?"0":mapObj.get("locationName").toString();
                String msg="";
                
                StorageStock stock=new StorageStock();
                
                WarehouseLocation location=warehouseLocationService.get(warehouseId, locationName);
                if(location==null){
                    res+=1;
                    msg="库位:["+mapObj.get("locationName")+"]不存在"+",";
                }else{
                    stock.setWarehouseLocationId(location.getId());
                    if(location.getType().intValue()!=WarehouseLoaclEnum.FIXED.getType()){
                        msg="库位:["+locationName+"]不是固定库位"+",";
                    }
                }
                StorageStock item=storageService.findByBarCode(warehouseId, barCode);
                if(item==null){
                    res+=1;
                    msg+="条码:["+mapObj.get("barCode")+"]不存在";
                }else{
                    stock.setItemId(item.getItemId());
                    StorageStock stock1=storageService.getStorageStock(warehouseId, item.getItemId());
                    if(stock1!=null){
                        msg+="条码:["+barCode+"]已绑定库位";
                    }
                }
                mapObj.put("msg", msg);
                
                stocks.add(stock);
            }
            if(res>0){
                String[] titles={"商品条码(必填)","固定库位标识(必填)","返回结果"};
                HSSFWorkbook wb = new HSSFWorkbook();
                HSSFSheet sheet = wb.createSheet("sheet0");
                HSSFRow row = sheet.createRow(0);
                HSSFCellStyle cellStyle = wb.createCellStyle();

                HSSFFont font = wb.createFont();font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
                cellStyle.setFont(font);
                row.setRowStyle(cellStyle);
                sheet.setColumnWidth(0, 20 * 256);
                sheet.setColumnWidth(1, 20 * 256);
                sheet.setColumnWidth(2, 20 * 256);

                for (int i = 0; i< titles.length ;i++) {
                    HSSFCell cell = row.createCell(i);
                    cell.setCellValue(titles[i]);
                    cell.setCellStyle(cellStyle);
                }
                for (int i = 0; i < list.size(); i++) {
                    row = sheet.createRow((int) i + 1);  
                    Map<String, Object> map = list.get(i);
                    row.createCell(0).setCellValue(map.get("barCode")+"");
                    row.createCell(1).setCellValue(map.get("locationName")+"");
                    row.createCell(2).setCellValue(map.get("msg")+"");
                }
                 FileOutputStream out = null;
                    String url = "";
                    try {
                        String xlsxName = "导入失败商品数据";
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
                        String dateTime = sdf.format(new Date());
                        xlsxName = xlsxName +dateTime + ".xlsx";
                        String codedFileName = new String( xlsxName.getBytes(), "UTF-8");
                        url = "/admin_excel/"+codedFileName;
                        out = new FileOutputStream(ScpEnum.SCP_EXCEL_PATH.getValue()+codedFileName);
                        wb.write(out);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            out.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    Map<String, Object> eMap = new HashMap<>();
                    eMap.put("url",url);
                    return APIUtil.toMap("0200", false, eMap);
            }else{
                int result=storageService.saveLocationItem(stocks);
                if(result>0){
                    return APIUtil.toMap("0000", false,"绑定成功");
                }
            }
        } catch (Exception e) {
            return APIUtil.toMap("0200", false,"系统异常");
        }
        return APIUtil.toMap("0200", false,"操作失败");
    }

 

posted on 2018-08-27 09:28  悠然886  阅读(209)  评论(0编辑  收藏  举报