Commit 3ad3070c authored by renjie's avatar renjie

ip购买,查询信息更新

parent 82e3b5a0
...@@ -88,6 +88,11 @@ ...@@ -88,6 +88,11 @@
<artifactId>ehcache</artifactId> <artifactId>ehcache</artifactId>
<version>2.10.5</version> <version>2.10.5</version>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency> <dependency>
<groupId>com.aliyun</groupId> <groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId> <artifactId>aliyun-java-sdk-core</artifactId>
......
package com.edgec.browserbackend.account.config;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSBuckets;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MongoConfig {
@Value("${spring.data.mongodb.uri}")
String dbUri;
@Bean
public GridFSBucket getGridFSBucket(MongoClient mongoClient){
String db = dbUri.substring(dbUri.lastIndexOf("/") + 1);
MongoDatabase database = mongoClient.getDatabase(db);
GridFSBucket bucket = GridFSBuckets.create(database);
return bucket;
}
}
...@@ -24,6 +24,10 @@ import org.springframework.web.multipart.MultipartFile; ...@@ -24,6 +24,10 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid; import javax.validation.Valid;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.security.Principal; import java.security.Principal;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
...@@ -396,7 +400,7 @@ public class AccountController { ...@@ -396,7 +400,7 @@ public class AccountController {
return accountService.deletePreOrder(username); return accountService.deletePreOrder(username);
} }
@RequestMapping(path = "/authorize", method = RequestMethod.POST) @RequestMapping(path = "/authorize/add", method = RequestMethod.POST)
public ResultDto companyAuthorize(Principal principal, public ResultDto companyAuthorize(Principal principal,
@RequestParam(value = "type", defaultValue = "0") int type, @RequestParam(value = "type", defaultValue = "0") int type,
@RequestParam(value = "companyName") String companyName, @RequestParam(value = "companyName") String companyName,
...@@ -439,4 +443,53 @@ public class AccountController { ...@@ -439,4 +443,53 @@ public class AccountController {
return resultDto; return resultDto;
} }
@RequestMapping(path = "/authorize/details")
public ResultDto getAuthorizeDetails(Principal principal) {
ResultDto resultDto = new ResultDto();
try {
resultDto.setData(accountService.getAuthorizeDetails(principal.getName()));
resultDto.setStatus(0);
}catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
}
return resultDto;
}
@RequestMapping(path = "/authorize/files")
public ResultDto getAuthorizeFiles(Principal principal, HttpServletResponse response) {
ResultDto resultDto = new ResultDto();
try {
File tempFile = accountService.getAuthorizeFiles(principal.getName());
try(OutputStream os = response.getOutputStream();
FileInputStream fis = new FileInputStream(tempFile)){
response.setHeader("content-type", "application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("压缩包.zip", "UTF-8"));
int len =0;
byte[] bt = new byte[5*1024];
while((len = fis.read(bt)) != -1) {
os.write(bt,0,len);
}
}catch(Exception e) {
e.printStackTrace();
throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
}finally {
tempFile.deleteOnExit();
}
accountService.getAuthorizeFiles(principal.getName());
resultDto.setStatus(0);
}catch (ClientRequestException e) {
resultDto.setStatus(-1);
Map<String, Object> statusInfo = new HashMap<>();
statusInfo.put("code", e.getErrorCode());
statusInfo.put("message", e.getMessage());
resultDto.setStatusInfo(statusInfo);
}
return resultDto;
}
} }
package com.edgec.browserbackend.account.dto; package com.edgec.browserbackend.account.dto;
import com.edgec.browserbackend.account.domain.CompanyAuthorize; import com.edgec.browserbackend.account.domain.CompanyAuthorize;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@JsonIgnoreProperties(ignoreUnknown = true)
public class CompanyAuthorizeDto { public class CompanyAuthorizeDto {
private String username; private String username;
private int type; //0 -- 企业, 1 -- 个体工商户 private int type; //0 -- 企业, 1 -- 个体工商户
......
...@@ -5,5 +5,5 @@ import com.edgec.browserbackend.account.dto.CompanyAuthorizeDto; ...@@ -5,5 +5,5 @@ import com.edgec.browserbackend.account.dto.CompanyAuthorizeDto;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
public interface CompanyAuthorizeRepository extends MongoRepository<CompanyAuthorize, String>, CompanyAuthorizeRepositoryCustom { public interface CompanyAuthorizeRepository extends MongoRepository<CompanyAuthorize, String>, CompanyAuthorizeRepositoryCustom {
CompanyAuthorize findByUsername(String username);
} }
package com.edgec.browserbackend.account.repository; package com.edgec.browserbackend.account.repository;
import com.mongodb.client.gridfs.model.GridFSFile;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
public interface CompanyAuthorizeRepositoryCustom { public interface CompanyAuthorizeRepositoryCustom {
String saveFile(String collectionName, MultipartFile file, String filename); String saveFile(MultipartFile file, String filename);
void deleteFile(String fileId);
File getFile(String fileId);
} }
...@@ -4,14 +4,21 @@ import com.edgec.browserbackend.browser.ErrorCode.BrowserErrorCode; ...@@ -4,14 +4,21 @@ import com.edgec.browserbackend.browser.ErrorCode.BrowserErrorCode;
import com.edgec.browserbackend.common.commons.error.ClientRequestException; import com.edgec.browserbackend.common.commons.error.ClientRequestException;
import com.mongodb.DB; import com.mongodb.DB;
import com.mongodb.client.gridfs.GridFSBucket; import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.mongodb.gridfs.GridFS; import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile; import com.mongodb.gridfs.GridFSInputFile;
import org.bson.types.ObjectId;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsOperations; import org.springframework.data.mongodb.gridfs.GridFsOperations;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate; import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -31,24 +38,51 @@ public class CompanyAuthorizeRepositoryCustomImpl implements CompanyAuthorizeRep ...@@ -31,24 +38,51 @@ public class CompanyAuthorizeRepositoryCustomImpl implements CompanyAuthorizeRep
@Autowired @Autowired
private MongoTemplate mongoTemplate; private MongoTemplate mongoTemplate;
@Autowired
GridFSBucket gridFSBucket;
@Override @Override
public String saveFile(String collectionName, MultipartFile file, String filename) { public String saveFile(MultipartFile file, String filename) {
try { try {
GridFS gridFS = new GridFS((DB) mongoTemplate.getDb());
InputStream in = file.getInputStream(); InputStream in = file.getInputStream();
ObjectId id = gridFsTemplate.store(in, filename, file.getContentType());
return id.toString();
} catch (Exception e) {
logger.error("存储文件时发生错误!!!", e);
throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
}
}
GridFSInputFile gridFSInputFile = gridFS.createFile(in); @Override
gridFSInputFile.setFilename(filename); public void deleteFile(String fileId) {
Query query = Query.query(Criteria.where("_id").is(fileId));
gridFSInputFile.setContentType(file.getContentType()); // 查询单个文件
GridFSFile gfsfile = gridFsTemplate.findOne(query);
gridFSInputFile.save(); if (gfsfile == null) {
return;
}
gridFsTemplate.delete(query);
}
return (String) gridFSInputFile.getId(); @Override
public File getFile(String fileId) {
Query query = Query.query(Criteria.where("_id").is(fileId));
// 查询单个文件
GridFSFile gridFSFile = gridFsTemplate.findOne(query);
if (gridFSFile == null)
return null;
try {
GridFSDownloadStream gridFSDownloadStream =
gridFSBucket.openDownloadStream(gridFSFile.getObjectId());
GridFsResource gridFsResource = new GridFsResource(gridFSFile,gridFSDownloadStream);
return gridFsResource.getFile();
} catch (Exception e) { } catch (Exception e) {
logger.error("存储文件时发生错误!!!", e); logger.error(e.getMessage());
throw new ClientRequestException(BrowserErrorCode.UNKNOWN); throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
} }
} }
} }
...@@ -2,9 +2,13 @@ package com.edgec.browserbackend.account.service; ...@@ -2,9 +2,13 @@ package com.edgec.browserbackend.account.service;
import com.edgec.browserbackend.account.domain.*; import com.edgec.browserbackend.account.domain.*;
import com.edgec.browserbackend.account.dto.*; import com.edgec.browserbackend.account.dto.*;
import com.mongodb.client.gridfs.model.GridFSFile;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
...@@ -102,4 +106,8 @@ public interface AccountService { ...@@ -102,4 +106,8 @@ public interface AccountService {
void authorizeCompany(String username, CompanyAuthorizeDto companyAuthorizeDto); void authorizeCompany(String username, CompanyAuthorizeDto companyAuthorizeDto);
CompanyAuthorizeDto getAuthorizeDetails(String username);
File getAuthorizeFiles(String username);
} }
...@@ -13,6 +13,7 @@ import com.edgec.browserbackend.account.utils.AccountServicePool; ...@@ -13,6 +13,7 @@ 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.repository.UserRepository; import com.edgec.browserbackend.auth.repository.UserRepository;
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.browser.domain.IpSummary; import com.edgec.browserbackend.browser.domain.IpSummary;
import com.edgec.browserbackend.browser.domain.ShopSummary; import com.edgec.browserbackend.browser.domain.ShopSummary;
import com.edgec.browserbackend.browser.dto.PageInfo; import com.edgec.browserbackend.browser.dto.PageInfo;
...@@ -22,12 +23,17 @@ import com.edgec.browserbackend.browser.service.IpResourceService; ...@@ -22,12 +23,17 @@ import com.edgec.browserbackend.browser.service.IpResourceService;
import com.edgec.browserbackend.browser.service.ShopService; import com.edgec.browserbackend.browser.service.ShopService;
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;
import com.edgec.browserbackend.common.utils.FileUtil;
import com.mongodb.DB; import com.mongodb.DB;
import com.mongodb.client.gridfs.GridFSBucket; import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSDownloadStream; import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.model.GridFSFile; import com.mongodb.client.gridfs.model.GridFSFile;
import com.mongodb.gridfs.GridFS; import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSInputFile; import com.mongodb.gridfs.GridFSInputFile;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItem;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -43,12 +49,11 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; ...@@ -43,12 +49,11 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.util.StringUtils; import org.thymeleaf.util.StringUtils;
import java.io.ByteArrayOutputStream; import java.io.*;
import java.io.File; import java.nio.file.Files;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.Instant; import java.time.Instant;
import java.time.YearMonth; import java.time.YearMonth;
...@@ -1015,25 +1020,74 @@ public class AccountServiceImpl implements AccountService { ...@@ -1015,25 +1020,74 @@ public class AccountServiceImpl implements AccountService {
@Override @Override
public void authorizeCompany(String username, CompanyAuthorizeDto companyAuthorizeDto) { public void authorizeCompany(String username, CompanyAuthorizeDto companyAuthorizeDto) {
Account account = repository.findByName(username); String companyLicenseId = null;
if (account == null) String coporationLicenseId = null;
throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
String companyLicenseName = "companyLicense-" + username;
String companyLicenseId = companyAuthorizeRepository.saveFile("companylicense", companyAuthorizeDto.getCompanyLicense(), companyLicenseName);
String coporationLicenseName = "coporationLicense-" + username;
String coporationLicenseId = companyAuthorizeRepository.saveFile("coporationlicense", companyAuthorizeDto.getCoporationLicense(), companyLicenseName);
String agencyId = null; String agencyId = null;
if (companyAuthorizeDto.getAgency() != null) { CompanyAuthorize existing = companyAuthorizeRepository.findByUsername(username);
String agencyName = "agencyLicense-" + username; if (existing != null) {
agencyId = companyAuthorizeRepository.saveFile("coporationlicense", companyAuthorizeDto.getAgency(), agencyName); throw new ClientRequestException(BrowserErrorCode.COMPANYAUTHORIZEEXIST);
} }
CompanyAuthorize companyAuthorize = new CompanyAuthorize(companyAuthorizeDto); try {
companyAuthorize.setUsername(username); Account account = repository.findByName(username);
companyAuthorize.setCompanyLicenseId(companyLicenseId); if (account == null)
companyAuthorize.setCoporationLicenseId(coporationLicenseId); throw new ClientRequestException(AccountErrorCode.NAMENOTEXIST);
if (agencyId != null) String companyLicenseName = "companyLicense-" + username;
companyAuthorize.setAgencyId(agencyId); companyLicenseId = companyAuthorizeRepository.saveFile(companyAuthorizeDto.getCompanyLicense(), companyLicenseName);
companyAuthorizeRepository.save(companyAuthorize); String coporationLicenseName = "coporationLicense-" + username;
coporationLicenseId = companyAuthorizeRepository.saveFile(companyAuthorizeDto.getCoporationLicense(), coporationLicenseName);
if (companyAuthorizeDto.getAgency() != null) {
String agencyName = "agencyLicense-" + username;
agencyId = companyAuthorizeRepository.saveFile(companyAuthorizeDto.getAgency(), agencyName);
}
CompanyAuthorize companyAuthorize = new CompanyAuthorize(companyAuthorizeDto);
companyAuthorize.setUsername(username);
companyAuthorize.setCompanyLicenseId(companyLicenseId);
companyAuthorize.setCoporationLicenseId(coporationLicenseId);
if (agencyId != null)
companyAuthorize.setAgencyId(agencyId);
companyAuthorizeRepository.save(companyAuthorize);
} catch (Exception e) {
if (agencyId != null)
companyAuthorizeRepository.deleteFile(agencyId);
if (coporationLicenseId != null)
companyAuthorizeRepository.deleteFile(coporationLicenseId);
if (companyLicenseId != null)
companyAuthorizeRepository.deleteFile(companyLicenseId);
throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
}
}
@Override
public CompanyAuthorizeDto getAuthorizeDetails(String username) {
CompanyAuthorize companyAuthorize = companyAuthorizeRepository.findByUsername(username);
if (companyAuthorize == null)
return null;
CompanyAuthorizeDto companyAuthorizeDto = new CompanyAuthorizeDto(companyAuthorize);
return companyAuthorizeDto;
} }
@Override
public File getAuthorizeFiles(String username) {
CompanyAuthorize companyAuthorize = companyAuthorizeRepository.findByUsername(username);
if (companyAuthorize == null)
return null;
File companyLicense = companyAuthorizeRepository.getFile(companyAuthorize.getCompanyLicenseId());
log.error("3");
File coporationLicense = companyAuthorizeRepository.getFile(companyAuthorize.getCoporationLicenseId());
File agencyLicense = null;
if (companyAuthorize.getAgencyId() != null)
agencyLicense = companyAuthorizeRepository.getFile(companyAuthorize.getAgencyId());
try {
File tempFile = File.createTempFile("tempFile", "zip");
List<File> files = Arrays.asList(companyLicense, coporationLicense);
if (agencyLicense != null)
files.add(agencyLicense);
tempFile = FileUtil.putBatchFilesInZip(files, tempFile);
return tempFile;
} catch (Exception e) {
throw new ClientRequestException(BrowserErrorCode.UNKNOWN);
}
}
} }
...@@ -9,15 +9,18 @@ public enum BrowserErrorCode implements ErrorCode { ...@@ -9,15 +9,18 @@ public enum BrowserErrorCode implements ErrorCode {
/* client errors */ /* client errors */
UNKNOWN(ErrorCode.COMMON_UNKNOWN,"unknown"), UNKNOWN(ErrorCode.COMMON_UNKNOWN,"unknown"),
INFORMATIONNOTCOMPELETE(BROWSER_BASE+100, "The information about shop do not complete"), INFORMATIONNOTCOMPELETE(BROWSER_BASE+100, "The information about shop does not complete"),
SHOPNOTEXIST(BROWSER_BASE+101, "The shop do not exist"), SHOPNOTEXIST(BROWSER_BASE+101, "The shop does not exist"),
IPNOTEXIST(BROWSER_BASE+201, "The ip do not exist"), IPNOTEXIST(BROWSER_BASE+201, "The ip do not exist"),
IPNOTBINDTOSHOP(BROWSER_BASE+202, "The ip do not bind this shop."), IPNOTBINDTOSHOP(BROWSER_BASE+202, "The ip does not bind this shop."),
GROUPNOTEXIST(BROWSER_BASE+301, "The group do not exist"), GROUPNOTEXIST(BROWSER_BASE+301, "The group does not exist"),
IPTRANSACTIONNOTEXIST(BROWSER_BASE+401, "The ipTransaction do not exist"); IPTRANSACTIONNOTEXIST(BROWSER_BASE+401, "The ipTransaction does not exist"),
COMPANYAUTHORIZEEXIST(BROWSER_BASE+501, "The authority exists"),
COMPANYAUTHORIZENOTEXIST(BROWSER_BASE+502, "The authority dose not exist");
......
...@@ -20,7 +20,7 @@ public class IpResource implements Serializable { ...@@ -20,7 +20,7 @@ public class IpResource implements Serializable {
private Vendor vendor; private Vendor vendor;
private String region; private String region;
private String regionCn; private String regionCn;
//0:正常, 1:已过期, 2:即将过期, 3:正在分配, 4:未使用, 5:已失效; //0:正常, 1:已过期, 2:即将过期, 3:正在分配, 4:未使用, 5:已失效, 6:未分配
private int status; private int status;
private List<String> port; private List<String> port;
private long purchasedTime; private long purchasedTime;
...@@ -35,6 +35,15 @@ public class IpResource implements Serializable { ...@@ -35,6 +35,15 @@ public class IpResource implements Serializable {
private String shopName; private String shopName;
private List<String> protocol; private List<String> protocol;
private String password; private String password;
private double price;
private boolean isLocked;
private long lockTimestamp;
private String unit;
private int period;
private String tid;
public String getDetails() { public String getDetails() {
return details; return details;
...@@ -195,4 +204,52 @@ public class IpResource implements Serializable { ...@@ -195,4 +204,52 @@ public class IpResource implements Serializable {
public void setOwner(String owner) { public void setOwner(String owner) {
this.owner = 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 String getTid() {
return tid;
}
public void setTid(String tid) {
this.tid = tid;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
} }
...@@ -8,7 +8,7 @@ import org.springframework.data.mongodb.repository.MongoRepository; ...@@ -8,7 +8,7 @@ import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List; import java.util.List;
public interface IpResourceRepository extends MongoRepository<IpResource, String> { public interface IpResourceRepository extends MongoRepository<IpResource, String>, IpResourceRepositoryCustom {
IpResource findByAddr(String addr); IpResource findByAddr(String addr);
IpResource findByAddrAndIsDeleted(String addr, boolean isDeleted); IpResource findByAddrAndIsDeleted(String addr, boolean isDeleted);
IpResource findByIdAndIsDeleted(String id, boolean isDeleted); IpResource findByIdAndIsDeleted(String id, boolean isDeleted);
...@@ -22,6 +22,8 @@ public interface IpResourceRepository extends MongoRepository<IpResource, String ...@@ -22,6 +22,8 @@ public interface IpResourceRepository extends MongoRepository<IpResource, String
Page<IpResource> findByVendorLikeAndIdInAndIsDeletedOrderByPurchasedTimeDesc(String vendor, List<String> ipIds, boolean isDeleted, Pageable pageable); Page<IpResource> findByVendorLikeAndIdInAndIsDeletedOrderByPurchasedTimeDesc(String vendor, List<String> ipIds, boolean isDeleted, Pageable pageable);
Page<IpResource> findByRegionLikeAndIdInAndIsDeletedOrderByPurchasedTimeDesc(String region, List<String> ipIds, boolean isDeleted, Pageable pageable); Page<IpResource> findByRegionLikeAndIdInAndIsDeletedOrderByPurchasedTimeDesc(String region, List<String> ipIds, boolean isDeleted, Pageable pageable);
List<IpResource> findByStatusAndLockedAndLockTimestampLessThan(int status, boolean locked, long timestamp);
List<IpResource> findByValidTimeBetween(long beginTime, long endTime); List<IpResource> findByValidTimeBetween(long beginTime, long endTime);
int countByStatusAndIdInAndIsDeleted(int status, List<String> ipIds, boolean isDeleted); int countByStatusAndIdInAndIsDeleted(int status, List<String> ipIds, boolean isDeleted);
......
package com.edgec.browserbackend.browser.repository;
import com.edgec.browserbackend.browser.domain.IpResource;
import java.util.List;
public interface IpResourceRepositoryCustom {
boolean lockTask(IpResource ipResource);
boolean unLockTask(String id);
List<IpResource> sampleTasks(int status, long timestamp);
}
package com.edgec.browserbackend.browser.repository;
import com.edgec.browserbackend.browser.domain.IpResource;
import com.mongodb.client.result.UpdateResult;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.elasticsearch.common.recycler.Recycler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.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.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Update;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import static org.springframework.data.mongodb.core.query.Criteria.where;
public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCustom {
@Autowired
MongoTemplate mongoTemplate;
@Override
public boolean lockTask(IpResource ipResource) {
Document doc = new Document();
BasicQuery basicQuery = new BasicQuery(doc);
Criteria criteria = new Criteria();
criteria.orOperator(where("id").is(ipResource.getId()).and("isLocked").is(false).and("status").is(ipResource.getStatus()),
where("lockTimestamp").lte(Instant.now().minusSeconds(300).toEpochMilli()).and("status").is(ipResource.getStatus()));
basicQuery.addCriteria(criteria);
Update update = new Update();
update.set("isLocked", true).set("lockTimestamp", Instant.now().toEpochMilli());
UpdateResult result = mongoTemplate.updateFirst(basicQuery, update, IpResource.class);
if (result.getModifiedCount() < 1)
return false;
else
return true;
}
@Override
public boolean unLockTask(String id) {
Document doc = new Document();
BasicQuery basicQuery = new BasicQuery(doc);
basicQuery.addCriteria(where("id").is(id));
Update update = new Update();
update.set("isLocked", false).set("lockTimestamp", Instant.now().toEpochMilli());
UpdateResult result = mongoTemplate.updateFirst(basicQuery, update, IpResource.class);
if (result.getModifiedCount() < 1)
return false;
else
return true;
}
@Override
public List<IpResource> sampleTasks(int status, long timestamp) {
Criteria matchCriteria = new Criteria();
matchCriteria.orOperator(where("status").is(status).and("isLocked").is(false),
where("isLocked").is(true).and("lockTimeStamp").lte(timestamp));
MatchOperation match = Aggregation.match(matchCriteria);
SampleOperation sample = Aggregation.sample(20);
AggregationResults<IpResource> results = mongoTemplate.aggregate(Aggregation.newAggregation(match, sample), IpResource.class, IpResource.class);
List<IpResource> mappedResults = results.getMappedResults();
return mappedResults;
}
}
...@@ -8,10 +8,13 @@ import org.apache.poi.ss.usermodel.*; ...@@ -8,10 +8,13 @@ import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import java.io.*; import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/** /**
* @Desc * @Desc
...@@ -160,5 +163,35 @@ public class FileUtil { ...@@ -160,5 +163,35 @@ public class FileUtil {
return cellValue; return cellValue;
} }
/**
* 方法说明将files打包放到一个zip中。
* @return
* @throws IOException
*/
public static File putBatchFilesInZip(List<File> filePaths,File tempFile) throws IOException {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tempFile));
for(File inputFile : filePaths) {
try(FileInputStream fis = new FileInputStream(inputFile);){
//压缩文件中写入名称
ZipEntry entry = new ZipEntry(inputFile.getName());
zos.putNextEntry(entry);
// 向压缩文件中输出数据
int len = 0;
byte[] bt = new byte[5*1024];
while ((len = fis.read(bt)) != -1) {
//压缩文件中写入真正的文件流
zos.write(bt, 0, len);
}
}catch(Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
}
zos.flush();
zos.close();
return tempFile;
}
} }
...@@ -19,26 +19,26 @@ public abstract class ThreadPoolUtils { ...@@ -19,26 +19,26 @@ public abstract class ThreadPoolUtils {
public static final BlockingQueue<Runnable> schedulerQueue = new UniquePriorityBlockingQueue<>(50); public static final BlockingQueue<Runnable> schedulerQueue = new UniquePriorityBlockingQueue<>(50);
private static final int TASK_POOL_COUNT = 80; private static final int TASK_POOL_COUNT = 20;
public static final ExecutorService taskExecutorPool = Executors.newFixedThreadPool(TASK_POOL_COUNT, new ThreadFactory() { public static final ExecutorService buyIpTasksPool = Executors.newFixedThreadPool(TASK_POOL_COUNT, new ThreadFactory() {
int count = 1; int count = 1;
@Override @Override
public Thread newThread(Runnable runnable) { public Thread newThread(Runnable runnable) {
return new Thread(runnable, "intelligroup-taskexec-" + count++); return new Thread(runnable, "browser-buyIp-task-" + count++);
} }
}); });
private static final int TIME_SCHEDULER_POOL_COUNT = 10; private static final int TIME_SCHEDULER_POOL_COUNT = 20;
public static final ScheduledExecutorService timeSchedulerPool = Executors.newScheduledThreadPool(TIME_SCHEDULER_POOL_COUNT, new ThreadFactory() { public static final ScheduledExecutorService queryIpTasksPool = Executors.newScheduledThreadPool(TIME_SCHEDULER_POOL_COUNT, new ThreadFactory() {
int count = 1; int count = 1;
@Override @Override
public Thread newThread(Runnable runnable) { public Thread newThread(Runnable runnable) {
return new Thread(runnable, "intelligroup-time-scheduler-" + count++); return new Thread(runnable, "browser-queyrIp-taks-" + count++);
} }
}); });
......
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