Commit 15ec249f authored by renjie's avatar renjie

删除多个ip接口

parent 82fec7a6
...@@ -58,7 +58,7 @@ public class IpControlloer { ...@@ -58,7 +58,7 @@ public class IpControlloer {
public ResultDto deleteIp(Principal principal, @RequestBody IpResourceRequestDto ipResourceRequestDto) { public ResultDto deleteIp(Principal principal, @RequestBody IpResourceRequestDto ipResourceRequestDto) {
ResultDto resultDto = new ResultDto(); ResultDto resultDto = new ResultDto();
try { try {
ipResourceService.deleteIp(principal.getName(), ipResourceRequestDto.getAddr().get(0)); resultDto.setData(ipResourceService.deleteIp(principal.getName(), ipResourceRequestDto.getAddr()));
resultDto.setStatus(0); resultDto.setStatus(0);
}catch (Exception e) { }catch (Exception e) {
resultDto.setStatus(-1); resultDto.setStatus(-1);
......
...@@ -3,8 +3,9 @@ package com.edgec.browserbackend.browser.dto; ...@@ -3,8 +3,9 @@ package com.edgec.browserbackend.browser.dto;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class RenewIpReturnDto { public class IpOperationResultDto {
List<String> successList = new ArrayList<>(); List<String> successList = new ArrayList<>();
List<String> failList = new ArrayList<>();
public List<String> getSuccessList() { public List<String> getSuccessList() {
return successList; return successList;
...@@ -13,4 +14,12 @@ public class RenewIpReturnDto { ...@@ -13,4 +14,12 @@ public class RenewIpReturnDto {
public void setSuccessList(List<String> successList) { public void setSuccessList(List<String> successList) {
this.successList = successList; this.successList = successList;
} }
public List<String> getFailList() {
return failList;
}
public void setFailList(List<String> failList) {
this.failList = failList;
}
} }
...@@ -3,7 +3,7 @@ package com.edgec.browserbackend.browser.dto; ...@@ -3,7 +3,7 @@ package com.edgec.browserbackend.browser.dto;
public class PageInfo { public class PageInfo {
int currentPage; int currentPage;
int totalPages; int totalPages;
int totalShops; int totalItems;
public int getCurrentPage() { public int getCurrentPage() {
return currentPage; return currentPage;
...@@ -21,11 +21,11 @@ public class PageInfo { ...@@ -21,11 +21,11 @@ public class PageInfo {
this.totalPages = totalPage; this.totalPages = totalPage;
} }
public int getTotalShops() { public int getTotalItems() {
return totalShops; return totalItems;
} }
public void setTotalShops(int totalShops) { public void setTotalItems(int totalItems) {
this.totalShops = totalShops; this.totalItems = totalItems;
} }
} }
...@@ -156,7 +156,7 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -156,7 +156,7 @@ public class IpResourceServiceImpl implements IpResourceService {
} }
@Override @Override
public RenewIpReturnDto renewIp(String username, IpResourceRequestDto ipResourceRequestDto) throws Exception { public IpOperationResultDto renewIp(String username, IpResourceRequestDto ipResourceRequestDto) throws Exception {
Account account = accountRepository.findByName(username); Account account = accountRepository.findByName(username);
if (account == null) if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST); throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
...@@ -184,14 +184,14 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -184,14 +184,14 @@ public class IpResourceServiceImpl implements IpResourceService {
RenewIpResultDto renewIpResultDto = JSON.parseObject(result.getBody(), RenewIpResultDto.class); RenewIpResultDto renewIpResultDto = JSON.parseObject(result.getBody(), RenewIpResultDto.class);
if (StringUtils.isNotBlank(renewIpResultDto.getErrorCode())) if (StringUtils.isNotBlank(renewIpResultDto.getErrorCode()))
throw new Exception(renewIpResultDto.getErrorCode()); throw new Exception(renewIpResultDto.getErrorCode());
RenewIpReturnDto renewIpReturnDto = new RenewIpReturnDto(); IpOperationResultDto ipOperationResultDto = new IpOperationResultDto();
renewIpResultDto.getIplist().forEach(x -> { renewIpResultDto.getIplist().forEach(x -> {
IpResource ipResource = ipResourceRepository.findByAddrAndIsDeleted(x.getIp(), false); IpResource ipResource = ipResourceRepository.findByAddrAndIsDeleted(x.getIp(), false);
ipResource.setValidTime(Instant.parse(x.getValidTill()).toEpochMilli()); ipResource.setValidTime(Instant.parse(x.getValidTill()).toEpochMilli());
ipResourceRepository.save(ipResource); ipResourceRepository.save(ipResource);
renewIpReturnDto.getSuccessList().add(x.getIp()); ipOperationResultDto.getSuccessList().add(x.getIp());
}); });
return renewIpReturnDto; return ipOperationResultDto;
} catch (Exception e) { } catch (Exception e) {
logger.error("fail to renew ip", e.getMessage()); logger.error("fail to renew ip", e.getMessage());
logger.error(e.getMessage()); logger.error(e.getMessage());
...@@ -200,47 +200,56 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -200,47 +200,56 @@ public class IpResourceServiceImpl implements IpResourceService {
} }
@Override @Override
public void deleteIp(String username, String ipAddr) throws Exception { public IpOperationResultDto deleteIp(String username, List<String> ipAddrs) throws Exception {
Account account = accountRepository.findByName(username); Account account = accountRepository.findByName(username);
if (account == null) if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST); throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
IpResource ipResource = ipResourceRepository.findByAddrAndIsDeleted(ipAddr, false); IpOperationResultDto ipOperationResultDto = new IpOperationResultDto();
if (ipResource == null) ipAddrs.forEach(ipAddr -> {
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST); IpResource ipResource = ipResourceRepository.findByAddrAndIsDeleted(ipAddr, false);
UserShop userShop = null; if (ipResource == null) {
if (ipResource.getShopId() != null) { ipOperationResultDto.getFailList().add(ipAddr);
userShop = userShopRepository.findByUsernameAndShopId(username, ipResource.getShopId()); return;
if (userShop == null) }
throw new ClientRequestException(AccountErrorCode.NOPERMISSION); UserShop userShop = null;
} else if (!ipResource.getOwner().equals(username)) { if (ipResource.getShopId() != null) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION); userShop = userShopRepository.findByUsernameAndShopId(username, ipResource.getShopId());
} if (userShop == null) {
RestTemplate restTemplate = new RestTemplate(); ipOperationResultDto.getFailList().add(ipAddr);
HttpHeaders headers = buildGetHeader(); return;
Map<String, String> params = new HashMap<String, String>(); }
HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(params, headers); } else if (!ipResource.getOwner().equals(username)) {
try { return;
ResponseEntity<String> result = restTemplate.exchange(TESTURL + "/intelligroup/ipresources?accountId=browser&ip={ip}", HttpMethod.DELETE, httpEntity, String.class, ipAddr); }
DeleteIpResultDto deleteIpResultDto = JSON.parseObject(result.getBody(), DeleteIpResultDto.class); RestTemplate restTemplate = new RestTemplate();
if (StringUtils.isNotBlank(deleteIpResultDto.getErrorCode())) HttpHeaders headers = buildGetHeader();
throw new Exception(deleteIpResultDto.getErrorCode()); Map<String, String> params = new HashMap<String, String>();
ipResourceRepository.delete(ipResource); HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(params, headers);
if (userShop != null){ try {
Shop shop = shopRepository.findById(ipResource.getShopId()).orElse(null); ResponseEntity<String> result = restTemplate.exchange(TESTURL + "/intelligroup/ipresources?accountId=browser&ip={ip}", HttpMethod.DELETE, httpEntity, String.class, ipAddr);
if (shop != null) { DeleteIpResultDto deleteIpResultDto = JSON.parseObject(result.getBody(), DeleteIpResultDto.class);
shop.setIp(null); if (StringUtils.isNotBlank(deleteIpResultDto.getErrorCode()))
shop.setIpId(null); throw new Exception(deleteIpResultDto.getErrorCode());
shop.setIpRegion(null); ipResourceRepository.delete(ipResource);
shopRepository.save(shop); if (userShop != null){
Shop shop = shopRepository.findById(ipResource.getShopId()).orElse(null);
if (shop != null) {
shop.setIp(null);
shop.setIpId(null);
shop.setIpRegion(null);
shopRepository.save(shop);
}
userShop.setIpId(null);
userShopRepository.save(userShop);
} }
userShop.setIpId(null); ipOperationResultDto.getSuccessList().add(ipAddr);
userShopRepository.save(userShop); } catch (Exception e) {
logger.error("fail to renew ip", e.getMessage());
ipOperationResultDto.getFailList().add(ipAddr);
throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
} }
} catch (Exception e) { });
logger.error("fail to renew ip", e.getMessage()); return ipOperationResultDto;
logger.error(e.getMessage());
throw new Exception(e.getMessage());
}
} }
@Override @Override
...@@ -347,7 +356,7 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -347,7 +356,7 @@ public class IpResourceServiceImpl implements IpResourceService {
PageInfo pageInfo = new PageInfo(); PageInfo pageInfo = new PageInfo();
pageInfo.setCurrentPage(ipResourceDtoPage.getPageable().getPageNumber()); pageInfo.setCurrentPage(ipResourceDtoPage.getPageable().getPageNumber());
pageInfo.setTotalPages(ipResourceDtoPage.getTotalPages()); pageInfo.setTotalPages(ipResourceDtoPage.getTotalPages());
pageInfo.setTotalShops(allIpIds.size()); pageInfo.setTotalItems(allIpIds.size());
ipPageResultDto.setIpPage(pageInfo); ipPageResultDto.setIpPage(pageInfo);
return ipPageResultDto; return ipPageResultDto;
} }
......
...@@ -338,7 +338,7 @@ public class ShopServiceImpl implements ShopService { ...@@ -338,7 +338,7 @@ public class ShopServiceImpl implements ShopService {
PageInfo pageInfo = new PageInfo(); PageInfo pageInfo = new PageInfo();
pageInfo.setCurrentPage(shopDtoPage.getPageable().getPageNumber()); pageInfo.setCurrentPage(shopDtoPage.getPageable().getPageNumber());
pageInfo.setTotalPages(shopDtoPage.getTotalPages()); pageInfo.setTotalPages(shopDtoPage.getTotalPages());
pageInfo.setTotalShops(shopIds.size()); pageInfo.setTotalItems(shopIds.size());
shopPageResultDto.setShopPage(pageInfo); shopPageResultDto.setShopPage(pageInfo);
return shopPageResultDto; return shopPageResultDto;
} }
......
...@@ -8,9 +8,9 @@ public interface IpResourceService { ...@@ -8,9 +8,9 @@ public interface IpResourceService {
List<IpResourceDto> buyIp(String username, IpResourceRequestDto ipResourceRequestDto) throws Exception; List<IpResourceDto> buyIp(String username, IpResourceRequestDto ipResourceRequestDto) throws Exception;
RenewIpReturnDto renewIp(String username, IpResourceRequestDto ipResourceRequestDto) throws Exception; IpOperationResultDto renewIp(String username, IpResourceRequestDto ipResourceRequestDto) throws Exception;
void deleteIp(String username, String ipAddr) throws Exception; IpOperationResultDto deleteIp(String username, List<String> ipAddrs) throws Exception;
IpPageResultDto getIpList(String username, int groupType, int page, int amount, IpFilterDto ipFilterDto); IpPageResultDto getIpList(String username, int groupType, int page, int amount, IpFilterDto ipFilterDto);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment