实现文件随意添加无限分类

在保证新增语句无误的情况下

//导入分区(待优化)
    @PostMapping("/fileImport")
    public R fileImport(String businessId,@NotEmpty(message = "文件不能为空") @RequestParam("file") MultipartFile[] files) throws Exception{
        if(businessId == null){
            businessId = "0";
        }
        List<String> fileList = UploadFile.createExcelUploadForExcel(fileUploadConfig.getSavePath(), files).save();
        List<MasterBusiness> valueList = masterBusinessService.handleFile("0",fileList.get(0));
        //文件内部处理
        //System.out.println("1.0数据集"+valueList);
        if(valueList.size()==0){
            //System.out.println("1.1:文件为空");
            return R.error("读取excel文件失败,内容为空");
        }
        for (int i = 0; i < valueList.size(); i++) {
            //System.out.println("-----"+valueList.get(i));
            if(valueList.get(i).getBusinessName() == null || valueList.get(i).getBusinessName().isEmpty()){
                //System.out.println("1.2:名称为空");
                return R.error("读取excel第"+(i+1)+"行数据失败,业务名称为空");
            }else{
                for (int j = i+1; j < valueList.size(); j++) {
                    if(valueList.get(i).getBusinessName()==valueList.get(j).getBusinessName()
                        &&valueList.get(i).getParentId()==valueList.get(j).getParentId() ){
                        //System.out.println("1.3:自身重名");
                        return R.error("读取excel第"+(i+1)+"行中,内容名称、级别与"+(j+1)+"行重复");
                    }
                }
            }
        }
        //System.out.println("2.0数据集"+valueList);
        for (int i = 0; i < valueList.size(); i++) {
            if(valueList.get(i).getParentId() == null || valueList.get(i).getParentId().isEmpty()){
                //System.out.println("2.1空值覆盖");
                valueList.get(i).setParentId(businessId);
            }else{
                List<MasterBusiness> selectRes = masterBusinessService.selectByBusinessName(valueList.get(i).getParentId());
                if(selectRes.size()>0){
                    //System.out.println("2.2查库换ID");
                    valueList.get(i).setParentId(Integer.toString(selectRes.get(0).getBusinessId()));
                }else{
                    int identical = 0;
                    for (int j = 0; j < valueList.size(); j++) {
                        if(valueList.get(i).getParentId() == valueList.get(j).getBusinessName()) {
                            //System.out.println("2.3最后查名称");
                            identical++;
                        }
                    }
                    if(identical == 0){
                        return R.error("读取excel第" + (i + 1) + "行中,上级分类校验失败");
                    }
                }
            }
        }
        //System.out.println("3.0数据集"+valueList);
        int addResult = 0;
        for (int i = 0; i < valueList.size(); i++) {
            try{
                //System.out.println("3.1转化数字成功,则查重");
                Integer a = Integer.parseInt(valueList.get(i).getParentId());
                if(masterBusinessService.selectByBNandPI(valueList.get(i))>0){
                    return R.error("读取成功"+valueList.size()+"条,"+addResult+"条添加成功,第"+(i+1)+"条失败,同级含同名业务");
                }else{
                    addResult = addResult + masterBusinessService.add(valueList.get(i));
                }
            }catch (NumberFormatException e){
                //System.out.println("3.2转化数字失败,则查询添加");
                List<MasterBusiness> selectRes = masterBusinessService.selectByBusinessName(valueList.get(i).getParentId());
                valueList.get(i).setParentId(Integer.toString(selectRes.get(0).getBusinessId()));
                addResult = addResult + masterBusinessService.add(valueList.get(i));
            }
        }
        return R.success("读取成功"+valueList.size()+"条,成功添加"+addResult+"条数据");
    }

 

posted @ 2022-01-17 11:15  一懒众衫小丶  阅读(52)  评论(0编辑  收藏  举报