Commit 36b44b1c authored by renjie's avatar renjie

Ip接口

parent 1c989b65
...@@ -20,7 +20,7 @@ public class AdminController { ...@@ -20,7 +20,7 @@ public class AdminController {
@Autowired @Autowired
AccountService accountService; AccountService accountService;
@RequestMapping(path = "/subuser/add", method = RequestMethod.POST) @RequestMapping(path = "/subuser/addone", method = RequestMethod.POST)
public ResultDto createSubAccount(Principal principal, @Valid @RequestBody AccountDto user) { public ResultDto createSubAccount(Principal principal, @Valid @RequestBody AccountDto user) {
ResultDto resultDto = new ResultDto(); ResultDto resultDto = new ResultDto();
try { try {
...@@ -38,7 +38,7 @@ public class AdminController { ...@@ -38,7 +38,7 @@ public class AdminController {
return resultDto; return resultDto;
} }
@RequestMapping(path = "/subuser/update", method = RequestMethod.PUT) @RequestMapping(path = "/subuser/updateone", method = RequestMethod.PUT)
public ResultDto saveSubAccount(Principal principal, @PathVariable String child, @Valid @RequestBody AccountDto user) { public ResultDto saveSubAccount(Principal principal, @PathVariable String child, @Valid @RequestBody AccountDto user) {
ResultDto resultDto = new ResultDto(); ResultDto resultDto = new ResultDto();
try { try {
......
...@@ -26,7 +26,7 @@ public class AccountDto { ...@@ -26,7 +26,7 @@ public class AccountDto {
private boolean allowedToCreateSubUser; private boolean allowedToCreateSubUser;
private int permission; private int permission = 0;
private int balance; private int balance;
......
...@@ -13,6 +13,7 @@ import com.edgec.browserbackend.account.service.EmailService; ...@@ -13,6 +13,7 @@ import com.edgec.browserbackend.account.service.EmailService;
import com.edgec.browserbackend.account.utils.AccountServicePool; import com.edgec.browserbackend.account.utils.AccountServicePool;
import com.edgec.browserbackend.auth.exception.AuthErrorCode; import com.edgec.browserbackend.auth.exception.AuthErrorCode;
import com.edgec.browserbackend.auth.service.UserAuthService; import com.edgec.browserbackend.auth.service.UserAuthService;
import com.edgec.browserbackend.browser.ErrorCode.BrowserErrorCode;
import com.edgec.browserbackend.common.charge.ChargeType; import com.edgec.browserbackend.common.charge.ChargeType;
import com.edgec.browserbackend.common.commons.error.ClientRequestException; import com.edgec.browserbackend.common.commons.error.ClientRequestException;
import com.edgec.browserbackend.common.commons.utils.CommonStringUtils; import com.edgec.browserbackend.common.commons.utils.CommonStringUtils;
...@@ -558,6 +559,9 @@ public class AccountServiceImpl implements AccountService { ...@@ -558,6 +559,9 @@ public class AccountServiceImpl implements AccountService {
userAuthService.deleteUser(child); userAuthService.deleteUser(child);
repository.delete(childAccount); repository.delete(childAccount);
Account parentAccount = repository.findByName(parent);
parentAccount.setChildCount(parentAccount.getChildCount() - 1);
repository.save(parentAccount);
} }
@Override @Override
...@@ -712,6 +716,10 @@ public class AccountServiceImpl implements AccountService { ...@@ -712,6 +716,10 @@ public class AccountServiceImpl implements AccountService {
if (!existing.isAllowedToCreateSubUser() && !StringUtils.isEmpty(existing.getParent())) if (!existing.isAllowedToCreateSubUser() && !StringUtils.isEmpty(existing.getParent()))
throw new ClientRequestException(AccountErrorCode.NOTALLOWEDTOCREATESUBUSER, "Not allowed to create sub user"); throw new ClientRequestException(AccountErrorCode.NOTALLOWEDTOCREATESUBUSER, "Not allowed to create sub user");
if (existing.getChildCount() >= 1000) {
throw new ClientRequestException(AccountErrorCode.CHILDMAX, "account cannot have more children");
}
existing = repository.findByName(user.getName()); existing = repository.findByName(user.getName());
if (existing != null) if (existing != null)
throw new ClientRequestException(AccountErrorCode.NAMEEXIST, "account already exists: " + user.getName()); throw new ClientRequestException(AccountErrorCode.NAMEEXIST, "account already exists: " + user.getName());
......
...@@ -3,7 +3,6 @@ package com.edgec.browserbackend.browser.controller; ...@@ -3,7 +3,6 @@ package com.edgec.browserbackend.browser.controller;
import com.edgec.browserbackend.account.dto.ResultDto; import com.edgec.browserbackend.account.dto.ResultDto;
import com.edgec.browserbackend.browser.dto.*; import com.edgec.browserbackend.browser.dto.*;
import com.edgec.browserbackend.browser.service.IpResourceService; import com.edgec.browserbackend.browser.service.IpResourceService;
import com.edgec.browserbackend.common.commons.error.ClientRequestException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
...@@ -39,15 +38,53 @@ public class IpControlloer { ...@@ -39,15 +38,53 @@ public class IpControlloer {
return resultDto; return resultDto;
} }
void renewIp(String username, IpResourceRequestDto ipResourceRequestDto) { @RequestMapping(value = "/renew", method = RequestMethod.POST)
public ResultDto renewIp(Principal principal, @RequestBody IpResourceRequestDto ipResourceRequestDto) {
ResultDto resultDto = new ResultDto();
try {
resultDto.setData(ipResourceService.renewIp(principal.getName(), ipResourceRequestDto));
resultDto.setStatus(0);
}catch (Exception e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", 80001);
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
}
return resultDto;
} }
void deleteIp(String username, String ipAddr) { @RequestMapping(value = "/del", method = RequestMethod.POST)
public ResultDto deleteIp(Principal principal, @RequestBody IpResourceRequestDto ipResourceRequestDto) {
ResultDto resultDto = new ResultDto();
try {
ipResourceService.deleteIp(principal.getName(), ipResourceRequestDto.getAddr().get(0));
resultDto.setStatus(0);
}catch (Exception e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", 80001);
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
}
return resultDto;
} }
List<IpResourceDto> getIpList(String username, int type, int page, int amount, FilterDto filterDto) { @RequestMapping(value = "/list", method = RequestMethod.POST)
return null; public ResultDto getIpList(Principal principal, IpListRequestDto ipListRequestDto) {
ResultDto resultDto = new ResultDto();
try {
List<IpResourceDto> ipResourceDto = ipResourceService.getIpList(principal.getName(), ipListRequestDto.getType(), ipListRequestDto.getPage(),
ipListRequestDto.getAmount(), ipListRequestDto.getIpFilterDto());
resultDto.setData(ipResourceDto);
resultDto.setStatus(0);
}catch (Exception e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", 80001);
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
}
return resultDto;
} }
} }
...@@ -24,6 +24,7 @@ public class IpResource { ...@@ -24,6 +24,7 @@ public class IpResource {
List<String> bindHistory; List<String> bindHistory;
private boolean isDeleted; private boolean isDeleted;
private String username; private String username;
private String owner;
private String userParent; private String userParent;
private String shopId; private String shopId;
private String shopName; private String shopName;
...@@ -181,4 +182,12 @@ public class IpResource { ...@@ -181,4 +182,12 @@ public class IpResource {
public void setPassword(String password) { public void setPassword(String password) {
this.password = password; this.password = password;
} }
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
} }
...@@ -17,6 +17,8 @@ public class UserShop { ...@@ -17,6 +17,8 @@ public class UserShop {
private String groupId; private String groupId;
private String ipId;
public UserShop(){ public UserShop(){
} }
...@@ -31,6 +33,13 @@ public class UserShop { ...@@ -31,6 +33,13 @@ public class UserShop {
this.groupId = groupId; this.groupId = groupId;
} }
public UserShop(String username, String shopId, String groupId, String ipId) {
this.username = username;
this.shopId = shopId;
this.groupId = groupId;
this.ipId = ipId;
}
public String getId() { public String getId() {
return id; return id;
} }
...@@ -62,4 +71,12 @@ public class UserShop { ...@@ -62,4 +71,12 @@ public class UserShop {
public void setGroupId(String groupId) { public void setGroupId(String groupId) {
this.groupId = groupId; this.groupId = groupId;
} }
public String getIpId() {
return ipId;
}
public void setIpId(String ipId) {
this.ipId = ipId;
}
} }
package com.edgec.browserbackend.browser.dto;
public class IpFilterDto {
String region;
String addr;
String vendor;
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getVendor() {
return vendor;
}
public void setVendor(String vendor) {
this.vendor = vendor;
}
}
package com.edgec.browserbackend.browser.dto;
import java.util.ArrayList;
import java.util.List;
public class IpListRequestDto {
private int type;
private int page;
private int amount;
private IpFilterDto ipFilterDto;
private List<String> addr = new ArrayList<>();
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public IpFilterDto getIpFilterDto() {
return ipFilterDto;
}
public void setIpFilterDto(IpFilterDto ipFilterDto) {
this.ipFilterDto = ipFilterDto;
}
public List<String> getAddr() {
return addr;
}
public void setAddr(List<String> addr) {
this.addr = addr;
}
}
...@@ -21,6 +21,7 @@ public class IpResourceDto { ...@@ -21,6 +21,7 @@ public class IpResourceDto {
private String username; private String username;
private String details; private String details;
private String password; private String password;
private List<String> protocol;
ShopDto shopDto; ShopDto shopDto;
public IpResourceDto(){ public IpResourceDto(){
...@@ -38,6 +39,7 @@ public class IpResourceDto { ...@@ -38,6 +39,7 @@ public class IpResourceDto {
this.username = ipResource.getUsername(); this.username = ipResource.getUsername();
this.details = ipResource.getDetails(); this.details = ipResource.getDetails();
this.password = ipResource.getPassword(); this.password = ipResource.getPassword();
this.protocol = ipResource.getProtocol();
this.shopDto = shopDto; this.shopDto = shopDto;
} }
...@@ -136,4 +138,12 @@ public class IpResourceDto { ...@@ -136,4 +138,12 @@ public class IpResourceDto {
public void setShopDto(ShopDto shopDto) { public void setShopDto(ShopDto shopDto) {
this.shopDto = shopDto; this.shopDto = shopDto;
} }
public List<String> getProtocol() {
return protocol;
}
public void setProtocol(List<String> protocol) {
this.protocol = protocol;
}
} }
...@@ -16,6 +16,7 @@ public class IpResourceRequestDto { ...@@ -16,6 +16,7 @@ public class IpResourceRequestDto {
private int amount = 1; private int amount = 1;
private boolean autorenew = false; private boolean autorenew = false;
private int ipkeptperiod = 0; private int ipkeptperiod = 0;
private String startscript = "";
private String instanceSpecKey; private String instanceSpecKey;
private String imageKey; private String imageKey;
...@@ -24,7 +25,7 @@ public class IpResourceRequestDto { ...@@ -24,7 +25,7 @@ public class IpResourceRequestDto {
private String system = "linux"; private String system = "linux";
private List<String> iplist = new ArrayList<>(); private List<String> addr = new ArrayList<>();
public String getName() { public String getName() {
return name; return name;
...@@ -86,12 +87,12 @@ public class IpResourceRequestDto { ...@@ -86,12 +87,12 @@ public class IpResourceRequestDto {
return ipkeptperiod; return ipkeptperiod;
} }
public List<String> getIplist() { public List<String> getAddr() {
return iplist; return addr;
} }
public void setIplist(List<String> iplist) { public void setAddr(List<String> addr) {
this.iplist = iplist; this.addr = addr;
} }
public String getInstanceSpecKey() { public String getInstanceSpecKey() {
...@@ -117,4 +118,20 @@ public class IpResourceRequestDto { ...@@ -117,4 +118,20 @@ public class IpResourceRequestDto {
public void setSystem(String system) { public void setSystem(String system) {
this.system = system; this.system = system;
} }
public String getLogintype() {
return logintype;
}
public void setLogintype(String logintype) {
this.logintype = logintype;
}
public String getStartscript() {
return startscript;
}
public void setStartscript(String startscript) {
this.startscript = startscript;
}
} }
package com.edgec.browserbackend.browser.dto;
import java.util.ArrayList;
import java.util.List;
public class RenewIpReturnDto {
List<String> successList = new ArrayList<>();
public List<String> getSuccessList() {
return successList;
}
public void setSuccessList(List<String> successList) {
this.successList = successList;
}
}
package com.edgec.browserbackend.browser.dto; package com.edgec.browserbackend.browser.dto;
import com.edgec.browserbackend.browser.domain.Shop;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
...@@ -11,6 +12,18 @@ public class ShopDto { ...@@ -11,6 +12,18 @@ public class ShopDto {
private String shopPassword; private String shopPassword;
private String shopPlatform; private String shopPlatform;
public ShopDto(){
}
public ShopDto(Shop shop) {
this.shopId = shop.getShopId();
this.shopName = shop.getShopName();
this.shopAccount = shop.getShopAccount();
this.shopPassword = shop.getShopPassword();
this.shopPlatform = shop.getShopPlatform();
}
public String getShopId() { public String getShopId() {
return shopId; return shopId;
} }
......
...@@ -3,7 +3,7 @@ package com.edgec.browserbackend.browser.dto; ...@@ -3,7 +3,7 @@ package com.edgec.browserbackend.browser.dto;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
public class FilterDto { public class ShopFilterDto {
private String shopName; private String shopName;
private String IpRegion; private String IpRegion;
private String shopAccount; private String shopAccount;
......
...@@ -23,13 +23,11 @@ public class ShopRequestDto { ...@@ -23,13 +23,11 @@ public class ShopRequestDto {
private List<String> toUsers; private List<String> toUsers;
private int type;
private int page = 0; private int page = 0;
private int amount = 50; private int amount = 50;
private FilterDto filter; private ShopFilterDto filter;
public String getIpAddr() { public String getIpAddr() {
return ipAddr; return ipAddr;
...@@ -63,11 +61,11 @@ public class ShopRequestDto { ...@@ -63,11 +61,11 @@ public class ShopRequestDto {
this.group = group; this.group = group;
} }
public FilterDto getFilter() { public ShopFilterDto getFilter() {
return filter; return filter;
} }
public void setFilter(FilterDto filter) { public void setFilter(ShopFilterDto filter) {
this.filter = filter; this.filter = filter;
} }
...@@ -87,14 +85,6 @@ public class ShopRequestDto { ...@@ -87,14 +85,6 @@ public class ShopRequestDto {
this.page = page; this.page = page;
} }
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public List<String> getShopIds() { public List<String> getShopIds() {
return shopIds; return shopIds;
} }
......
package com.edgec.browserbackend.browser.repository; package com.edgec.browserbackend.browser.repository;
import com.edgec.browserbackend.browser.domain.IpResource; import com.edgec.browserbackend.browser.domain.IpResource;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
public interface IpResourceRepository extends MongoRepository<IpResource, String> { public interface IpResourceRepository extends MongoRepository<IpResource, String> {
IpResource findByAddrAndIsDeleted(String addr, boolean isDeleted); IpResource findByAddrAndIsDeleted(String addr, boolean isDeleted);
List<IpResource> findByOwnerAndShopIdIsNull(String owner);
List<IpResource> findByIdIn(List<String> ipIds);
List<IpResource> findByAddrLikeAndIdIn(String addr, List<String> ipIds);
List<IpResource> findByVendorLikeAndIdIn(String vendor, List<String> ipIds);
List<IpResource> findByRegionLikeAndIdIn(String region, List<String> ipIds);
} }
...@@ -48,6 +48,9 @@ public class GroupServiceImpl implements GroupService { ...@@ -48,6 +48,9 @@ public class GroupServiceImpl implements GroupService {
String id; String id;
try { try {
Group group1 = groupRepository.save(group); Group group1 = groupRepository.save(group);
//可以优化
account.setGroupCount(account.getGroupCount() + 1);
accountRepository.save(account);
id = group1.getId(); id = group1.getId();
} catch (Exception e) { } catch (Exception e) {
logger.error("fail to add group", e.getMessage()); logger.error("fail to add group", e.getMessage());
...@@ -76,9 +79,15 @@ public class GroupServiceImpl implements GroupService { ...@@ -76,9 +79,15 @@ public class GroupServiceImpl implements GroupService {
public void deleteGroup(String username, String groupId) { public void deleteGroup(String username, String groupId) {
if (StringUtils.isBlank(username) || StringUtils.isBlank(groupId)) if (StringUtils.isBlank(username) || StringUtils.isBlank(groupId))
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE); throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
Account account = accountRepository.findByName(username);
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
try { try {
userShopRepository.updateGroupId(groupId, null); userShopRepository.updateGroupId(groupId, null);
groupRepository.deleteById(groupId); groupRepository.deleteById(groupId);
//可以优化
account.setGroupCount(account.getGroupCount()-1);
accountRepository.save(account);
} catch (Exception e) { } catch (Exception e) {
logger.error("fail to delete group", e.getMessage()); logger.error("fail to delete group", e.getMessage());
throw new ClientRequestException(BrowserErrorCode.UNKNOWN); throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
......
package com.edgec.browserbackend.browser.service.Impl; package com.edgec.browserbackend.browser.service.Impl;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.edgec.browserbackend.account.domain.Account; import com.edgec.browserbackend.account.domain.Account;
import com.edgec.browserbackend.account.domain.User;
import com.edgec.browserbackend.account.exception.AccountErrorCode; import com.edgec.browserbackend.account.exception.AccountErrorCode;
import com.edgec.browserbackend.account.repository.AccountRepository; import com.edgec.browserbackend.account.repository.AccountRepository;
import com.edgec.browserbackend.browser.ErrorCode.BrowserErrorCode; import com.edgec.browserbackend.browser.ErrorCode.BrowserErrorCode;
import com.edgec.browserbackend.browser.domain.IpResource; import com.edgec.browserbackend.browser.domain.*;
import com.edgec.browserbackend.browser.domain.IpStatus;
import com.edgec.browserbackend.browser.domain.IpType;
import com.edgec.browserbackend.browser.dto.*; import com.edgec.browserbackend.browser.dto.*;
import com.edgec.browserbackend.browser.repository.IpResourceRepository; import com.edgec.browserbackend.browser.repository.IpResourceRepository;
import com.edgec.browserbackend.browser.repository.ShopRepository;
import com.edgec.browserbackend.browser.repository.UserShopRepository;
import com.edgec.browserbackend.browser.service.IpResourceService; import com.edgec.browserbackend.browser.service.IpResourceService;
import com.edgec.browserbackend.common.commons.error.ClientRequestException; import com.edgec.browserbackend.common.commons.error.ClientRequestException;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
...@@ -23,6 +23,7 @@ import org.springframework.web.client.RestTemplate; ...@@ -23,6 +23,7 @@ import org.springframework.web.client.RestTemplate;
import java.time.Instant; import java.time.Instant;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
@Service @Service
public class IpResourceServiceImpl implements IpResourceService { public class IpResourceServiceImpl implements IpResourceService {
...@@ -37,12 +38,20 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -37,12 +38,20 @@ public class IpResourceServiceImpl implements IpResourceService {
private static List<String> protocol = Arrays.asList("http", "https", "socks5", "ssr"); private static List<String> protocol = Arrays.asList("http", "https", "socks5", "ssr");
private static String startscript = "";
@Autowired @Autowired
private AccountRepository accountRepository; private AccountRepository accountRepository;
@Autowired @Autowired
private IpResourceRepository ipResourceRepository; private IpResourceRepository ipResourceRepository;
@Autowired
private UserShopRepository userShopRepository;
@Autowired
private ShopRepository shopRepository;
public HttpHeaders buildPostHeader() { public HttpHeaders buildPostHeader() {
HttpHeaders header = new HttpHeaders(); HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_JSON); header.setContentType(MediaType.APPLICATION_JSON);
...@@ -67,10 +76,13 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -67,10 +76,13 @@ public class IpResourceServiceImpl implements IpResourceService {
} }
@Override @Override
public List<IpResourceDto> buyIp(String username, IpResourceRequestDto ipResourceRequestDto) { public List<IpResourceDto> buyIp(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);
if (account.getPermission() < 8)
throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
//todo 预扣费
List<IpResourceDto> ipResourceDtos = new ArrayList<>(); List<IpResourceDto> ipResourceDtos = new ArrayList<>();
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
HttpHeaders header = buildPostHeader(); HttpHeaders header = buildPostHeader();
...@@ -81,20 +93,22 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -81,20 +93,22 @@ public class IpResourceServiceImpl implements IpResourceService {
map.put("provider", ipResourceRequestDto.getVendor()); map.put("provider", ipResourceRequestDto.getVendor());
map.put("unit", ipResourceRequestDto.getUnit()); map.put("unit", ipResourceRequestDto.getUnit());
map.put("amount", String.valueOf(ipResourceRequestDto.getAmount())); map.put("amount", String.valueOf(ipResourceRequestDto.getAmount()));
map.put("loginPassword", makeRandomPassword(16)); String password = makeRandomPassword(16);
map.put("loginPassword", password);
map.put("startscript", startscript);
HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(map, header); HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(map, header);
IpBuyResultDto ipBuyResultDto = null; IpBuyResultDto ipBuyResultDto = null;
try { try {
ipBuyResultDto = restTemplate.postForObject(TESTURL + "/intelligroup/ipresources?accountId=browser", httpEntity, IpBuyResultDto.class); ipBuyResultDto = restTemplate.postForObject(TESTURL + "/intelligroup/ipresources?accountId=browser", httpEntity, IpBuyResultDto.class);
if (StringUtils.isNotBlank(ipBuyResultDto.getErrorCode())) if (StringUtils.isNotBlank(ipBuyResultDto.getErrorCode()))
throw new Exception(ipBuyResultDto.getErrorCode()); throw new Exception(ipBuyResultDto.getErrorCode());
} catch (Exception e) { } catch (Throwable e) {
logger.error("fail to post request", e.getMessage()); logger.error("fail to post request", e.getMessage());
logger.error(e.getMessage()); logger.error(e.getMessage());
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE); throw e;
} }
try { try {
if (ipBuyResultDto != null && ipBuyResultDto.getIplist() != null && ipBuyResultDto.getIplist().size() < 1) { if (ipBuyResultDto != null && ipBuyResultDto.getIplist() != null && ipBuyResultDto.getIplist().size() >= 1) {
ipBuyResultDto.getIplist().forEach(x -> { ipBuyResultDto.getIplist().forEach(x -> {
// IpInfoResultDto ipInfoResultDto = new IpInfoResultDto(); // IpInfoResultDto ipInfoResultDto = new IpInfoResultDto();
// Map<String, String> params = new HashMap<String, String>(); // Map<String, String> params = new HashMap<String, String>();
...@@ -118,11 +132,13 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -118,11 +132,13 @@ public class IpResourceServiceImpl implements IpResourceService {
ipResource.setPort(port); ipResource.setPort(port);
ipResource.setVendor(ipResourceRequestDto.getVendor()); ipResource.setVendor(ipResourceRequestDto.getVendor());
ipResource.setIpStatus(IpStatus.NORMAL); ipResource.setIpStatus(IpStatus.NORMAL);
ipResource.setUsername(username); ipResource.setUsername(USERNAME);
if (account.getParent() != null) if (account.getParent() != null)
ipResource.setUserParent(account.getParent()); ipResource.setUserParent(account.getParent());
ipResource.setRegion(ipResourceRequestDto.getRegion()); ipResource.setRegion(ipResourceRequestDto.getRegion());
ipResource.setProtocol(protocol); ipResource.setProtocol(protocol);
ipResource.setPassword(password);
ipResource.setOwner(username);
ipResourceRepository.save(ipResource); ipResourceRepository.save(ipResource);
ipResourceDtos.add(new IpResourceDto(ipResource, null)); ipResourceDtos.add(new IpResourceDto(ipResource, null));
// } // }
...@@ -137,31 +153,42 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -137,31 +153,42 @@ public class IpResourceServiceImpl implements IpResourceService {
} }
@Override @Override
public void renewIp(String username, IpResourceRequestDto ipResourceRequestDto) throws Exception { public RenewIpReturnDto 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);
ipResourceRequestDto.getAddr().forEach(x -> {
IpResource ipResource = ipResourceRepository.findByAddrAndIsDeleted(x, false);
if (ipResource == null)
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST);
if (ipResource.getShopId() != null) {
UserShop 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);
}
});
//todo 预扣费
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
HashMap<String, Object> map = new HashMap<>(); HashMap<String, Object> map = new HashMap<>();
map.put("iplist", ipResourceRequestDto.getIplist()); map.put("iplist", ipResourceRequestDto.getAddr());
map.put("period", ipResourceRequestDto.getPeriod()); map.put("period", ipResourceRequestDto.getPeriod());
HttpHeaders headers = buildPostHeader(); HttpHeaders headers = buildPostHeader();
HttpEntity<Map<String, Object>> entity = new HttpEntity<>(map, headers); HttpEntity<Map<String, Object>> entity = new HttpEntity<>(map, headers);
ipResourceRequestDto.getIplist().forEach(x -> {
IpResource ipResource = ipResourceRepository.findByAddrAndIsDeleted(x, false);
if (ipResource == null)
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST);
});
try { try {
ResponseEntity<String> result = restTemplate.exchange(TESTURL + "/intelligroup/renewip?accountId=browser", HttpMethod.PUT, entity, String.class); ResponseEntity<String> result = restTemplate.exchange(TESTURL + "/intelligroup/renewip?accountId=browser", HttpMethod.PUT, entity, String.class);
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();
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());
}); });
return renewIpReturnDto;
} 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());
...@@ -177,13 +204,19 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -177,13 +204,19 @@ public class IpResourceServiceImpl implements IpResourceService {
IpResource ipResource = ipResourceRepository.findByAddrAndIsDeleted(ipAddr, false); IpResource ipResource = ipResourceRepository.findByAddrAndIsDeleted(ipAddr, false);
if (ipResource == null) if (ipResource == null)
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST); throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST);
if (ipResource.getShopId() != null) {
UserShop 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(); RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = buildGetHeader(); HttpHeaders headers = buildGetHeader();
Map<String, String> params = new HashMap<String, String>(); Map<String, String> params = new HashMap<String, String>();
params.put("id", "1");
HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(params, headers); HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(params, headers);
try { try {
ResponseEntity<String> result = restTemplate.exchange(TESTURL + "/intelligroup/ipresources?accountId=browser&ip={ip}", HttpMethod.DELETE, httpEntity, String.class); 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); DeleteIpResultDto deleteIpResultDto = JSON.parseObject(result.getBody(), DeleteIpResultDto.class);
if (StringUtils.isNotBlank(deleteIpResultDto.getErrorCode())) if (StringUtils.isNotBlank(deleteIpResultDto.getErrorCode()))
throw new Exception(deleteIpResultDto.getErrorCode()); throw new Exception(deleteIpResultDto.getErrorCode());
...@@ -196,8 +229,67 @@ public class IpResourceServiceImpl implements IpResourceService { ...@@ -196,8 +229,67 @@ public class IpResourceServiceImpl implements IpResourceService {
} }
@Override @Override
public List<IpResourceDto> getIpList(String username, int type, int page, int amount, FilterDto filterDto) { public List<IpResourceDto> getIpList(String username, int type, int page, int amount, IpFilterDto ipFilterDto) {
return null; Account account = accountRepository.findByName(username);
if (account == null)
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
List<String> IpIds = userShopRepository.findByUsername(username).stream().map(x -> x.getShopId()).filter(x -> x == null).collect(Collectors.toList());
List<IpResource> ipResources = ipResourceRepository.findByIdIn(IpIds);
List<IpResource> notUsed = ipResourceRepository.findByOwnerAndShopIdIsNull(username);
ipResources.addAll(notUsed);
List<IpResourceDto> ipResourceDtos = new ArrayList<>();
if (ipFilterDto != null && StringUtils.isNotBlank(ipFilterDto.getRegion())) {
List<String> allIpIds = ipResources.stream().map(x -> x.getId()).collect(Collectors.toList());
List<IpResource> ipResources1 = ipResourceRepository.findByRegionLikeAndIdIn(ipFilterDto.getRegion(), allIpIds);
ipResources1.forEach(x -> {
Shop shop = shopRepository.findById(x.getShopId()).orElse(null);
ShopDto shopDto;
if (shop != null)
shopDto = new ShopDto(shop);
else
shopDto = new ShopDto();
ipResourceDtos.add(new IpResourceDto(x, shopDto));
});
}
else if (ipFilterDto != null && StringUtils.isNotBlank(ipFilterDto.getAddr())) {
List<String> allIpIds = ipResources.stream().map(x -> x.getId()).collect(Collectors.toList());
List<IpResource> ipResources1 = ipResourceRepository.findByAddrLikeAndIdIn(ipFilterDto.getAddr(), allIpIds);
ipResources1.forEach(x -> {
Shop shop = shopRepository.findById(x.getShopId()).orElse(null);
ShopDto shopDto;
if (shop != null)
shopDto = new ShopDto(shop);
else
shopDto = new ShopDto();
ipResourceDtos.add(new IpResourceDto(x, shopDto));
});
}
else if (ipFilterDto != null && StringUtils.isNotBlank(ipFilterDto.getVendor())) {
List<String> allIpIds = ipResources.stream().map(x -> x.getId()).collect(Collectors.toList());
List<IpResource> ipResources1 = ipResourceRepository.findByVendorLikeAndIdIn(ipFilterDto.getVendor(), allIpIds);
ipResources1.forEach(x -> {
Shop shop = shopRepository.findById(x.getShopId()).orElse(null);
ShopDto shopDto;
if (shop != null)
shopDto = new ShopDto(shop);
else
shopDto = new ShopDto();
ipResourceDtos.add(new IpResourceDto(x, shopDto));
});
} else {
ipResources.forEach(x -> {
Shop shop = shopRepository.findById(x.getShopId()).orElse(null);
ShopDto shopDto;
if (shop != null)
shopDto = new ShopDto(shop);
else
shopDto = new ShopDto();
ipResourceDtos.add(new IpResourceDto(x, shopDto));
});
}
return ipResourceDtos;
} }
......
package com.edgec.browserbackend.browser.service.Impl; package com.edgec.browserbackend.browser.service.Impl;
import com.edgec.browserbackend.account.domain.Account; import com.edgec.browserbackend.account.domain.Account;
import com.edgec.browserbackend.account.domain.User;
import com.edgec.browserbackend.account.exception.AccountErrorCode; import com.edgec.browserbackend.account.exception.AccountErrorCode;
import com.edgec.browserbackend.account.repository.AccountRepository; import com.edgec.browserbackend.account.repository.AccountRepository;
import com.edgec.browserbackend.browser.ErrorCode.BrowserErrorCode; import com.edgec.browserbackend.browser.ErrorCode.BrowserErrorCode;
...@@ -8,7 +9,7 @@ import com.edgec.browserbackend.browser.domain.Group; ...@@ -8,7 +9,7 @@ import com.edgec.browserbackend.browser.domain.Group;
import com.edgec.browserbackend.browser.domain.IpResource; import com.edgec.browserbackend.browser.domain.IpResource;
import com.edgec.browserbackend.browser.domain.Shop; import com.edgec.browserbackend.browser.domain.Shop;
import com.edgec.browserbackend.browser.domain.UserShop; import com.edgec.browserbackend.browser.domain.UserShop;
import com.edgec.browserbackend.browser.dto.FilterDto; import com.edgec.browserbackend.browser.dto.ShopFilterDto;
import com.edgec.browserbackend.browser.dto.ShopResultDto; import com.edgec.browserbackend.browser.dto.ShopResultDto;
import com.edgec.browserbackend.browser.dto.ShopPageInfo; import com.edgec.browserbackend.browser.dto.ShopPageInfo;
import com.edgec.browserbackend.browser.dto.ShopPageResultDto; import com.edgec.browserbackend.browser.dto.ShopPageResultDto;
...@@ -84,6 +85,9 @@ public class ShopServiceImpl implements ShopService { ...@@ -84,6 +85,9 @@ public class ShopServiceImpl implements ShopService {
userShop.setGroupId(shopResultDto.getGroup()); userShop.setGroupId(shopResultDto.getGroup());
} }
userShopRepository.save(userShop); userShopRepository.save(userShop);
//可以优化
account.setShopCount(account.getShopCount() + 1);
accountRepository.save(account);
}catch (Exception e) { }catch (Exception e) {
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE); throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
} }
...@@ -144,6 +148,8 @@ public class ShopServiceImpl implements ShopService { ...@@ -144,6 +148,8 @@ public class ShopServiceImpl implements ShopService {
boolean result = userShopRepository.deleteByUsernameAndShopId(username, shopId); boolean result = userShopRepository.deleteByUsernameAndShopId(username, shopId);
if (result) { if (result) {
shopRepository.deleteById(shopId); shopRepository.deleteById(shopId);
account.setShopCount(account.getShopCount() - 1);
accountRepository.save(account);
} else throw new ClientRequestException(AccountErrorCode.NOPERMISSION); } else throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
} }
...@@ -177,6 +183,8 @@ public class ShopServiceImpl implements ShopService { ...@@ -177,6 +183,8 @@ public class ShopServiceImpl implements ShopService {
if (shop.getShopName()!=null) if (shop.getShopName()!=null)
ipResource.setShopName(shop.getShopName()); ipResource.setShopName(shop.getShopName());
ipResourceRepository.save(ipResource); ipResourceRepository.save(ipResource);
userShop.setIpId(ipResource.getId());
userShopRepository.save(userShop);
} catch (Exception e) { } catch (Exception e) {
logger.error("fail to bind shop and ip", e.getMessage()); logger.error("fail to bind shop and ip", e.getMessage());
throw new ClientRequestException(BrowserErrorCode.UNKNOWN); throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
...@@ -207,7 +215,10 @@ public class ShopServiceImpl implements ShopService { ...@@ -207,7 +215,10 @@ public class ShopServiceImpl implements ShopService {
shop.setIpRegion(null); shop.setIpRegion(null);
ipResource.setShopId(null); ipResource.setShopId(null);
ipResource.setShopName(null); ipResource.setShopName(null);
ipResourceRepository.save(ipResource);
shopRepository.save(shop); shopRepository.save(shop);
userShop.setIpId(ipResource.getId());
userShopRepository.save(userShop);
} catch (Exception e) { } catch (Exception e) {
logger.error("fail to unbind", e.getMessage()); logger.error("fail to unbind", e.getMessage());
throw new ClientRequestException(BrowserErrorCode.UNKNOWN); throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
...@@ -261,7 +272,10 @@ public class ShopServiceImpl implements ShopService { ...@@ -261,7 +272,10 @@ public class ShopServiceImpl implements ShopService {
UserShop userShop1 = userShopRepository.findByUsernameAndShopId(x.getName(), shop.getShopId()); UserShop userShop1 = userShopRepository.findByUsernameAndShopId(x.getName(), shop.getShopId());
if (userShop1 != null) if (userShop1 != null)
return; return;
userShopRepository.save(new UserShop(x.getName(), shop.getShopId())); userShop1 = new UserShop(x.getName(), shop.getShopId(), "-1");
if (shop.getIpId() != null)
userShop1.setIpId(shop.getIpId());
userShopRepository.save(userShop1);
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("fail to assign", e.getMessage()); logger.error("fail to assign", e.getMessage());
...@@ -271,7 +285,7 @@ public class ShopServiceImpl implements ShopService { ...@@ -271,7 +285,7 @@ public class ShopServiceImpl implements ShopService {
} }
@Override @Override
public ShopPageResultDto getShopList(String username, String groupId, int page, int amout, FilterDto filterDto) { public ShopPageResultDto getShopList(String username, String groupId, int page, int amout, ShopFilterDto shopFilterDto) {
if (amout > 100) if (amout > 100)
amout = 100; amout = 100;
Pageable pageable = PageRequest.of(page, amout); Pageable pageable = PageRequest.of(page, amout);
...@@ -297,12 +311,12 @@ public class ShopServiceImpl implements ShopService { ...@@ -297,12 +311,12 @@ public class ShopServiceImpl implements ShopService {
} }
logger.error("shopIds.size " + shopIds.size()); logger.error("shopIds.size " + shopIds.size());
Page<Shop> shops; Page<Shop> shops;
if (filterDto != null && StringUtils.isNotBlank(filterDto.getIpRegion())) if (shopFilterDto != null && StringUtils.isNotBlank(shopFilterDto.getIpRegion()))
shops = shopRepository.findByShopIdInAndIpRegionLike(shopIds, filterDto.getIpRegion(), pageable); shops = shopRepository.findByShopIdInAndIpRegionLike(shopIds, shopFilterDto.getIpRegion(), pageable);
else if (filterDto != null && StringUtils.isNotBlank(filterDto.getShopAccount())) else if (shopFilterDto != null && StringUtils.isNotBlank(shopFilterDto.getShopAccount()))
shops = shopRepository.findByShopIdInAndShopAccountLike(shopIds, filterDto.getShopAccount(), pageable); shops = shopRepository.findByShopIdInAndShopAccountLike(shopIds, shopFilterDto.getShopAccount(), pageable);
else if (filterDto != null && StringUtils.isNotBlank(filterDto.getShopName())) else if (shopFilterDto != null && StringUtils.isNotBlank(shopFilterDto.getShopName()))
shops = shopRepository.findByShopIdInAndShopNameLike(shopIds, filterDto.getShopName(), pageable); shops = shopRepository.findByShopIdInAndShopNameLike(shopIds, shopFilterDto.getShopName(), pageable);
else else
shops = shopRepository.findByShopIdIn(shopIds, pageable); shops = shopRepository.findByShopIdIn(shopIds, pageable);
if (shops == null || shops.getNumberOfElements() < 1) if (shops == null || shops.getNumberOfElements() < 1)
......
package com.edgec.browserbackend.browser.service; package com.edgec.browserbackend.browser.service;
import com.edgec.browserbackend.browser.dto.FilterDto; import com.edgec.browserbackend.browser.dto.*;
import com.edgec.browserbackend.browser.dto.IpResourceDto;
import com.edgec.browserbackend.browser.dto.IpResourceRequestDto;
import java.util.List; import java.util.List;
public interface IpResourceService { public interface IpResourceService {
List<IpResourceDto> buyIp(String username, IpResourceRequestDto ipResourceRequestDto); List<IpResourceDto> buyIp(String username, IpResourceRequestDto ipResourceRequestDto) throws Exception;
void renewIp(String username, IpResourceRequestDto ipResourceRequestDto) throws Exception; RenewIpReturnDto renewIp(String username, IpResourceRequestDto ipResourceRequestDto) throws Exception;
void deleteIp(String username, String ipAddr) throws Exception; void deleteIp(String username, String ipAddr) throws Exception;
List<IpResourceDto> getIpList(String username, int type, int page, int amount, FilterDto filterDto); List<IpResourceDto> getIpList(String username, int type, int page, int amount, IpFilterDto ipFilterDto);
} }
package com.edgec.browserbackend.browser.service; package com.edgec.browserbackend.browser.service;
import com.edgec.browserbackend.browser.domain.Shop; import com.edgec.browserbackend.browser.domain.Shop;
import com.edgec.browserbackend.browser.dto.FilterDto; import com.edgec.browserbackend.browser.dto.ShopFilterDto;
import com.edgec.browserbackend.browser.dto.ShopPageResultDto; import com.edgec.browserbackend.browser.dto.ShopPageResultDto;
import com.edgec.browserbackend.browser.dto.ShopResultDto; import com.edgec.browserbackend.browser.dto.ShopResultDto;
...@@ -25,5 +25,5 @@ public interface ShopService { ...@@ -25,5 +25,5 @@ public interface ShopService {
void assignShops(String username, List<String> shopIds, List<String> users); void assignShops(String username, List<String> shopIds, List<String> users);
ShopPageResultDto getShopList(String username, String groupId, int page, int amount, FilterDto filterDto); ShopPageResultDto getShopList(String username, String groupId, int page, int amount, ShopFilterDto shopFilterDto);
} }
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