Commit 82fec7a6 authored by renjie's avatar renjie

店铺绑定解绑接口

parent 36b44b1c
......@@ -71,10 +71,10 @@ public class IpControlloer {
}
@RequestMapping(value = "/list", method = RequestMethod.POST)
public ResultDto getIpList(Principal principal, IpListRequestDto ipListRequestDto) {
public ResultDto getIpList(Principal principal, @RequestBody IpListRequestDto ipListRequestDto) {
ResultDto resultDto = new ResultDto();
try {
List<IpResourceDto> ipResourceDto = ipResourceService.getIpList(principal.getName(), ipListRequestDto.getType(), ipListRequestDto.getPage(),
IpPageResultDto ipResourceDto = ipResourceService.getIpList(principal.getName(), ipListRequestDto.getGroupType(), ipListRequestDto.getPage(),
ipListRequestDto.getAmount(), ipListRequestDto.getIpFilterDto());
resultDto.setData(ipResourceDto);
resultDto.setStatus(0);
......
......@@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.ArrayList;
import java.util.List;
@Document(collection = "ipresource")
......@@ -16,12 +17,13 @@ public class IpResource {
private IpType ipType = IpType.VENDOR;
private String vendor;
private String region;
private IpStatus ipStatus;
private List<Interval> port;
//0:正常, 1:已过期, 2:即将过期
private int status;
private List<String> port;
private long purchasedTime;
private long validTime;
private String details;
List<String> bindHistory;
List<String> bindHistory = new ArrayList<>();
private boolean isDeleted;
private String username;
private String owner;
......@@ -55,12 +57,12 @@ public class IpResource {
this.region = region;
}
public IpStatus getIpStatus() {
return ipStatus;
public int getStatus() {
return status;
}
public void setIpStatus(IpStatus ipStatus) {
this.ipStatus = ipStatus;
public void setStatus(int status) {
this.status = status;
}
public IpType getIpType() {
......@@ -127,11 +129,11 @@ public class IpResource {
this.username = username;
}
public List<Interval> getPort() {
public List<String> getPort() {
return port;
}
public void setPort(List<Interval> port) {
public void setPort(List<String> port) {
this.port = port;
}
......
package com.edgec.browserbackend.browser.dto;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.ArrayList;
import java.util.List;
@JsonIgnoreProperties(ignoreUnknown = true)
public class IpListRequestDto {
private int type;
private int groupType;
private int page;
private int amount;
private IpFilterDto ipFilterDto;
private List<String> addr = new ArrayList<>();
public int getAmount() {
return amount;
......@@ -18,12 +20,12 @@ public class IpListRequestDto {
this.amount = amount;
}
public int getType() {
return type;
public int getGroupType() {
return groupType;
}
public void setType(int type) {
this.type = type;
public void setGroupType(int groupType) {
this.groupType = groupType;
}
public int getPage() {
......@@ -41,12 +43,4 @@ public class IpListRequestDto {
public void setIpFilterDto(IpFilterDto ipFilterDto) {
this.ipFilterDto = ipFilterDto;
}
public List<String> getAddr() {
return addr;
}
public void setAddr(List<String> addr) {
this.addr = addr;
}
}
package com.edgec.browserbackend.browser.dto;
import java.util.List;
/**
* @Desc
* @Author jason
* @CreateTime 2020/3/11 11:13 上午
**/
public class IpPageResultDto<T> {
List<T> ipList;
PageInfo ipPage;
public List<T> getIpList() {
return ipList;
}
public void setIpList(List<T> ipList) {
this.ipList = ipList;
}
public PageInfo getIpPage() {
return ipPage;
}
public void setIpPage(PageInfo ipPage) {
this.ipPage = ipPage;
}
}
......@@ -14,8 +14,8 @@ public class IpResourceDto {
private String addr;
private String vendor;
private String region;
private IpStatus ipStatus;
private List<Interval> port;
private int status;
private List<String> port;
private long purchasedTIme;
private long validTime;
private String username;
......@@ -32,7 +32,7 @@ public class IpResourceDto {
this.addr = ipResource.getAddr();
this.vendor = ipResource.getVendor();
this.region = ipResource.getRegion();
this.ipStatus = ipResource.getIpStatus();
this.status = ipResource.getStatus();
this.port = ipResource.getPort();
this.purchasedTIme = ipResource.getPurchasedTime();
this.validTime = ipResource.getValidTime();
......@@ -51,11 +51,11 @@ public class IpResourceDto {
this.id = id;
}
public List<Interval> getPort() {
public List<String> getPort() {
return port;
}
public void setPort(List<Interval> port) {
public void setPort(List<String> port) {
this.port = port;
}
......@@ -99,12 +99,12 @@ public class IpResourceDto {
this.purchasedTIme = purchasedTIme;
}
public IpStatus getIpStatus() {
return ipStatus;
public int getStatus() {
return status;
}
public void setIpStatus(IpStatus ipStatus) {
this.ipStatus = ipStatus;
public void setStatus(int status) {
this.status = status;
}
public String getDetails() {
......
package com.edgec.browserbackend.browser.dto;
public class ShopPageInfo {
public class PageInfo {
int currentPage;
int totalPages;
int totalShops;
......
......@@ -2,23 +2,23 @@ package com.edgec.browserbackend.browser.dto;
import java.util.List;
public class ShopPageResultDto {
List<ShopResultDto> shopList;
ShopPageInfo shopPage;
public class ShopPageResultDto<T> {
List<T> shopList;
PageInfo shopPage;
public List<ShopResultDto> getShopList() {
public List<T> getShopList() {
return shopList;
}
public void setShopList(List<ShopResultDto> shopList) {
public void setShopList(List<T> shopList) {
this.shopList = shopList;
}
public ShopPageInfo getShopPage() {
public PageInfo getShopPage() {
return shopPage;
}
public void setShopPage(ShopPageInfo shopPage) {
public void setShopPage(PageInfo shopPage) {
this.shopPage = shopPage;
}
}
package com.edgec.browserbackend.browser.repository;
import com.edgec.browserbackend.browser.domain.IpResource;
import com.google.gson.internal.$Gson$Preconditions;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
public interface IpResourceRepository extends MongoRepository<IpResource, String> {
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);
List<IpResource> findByOwnerAndShopIdIsNull(String owner);
List<IpResource> findByOwnerAndStatusAndShopIdIsNull(String owner, int status);
List<IpResource> findByStatusAndIdIn(int status, List<String> ipIds);
Page<IpResource> findByAddrLikeAndIdIn(String addr, List<String> ipIds, Pageable pageable);
Page<IpResource> findByVendorLikeAndIdIn(String vendor, List<String> ipIds, Pageable pageable);
Page<IpResource> findByRegionLikeAndIdIn(String region, List<String> ipIds, Pageable pageable);
}
......@@ -2,7 +2,6 @@ package com.edgec.browserbackend.browser.service.Impl;
import com.alibaba.fastjson.JSON;
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.repository.AccountRepository;
import com.edgec.browserbackend.browser.ErrorCode.BrowserErrorCode;
......@@ -17,6 +16,10 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
......@@ -34,9 +37,9 @@ public class IpResourceServiceImpl implements IpResourceService {
private static String TESTURL = "http://112.74.13.2";
private static String USERNAME = "fangguanlianbrowser";
private static List<Interval> port = Arrays.asList(new Interval("22"), new Interval("443"), new Interval("20000"), new Interval("20001"));
private static List<String> port = Arrays.asList("20000", "20001");
private static List<String> protocol = Arrays.asList("http", "https", "socks5", "ssr");
private static List<String> protocol = Arrays.asList("socks5", "proxy");
private static String startscript = "";
......@@ -131,7 +134,7 @@ public class IpResourceServiceImpl implements IpResourceService {
ipResource.setValidTime(Instant.now().plusSeconds(60*60*24*7).toEpochMilli());
ipResource.setPort(port);
ipResource.setVendor(ipResourceRequestDto.getVendor());
ipResource.setIpStatus(IpStatus.NORMAL);
ipResource.setStatus(0);
ipResource.setUsername(USERNAME);
if (account.getParent() != null)
ipResource.setUserParent(account.getParent());
......@@ -204,8 +207,9 @@ public class IpResourceServiceImpl implements IpResourceService {
IpResource ipResource = ipResourceRepository.findByAddrAndIsDeleted(ipAddr, false);
if (ipResource == null)
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST);
UserShop userShop = null;
if (ipResource.getShopId() != null) {
UserShop userShop = userShopRepository.findByUsernameAndShopId(username, ipResource.getShopId());
userShop = userShopRepository.findByUsernameAndShopId(username, ipResource.getShopId());
if (userShop == null)
throw new ClientRequestException(AccountErrorCode.NOPERMISSION);
} else if (!ipResource.getOwner().equals(username)) {
......@@ -221,6 +225,17 @@ public class IpResourceServiceImpl implements IpResourceService {
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);
}
} catch (Exception e) {
logger.error("fail to renew ip", e.getMessage());
logger.error(e.getMessage());
......@@ -229,67 +244,112 @@ public class IpResourceServiceImpl implements IpResourceService {
}
@Override
public List<IpResourceDto> getIpList(String username, int type, int page, int amount, IpFilterDto ipFilterDto) {
public IpPageResultDto getIpList(String username, int groupType, int page, int amount, IpFilterDto ipFilterDto) {
if (amount > 100)
amount = 100;
Pageable pageable = PageRequest.of(page, amount);
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<>();
List<String> ipIds = userShopRepository.findByUsername(username).stream().filter(x -> (x.getShopId() != null && x.getIpId() != null))
.map(x -> x.getIpId()).collect(Collectors.toList());
List<IpResource> ipResources = new ArrayList<>();
List<IpResource> notUsed = null;
switch (groupType) {
case 1:
ipResources = ipResourceRepository.findByIdIn(ipIds);;
notUsed = ipResourceRepository.findByOwnerAndShopIdIsNull(username);
break;
case 2:
ipResources = ipResourceRepository.findByStatusAndIdIn(2, ipIds);;
notUsed = ipResourceRepository.findByOwnerAndStatusAndShopIdIsNull(username, 2);
break;
case 3:
ipResources = ipResourceRepository.findByStatusAndIdIn(1, ipIds);;
notUsed = ipResourceRepository.findByOwnerAndStatusAndShopIdIsNull(username, 1);
break;
case 4:
notUsed = ipResourceRepository.findByOwnerAndShopIdIsNull(username);
break;
}
if (notUsed != null)
ipResources.addAll(notUsed);
List<IpResourceDto> ipResourceDtos = new ArrayList<>();
List<String> allIpIds = ipResources.stream().map(x -> x.getId()).collect(Collectors.toList());
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);
Page<IpResource> ipResources1 = ipResourceRepository.findByRegionLikeAndIdIn(ipFilterDto.getRegion(), allIpIds, pageable);
ipResources1.getContent().forEach(x -> {
ShopDto shopDto;
if (shop != null)
shopDto = new ShopDto(shop);
else
if (x.getShopId() == null) {
shopDto = new ShopDto();
} else {
Shop shop = shopRepository.findById(x.getShopId()).orElse(null);
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);
Page<IpResource> ipResources1 = ipResourceRepository.findByAddrLikeAndIdIn(ipFilterDto.getAddr(), allIpIds, pageable);
ipResources1.getContent().forEach(x -> {
ShopDto shopDto;
if (shop != null)
shopDto = new ShopDto(shop);
else
if (x.getShopId() == null) {
shopDto = new ShopDto();
} else {
Shop shop = shopRepository.findById(x.getShopId()).orElse(null);
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);
Page<IpResource> ipResources1 = ipResourceRepository.findByVendorLikeAndIdIn(ipFilterDto.getVendor(), allIpIds, pageable);
ipResources1.getContent().forEach(x -> {
ShopDto shopDto;
if (shop != null)
shopDto = new ShopDto(shop);
else
if (x.getShopId() == null) {
shopDto = new ShopDto();
} else {
Shop shop = shopRepository.findById(x.getShopId()).orElse(null);
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
if (x.getShopId() == null) {
shopDto = new ShopDto();
} else {
Shop shop = shopRepository.findById(x.getShopId()).orElse(null);
if (shop != null)
shopDto = new ShopDto(shop);
else
shopDto = new ShopDto();
}
ipResourceDtos.add(new IpResourceDto(x, shopDto));
});
}
return ipResourceDtos;
Page<IpResourceDto> ipResourceDtoPage = new PageImpl<>(ipResourceDtos, pageable, allIpIds.size());
IpPageResultDto ipPageResultDto = new IpPageResultDto();
ipPageResultDto.setIpList(ipResourceDtoPage.getContent());
PageInfo pageInfo = new PageInfo();
pageInfo.setCurrentPage(ipResourceDtoPage.getPageable().getPageNumber());
pageInfo.setTotalPages(ipResourceDtoPage.getTotalPages());
pageInfo.setTotalShops(allIpIds.size());
ipPageResultDto.setIpPage(pageInfo);
return ipPageResultDto;
}
......
package com.edgec.browserbackend.browser.service.Impl;
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.repository.AccountRepository;
import com.edgec.browserbackend.browser.ErrorCode.BrowserErrorCode;
......@@ -11,7 +10,7 @@ import com.edgec.browserbackend.browser.domain.Shop;
import com.edgec.browserbackend.browser.domain.UserShop;
import com.edgec.browserbackend.browser.dto.ShopFilterDto;
import com.edgec.browserbackend.browser.dto.ShopResultDto;
import com.edgec.browserbackend.browser.dto.ShopPageInfo;
import com.edgec.browserbackend.browser.dto.PageInfo;
import com.edgec.browserbackend.browser.dto.ShopPageResultDto;
import com.edgec.browserbackend.browser.repository.GroupRepository;
import com.edgec.browserbackend.browser.repository.IpResourceRepository;
......@@ -68,7 +67,11 @@ public class ShopServiceImpl implements ShopService {
if (account.getShopCount() >= 10000) {
throw new ClientRequestException(AccountErrorCode.SHOPMAX);
}
UserShop us = userShopRepository.findByUsernameAndShopId(username, shopResultDto.getShopId());
UserShop us = null;
if (shopResultDto.getShopId() != null)
us = userShopRepository.findByUsernameAndShopId(username, shopResultDto.getShopId());
if (shopResultDto.getGroup() == null)
throw new ClientRequestException(BrowserErrorCode.INFORMATIONNOTCOMPELETE);
Group group = groupRepository.findById(shopResultDto.getGroup()).orElse(null);
if (group == null) {
throw new ClientRequestException(BrowserErrorCode.GROUPNOTEXIST);
......@@ -285,10 +288,10 @@ public class ShopServiceImpl implements ShopService {
}
@Override
public ShopPageResultDto getShopList(String username, String groupId, int page, int amout, ShopFilterDto shopFilterDto) {
if (amout > 100)
amout = 100;
Pageable pageable = PageRequest.of(page, amout);
public ShopPageResultDto getShopList(String username, String groupId, int page, int amount, ShopFilterDto shopFilterDto) {
if (amount > 100)
amount = 100;
Pageable pageable = PageRequest.of(page, amount);
Account account = accountRepository.findByName(username);
if (account == null) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
......@@ -332,11 +335,11 @@ public class ShopServiceImpl implements ShopService {
Page<ShopResultDto> shopDtoPage = new PageImpl<>(shopResultDtos, pageable, shopIds.size());
ShopPageResultDto shopPageResultDto = new ShopPageResultDto();
shopPageResultDto.setShopList(shopDtoPage.getContent());
ShopPageInfo shopPageInfo = new ShopPageInfo();
shopPageInfo.setCurrentPage(shopDtoPage.getPageable().getPageNumber());
shopPageInfo.setTotalPages(shopDtoPage.getTotalPages());
shopPageInfo.setTotalShops(shopIds.size());
shopPageResultDto.setShopPage(shopPageInfo);
PageInfo pageInfo = new PageInfo();
pageInfo.setCurrentPage(shopDtoPage.getPageable().getPageNumber());
pageInfo.setTotalPages(shopDtoPage.getTotalPages());
pageInfo.setTotalShops(shopIds.size());
shopPageResultDto.setShopPage(pageInfo);
return shopPageResultDto;
}
}
......@@ -12,5 +12,5 @@ public interface IpResourceService {
void deleteIp(String username, String ipAddr) throws Exception;
List<IpResourceDto> getIpList(String username, int type, 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