Commit 15ec249f authored by renjie's avatar renjie

删除多个ip接口

parent 82fec7a6
......@@ -58,7 +58,7 @@ public class IpControlloer {
public ResultDto deleteIp(Principal principal, @RequestBody IpResourceRequestDto ipResourceRequestDto) {
ResultDto resultDto = new ResultDto();
try {
ipResourceService.deleteIp(principal.getName(), ipResourceRequestDto.getAddr().get(0));
resultDto.setData(ipResourceService.deleteIp(principal.getName(), ipResourceRequestDto.getAddr()));
resultDto.setStatus(0);
}catch (Exception e) {
resultDto.setStatus(-1);
......
......@@ -3,8 +3,9 @@ package com.edgec.browserbackend.browser.dto;
import java.util.ArrayList;
import java.util.List;
public class RenewIpReturnDto {
public class IpOperationResultDto {
List<String> successList = new ArrayList<>();
List<String> failList = new ArrayList<>();
public List<String> getSuccessList() {
return successList;
......@@ -13,4 +14,12 @@ public class RenewIpReturnDto {
public void setSuccessList(List<String> 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;
public class PageInfo {
int currentPage;
int totalPages;
int totalShops;
int totalItems;
public int getCurrentPage() {
return currentPage;
......@@ -21,11 +21,11 @@ public class PageInfo {
this.totalPages = totalPage;
}
public int getTotalShops() {
return totalShops;
public int getTotalItems() {
return totalItems;
}
public void setTotalShops(int totalShops) {
this.totalShops = totalShops;
public void setTotalItems(int totalItems) {
this.totalItems = totalItems;
}
}
......@@ -156,7 +156,7 @@ public class IpResourceServiceImpl implements IpResourceService {
}
@Override
public RenewIpReturnDto renewIp(String username, IpResourceRequestDto ipResourceRequestDto) throws Exception {
public IpOperationResultDto renewIp(String username, IpResourceRequestDto ipResourceRequestDto) throws Exception {
Account account = accountRepository.findByName(username);
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
......@@ -184,14 +184,14 @@ public class IpResourceServiceImpl implements IpResourceService {
RenewIpResultDto renewIpResultDto = JSON.parseObject(result.getBody(), RenewIpResultDto.class);
if (StringUtils.isNotBlank(renewIpResultDto.getErrorCode()))
throw new Exception(renewIpResultDto.getErrorCode());
RenewIpReturnDto renewIpReturnDto = new RenewIpReturnDto();
IpOperationResultDto ipOperationResultDto = new IpOperationResultDto();
renewIpResultDto.getIplist().forEach(x -> {
IpResource ipResource = ipResourceRepository.findByAddrAndIsDeleted(x.getIp(), false);
ipResource.setValidTime(Instant.parse(x.getValidTill()).toEpochMilli());
ipResourceRepository.save(ipResource);
renewIpReturnDto.getSuccessList().add(x.getIp());
ipOperationResultDto.getSuccessList().add(x.getIp());
});
return renewIpReturnDto;
return ipOperationResultDto;
} catch (Exception e) {
logger.error("fail to renew ip", e.getMessage());
logger.error(e.getMessage());
......@@ -200,47 +200,56 @@ public class IpResourceServiceImpl implements IpResourceService {
}
@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);
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
IpResource ipResource = ipResourceRepository.findByAddrAndIsDeleted(ipAddr, false);
if (ipResource == null)
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST);
UserShop userShop = null;
if (ipResource.getShopId() != null) {
userShop = userShopRepository.findByUsernameAndShopId(username, ipResource.getShopId());
if (userShop == null)
throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
} else if (!ipResource.getOwner().equals(username)) {
throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
}
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = buildGetHeader();
Map<String, String> params = new HashMap<String, String>();
HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(params, headers);
try {
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);
if (StringUtils.isNotBlank(deleteIpResultDto.getErrorCode()))
throw new Exception(deleteIpResultDto.getErrorCode());
ipResourceRepository.delete(ipResource);
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);
IpOperationResultDto ipOperationResultDto = new IpOperationResultDto();
ipAddrs.forEach(ipAddr -> {
IpResource ipResource = ipResourceRepository.findByAddrAndIsDeleted(ipAddr, false);
if (ipResource == null) {
ipOperationResultDto.getFailList().add(ipAddr);
return;
}
UserShop userShop = null;
if (ipResource.getShopId() != null) {
userShop = userShopRepository.findByUsernameAndShopId(username, ipResource.getShopId());
if (userShop == null) {
ipOperationResultDto.getFailList().add(ipAddr);
return;
}
} else if (!ipResource.getOwner().equals(username)) {
return;
}
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = buildGetHeader();
Map<String, String> params = new HashMap<String, String>();
HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(params, headers);
try {
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);
if (StringUtils.isNotBlank(deleteIpResultDto.getErrorCode()))
throw new Exception(deleteIpResultDto.getErrorCode());
ipResourceRepository.delete(ipResource);
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);
userShopRepository.save(userShop);
ipOperationResultDto.getSuccessList().add(ipAddr);
} 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());
logger.error(e.getMessage());
throw new Exception(e.getMessage());
}
});
return ipOperationResultDto;
}
@Override
......@@ -347,7 +356,7 @@ public class IpResourceServiceImpl implements IpResourceService {
PageInfo pageInfo = new PageInfo();
pageInfo.setCurrentPage(ipResourceDtoPage.getPageable().getPageNumber());
pageInfo.setTotalPages(ipResourceDtoPage.getTotalPages());
pageInfo.setTotalShops(allIpIds.size());
pageInfo.setTotalItems(allIpIds.size());
ipPageResultDto.setIpPage(pageInfo);
return ipPageResultDto;
}
......
......@@ -338,7 +338,7 @@ public class ShopServiceImpl implements ShopService {
PageInfo pageInfo = new PageInfo();
pageInfo.setCurrentPage(shopDtoPage.getPageable().getPageNumber());
pageInfo.setTotalPages(shopDtoPage.getTotalPages());
pageInfo.setTotalShops(shopIds.size());
pageInfo.setTotalItems(shopIds.size());
shopPageResultDto.setShopPage(pageInfo);
return shopPageResultDto;
}
......
......@@ -8,9 +8,9 @@ public interface IpResourceService {
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);
}
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