Commit 70d76bbd authored by renjie's avatar renjie

Merge branch 'dev-zrj' into 'staging'

共有ip修改

See merge request !139
parents ea679c63 869ec8ce
...@@ -42,8 +42,8 @@ public class IpResource implements Serializable { ...@@ -42,8 +42,8 @@ public class IpResource implements Serializable {
private String proxyUsername; //专线的代理用户名 private String proxyUsername; //专线的代理用户名
private String proxyPassword; //专线的代理密码 private String proxyPassword; //专线的代理密码
private String shopId; private List<String> shopIds = new ArrayList<>();
private String shopName; private boolean bind;
private boolean isLocked; private boolean isLocked;
private long lockTimestamp; private long lockTimestamp;
...@@ -239,20 +239,12 @@ public class IpResource implements Serializable { ...@@ -239,20 +239,12 @@ public class IpResource implements Serializable {
this.price = price; this.price = price;
} }
public String getShopId() { public List<String> getShopIds() {
return shopId; return shopIds;
} }
public void setShopId(String shopId) { public void setShopIds(List<String> shopIds) {
this.shopId = shopId; this.shopIds = shopIds;
}
public String getShopName() {
return shopName;
}
public void setShopName(String shopName) {
this.shopName = shopName;
} }
public String getVendorCn() { public String getVendorCn() {
...@@ -302,4 +294,12 @@ public class IpResource implements Serializable { ...@@ -302,4 +294,12 @@ public class IpResource implements Serializable {
public void setUsingSpecialLine(boolean usingSpecialLine) { public void setUsingSpecialLine(boolean usingSpecialLine) {
this.usingSpecialLine = usingSpecialLine; this.usingSpecialLine = usingSpecialLine;
} }
public boolean isBind() {
return bind;
}
public void setBind(boolean bind) {
this.bind = bind;
}
} }
...@@ -21,7 +21,8 @@ public class IpResourceDto { ...@@ -21,7 +21,8 @@ public class IpResourceDto {
private String details; private String details;
private String password; private String password;
private List<String> protocol; private List<String> protocol;
ShopDto bindShop; List<ShopDto> bindShops;
private ShopDto bindShop;
private List<BindHistory> bindHistories; private List<BindHistory> bindHistories;
private boolean specialLine; private boolean specialLine;
...@@ -36,7 +37,7 @@ public class IpResourceDto { ...@@ -36,7 +37,7 @@ public class IpResourceDto {
public IpResourceDto(){ public IpResourceDto(){
} }
public IpResourceDto(IpResource ipResource, ShopDto shopDto, boolean useHistory){ public IpResourceDto(IpResource ipResource, List<ShopDto> shopDtos, boolean useHistory){
this.id = ipResource.getId(); this.id = ipResource.getId();
this.addr = ipResource.getAddr(); this.addr = ipResource.getAddr();
this.vendor = ipResource.getVendor(); this.vendor = ipResource.getVendor();
...@@ -50,15 +51,18 @@ public class IpResourceDto { ...@@ -50,15 +51,18 @@ public class IpResourceDto {
this.password = ipResource.getPassword(); this.password = ipResource.getPassword();
this.protocol = ipResource.getProtocol(); this.protocol = ipResource.getProtocol();
this.specialLine = ipResource.isSpecialLine(); this.specialLine = ipResource.isSpecialLine();
if (shopDto != null) if (shopDtos != null)
this.bindShop = shopDto; this.bindShops = shopDtos;
else
this.bindShops = new ArrayList<>();
if (useHistory) if (useHistory)
this.bindHistories = ipResource.getBindHistory(); this.bindHistories = ipResource.getBindHistory();
else else
this.bindHistories = new ArrayList<>(); this.bindHistories = new ArrayList<>();
this.bindShop = new ShopDto();
} }
public IpResourceDto(IpResource ipResource, ShopDto shopDto, boolean useHistory, SpecialLine specialLine1){ public IpResourceDto(IpResource ipResource, List<ShopDto> shopDtos, boolean useHistory, SpecialLine specialLine1){
this.id = ipResource.getId(); this.id = ipResource.getId();
this.addr = ipResource.getAddr(); this.addr = ipResource.getAddr();
this.vendor = ipResource.getVendor(); this.vendor = ipResource.getVendor();
...@@ -76,12 +80,16 @@ public class IpResourceDto { ...@@ -76,12 +80,16 @@ public class IpResourceDto {
this.proxyPort = specialLine1.getProxyPort(); this.proxyPort = specialLine1.getProxyPort();
this.proxyProtocol = specialLine1.getProxyProtocol(); this.proxyProtocol = specialLine1.getProxyProtocol();
this.specialLine = ipResource.isSpecialLine(); this.specialLine = ipResource.isSpecialLine();
this.bindShop = shopDto; if (shopDtos != null)
this.bindShops = shopDtos;
else
this.bindShops = new ArrayList<>();
this.specialLineIp = specialLine1.getIp(); this.specialLineIp = specialLine1.getIp();
if (useHistory) if (useHistory)
this.bindHistories = ipResource.getBindHistory(); this.bindHistories = ipResource.getBindHistory();
else else
this.bindHistories = new ArrayList<>(); this.bindHistories = new ArrayList<>();
this.bindShop = new ShopDto();
} }
public String getId() { public String getId() {
...@@ -172,12 +180,12 @@ public class IpResourceDto { ...@@ -172,12 +180,12 @@ public class IpResourceDto {
this.password = password; this.password = password;
} }
public ShopDto getBindShop() { public List<ShopDto> getBindShops() {
return bindShop; return bindShops;
} }
public void setBindShop(ShopDto bindShop) { public void setBindShops(List<ShopDto> bindShops) {
this.bindShop = bindShop; this.bindShops = bindShops;
} }
public List<String> getProtocol() { public List<String> getProtocol() {
...@@ -243,4 +251,12 @@ public class IpResourceDto { ...@@ -243,4 +251,12 @@ public class IpResourceDto {
public void setProxyPort(List<String> proxyPort) { public void setProxyPort(List<String> proxyPort) {
this.proxyPort = proxyPort; this.proxyPort = proxyPort;
} }
public ShopDto getBindShop() {
return bindShop;
}
public void setBindShop(ShopDto bindShop) {
this.bindShop = bindShop;
}
} }
package com.edgec.browserbackend.browser.dto;
import com.edgec.browserbackend.browser.domain.BindHistory;
import com.edgec.browserbackend.browser.domain.IpResource;
import com.edgec.browserbackend.browser.domain.IpType;
import com.edgec.browserbackend.browser.domain.Vendor;
import org.springframework.data.annotation.Id;
import java.util.ArrayList;
import java.util.List;
public class IpResourceUnwindResultDto {
private String id;
private String addr;
private IpType ipType = IpType.VENDOR;
private Vendor vendor;
private String vendorCn;
private String region;
private String regionCn;
//0:正常, 1:已过期, 2:即将过期, 3:正在分配, 4:未使用, 5:已失效, 6:未分配, 7:未缴费, 8:分配成功
private int status;
private List<String> port;
private long purchasedTime;
private long validTime;
private String details;
private List<BindHistory> bindHistory = new ArrayList<>();
private boolean isDeleted;
private String username;
private String owner;
private String userParent;
private List<String> protocol;
private String password;
private double price;
private boolean specialLine; //是否使用专线
private boolean usingSpecialLine; //是否正在使用专线
//专线数据
private String proxyUsername; //专线的代理用户名
private String proxyPassword; //专线的代理密码
private String shopId;
private boolean bind;
private boolean isLocked;
private long lockTimestamp;
private long healthLockTimestamp;
private String unit;
private int period;
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getRegionCn() {
return regionCn;
}
public void setRegionCn(String regionCn) {
this.regionCn = regionCn;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public IpType getIpType() {
return ipType;
}
public void setIpType(IpType ipType) {
this.ipType = ipType;
}
public List<BindHistory> getBindHistory() {
return bindHistory;
}
public void setBindHistory(List<BindHistory> bindHistory) {
this.bindHistory = bindHistory;
}
public long getPurchasedTime() {
return purchasedTime;
}
public void setPurchasedTime(long purchasedTime) {
this.purchasedTime = purchasedTime;
}
public long getValidTime() {
return validTime;
}
public void setValidTime(long validTime) {
this.validTime = validTime;
}
public Vendor getVendor() {
return vendor;
}
public void setVendor(Vendor vendor) {
this.vendor = vendor;
}
public boolean isDeleted() {
return isDeleted;
}
public void setDeleted(boolean deleted) {
isDeleted = deleted;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public List<String> getPort() {
return port;
}
public void setPort(List<String> port) {
this.port = port;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getUserParent() {
return userParent;
}
public void setUserParent(String userParent) {
this.userParent = userParent;
}
public List<String> getProtocol() {
return protocol;
}
public void setProtocol(List<String> protocol) {
this.protocol = protocol;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public boolean isLocked() {
return isLocked;
}
public void setLocked(boolean locked) {
isLocked = locked;
}
public long getLockTimestamp() {
return lockTimestamp;
}
public void setLockTimestamp(long lockTimestamp) {
this.lockTimestamp = lockTimestamp;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public int getPeriod() {
return period;
}
public void setPeriod(int period) {
this.period = period;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getShopId() {
return shopId;
}
public void setShopId(String shopId) {
this.shopId = shopId;
}
public String getVendorCn() {
return vendorCn;
}
public void setVendorCn(String vendorCn) {
this.vendorCn = vendorCn;
}
public String getProxyPassword() {
return proxyPassword;
}
public void setProxyPassword(String proxyPassword) {
this.proxyPassword = proxyPassword;
}
public String getProxyUsername() {
return proxyUsername;
}
public void setProxyUsername(String proxyUsername) {
this.proxyUsername = proxyUsername;
}
public boolean isSpecialLine() {
return specialLine;
}
public void setSpecialLine(boolean specialLine) {
this.specialLine = specialLine;
}
public long getHealthLockTimestamp() {
return healthLockTimestamp;
}
public void setHealthLockTimestamp(long healthLockTimestamp) {
this.healthLockTimestamp = healthLockTimestamp;
}
public boolean isUsingSpecialLine() {
return usingSpecialLine;
}
public void setUsingSpecialLine(boolean usingSpecialLine) {
this.usingSpecialLine = usingSpecialLine;
}
public boolean isBind() {
return bind;
}
public void setBind(boolean bind) {
this.bind = bind;
}
public IpResource toResource() {
IpResource ipResource = new IpResource();
ipResource.setId(getId());
if (shopId != null)
ipResource.getShopIds().add(getShopId());
ipResource.setHealthLockTimestamp(getHealthLockTimestamp());
ipResource.setUsingSpecialLine(isUsingSpecialLine());
ipResource.setPeriod(getPeriod());
ipResource.setSpecialLine(isSpecialLine());
if (addr != null)
ipResource.setAddr(getAddr());
ipResource.setStatus(getStatus());
if (proxyPassword != null)
ipResource.setProxyPassword(getProxyPassword());
if (proxyUsername != null)
ipResource.setProxyUsername(getProxyUsername());
ipResource.setLockTimestamp(getLockTimestamp());
if (username != null)
ipResource.setUsername(getUsername());
if (regionCn != null)
ipResource.setRegionCn(getRegionCn());
if (vendorCn != null)
ipResource.setVendorCn(getVendorCn());
if (password != null)
ipResource.setPassword(getPassword());
if (unit != null)
ipResource.setUnit(getUnit());
ipResource.setLocked(isLocked());
ipResource.setValidTime(getValidTime());
if (bindHistory != null)
ipResource.setBindHistory(getBindHistory());
ipResource.setDeleted(isDeleted());
if (owner != null)
ipResource.setOwner(getOwner());
if (protocol != null)
ipResource.setProtocol(getProtocol());
if (userParent != null)
ipResource.setUserParent(getUserParent());
ipResource.setPurchasedTime(getPurchasedTime());
ipResource.setIpType(getIpType());
if (port != null)
ipResource.setPort(getPort());
ipResource.setPrice(getPrice());
if (details != null)
ipResource.setDetails(getDetails());
return ipResource;
}
}
...@@ -6,6 +6,10 @@ import com.edgec.browserbackend.browser.domain.Shop; ...@@ -6,6 +6,10 @@ import com.edgec.browserbackend.browser.domain.Shop;
import com.edgec.browserbackend.browser.domain.TransferStatus; import com.edgec.browserbackend.browser.domain.TransferStatus;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import org.elasticsearch.client.license.LicensesStatus;
import java.util.ArrayList;
import java.util.List;
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
public class ShopResultDto { public class ShopResultDto {
...@@ -32,11 +36,11 @@ public class ShopResultDto { ...@@ -32,11 +36,11 @@ public class ShopResultDto {
private String shopCookie; private String shopCookie;
private IpResourceDto bindIp; private List<IpResourceDto> bindIp = new ArrayList<>();
private long createTime; private long createTime;
public static ShopResultDto of(Shop shop, String group, IpResourceDto ipResources) { public static ShopResultDto of(Shop shop, String group, List<IpResourceDto> ipResources) {
ShopResultDto shopResultDto = new ShopResultDto(); ShopResultDto shopResultDto = new ShopResultDto();
shopResultDto.setShopId(shop.getShopId()); shopResultDto.setShopId(shop.getShopId());
if (shop.getShopName() != null) if (shop.getShopName() != null)
...@@ -63,6 +67,33 @@ public class ShopResultDto { ...@@ -63,6 +67,33 @@ public class ShopResultDto {
return shopResultDto; return shopResultDto;
} }
public static ShopResultDto of(Shop shop, String group, IpResourceDto ipResource) {
ShopResultDto shopResultDto = new ShopResultDto();
shopResultDto.setShopId(shop.getShopId());
if (shop.getShopName() != null)
shopResultDto.setShopName(shop.getShopName());
if (shop.getShopPlatform() != null)
shopResultDto.setShopPlatform(shop.getShopPlatform());
if (shop.getShopPassword() != null)
shopResultDto.setShopPassword(shop.getShopPassword());
if (shop.getTransferStatus() != null)
shopResultDto.setTransferStatus(shop.getTransferStatus());
if (shop.getOwner() != null)
shopResultDto.setOwner(shop.getOwner());
if (shop.getShopUrl() != null)
shopResultDto.setShopUrl(shop.getShopUrl());
if (shop.getShopUA() != null)
shopResultDto.setShopUA(shop.getShopUA());
if (shop.getShopCookie() != null)
shopResultDto.setShopCookie(shop.getShopCookie());
if (shop.getShopAccount() != null)
shopResultDto.setShopAccount(shop.getShopAccount());
shopResultDto.setGroup(group);
shopResultDto.getBindIp().add(ipResource);
shopResultDto.setCreateTime(shop.getCreateTime());
return shopResultDto;
}
public String getShopPlatform() { public String getShopPlatform() {
return shopPlatform; return shopPlatform;
} }
...@@ -135,11 +166,11 @@ public class ShopResultDto { ...@@ -135,11 +166,11 @@ public class ShopResultDto {
this.shopUrl = shopUrl; this.shopUrl = shopUrl;
} }
public IpResourceDto getBindIp() { public List<IpResourceDto> getBindIp() {
return bindIp; return bindIp;
} }
public void setBindIp(IpResourceDto bindIp) { public void setBindIp(List<IpResourceDto> bindIp) {
this.bindIp = bindIp; this.bindIp = bindIp;
} }
......
...@@ -17,20 +17,19 @@ public interface IpResourceRepository extends MongoRepository<IpResource, String ...@@ -17,20 +17,19 @@ public interface IpResourceRepository extends MongoRepository<IpResource, String
IpResource findByAddrAndIsDeleted(String addr, boolean isDeleted); IpResource findByAddrAndIsDeleted(String addr, boolean isDeleted);
IpResource findByIdAndIsDeletedAndIsLocked(String id, boolean isDeleted, boolean isLocked); IpResource findByIdAndIsDeletedAndIsLocked(String id, boolean isDeleted, boolean isLocked);
IpResource findByIdAndIsDeleted(String id, boolean isDeleted); IpResource findByIdAndIsDeleted(String id, boolean isDeleted);
List<IpResource> findByShopIdInAndIsDeleted(List<String> ipIds, boolean isDeleted);
Page<IpResource> findByIdInAndIsDeletedOrderByPurchasedTimeDesc(List<String> ipIds, boolean isDeleted, Pageable pageable); Page<IpResource> findByIdInAndIsDeletedOrderByPurchasedTimeDesc(List<String> ipIds, boolean isDeleted, Pageable pageable);
List<IpResource> findByOwnerAndIsDeletedAndShopIdIsNull(String owner, boolean isDeleted); List<IpResource> findByOwnerAndIsDeletedAndBind(String owner, boolean isDeleted, boolean bind);
List<IpResource> findByOwnerAndStatusIsNotInAndIsDeletedAndShopIdIsNull(String owner, List<Integer> status, boolean isDeleted); List<IpResource> findByOwnerAndStatusIsNotInAndIsDeletedAndBind(String owner, List<Integer> status, boolean isDeleted, boolean bind);
List<IpResource> findByOwnerAndStatusAndIsDeletedAndShopIdIsNull(String owner, int status, boolean isDeleted); List<IpResource> findByOwnerAndStatusAndIsDeletedAndBind(String owner, int status, boolean isDeleted, boolean bind);
List<IpResource> findByOwnerInAndIsDeletedAndShopIdIsNull(List<String> owner, boolean isDeleted); List<IpResource> findByOwnerInAndIsDeletedAndBind(List<String> owner, boolean isDeleted, boolean bind);
List<IpResource> findByOwnerInAndStatusIsNotInAndIsDeletedAndShopIdIsNull(List<String> owner, List<Integer> status, boolean isDeleted); List<IpResource> findByOwnerInAndStatusIsNotInAndIsDeletedAndBind(List<String> owner, List<Integer> status, boolean isDeleted, boolean bind);
List<IpResource> findByOwnerInAndStatusAndIsDeletedAndShopIdIsNull(List<String> owner, int status, boolean isDeleted); List<IpResource> findByOwnerInAndStatusAndIsDeletedAndBind(List<String> owner, int status, boolean isDeleted, boolean bind);
List<IpResource> findByStatusAndShopIdInAndIsDeleted(int status, List<String> ipIds, boolean isDeleted);
Page<IpResource> findByIsDeletedAndIdInAndAddrLikeOrderByPurchasedTimeDesc(boolean isDeleted, List<String> ipIds, String addr, Pageable pageable); Page<IpResource> findByIsDeletedAndIdInAndAddrLikeOrderByPurchasedTimeDesc(boolean isDeleted, List<String> ipIds, String addr, Pageable pageable);
Page<IpResource> findByIsDeletedAndIdInAndVendorCnLikeOrderByPurchasedTimeDesc(boolean isDeleted, List<String> ipIds, String vendorCn, Pageable pageable); Page<IpResource> findByIsDeletedAndIdInAndVendorCnLikeOrderByPurchasedTimeDesc(boolean isDeleted, List<String> ipIds, String vendorCn, Pageable pageable);
Page<IpResource> findByIsDeletedAndIdInAndVendorLikeOrderByPurchasedTimeDesc(boolean isDeleted, List<String> ipIds, String vendor, Pageable pageable); Page<IpResource> findByIsDeletedAndIdInAndVendorLikeOrderByPurchasedTimeDesc(boolean isDeleted, List<String> ipIds, String vendor, Pageable pageable);
Page<IpResource> findByIsDeletedAndIdInAndRegionCnLikeOrderByPurchasedTimeDesc(boolean isDeleted, List<String> ipIds, String region, Pageable pageable); Page<IpResource> findByIsDeletedAndIdInAndRegionCnLikeOrderByPurchasedTimeDesc(boolean isDeleted, List<String> ipIds, String region, Pageable pageable);
List<IpResource> findByIsDeletedAndShopIdInAndRegionCnLike(boolean isDeleted, List<String> shopIds, String regionCn);
List<IpResource> findByIsDeleted(boolean isDeleted); List<IpResource> findByIsDeleted(boolean isDeleted);
...@@ -43,7 +42,7 @@ public interface IpResourceRepository extends MongoRepository<IpResource, String ...@@ -43,7 +42,7 @@ public interface IpResourceRepository extends MongoRepository<IpResource, String
List<IpResource> findByValidTimeBetweenAndIsDeleted(long beginTime, long endTime, boolean isDeleted); List<IpResource> findByValidTimeBetweenAndIsDeleted(long beginTime, long endTime, boolean isDeleted);
IpResource findFirstByShopIdAndIsDeleted(String shopId, boolean isDeleted); IpResource findFirstByShopIdsIsAndIsDeleted(String shopId, boolean isDeleted);
int countByStatusAndIdInAndIsDeleted(int status, List<String> ipIds, boolean isDeleted); int countByStatusAndIdInAndIsDeleted(int status, List<String> ipIds, boolean isDeleted);
......
package com.edgec.browserbackend.browser.repository; package com.edgec.browserbackend.browser.repository;
import com.edgec.browserbackend.browser.domain.BindHistory;
import com.edgec.browserbackend.browser.domain.IpResource; import com.edgec.browserbackend.browser.domain.IpResource;
import java.util.List; import java.util.List;
...@@ -17,4 +18,18 @@ public interface IpResourceRepositoryCustom { ...@@ -17,4 +18,18 @@ public interface IpResourceRepositoryCustom {
List<IpResource> sampleTasks(int status, long timestamp); List<IpResource> sampleTasks(int status, long timestamp);
List<IpResource> sampleTasks(List<Integer> status); List<IpResource> sampleTasks(List<Integer> status);
boolean addShopId(String ipId, String shopId);
boolean deleteShopId(String ipId, String shopId, BindHistory bindHistory);
boolean updateStatus(String id, int status);
boolean updateBind(String id, boolean isbind);
List<IpResource> findShopIdInList(List<String> shopIds, boolean isDeleted);
List<IpResource> findShopIdInListAndStatus(List<String> shopIds, boolean isDeleted, int status);
List<IpResource> findShopIdInListAndRegionLike(List<String> shopIds, boolean isDeleted, String region);
} }
package com.edgec.browserbackend.browser.repository; package com.edgec.browserbackend.browser.repository;
import com.edgec.browserbackend.browser.domain.BindHistory;
import com.edgec.browserbackend.browser.domain.IpResource; import com.edgec.browserbackend.browser.domain.IpResource;
import com.edgec.browserbackend.browser.dto.IpResourceUnwindResultDto;
import com.edgec.browserbackend.common.utils.JsonUtils;
import com.mongodb.client.result.UpdateResult; import com.mongodb.client.result.UpdateResult;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.bson.Document; import org.bson.Document;
import org.elasticsearch.common.recycler.Recycler; import org.elasticsearch.common.recycler.Recycler;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.aggregation.*;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.MatchOperation;
import org.springframework.data.mongodb.core.aggregation.SampleOperation;
import org.springframework.data.mongodb.core.query.BasicQuery; import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition; import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Update; import org.springframework.data.mongodb.core.query.Update;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import static org.springframework.data.mongodb.core.query.Criteria.where; import static org.springframework.data.mongodb.core.query.Criteria.where;
...@@ -124,4 +123,142 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto ...@@ -124,4 +123,142 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
return mappedResults; return mappedResults;
} }
@Override
public boolean addShopId(String ipId, String shopId) {
Document doc = new Document();
BasicQuery basicQuery = new BasicQuery(doc);
basicQuery.addCriteria(where("id").is(ipId).and("isDeleted").is(false));
Update update = new Update();
update.push("shopIds", shopId);
update.set("bind", true);
UpdateResult result = mongoTemplate.updateFirst(basicQuery, update, IpResource.class);
if (result.getModifiedCount() < 1)
return false;
else
return true;
}
@Override
public boolean deleteShopId(String ipId, String shopId, BindHistory bindHistory) {
Document doc = new Document();
BasicQuery basicQuery = new BasicQuery(doc);
basicQuery.addCriteria(where("id").is(ipId).and("isDeleted").is(false));
Update update = new Update();
update.pull("shopIds", shopId).push("bindhistory", bindHistory);
UpdateResult result = mongoTemplate.updateFirst(basicQuery, update, IpResource.class);
if (result.getModifiedCount() < 1)
return false;
else
return true;
}
@Override
public boolean updateStatus(String id, int status) {
Document doc = new Document();
BasicQuery basicQuery = new BasicQuery(doc);
basicQuery.addCriteria(where("id").is(id).and("isDeleted").is(false));
Update update = new Update();
update.set("status", status);
UpdateResult result = mongoTemplate.updateFirst(basicQuery, update, IpResource.class);
if (result.getModifiedCount() < 1)
return false;
else
return true;
}
@Override
public boolean updateBind(String id, boolean isbind) {
Document doc = new Document();
BasicQuery basicQuery = new BasicQuery(doc);
basicQuery.addCriteria(where("id").is(id).and("isDeleted").is(false));
Update update = new Update();
update.set("bind", isbind);
UpdateResult result = mongoTemplate.updateFirst(basicQuery, update, IpResource.class);
if (result.getModifiedCount() < 1)
return false;
else
return true;
}
@Override
public List<IpResource> findShopIdInList(List<String> shopIds, boolean isDeleted) {
MatchOperation matchOperation = Aggregation.match(where("isDeleted").is(isDeleted));
UnwindOperation unwind = Aggregation.unwind("shopIds");
MatchOperation matchshopId = Aggregation.match(where("shopIds").in(shopIds));
List<IpResourceUnwindResultDto> ipResourceUnwindResultDtos = mongoTemplate.aggregate(
Aggregation.newAggregation(matchOperation, unwind, matchshopId), IpResource.class, IpResourceUnwindResultDto.class).getMappedResults();
if (ipResourceUnwindResultDtos.isEmpty()) {
return new ArrayList<>();
}
HashMap<String ,IpResource> ipResourceHashMap = new HashMap<>();
ipResourceUnwindResultDtos.forEach(x -> {
if (ipResourceHashMap.containsKey(x.getId())) {
ipResourceHashMap.get(x.getId()).getShopIds().add(x.getShopId());
} else {
ipResourceHashMap.put(x.getId(), x.toResource());
}
});
List<IpResource> result = new ArrayList<>();
Set<Map.Entry<String,IpResource>> entry = ipResourceHashMap.entrySet();
for(Map.Entry<String,IpResource> e:entry){
result.add(e.getValue());
}
return result;
}
@Override
public List<IpResource> findShopIdInListAndStatus(List<String> shopIds, boolean isDeleted, int status) {
MatchOperation matchOperation = Aggregation.match(where("isDeleted").is(isDeleted).and("status").is(status));
UnwindOperation unwind = Aggregation.unwind("shopIds");
MatchOperation matchshopId = Aggregation.match(where("shopIds").in(shopIds));
List<IpResourceUnwindResultDto> ipResourceUnwindResultDtos = mongoTemplate.aggregate(
Aggregation.newAggregation(matchOperation, unwind, matchshopId), IpResource.class, IpResourceUnwindResultDto.class).getMappedResults();
if (ipResourceUnwindResultDtos.isEmpty()) {
return new ArrayList<>();
}
HashMap<String ,IpResource> ipResourceHashMap = new HashMap<>();
ipResourceUnwindResultDtos.forEach(x -> {
if (ipResourceHashMap.containsKey(x.getId())) {
ipResourceHashMap.get(x.getId()).getShopIds().add(x.getShopId());
} else {
ipResourceHashMap.put(x.getId(), x.toResource());
}
});
List<IpResource> result = new ArrayList<>();
Set<Map.Entry<String,IpResource>> entry = ipResourceHashMap.entrySet();
for(Map.Entry<String,IpResource> e:entry){
result.add(e.getValue());
}
return result;
}
@Override
public List<IpResource> findShopIdInListAndRegionLike(List<String> shopIds, boolean isDeleted, String region) {
MatchOperation matchOperation = Aggregation.match(where("isDeleted").is(isDeleted).and("regionCn").regex(".*?\\" + region + ".*"));
UnwindOperation unwind = Aggregation.unwind("shopIds");
MatchOperation matchshopId = Aggregation.match(where("shopIds").in(shopIds));
List<IpResourceUnwindResultDto> ipResourceUnwindResultDtos = mongoTemplate.aggregate(
Aggregation.newAggregation(matchOperation, unwind, matchshopId), IpResource.class, IpResourceUnwindResultDto.class).getMappedResults();
if (ipResourceUnwindResultDtos.isEmpty()) {
return new ArrayList<>();
}
HashMap<String ,IpResource> ipResourceHashMap = new HashMap<>();
ipResourceUnwindResultDtos.forEach(x -> {
if (ipResourceHashMap.containsKey(x.getId())) {
ipResourceHashMap.get(x.getId()).getShopIds().add(x.getShopId());
} else {
ipResourceHashMap.put(x.getId(), x.toResource());
}
});
List<IpResource> result = new ArrayList<>();
Set<Map.Entry<String,IpResource>> entry = ipResourceHashMap.entrySet();
for(Map.Entry<String,IpResource> e:entry){
result.add(e.getValue());
}
return result;
}
} }
...@@ -11,4 +11,5 @@ public interface ShopRepository extends MongoRepository<Shop, String> { ...@@ -11,4 +11,5 @@ public interface ShopRepository extends MongoRepository<Shop, String> {
Page<Shop> findByShopIdInAndShopAccountLikeOrderByCreateTimeDesc(List<String> shopIds, String shopAccount, Pageable pageable); Page<Shop> findByShopIdInAndShopAccountLikeOrderByCreateTimeDesc(List<String> shopIds, String shopAccount, Pageable pageable);
Page<Shop> findByShopIdInAndShopNameLikeOrderByCreateTimeDesc(List<String> shopIds, String shopName, Pageable pageable); Page<Shop> findByShopIdInAndShopNameLikeOrderByCreateTimeDesc(List<String> shopIds, String shopName, Pageable pageable);
Page<Shop> findByShopIdInOrderByCreateTimeDesc(List<String> shopIds, Pageable pageable); Page<Shop> findByShopIdInOrderByCreateTimeDesc(List<String> shopIds, Pageable pageable);
List<Shop> findByShopIdIn(List<String> shopIds);
} }
...@@ -65,16 +65,12 @@ public class IpAndShopServiceImpl implements IpAndShopService { ...@@ -65,16 +65,12 @@ public class IpAndShopServiceImpl implements IpAndShopService {
if (ipResource == null) if (ipResource == null)
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST); throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST);
IpResource bind = ipResourceRepository.findFirstByShopIdAndIsDeleted(shopId, false); IpResource bind = ipResourceRepository.findFirstByShopIdsIsAndIsDeleted(shopId, false);
if (bind != null) { if (bind != null) {
shopRequestDto.setShopId(shopId); return;
shopRequestDto.setIpId(ipResource.getId());
unBindShop(username, shopRequestDto);
} }
try { try {
ipResource.setShopId(shop.getShopId()); ipResourceRepository.addShopId(ipResource.getId(), shopId);
ipResource.setShopName(shop.getShopName());
ipResourceRepository.save(ipResource);
} 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);
...@@ -105,22 +101,65 @@ public class IpAndShopServiceImpl implements IpAndShopService { ...@@ -105,22 +101,65 @@ public class IpAndShopServiceImpl implements IpAndShopService {
} }
if (ipResource == null) if (ipResource == null)
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST); throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST);
if (ipResource.getShopId() == null && !ipResource.getShopId().equals(shop.getShopId())) if (ipResource.getShopIds() == null && !ipResource.getShopIds().contains(shop.getShopId()))
throw new ClientRequestException(BrowserErrorCode.IPNOTBINDTOSHOP); throw new ClientRequestException(BrowserErrorCode.IPNOTBINDTOSHOP);
try { try {
List<BindHistory> history = ipResource.getBindHistory();
BindHistory bindHistory = new BindHistory(); BindHistory bindHistory = new BindHistory();
bindHistory.setIp(ipResource.getAddr()); bindHistory.setIp(ipResource.getAddr());
bindHistory.setPlatform(shop.getShopPlatform()); bindHistory.setPlatform(shop.getShopPlatform());
bindHistory.setShopName(shop.getShopName()); bindHistory.setShopName(shop.getShopName());
bindHistory.setUnbindTime(ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); bindHistory.setUnbindTime(ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
history.add(bindHistory); ipResourceRepository.deleteShopId(ipResource.getId(), shopId, bindHistory);
ipResource.setShopId(null); IpResource newIp = ipResourceRepository.findById(ipResource.getId()).orElse(null);
ipResource.setShopName(null); if (newIp.getShopIds() == null || newIp.getShopIds().size() == 0)
ipResourceRepository.save(ipResource); ipResourceRepository.updateBind(ipResource.getId(), false);
} 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);
} }
} }
@Override
public void unBindShops(String username, ShopRequestDto shopRequestDto) {
List<String> shopIds = null;
if (shopRequestDto.getShopIds() != null && shopRequestDto.getShopIds().size() > 0)
shopIds = shopRequestDto.getShopIds();
Account account = accountRepository.findByName(username);
if (account == null) {
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
}
IpResource ipResource = null;
if (StringUtils.isNotBlank(shopRequestDto.getIpAddr())) {
ipResource = ipResourceRepository.findByAddrAndIsDeleted(shopRequestDto.getIpAddr(), false);
} else if (StringUtils.isNotBlank(shopRequestDto.getIpId())) {
ipResource = ipResourceRepository.findByIdAndIsDeleted(shopRequestDto.getIpId(), false);
}
if (ipResource == null)
throw new ClientRequestException(BrowserErrorCode.IPNOTEXIST);
for (String shopId : shopIds) {
UserShop userShop = userShopRepository.findByUsernameAndShopId(username, shopId);
if (account.getPermission() < 4 || userShop == null) {
continue;
}
Shop shop = shopRepository.findById(shopId).orElse(null);
if (shop == null)
continue;
if (ipResource.getShopIds() == null && !ipResource.getShopIds().contains(shop.getShopId()))
continue;
try {
BindHistory bindHistory = new BindHistory();
bindHistory.setIp(ipResource.getAddr());
bindHistory.setPlatform(shop.getShopPlatform());
bindHistory.setShopName(shop.getShopName());
bindHistory.setUnbindTime(ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
ipResourceRepository.deleteShopId(ipResource.getId(), shopId, bindHistory);
IpResource newIp = ipResourceRepository.findById(ipResource.getId()).orElse(null);
if (newIp.getShopIds() == null || newIp.getShopIds().size() == 0)
ipResourceRepository.updateBind(ipResource.getId(), false);
} catch (Exception e) {
logger.error("fail to unbind", e.getMessage());
throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
}
}
}
} }
...@@ -7,4 +7,6 @@ public interface IpAndShopService { ...@@ -7,4 +7,6 @@ public interface IpAndShopService {
void bindShop(String username, ShopRequestDto shopRequestDto); void bindShop(String username, ShopRequestDto shopRequestDto);
void unBindShop(String username, ShopRequestDto shopRequestDto); void unBindShop(String username, ShopRequestDto shopRequestDto);
void unBindShops(String username, ShopRequestDto shopRequestDto);
} }
...@@ -168,11 +168,11 @@ public class BrowserTask { ...@@ -168,11 +168,11 @@ public class BrowserTask {
if (result == false && (ipResource.getPurchasedTime() < Instant.now().minusSeconds(7200).toEpochMilli())) { if (result == false && (ipResource.getPurchasedTime() < Instant.now().minusSeconds(7200).toEpochMilli())) {
IpChargeRequestDto ipChargeRequestDto = buildIpChargeRequestDto(ipResource, 3, 0); IpChargeRequestDto ipChargeRequestDto = buildIpChargeRequestDto(ipResource, 3, 0);
accountService.chargeByMoney(ipResource.getUsername(), -ipResource.getPrice(), ipChargeRequestDto); accountService.chargeByMoney(ipResource.getUsername(), -ipResource.getPrice(), ipChargeRequestDto);
if (ipResource.getShopId() != null) { if (ipResource.getShopIds() != null && ipResource.getShopIds().size() > 0) {
ShopRequestDto shopRequestDto = new ShopRequestDto(); ShopRequestDto shopRequestDto = new ShopRequestDto();
shopRequestDto.setIpId(ipResource.getId()); shopRequestDto.setIpId(ipResource.getId());
shopRequestDto.setShopId(ipResource.getShopId()); shopRequestDto.setShopIds(ipResource.getShopIds());
ipAndShopService.unBindShop(ipResource.getUsername(), shopRequestDto); ipAndShopService.unBindShops(ipResource.getUsername(), shopRequestDto);
} }
ipResourceRepository.deleteById(ipResource.getId()); ipResourceRepository.deleteById(ipResource.getId());
} }
...@@ -202,14 +202,17 @@ public class BrowserTask { ...@@ -202,14 +202,17 @@ public class BrowserTask {
CompletableFuture.runAsync(() -> { CompletableFuture.runAsync(() -> {
if (ipResourceRepository.lockTask(ipResource)) { if (ipResourceRepository.lockTask(ipResource)) {
try { try {
log.error("test");
String url = URL + "/ecc/ipinfo?accountId=browser&ip=" + ipResource.getAddr(); String url = URL + "/ecc/ipinfo?accountId=browser&ip=" + ipResource.getAddr();
Map<String, String> header = buildGetHeader(); Map<String, String> header = buildGetHeader();
String rs = HttpClientutils.doGet(url, header); String rs = HttpClientutils.doGet(url, header);
IpInfoResultDto ipInfoResultDto = JSONObject.parseObject(rs, IpInfoResultDto.class); IpInfoResultDto ipInfoResultDto = JSONObject.parseObject(rs, IpInfoResultDto.class);
if (ipInfoResultDto != null && StringUtils.isBlank(ipInfoResultDto.getErrorCode())) { if (ipInfoResultDto != null && StringUtils.isBlank(ipInfoResultDto.getErrorCode())) {
log.error(ipInfoResultDto.getErrorCode());
log.error(ipInfoResultDto.getStatus());
log.error(ipInfoResultDto.getEipIPAddr());
if (StringUtils.isNotEmpty(ipInfoResultDto.getStatus())) { if (StringUtils.isNotEmpty(ipInfoResultDto.getStatus())) {
ipResource.setStatus(0); ipResourceRepository.updateStatus(ipResource.getId(), 0);
ipResourceRepository.save(ipResource);
} }
} }
} catch (Exception e) { } catch (Exception e) {
......
package com.edgec.browserbackend.common.utils;
import com.alibaba.fastjson.JSONArray;
import com.edgec.browserbackend.account.exception.AccountErrorCode;
import com.edgec.browserbackend.common.commons.error.ClientRequestException;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.util.*;
/**
* Json工具类
*/
public class JsonUtils {
private static final ObjectMapper mapper = new ObjectMapper();
static {
mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
mapper.setSerializationInclusion(Include.NON_NULL);
}
private JsonUtils() {
}
/**
* json字符串转换为类
*/
public static <T> T toBean(String json, Class<T> clazz) {
try {
T bean = (T) mapper.readValue(json, clazz);
return bean;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("unchecked")
public static HashMap<String, Object> toBean(String json) {
return toBean(json, HashMap.class);
}
@SuppressWarnings("unchecked")
public static HashMap<String,String> toBeanStr(String json) {
return toBean(json, HashMap.class);
}
@SuppressWarnings("unchecked")
public static <T> T toBean(String json, TypeReference<T> tr){
try {
T bean = (T) mapper.readValue(json, tr);
return bean;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 对象转换为json字符串
*/
public static String toJson(Object bean) {
String json = null;
JsonGenerator gen = null;
StringWriter sw = new StringWriter();
try {
gen = new JsonFactory().createGenerator(sw);
mapper.writeValue(gen, bean);
json = sw.toString();
} catch (IOException e) {
throw new ClientRequestException(AccountErrorCode.UNKNOWN);
} finally {
try {
if (gen != null) {
gen.close();
}
if (sw != null) {
sw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return json;
}
@SuppressWarnings("unchecked")
public static List<Object> toList(String json) {
return toBean(json, ArrayList.class);
}
/**
* 对象转换为Map
*/
public static Map<String, Object> transBean2Map(Object obj) {
if (obj == null) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (!key.equals("class")) {
Method getter = property.getReadMethod();
Object value = getter.invoke(obj);
map.put(key, value);
}
}
} catch (Exception e) {
System.out.println("transBean2Map Error " + e);
}
return map;
}
/**
* json字符串转换为List
*/
public static <T> List<T>json2ListBean(String json,Class<T>cls){
JSONArray jArray= JSONArray.parseArray(json);
List<T> collection =jArray.toJavaList(cls);
return collection;
}
}
\ No newline at end of file
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