| | |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | | import org.apache.poi.ss.usermodel.WorkbookFactory; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.PlatformTransactionManager; |
| | | import org.springframework.transaction.TransactionDefinition; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.transaction.support.TransactionTemplate; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | |
| | | private TokenService tokenService; |
| | | @Resource |
| | | private SysUserClient sysUserClient; |
| | | @Resource |
| | | private PlatformTransactionManager transactionManager; |
| | | |
| | | @Override |
| | | public Page<TAppUserWhitelist> pageList(AppUserWhitelistQueryDTO queryDTO) { |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public AppUserWhitelistImportResultVO importWhitelist(MultipartFile file) throws Exception { |
| | | if (file == null || file.isEmpty()) { |
| | | throw new ServiceException("导入文件不能为空"); |
| | |
| | | |
| | | Map<String, Site> siteCodeMap = getSiteCodeMap(); |
| | | Map<String, AppUserWhitelistSaveDTO> validRows = new LinkedHashMap<>(); |
| | | Map<String, Integer> validRowNumbers = new HashMap<>(); |
| | | List<AppUserWhitelistImportErrorVO> errors = new ArrayList<>(); |
| | | Set<String> importPhones = new HashSet<>(); |
| | | for (int i = 0; i < rows.size(); i++) { |
| | |
| | | continue; |
| | | } |
| | | validRows.put(dto.getPhone(), dto); |
| | | validRowNumbers.put(dto.getPhone(), rowNum); |
| | | } |
| | | |
| | | TransactionTemplate rowTransaction = new TransactionTemplate(transactionManager); |
| | | rowTransaction.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); |
| | | int successCount = 0; |
| | | for (Map.Entry<String, AppUserWhitelistSaveDTO> entry : validRows.entrySet()) { |
| | | try { |
| | | rowTransaction.execute(status -> { |
| | | createWhitelist(entry.getValue()); |
| | | return null; |
| | | }); |
| | | successCount++; |
| | | } catch (Exception exception) { |
| | | errors.add(new AppUserWhitelistImportErrorVO(validRowNumbers.get(entry.getKey()), entry.getKey(), |
| | | getImportSaveErrorMessage(exception))); |
| | | } |
| | | } |
| | | |
| | | AppUserWhitelistImportResultVO result = new AppUserWhitelistImportResultVO(); |
| | | result.setTotalCount(rows.size()); |
| | | result.setErrorCount(errors.size()); |
| | | result.setSuccessCount(errors.isEmpty() ? validRows.size() : 0); |
| | | result.setSuccessCount(successCount); |
| | | result.setErrorMessage(errors); |
| | | if (!errors.isEmpty()) { |
| | | return result; |
| | | } |
| | | |
| | | for (AppUserWhitelistSaveDTO dto : validRows.values()) { |
| | | createWhitelist(dto); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | private String getImportSaveErrorMessage(Exception exception) { |
| | | Throwable current = exception; |
| | | while (current != null) { |
| | | if (current instanceof ServiceException && StringUtils.isNotBlank(current.getMessage())) { |
| | | return current.getMessage(); |
| | | } |
| | | current = current.getCause(); |
| | | } |
| | | return "数据保存失败"; |
| | | } |
| | | |
| | | /** |
| | | * 判断指定用户在指定站点是否命中启用状态的白名单规则 |
| | | * |