xuhy
2025-11-28 4054bd04c27698125a1605f1c5d554d6aa783816
ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TLargeKeyGridUserController.java
@@ -282,5 +282,116 @@
        return R.ok();
    }
    @Log(title = "重点人员-网格人员信息-网格人员信息导入", businessType = BusinessType.INSERT)
    @ApiOperation(value = "网格人员信息导入")
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    @PostMapping("/importGrid")
    public R<String> importGrid(@RequestPart("file") MultipartFile file) {
        List<TLargeKeyGridUser> largeKeyGridUsers = new ArrayList<>();
        ImportParams params = new ImportParams();
        params.setHeadRows(1); //表头行数
        InputStream inputStream = null;
        List<LargeKeyGridUserImport> largeKeyGridUserImports;
        try {
            inputStream = file.getInputStream();
            largeKeyGridUserImports = ExcelImportUtil.importExcel(inputStream,
                    LargeKeyGridUserImport.class, params);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("网格人员信息导入失败:{}", e.getMessage());
            throw new ServiceException("网格人员信息导入失败!");
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                throw new ServiceException(e.getMessage());
            }
        }
        if (CollectionUtils.isEmpty(largeKeyGridUserImports)) {
            throw new ServiceException("网格人员信息数据为空!");
        }
        JSONObject result = new JSONObject();
        int i = 1;
        for (LargeKeyGridUserImport largeKeyGridUserImport : largeKeyGridUserImports) {
            if (!StringUtils.hasLength(largeKeyGridUserImport.getGridInfo())){
                result.append("第"+i+"行:[", "网格"+"]不能为空");
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getCommunityName())){
                result.append("第"+i+"行:[", "小区名称"+"]不能为空");
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getBuildingInfo())){
                result.append("第"+i+"行:[", "楼栋信息"+"]不能为空");
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getUnitNumber())){
                result.append("第"+i+"行:[", "单元号"+"]不能为空");
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getHouseNumber())){
                result.append("第"+i+"行:[", "门牌号"+"]不能为空");
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getUserName())){
                result.append("第"+i+"行:[", "姓名"+"]不能为空");
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getUserSexStr())){
                result.append("第"+i+"行:[", "性别"+"]不能为空");
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getIdCard())){
                result.append("第"+i+"行:[", "身份证号"+"]不能为空");
                continue;
            }
            if (largeKeyGridUserImport.getIdCard().matches("^$|(^[1-9]\\\\d{7}((0\\\\d)|(1[0-2]))(([0-2]\\\\d)|3[0-1])\\\\d{3}$)|(^[1-9]\\\\d{5}[1-9]\\\\d{3}((0\\\\d)|(1[0-2]))(([0-2]\\\\d)|3[0-1])((\\\\d{4})|\\\\d{3}[Xx])$)")){
                result.append("第"+i+"行:[", "身份证号"+"]格式不正确");
                continue;
            }
            if (!StringUtils.hasLength(largeKeyGridUserImport.getContactNumber())){
                result.append("第"+i+"行:[", "联系电话"+"]不能为空");
                continue;
            }
            if (largeKeyGridUserImport.getContactNumber().matches("^1[3-9]\\\\d{9}$")){
                result.append("第"+i+"行:[", "联系电话"+"]格式不正确");
                continue;
            }
            TLargeKeyGridUser largeKeyGridUser = new TLargeKeyGridUser();
            BeanUtils.copyProperties(largeKeyGridUserImport, largeKeyGridUser);
            switch (largeKeyGridUserImport.getUserSexStr()){
                case "男":
                    largeKeyGridUser.setUserSex(1);
                    break;
                case "女":
                    largeKeyGridUser.setUserSex(2);
                    break;
                default:
                    result.append("第"+i+"行:[", "性别"+"]格式错误");
                    continue;
            }
            if(StringUtils.hasLength(largeKeyGridUserImport.getTypeName())){
                TLargeKeyPersonnelType largeKeyPersonnelType = largeKeyPersonnelTypeService.getOne(Wrappers.lambdaQuery(TLargeKeyPersonnelType.class)
                        .eq(TLargeKeyPersonnelType::getTypeName, largeKeyGridUserImport.getTypeName())
                        .last("LIMIT 1"));
                if(Objects.isNull(largeKeyPersonnelType)){
                    result.append("第"+i+"行:[", "类型名称"+"]不存在");
                    continue;
                }
                largeKeyGridUser.setTypeId(largeKeyPersonnelType.getId());
            }
            largeKeyGridUser.setUserType(2);
            largeKeyGridUsers.add(largeKeyGridUser);
            i++;
        }
        largeKeyGridUserService.saveBatch(largeKeyGridUsers);
        if(!result.isEmpty()){
            return R.fail(result.toString());
        }
        return R.ok();
    }
}