Merge branch 'dev' into main

main
ZhouXY108 2023-02-17 15:57:05 +08:00
commit 1b551eeb4c
61 changed files with 1171 additions and 405 deletions

View File

@ -4,6 +4,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler.ExceptionInfoHolder;
/** /**
* AllExceptionHandlerConfig * AllExceptionHandlerConfig
*/ */
@ -12,7 +14,7 @@ import org.springframework.context.annotation.Configuration;
public class AllExceptionHandlerConfig { public class AllExceptionHandlerConfig {
@Bean @Bean
AllExceptionHandler getAllExceptionHandler() { AllExceptionHandler getAllExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) {
return new AllExceptionHandler(ExceptionInfoHolderFactory.newDefaultExceptionInfoHolder()); return new AllExceptionHandler(exceptionInfoHolder);
} }
} }

View File

@ -40,9 +40,8 @@ import xyz.zhouxy.plusone.util.RestfulResult;
@Order(Ordered.LOWEST_PRECEDENCE - 1) @Order(Ordered.LOWEST_PRECEDENCE - 1)
@Slf4j @Slf4j
public class DefaultExceptionHandler extends BaseExceptionHandler { public class DefaultExceptionHandler extends BaseExceptionHandler {
public DefaultExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) {
public DefaultExceptionHandler() { super(exceptionInfoHolder);
super(ExceptionInfoHolderFactory.newDefaultExceptionInfoHolder());
set(IllegalArgumentException.class, 4010000, "格式错误", HttpStatus.FORBIDDEN); set(IllegalArgumentException.class, 4010000, "格式错误", HttpStatus.FORBIDDEN);
set(DataAccessException.class, 6030000, "数据库错误", HttpStatus.INTERNAL_SERVER_ERROR, true); set(DataAccessException.class, 6030000, "数据库错误", HttpStatus.INTERNAL_SERVER_ERROR, true);
set(MethodArgumentNotValidException.class, set(MethodArgumentNotValidException.class,

View File

@ -31,6 +31,11 @@
<artifactId>spring-webmvc</artifactId> <artifactId>spring-webmvc</artifactId>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency> <dependency>
<groupId>xyz.zhouxy.plusone</groupId> <groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-commons</artifactId> <artifactId>plusone-commons</artifactId>

View File

@ -6,12 +6,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
/** /**
* *
* *
* <p>
*
* </p>
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a> * @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
* @see xyz.zhouxy.plusone.util.AssertResult
*/ */
@ResponseStatus(HttpStatus.NOT_FOUND) @ResponseStatus(HttpStatus.NOT_FOUND)
public class DataNotExistException extends PlusoneException { public class DataNotExistException extends PlusoneException {

View File

@ -4,7 +4,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.ResponseStatus;
/** /**
* *
* *
* <p> * <p>
* *
@ -14,18 +14,18 @@ import org.springframework.web.bind.annotation.ResponseStatus;
* @see xyz.zhouxy.plusone.util.AssertResult * @see xyz.zhouxy.plusone.util.AssertResult
*/ */
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public class DataOperationNumberException extends PlusoneException { public class DataOperationResultException extends PlusoneException {
@java.io.Serial @java.io.Serial
private static final long serialVersionUID = -9220765735990318186L; private static final long serialVersionUID = -9220765735990318186L;
public static final int ERROR_CODE = 4110200; public static final int ERROR_CODE = 4110200;
public DataOperationNumberException() { public DataOperationResultException() {
super(ERROR_CODE, "数据操作的行数不符合预期"); super(ERROR_CODE, "数据操作结果不符合预期");
} }
public DataOperationNumberException(String message) { public DataOperationResultException(String message) {
super(ERROR_CODE, message); super(ERROR_CODE, message);
} }
} }

View File

@ -1,10 +1,10 @@
package xyz.zhouxy.plusone.util; package xyz.zhouxy.plusone.util;
import xyz.zhouxy.plusone.exception.DataNotExistException; import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.exception.DataOperationNumberException; import xyz.zhouxy.plusone.exception.DataOperationResultException;
import java.util.Objects; import java.util.Objects;
import java.util.function.Supplier;
/** /**
* *
@ -17,51 +17,34 @@ public final class AssertResult {
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");
} }
public static void update(boolean expression) { public static <E extends Throwable> void isTrue(boolean condition, Supplier<E> e) throws E {
if (!expression) { if (!condition) {
throw new DataOperationNumberException(); throw e.get();
} }
} }
public static void update(boolean expression, String message) { public static <T> void equals(T result, T expectedValue) {
if (!expression) { isTrue(Objects.equals(result, expectedValue), DataOperationResultException::new);
throw new DataOperationNumberException(message);
}
} }
public static void update(Object i, int expectedValue) { public static <T> void equals(T result, T expectedValue, String msgTemplate, Object... args) {
if (!Objects.equals(i, expectedValue)) { isTrue(!Objects.equals(result, expectedValue),
throw new DataOperationNumberException(); () -> new DataOperationResultException(String.format(msgTemplate, args)));
}
} }
public static void update(Object i, int expectedValue, String format) { public static void updateOneRow(int i) {
if (!Objects.equals(i, expectedValue)) { equals(i, 1);
throw new DataOperationNumberException(String.format(format, i));
}
} }
public static void exist(boolean expression) { public static void updateOneRow(int i, String format, Object... args) {
if (!expression) { equals(i, 1, format, args);
throw new DataNotExistException();
}
}
public static void exist(boolean expression, String message) {
if (!expression) {
throw new DataNotExistException(message);
}
} }
public static void nonNull(Object obj) { public static void nonNull(Object obj) {
if (Objects.isNull(obj)) { isTrue(Objects.nonNull(obj), DataNotExistException::new);
throw new DataNotExistException();
}
} }
public static void nonNull(Object obj, String message) { public static void nonNull(Object obj, String message) {
if (Objects.isNull(obj)) { isTrue(Objects.nonNull(obj), () -> new DataNotExistException(message));
throw new DataNotExistException(message);
}
} }
} }

View File

@ -1,6 +1,7 @@
package xyz.zhouxy.plusone.domain; package xyz.zhouxy.plusone.domain;
import java.io.Serializable; import java.io.Serializable;
import java.util.Optional;
/** /**
* Repository * Repository
@ -10,7 +11,7 @@ import java.io.Serializable;
*/ */
public interface IRepository<T extends AggregateRoot<ID>, ID extends Serializable> { public interface IRepository<T extends AggregateRoot<ID>, ID extends Serializable> {
T find(ID id); Optional<T> find(ID id);
T save(T entity); T save(T entity);

View File

@ -1,5 +1,7 @@
package xyz.zhouxy.plusone.domain; package xyz.zhouxy.plusone.domain;
import java.util.Optional;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonValue; import com.fasterxml.jackson.annotation.JsonValue;
@ -31,4 +33,8 @@ public abstract class ValidatableStringRecord implements IValueObject {
public String toString() { public String toString() {
return value; return value;
} }
public static String getValueOrNull(Optional<? extends ValidatableStringRecord> s) {
return s.map(ValidatableStringRecord::value).orElse(null);
}
} }

View File

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>xyz.zhouxy</groupId> <groupId>xyz.zhouxy</groupId>
@ -84,6 +86,11 @@
<groupId>com.tencentcloudapi</groupId> <groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java-sms</artifactId> <artifactId>tencentcloud-sdk-java-sms</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -0,0 +1,18 @@
package xyz.zhouxy.plusone.exception.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xyz.zhouxy.plusone.constant.ErrorCodeConsts;
import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler.ExceptionInfoHolder;
@Configuration
public class PlusoneExceptionHandlerConfig {
@Bean
@ConditionalOnMissingBean
ExceptionInfoHolder exceptionInfoHolder() {
return new ExceptionInfoHolder(ErrorCodeConsts.DEFAULT_ERROR_CODE);
}
}

View File

@ -1,15 +0,0 @@
package xyz.zhouxy.plusone.exception.handler;
import xyz.zhouxy.plusone.constant.ErrorCodeConsts;
import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler.ExceptionInfoHolder;
public class ExceptionInfoHolderFactory {
private ExceptionInfoHolderFactory() {
throw new IllegalStateException("Utility class");
}
public static ExceptionInfoHolder newDefaultExceptionInfoHolder() {
return new ExceptionInfoHolder(ErrorCodeConsts.DEFAULT_ERROR_CODE);
}
}

View File

@ -4,60 +4,61 @@ import java.io.Serializable;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import org.springframework.jdbc.core.ResultSetExtractor; import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource; import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import xyz.zhouxy.plusone.domain.Entity; import xyz.zhouxy.plusone.domain.Entity;
public abstract class JdbcEntityDaoSupport<T extends Entity<ID>, ID extends Serializable> { public abstract class JdbcEntityDaoSupport<T extends Entity<ID>, ID extends Serializable>
protected final NamedParameterJdbcTemplate jdbc; extends PlusoneJdbcDaoSupport {
protected RowMapper<T> rowMapper; protected RowMapper<T> rowMapper;
protected ResultSetExtractor<T> resultSetExtractor; protected ResultSetExtractor<Optional<T>> resultSetExtractor;
protected JdbcEntityDaoSupport(@Nonnull NamedParameterJdbcTemplate namedParameterJdbcTemplate) { protected JdbcEntityDaoSupport(@Nonnull NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
this.jdbc = namedParameterJdbcTemplate; super(namedParameterJdbcTemplate);
this.rowMapper = (ResultSet rs, int rowNum) -> mapRow(rs); this.rowMapper = (ResultSet rs, int rowNum) -> mapRow(rs);
this.resultSetExtractor = (ResultSet rs) -> rs.next() ? mapRow(rs) : null; this.resultSetExtractor = (ResultSet rs) -> rs.next() ? Optional.of(mapRow(rs)) : Optional.empty();
} }
protected final T queryForObject(String sql) { protected final Optional<T> queryForObject(String sql) {
return this.jdbc.query(sql, this.resultSetExtractor); return queryForObject(sql, this.resultSetExtractor);
} }
protected final T queryForObject(String sql, SqlParameterSource paramSource) { protected final Optional<T> queryForObject(String sql, SqlParameterSource paramSource) {
return this.jdbc.query(sql, paramSource, this.resultSetExtractor); return queryForObject(sql, paramSource, this.resultSetExtractor);
}
protected final Optional<T> queryForObject(String sql, String paramName, Object value) {
return queryForObject(sql, new MapSqlParameterSource(paramName, value), this.resultSetExtractor);
} }
protected final List<T> queryForList(String sql) { protected final List<T> queryForList(String sql) {
return this.jdbc.query(sql, this.rowMapper); return queryForList(sql, this.rowMapper);
} }
protected final List<T> queryForList(String sql, SqlParameterSource parameterSource) { protected final List<T> queryForList(String sql, SqlParameterSource parameterSource) {
return this.jdbc.query(sql, parameterSource, this.rowMapper); return queryForList(sql, parameterSource, this.rowMapper);
}
protected final List<T> queryForList(String sql, String paramName, Object value) {
return queryForList(sql, new MapSqlParameterSource(paramName, value), this.rowMapper);
} }
protected final Stream<T> queryForStream(String sql, SqlParameterSource parameterSource) { protected final Stream<T> queryForStream(String sql, SqlParameterSource parameterSource) {
return this.jdbc.queryForStream(sql, parameterSource, this.rowMapper); return queryForStream(sql, parameterSource, this.rowMapper);
} }
protected final <E> Stream<E> queryForStream(String sql, SqlParameterSource parameterSource, Class<E> elementType) { protected final Stream<T> queryForStream(String sql, String paramName, Object value) {
return this.jdbc.queryForList(sql, parameterSource, elementType).stream(); return queryForStream(sql, new MapSqlParameterSource(paramName, value), this.rowMapper);
}
protected final boolean queryExists(String sql, SqlParameterSource parameterSource) {
Boolean isExists = this.jdbc.query(sql, parameterSource, ResultSet::next);
return Boolean.TRUE.equals(isExists);
}
protected final int update(String sql, SqlParameterSource parameterSource) {
return this.jdbc.update(sql, parameterSource);
} }
protected abstract T mapRow(ResultSet rs) throws SQLException; protected abstract T mapRow(ResultSet rs) throws SQLException;
@ -66,7 +67,7 @@ public abstract class JdbcEntityDaoSupport<T extends Entity<ID>, ID extends Seri
this.rowMapper = rowMapper; this.rowMapper = rowMapper;
} }
protected void setResultSetExtractor(@Nonnull ResultSetExtractor<T> resultSetExtractor) { protected void setResultSetExtractor(@Nonnull ResultSetExtractor<Optional<T>> resultSetExtractor) {
this.resultSetExtractor = resultSetExtractor; this.resultSetExtractor = resultSetExtractor;
} }
} }

View File

@ -1,5 +1,6 @@
package xyz.zhouxy.plusone.jdbc; package xyz.zhouxy.plusone.jdbc;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
@ -16,15 +17,17 @@ import xyz.zhouxy.plusone.spring.SpringContextHolder;
*/ */
public final class JdbcFactory { public final class JdbcFactory {
private static final ApplicationContext CONTEXT = SpringContextHolder.getContext();
private JdbcFactory() { private JdbcFactory() {
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");
} }
public static JdbcTemplate getJdbcTemplate() { public static JdbcTemplate getJdbcTemplate() {
return SpringContextHolder.getContext().getBean(JdbcTemplate.class); return CONTEXT.getBean(JdbcTemplate.class);
} }
public static NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() { public static NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
return SpringContextHolder.getContext().getBean(NamedParameterJdbcTemplate.class); return CONTEXT.getBean(NamedParameterJdbcTemplate.class);
} }
} }

View File

@ -1,6 +1,7 @@
package xyz.zhouxy.plusone.jdbc; package xyz.zhouxy.plusone.jdbc;
import java.io.Serializable; import java.io.Serializable;
import java.util.Optional;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -20,7 +21,7 @@ public abstract class JdbcRepositorySupport<T extends AggregateRoot<ID>, ID exte
protected abstract void doDelete(@Nonnull T entity); protected abstract void doDelete(@Nonnull T entity);
protected abstract T doFindById(@Nonnull ID id); protected abstract Optional<T> doFindById(@Nonnull ID id);
protected abstract T doInsert(@Nonnull T entity); protected abstract T doInsert(@Nonnull T entity);
@ -35,7 +36,7 @@ public abstract class JdbcRepositorySupport<T extends AggregateRoot<ID>, ID exte
} }
@Override @Override
public final T find(ID id) { public final Optional<T> find(ID id) {
if (id == null) { if (id == null) {
throw new IllegalArgumentException("Id cannot be null."); throw new IllegalArgumentException("Id cannot be null.");
} }

View File

@ -0,0 +1,172 @@
package xyz.zhouxy.plusone.jdbc;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import xyz.zhouxy.plusone.exception.DataOperationResultException;
import xyz.zhouxy.plusone.util.NumberUtil;
public abstract class PlusoneJdbcDaoSupport {
protected final NamedParameterJdbcTemplate jdbc;
protected PlusoneJdbcDaoSupport(@Nonnull NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
this.jdbc = namedParameterJdbcTemplate;
}
protected final <T> T queryForObject(String sql, ResultSetExtractor<T> resultSetExtractor) {
return this.jdbc.query(sql, resultSetExtractor);
}
protected final <T> T queryForObject(String sql, SqlParameterSource paramSource,
ResultSetExtractor<T> resultSetExtractor) {
return this.jdbc.query(sql, paramSource, resultSetExtractor);
}
protected final <T> T queryForObject(String sql, String paramName, Object value,
ResultSetExtractor<T> resultSetExtractor) {
return this.jdbc.query(sql, new MapSqlParameterSource(paramName, value), resultSetExtractor);
}
protected final <T> List<T> queryForList(String sql, RowMapper<T> rowMapper) {
return this.jdbc.query(sql, rowMapper);
}
protected final <T> List<T> queryForList(String sql, SqlParameterSource parameterSource, RowMapper<T> rowMapper) {
return this.jdbc.query(sql, parameterSource, rowMapper);
}
protected final <T> List<T> queryForList(String sql, String paramName, Object value, RowMapper<T> rowMapper) {
return this.jdbc.query(sql, new MapSqlParameterSource(paramName, value), rowMapper);
}
protected final <T> Stream<T> queryForStream(String sql, SqlParameterSource parameterSource,
RowMapper<T> rowMapper) {
return this.jdbc.queryForStream(sql, parameterSource, rowMapper);
}
protected final <T> Stream<T> queryForStream(String sql, String paramName, Object value, RowMapper<T> rowMapper) {
return this.jdbc.queryForStream(sql, new MapSqlParameterSource(paramName, value), rowMapper);
}
protected final <T> Stream<T> queryForStream(String sql, SqlParameterSource parameterSource, Class<T> elementType) {
return this.jdbc.queryForList(sql, parameterSource, elementType).stream();
}
protected final <T> Stream<T> queryForStream(String sql, String paramName, Object value, Class<T> elementType) {
return queryForStream(sql, new MapSqlParameterSource(paramName, value), elementType);
}
protected final boolean queryExists(String sql, SqlParameterSource parameterSource) {
Boolean isExists = this.jdbc.queryForObject(sql, parameterSource, Boolean.TYPE);
return Boolean.TRUE.equals(isExists);
}
protected final boolean queryExists(String sql, String paramName, Object value) {
return queryExists(sql, new MapSqlParameterSource(paramName, value));
}
protected final int update(String sql, SqlParameterSource parameterSource) {
return this.jdbc.update(sql, parameterSource);
}
protected final int update(String sql, String paramName, Object value) {
return update(sql, new MapSqlParameterSource(paramName, value));
}
protected final int batchUpdate(String sql, SqlParameterSource[] batchArgs) {
int[] i = this.jdbc.batchUpdate(sql, batchArgs);
return NumberUtil.sum(i);
}
protected final <T> int batchUpdate(String sql, Stream<T> c,
@Nonnull Function<T, SqlParameterSource> paramSourceBuilder) {
int[] i = this.jdbc.batchUpdate(sql, buildSqlParameterSourceArray(c, paramSourceBuilder));
return NumberUtil.sum(i);
}
protected final <T> int batchUpdate(String sql, Collection<T> c,
@Nonnull Function<T, SqlParameterSource> paramSourceBuilder) {
int[] i = this.jdbc.batchUpdate(sql, buildSqlParameterSourceArray(c, paramSourceBuilder));
return NumberUtil.sum(i);
}
protected static final <T> void assertResultEquals(T result, T expectedValue) {
if (!Objects.equals(result, expectedValue)) {
throw new DataOperationResultException();
}
}
protected static final <T> void assertResultEquals(T result, T expectedValue, Function<T, String> errMsg) {
if (!Objects.equals(result, expectedValue)) {
throw new DataOperationResultException(errMsg.apply(result));
}
}
protected static final <T> void assertResultEquals(T result, T expectedValue, String msgTemplate, Object... args) {
if (!Objects.equals(result, expectedValue)) {
throw new DataOperationResultException(String.format(msgTemplate, args));
}
}
protected static final <T, E extends Throwable> void assertResultEqualsOrThrow(T result, T expectedValue,
Function<T, E> e) throws E {
if (!Objects.equals(result, expectedValue)) {
throw e.apply(result);
}
}
protected static final void assertUpdateOneRow(int result) {
assertResultEquals(result, 1);
}
protected static final void assertUpdateOneRow(int result, Function<Integer, String> errMsg) {
assertResultEquals(result, 1, errMsg);
}
protected static final void assertUpdateOneRow(int result, String msgTemplate, Object... args) {
assertResultEquals(result, 1, msgTemplate, args);
}
protected static final <E extends Throwable> void assertUpdateOneRowOrThrow(int result, Function<Integer, E> e)
throws E {
assertResultEqualsOrThrow(result, 1, e);
}
protected static final <T> SqlParameterSource[] buildSqlParameterSourceArray(
T[] c,
@Nonnull Function<T, SqlParameterSource> paramSourceBuilder) {
if (c == null || c.length == 0) {
return new SqlParameterSource[] {};
}
return buildSqlParameterSourceArray(Arrays.stream(c), paramSourceBuilder);
}
protected static final <T> SqlParameterSource[] buildSqlParameterSourceArray(
Collection<T> c,
@Nonnull Function<T, SqlParameterSource> paramSourceBuilder) {
if (c == null || c.isEmpty()) {
return new SqlParameterSource[] {};
}
return buildSqlParameterSourceArray(c.stream(), paramSourceBuilder);
}
protected static final <T> SqlParameterSource[] buildSqlParameterSourceArray(
Stream<T> stream,
@Nonnull Function<T, SqlParameterSource> paramSourceBuilder) {
Objects.requireNonNull(stream);
Objects.requireNonNull(paramSourceBuilder);
return stream.map(paramSourceBuilder).toArray(SqlParameterSource[]::new);
}
}

View File

@ -1,6 +1,5 @@
package xyz.zhouxy.plusone.mail; package xyz.zhouxy.plusone.mail;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -15,11 +14,10 @@ import org.springframework.mail.javamail.JavaMailSender;
@Configuration @Configuration
@EnableConfigurationProperties(PlusoneMailProperties.class) @EnableConfigurationProperties(PlusoneMailProperties.class)
@ConditionalOnClass(MailService.class) @ConditionalOnClass(MailService.class)
@EnableAutoConfiguration
public class PlusoneMailAutoConfiguration { public class PlusoneMailAutoConfiguration {
@Bean @Bean
public MailService mailService(JavaMailSender mailSender, PlusoneMailProperties mailProperties) { MailService mailService(JavaMailSender mailSender, PlusoneMailProperties mailProperties) {
MailMessageFactory mailMessageFactory = new MailMessageFactory(mailProperties); MailMessageFactory mailMessageFactory = new MailMessageFactory(mailProperties);
return new SimpleMailService(mailSender, mailMessageFactory); return new SimpleMailService(mailSender, mailMessageFactory);
} }

View File

@ -0,0 +1,92 @@
package xyz.zhouxy.plusone.oss;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Objects;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import xyz.zhouxy.plusone.oss.FastDFSProperties.ConnectionPool;
@Configuration
@EnableConfigurationProperties(FastDFSProperties.class)
@ConditionalOnClass(FastDFSUtil.class)
public class FastDFSAutoConfig {
@Bean
@SuppressWarnings("all")
FastDFSUtil fastDFSUtil(FastDFSProperties props) throws IOException, FastDFSException {
List<String> trackerServerStrList = props.getTrackerServers();
if (CollectionUtils.isEmpty(trackerServerStrList)) {
throw new FastDFSException(
String.format("configure item %s is required - ", ClientGlobal.PROP_KEY_TRACKER_SERVERS));
}
try {
InetSocketAddress[] trackerServers = trackerServerStrList.stream()
.map(trackerServer -> {
String[] hostPort = trackerServer.trim().split(":");
String host = hostPort[0].trim();
int port = Integer.parseInt(hostPort[1].trim());
return new InetSocketAddress(host, port);
})
.toArray(InetSocketAddress[]::new);
ClientGlobal.initByTrackers(trackerServers);
var connectTimeoutInSecondsConf = props.getConnectTimeoutInSeconds();
if (connectTimeoutInSecondsConf != null) {
ClientGlobal.setG_connect_timeout(connectTimeoutInSecondsConf * 1000);
}
var networkTimeoutInSecondsConf = props.getNetworkTimeoutInSeconds();
if (networkTimeoutInSecondsConf != null) {
ClientGlobal.setG_network_timeout(networkTimeoutInSecondsConf * 1000);
}
var charsetConf = props.getCharset();
if (StringUtils.hasText(charsetConf)) {
ClientGlobal.setG_charset(charsetConf);
}
var httpAntiStealTokenConf = props.getHttpAntiStealToken();
if (httpAntiStealTokenConf != null) {
ClientGlobal.setG_anti_steal_token(httpAntiStealTokenConf);
}
var httpSecretKeyConf = props.getHttpSecretKey();
if (StringUtils.hasText(httpSecretKeyConf)) {
ClientGlobal.setG_secret_key(httpSecretKeyConf);
}
var httpTrackerHttpPortConf = props.getHttpTrackerHttpPort();
if (httpTrackerHttpPortConf != null) {
ClientGlobal.setG_tracker_http_port(httpTrackerHttpPortConf);
}
ConnectionPool connectionPool = props.getConnectionPool();
var poolEnabled = Objects.nonNull(connectionPool)
&& Boolean.TRUE.equals(connectionPool.getEnabled());
if (poolEnabled) {
var poolMaxCountPerEntry = connectionPool.getMaxCountPerEntry();
if (poolMaxCountPerEntry != null) {
ClientGlobal.g_connection_pool_max_count_per_entry = poolMaxCountPerEntry;
}
var poolMaxIdleTime = connectionPool.getMaxIdleTime();
if (poolMaxIdleTime != null) {
ClientGlobal.g_connection_pool_max_idle_time = poolMaxIdleTime * 1000;
}
var poolMaxWaitTimeInMS = connectionPool.getMaxWaitTimeInMs();
if (poolMaxWaitTimeInMS != null) {
ClientGlobal.g_connection_pool_max_wait_time_in_ms = poolMaxWaitTimeInMS;
}
}
return new FastDFSUtil();
} catch (MyException e) {
throw new FastDFSException(e);
}
}
}

View File

@ -0,0 +1,28 @@
package xyz.zhouxy.plusone.oss;
/**
* FastDFS {@link org.csource.common.MyException}
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
public class FastDFSException extends Exception {
public FastDFSException() {
}
public FastDFSException(String message) {
super(message);
}
public FastDFSException(Throwable cause) {
super(cause);
}
public FastDFSException(String message, Throwable cause) {
super(message, cause);
}
public FastDFSException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@ -0,0 +1,121 @@
package xyz.zhouxy.plusone.oss;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("fastdfs")
public class FastDFSProperties {
private Integer connectTimeoutInSeconds;
private Integer networkTimeoutInSeconds;
private String charset;
private Boolean httpAntiStealToken;
private String httpSecretKey;
private Integer httpTrackerHttpPort;
private List<String> trackerServers;
private ConnectionPool connectionPool;
public Integer getConnectTimeoutInSeconds() {
return connectTimeoutInSeconds;
}
public void setConnectTimeoutInSeconds(Integer connectTimeoutInSeconds) {
this.connectTimeoutInSeconds = connectTimeoutInSeconds;
}
public Integer getNetworkTimeoutInSeconds() {
return networkTimeoutInSeconds;
}
public void setNetworkTimeoutInSeconds(Integer networkTimeoutInSeconds) {
this.networkTimeoutInSeconds = networkTimeoutInSeconds;
}
public String getCharset() {
return charset;
}
public void setCharset(String charset) {
this.charset = charset;
}
public Boolean getHttpAntiStealToken() {
return httpAntiStealToken;
}
public void setHttpAntiStealToken(Boolean httpAntiStealToken) {
this.httpAntiStealToken = httpAntiStealToken;
}
public String getHttpSecretKey() {
return httpSecretKey;
}
public void setHttpSecretKey(String httpSecretKey) {
this.httpSecretKey = httpSecretKey;
}
public Integer getHttpTrackerHttpPort() {
return httpTrackerHttpPort;
}
public void setHttpTrackerHttpPort(Integer httpTrackerHttpPort) {
this.httpTrackerHttpPort = httpTrackerHttpPort;
}
public List<String> getTrackerServers() {
return trackerServers;
}
public void setTrackerServers(List<String> trackerServers) {
this.trackerServers = trackerServers;
}
public ConnectionPool getConnectionPool() {
return connectionPool;
}
public void setConnectionPool(ConnectionPool connectionPool) {
this.connectionPool = connectionPool;
}
public static class ConnectionPool {
private Boolean enabled;
private Integer maxCountPerEntry;
private Integer maxIdleTime;
private Integer maxWaitTimeInMs;
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public Integer getMaxCountPerEntry() {
return maxCountPerEntry;
}
public void setMaxCountPerEntry(Integer maxCountPerEntry) {
this.maxCountPerEntry = maxCountPerEntry;
}
public Integer getMaxIdleTime() {
return maxIdleTime;
}
public void setMaxIdleTime(Integer maxIdleTime) {
this.maxIdleTime = maxIdleTime;
}
public Integer getMaxWaitTimeInMs() {
return maxWaitTimeInMs;
}
public void setMaxWaitTimeInMs(Integer maxWaitTimeInMs) {
this.maxWaitTimeInMs = maxWaitTimeInMs;
}
}
}

View File

@ -0,0 +1,126 @@
package xyz.zhouxy.plusone.oss;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.Getter;
public class FastDFSUtil {
private final TrackerServer trackerServer;
private final StorageServer storageServer;
private static final Logger logger = LoggerFactory.getLogger(FastDFSUtil.class);
FastDFSUtil() throws IOException, MyException {
TrackerClient trackerClient = new TrackerClient();
this.trackerServer = trackerClient.getTrackerServer();
this.storageServer = trackerClient.getStoreStorage(trackerServer);
}
/**
* Fast DFS
*
* @param file
* @return 2 elements string array if success:<br>
* <ul>
* <li>results[0]: the group name to store the file</li>
* </ul>
* <ul>
* <li>results[1]: the new created filename</li>
* </ul>
* return null if fail
* @throws FastDFSException
*/
public String[] upload(FastDFSFile file) throws FastDFSException {
logger.info("File Name: {}, File Length: {}", file.getName(), file.getContent().length);
NameValuePair[] metaList = new NameValuePair[1];
metaList[0] = new NameValuePair("author", file.getAuthor());
long startTime = System.currentTimeMillis();
StorageClient storageClient = null;
String[] uploadResults = null;
try {
storageClient = new StorageClient(this.trackerServer, this.storageServer);
uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), metaList);
if (uploadResults == null) {
throw new FastDFSException("upload file fail, error code: " + storageClient.getErrorCode());
}
logger.info("Upload file successfully!!! group_name: {}, remoteFileName: {}, time used: {} ms",
uploadResults[0], uploadResults[1], System.currentTimeMillis() - startTime);
} catch (IOException e) {
throw new FastDFSException("IO Exception when uploadind the file:" + file.getName(), e);
} catch (MyException e) {
throw new FastDFSException(e);
}
return uploadResults;
}
public FileInfo getFile(String groupName, String remoteFileName) throws FastDFSException {
try {
StorageClient storageClient = new StorageClient(this.trackerServer, this.storageServer);
return storageClient.get_file_info(groupName, remoteFileName);
} catch (IOException e) {
throw new FastDFSException("IO Exception: Get File from Fast DFS failed", e);
} catch (Exception e) {
throw new FastDFSException("Non IO Exception: Get File from Fast DFS failed", e);
}
}
public InputStream downFile(String groupName, String remoteFileName) throws FastDFSException {
try {
StorageClient storageClient = new StorageClient(this.trackerServer, this.storageServer);
byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
InputStream ins = new ByteArrayInputStream(fileByte);
return ins;
} catch (IOException e) {
throw new FastDFSException("IO Exception: Get File from Fast DFS failed", e);
} catch (Exception e) {
throw new FastDFSException("Non IO Exception: Get File from Fast DFS failed", e);
}
}
public void deleteFile(String groupName, String remoteFileName) throws FastDFSException {
StorageClient storageClient = new StorageClient(this.trackerServer, this.storageServer);
try {
int i = storageClient.delete_file(groupName, remoteFileName);
if (i == 0) {
logger.info("Delete file SUCCESSFULLY!!!");
} else {
throw new FastDFSException("Delete file failed, error code is: " + i);
}
} catch (IOException | MyException e) {
throw new FastDFSException(e);
}
}
@Getter
public static final class FastDFSFile {
private String name;
private byte[] content;
private String ext;
private String md5;
private String author;
public FastDFSFile(String name, byte[] content, String ext) {
this.name = name;
this.content = content;
this.ext = ext;
}
}
}

View File

@ -11,17 +11,12 @@ import org.springframework.context.annotation.Configuration;
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a> * @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/ */
@Configuration @Configuration
@EnableConfigurationProperties(value = { @EnableConfigurationProperties(SmsProperties.class)
SmsProperties.class,
SmsCredentialProperties.class,
SmsClientProperties.class,
SmsHttpProperties.class,
SmsProxyProperties.class})
@ConditionalOnClass(SmsService.class) @ConditionalOnClass(SmsService.class)
public class PlusoneSmsAutoConfiguration { public class PlusoneSmsAutoConfiguration {
@Bean @Bean
public SmsService smsService(SmsProperties smsProperties) { SmsService smsService(SmsProperties smsProperties) {
return new TencentSmsServiceImpl(smsProperties); return new TencentSmsServiceImpl(smsProperties);
} }
} }

View File

@ -4,14 +4,11 @@ import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import lombok.Data;
/** /**
* SMS * SMS
* *
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a> * @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/ */
@Data
@ConfigurationProperties("plusone.sms") @ConfigurationProperties("plusone.sms")
public class SmsProperties { public class SmsProperties {
private String region; private String region;
@ -19,33 +16,138 @@ public class SmsProperties {
private SmsClientProperties client; private SmsClientProperties client;
private String appId; private String appId;
private Map<String, String> templates; private Map<String, String> templates;
}
@Data public String getRegion() {
@ConfigurationProperties("plusone.sms.credential") return region;
class SmsCredentialProperties { }
public void setRegion(String region) {
this.region = region;
}
public SmsCredentialProperties getCredential() {
return credential;
}
public void setCredential(SmsCredentialProperties credential) {
this.credential = credential;
}
public SmsClientProperties getClient() {
return client;
}
public void setClient(SmsClientProperties client) {
this.client = client;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public Map<String, String> getTemplates() {
return templates;
}
public void setTemplates(Map<String, String> templates) {
this.templates = templates;
}
public static class SmsCredentialProperties {
private String secretId; private String secretId;
private String secretKey; private String secretKey;
}
@Data public String getSecretId() {
@ConfigurationProperties("plusone.sms.client") return secretId;
class SmsClientProperties { }
public void setSecretId(String secretId) {
this.secretId = secretId;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
}
public static class SmsClientProperties {
private String signMethod; private String signMethod;
private SmsHttpProperties http; private SmsHttpProperties http;
}
@Data public String getSignMethod() {
@ConfigurationProperties("plusone.sms.client.http") return signMethod;
class SmsHttpProperties { }
public void setSignMethod(String signMethod) {
this.signMethod = signMethod;
}
public SmsHttpProperties getHttp() {
return http;
}
public void setHttp(SmsHttpProperties http) {
this.http = http;
}
}
public static class SmsHttpProperties {
private SmsProxyProperties proxy; private SmsProxyProperties proxy;
private String reqMethod; private String reqMethod;
private Integer connTimeout; private Integer connTimeout;
}
@Data public SmsProxyProperties getProxy() {
@ConfigurationProperties("plusone.sms.client.http.proxy") return proxy;
class SmsProxyProperties { }
public void setProxy(SmsProxyProperties proxy) {
this.proxy = proxy;
}
public String getReqMethod() {
return reqMethod;
}
public void setReqMethod(String reqMethod) {
this.reqMethod = reqMethod;
}
public Integer getConnTimeout() {
return connTimeout;
}
public void setConnTimeout(Integer connTimeout) {
this.connTimeout = connTimeout;
}
}
public static class SmsProxyProperties {
private String host; private String host;
private Integer port; private Integer port;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
}
} }

View File

@ -13,6 +13,9 @@ import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.constant.ErrorCodeConsts; import xyz.zhouxy.plusone.constant.ErrorCodeConsts;
import xyz.zhouxy.plusone.exception.PlusoneException; import xyz.zhouxy.plusone.exception.PlusoneException;
import xyz.zhouxy.plusone.sms.SmsProperties.SmsCredentialProperties;
import xyz.zhouxy.plusone.sms.SmsProperties.SmsHttpProperties;
import xyz.zhouxy.plusone.sms.SmsProperties.SmsProxyProperties;
/** /**
* 使 SMS * 使 SMS

View File

@ -3,13 +3,12 @@ package xyz.zhouxy.plusone.validator;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler; import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler;
import xyz.zhouxy.plusone.exception.handler.ExceptionInfoHolderFactory;
@RestControllerAdvice @RestControllerAdvice
public class InvalidInputExceptionHandler extends BaseExceptionHandler { public class InvalidInputExceptionHandler extends BaseExceptionHandler {
protected InvalidInputExceptionHandler() { public InvalidInputExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) {
super(ExceptionInfoHolderFactory.newDefaultExceptionInfoHolder()); super(exceptionInfoHolder);
set(InvalidInputException.class, InvalidInputException.ERROR_CODE, "无效的用户输入"); set(InvalidInputException.class, InvalidInputException.ERROR_CODE, "无效的用户输入");
} }
} }

View File

@ -0,0 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
xyz.zhouxy.plusone.sms.PlusoneSmsAutoConfiguration,\
xyz.zhouxy.plusone.mail.PlusoneMailAutoConfiguration,\
xyz.zhouxy.plusone.oss.FastDFSAutoConfig

View File

@ -8,7 +8,7 @@ import lombok.NoArgsConstructor;
import xyz.zhouxy.plusone.constant.RegexConsts; import xyz.zhouxy.plusone.constant.RegexConsts;
import xyz.zhouxy.plusone.validator.BaseValidator; import xyz.zhouxy.plusone.validator.BaseValidator;
class BaseValidator2Test { class BaseValidatorTest {
@Test @Test
void testValid() { void testValid() {

View File

@ -1,5 +1,4 @@
{ {"properties": [
"properties": [
{ {
"name": "plusone.application.name", "name": "plusone.application.name",
"type": "java.lang.String", "type": "java.lang.String",
@ -30,5 +29,4 @@
"type": "java.lang.Boolean", "type": "java.lang.Boolean",
"description": "A description for 'plusone.exception.handle-all-exception'" "description": "A description for 'plusone.exception.handle-all-exception'"
} }
] ]}
}

View File

@ -50,7 +50,7 @@ plusone:
conn-timeout: 60 conn-timeout: 60
app-id: 1111111111 app-id: 1111111111
templates: templates:
code: 0000000 '[code]': 0000000
# 邮件发送相关参数 # 邮件发送相关参数
mail: mail:
@ -63,7 +63,16 @@ plusone:
exception: exception:
handle-all-exception: false handle-all-exception: false
# 日志配置 fastdfs:
logging: connect_timeout_in_seconds: 5
level: network_timeout_in_seconds: 30
root: debug charset: UTF-8
http_anti_steal_token: false
http_secret_key: FastDFS1234567890
http_tracker_http_port: 80
tracker_servers: 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122
connection_pool:
enabled: true
max_count_per_entry: 500
max_idle_time: 3600
max_wait_time_in_ms: 1000

View File

@ -6,6 +6,11 @@ plusone:
# 邮件发送相关参数 # 邮件发送相关参数
mail: mail:
subject: subject:
code: Plusone '[code]': Plusone
template: template:
code: 【Plusone】验证码%s10分钟内有效请勿泄露。 '[code]': 【Plusone】验证码%s10分钟内有效请勿泄露。
# 日志配置
logging:
level:
root: info
'[xyz.zhouxy.plusone]': debug

View File

@ -0,0 +1,33 @@
package xyz.zhouxy.plusone;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.annotation.Resource;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.oss.FastDFSException;
import xyz.zhouxy.plusone.oss.FastDFSUtil;
import xyz.zhouxy.plusone.oss.FastDFSUtil.FastDFSFile;
@SpringBootTest(classes = PlusoneApplication.class)
@Slf4j
class FastDFSTests {
@Resource
FastDFSUtil fastDFSUtil;
@Test
void testOSS() throws FileNotFoundException, IOException, FastDFSException {
try (FileInputStream in = new FileInputStream("D:\\ZhouXY\\Desktop\\666.png");) {
byte[] content = IOUtils.toByteArray(in);
String[] upload = fastDFSUtil.upload(new FastDFSFile("666.png", content, "png"));
log.info(String.join("/", upload));
}
}
}

View File

@ -0,0 +1,16 @@
spring:
profiles:
active: secret
plusone:
# 邮件发送相关参数
mail:
subject:
'[code]': Plusone
template:
'[code]': 【Plusone】验证码%s10分钟内有效请勿泄露。
# 日志配置
logging:
level:
root: info
'[xyz.zhouxy.plusone]': debug

View File

@ -20,7 +20,6 @@ import xyz.zhouxy.plusone.system.application.query.params.AccountQueryParams;
import xyz.zhouxy.plusone.system.application.service.AccountManagementService; import xyz.zhouxy.plusone.system.application.service.AccountManagementService;
import xyz.zhouxy.plusone.system.application.service.command.CreateAccountCommand; import xyz.zhouxy.plusone.system.application.service.command.CreateAccountCommand;
import xyz.zhouxy.plusone.system.application.service.command.UpdateAccountCommand; import xyz.zhouxy.plusone.system.application.service.command.UpdateAccountCommand;
import xyz.zhouxy.plusone.util.AssertResult;
import xyz.zhouxy.plusone.util.RestfulResult; import xyz.zhouxy.plusone.util.RestfulResult;
/** /**
@ -77,7 +76,6 @@ public class AccountManagementController {
adminAuthLogic.checkLogin(); adminAuthLogic.checkLogin();
adminAuthLogic.checkPermission("sys-account-details"); adminAuthLogic.checkPermission("sys-account-details");
var accountDetails = service.queryAccountDetails(accountId); var accountDetails = service.queryAccountDetails(accountId);
AssertResult.nonNull(accountDetails);
return success("查询成功", accountDetails); return success("查询成功", accountDetails);
} }
} }

View File

@ -21,18 +21,34 @@ public class AccountLoginException extends PlusoneException {
} }
public static AccountLoginException accountNotExistException() { public static AccountLoginException accountNotExistException() {
return new AccountLoginException(4030101, "用户账户不存在"); return accountNotExistException("用户账户不存在");
}
public static AccountLoginException accountNotExistException(String msg) {
return new AccountLoginException(4030101, msg);
} }
public static AccountLoginException otpErrorException() { public static AccountLoginException otpErrorException() {
return new AccountLoginException(4030501, "验证码错误"); return otpErrorException("验证码错误");
}
public static AccountLoginException otpErrorException(String msg) {
return new AccountLoginException(4030501, msg);
} }
public static AccountLoginException otpNotExistsException() { public static AccountLoginException otpNotExistsException() {
return new AccountLoginException(4030502, "验证码不存在或已过期"); return otpNotExistsException("验证码不存在或已过期");
}
public static AccountLoginException otpNotExistsException(String msg) {
return new AccountLoginException(4030502, msg);
} }
public static AccountLoginException passwordErrorException() { public static AccountLoginException passwordErrorException() {
return new AccountLoginException(4030200, "用户密码错误"); return passwordErrorException("用户密码错误");
}
public static AccountLoginException passwordErrorException(String msg) {
return new AccountLoginException(4030200, msg);
} }
} }

View File

@ -7,15 +7,14 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler; import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler;
import xyz.zhouxy.plusone.exception.handler.ExceptionInfoHolderFactory;
import xyz.zhouxy.plusone.system.application.exception.AccountLoginException; import xyz.zhouxy.plusone.system.application.exception.AccountLoginException;
import xyz.zhouxy.plusone.util.RestfulResult; import xyz.zhouxy.plusone.util.RestfulResult;
@RestControllerAdvice @RestControllerAdvice
public class AccountLoginExceptionHandler extends BaseExceptionHandler { public class AccountLoginExceptionHandler extends BaseExceptionHandler {
protected AccountLoginExceptionHandler() { public AccountLoginExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) {
super(ExceptionInfoHolderFactory.newDefaultExceptionInfoHolder()); super(exceptionInfoHolder);
} }
@ExceptionHandler({ AccountLoginException.class }) @ExceptionHandler({ AccountLoginException.class })

View File

@ -15,7 +15,6 @@ import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.exception.SameTokenInvalidException; import cn.dev33.satoken.exception.SameTokenInvalidException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler; import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler;
import xyz.zhouxy.plusone.exception.handler.ExceptionInfoHolderFactory;
import xyz.zhouxy.plusone.util.RestfulResult; import xyz.zhouxy.plusone.util.RestfulResult;
/** /**
@ -27,8 +26,9 @@ import xyz.zhouxy.plusone.util.RestfulResult;
@Slf4j @Slf4j
public class SaTokenExceptionHandler extends BaseExceptionHandler { public class SaTokenExceptionHandler extends BaseExceptionHandler {
public SaTokenExceptionHandler() {
super(ExceptionInfoHolderFactory.newDefaultExceptionInfoHolder()); public SaTokenExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) {
super(exceptionInfoHolder);
set(NotPermissionException.class, 4030103, "会话未能通过权限认证", HttpStatus.FORBIDDEN); set(NotPermissionException.class, 4030103, "会话未能通过权限认证", HttpStatus.FORBIDDEN);
set(NotRoleException.class, 4030103, "会话未能通过角色认证", HttpStatus.FORBIDDEN); set(NotRoleException.class, 4030103, "会话未能通过角色认证", HttpStatus.FORBIDDEN);
set(DisableServiceException.class, 4030202, "账号指定服务已被封禁", HttpStatus.FORBIDDEN); set(DisableServiceException.class, 4030202, "账号指定服务已被封禁", HttpStatus.FORBIDDEN);

View File

@ -1,8 +1,7 @@
package xyz.zhouxy.plusone.system.application.service; package xyz.zhouxy.plusone.system.application.service;
import xyz.zhouxy.plusone.system.constant.AuthLogic;
import java.util.List; import java.util.List;
import java.util.Optional;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -10,7 +9,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import cn.dev33.satoken.stp.StpLogic; import cn.dev33.satoken.stp.StpLogic;
import cn.hutool.core.lang.Assert;
import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil; import xyz.zhouxy.plusone.system.application.common.util.PrincipalUtil;
import xyz.zhouxy.plusone.system.application.exception.AccountLoginException; import xyz.zhouxy.plusone.system.application.exception.AccountLoginException;
import xyz.zhouxy.plusone.system.application.query.AccountQueries; import xyz.zhouxy.plusone.system.application.query.AccountQueries;
@ -19,6 +17,7 @@ import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject;
import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordByOtpCommand; import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordByOtpCommand;
import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordCommand; import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordCommand;
import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordWithoutLoginCommand; import xyz.zhouxy.plusone.system.application.service.command.ChangePasswordWithoutLoginCommand;
import xyz.zhouxy.plusone.system.constant.AuthLogic;
import xyz.zhouxy.plusone.system.domain.model.account.Account; import xyz.zhouxy.plusone.system.domain.model.account.Account;
import xyz.zhouxy.plusone.system.domain.model.account.AccountRepository; import xyz.zhouxy.plusone.system.domain.model.account.AccountRepository;
import xyz.zhouxy.plusone.system.domain.model.account.Email; import xyz.zhouxy.plusone.system.domain.model.account.Email;
@ -34,7 +33,7 @@ import xyz.zhouxy.plusone.validator.InvalidInputException;
@Service @Service
public class AccountContextService { public class AccountContextService {
private final static StpLogic adminAuthLogic = AuthLogic.adminAuthLogic; private static final StpLogic adminAuthLogic = AuthLogic.adminAuthLogic;
@Resource @Resource
private AccountQueries accountQueries; private AccountQueries accountQueries;
@ -65,7 +64,8 @@ public class AccountContextService {
@Transactional @Transactional
public void changePassword(ChangePasswordCommand command) { public void changePassword(ChangePasswordCommand command) {
adminAuthLogic.checkLogin(); adminAuthLogic.checkLogin();
Account account = accountRepository.find(adminAuthLogic.getLoginIdAsLong()); Account account = accountRepository.find(adminAuthLogic.getLoginIdAsLong())
.orElseThrow(() -> AccountLoginException.accountNotExistException("当前所登录的账号不存在"));
account.checkPassword(command.getPassword()); account.checkPassword(command.getPassword());
account.changePassword(command.getNewPassword(), command.getPasswordConfirmation()); account.changePassword(command.getNewPassword(), command.getPasswordConfirmation());
accountRepository.save(account); accountRepository.save(account);
@ -77,9 +77,10 @@ public class AccountContextService {
String principal = command.getAccount(); String principal = command.getAccount();
Principal emailOrMobilePhone = PrincipalUtil.getEmailOrMobilePhone(principal); Principal emailOrMobilePhone = PrincipalUtil.getEmailOrMobilePhone(principal);
Account account = emailOrMobilePhone instanceof Email Account account = (emailOrMobilePhone instanceof Email
? accountRepository.findByEmail((Email) emailOrMobilePhone) ? accountRepository.findByEmail((Email) emailOrMobilePhone)
: accountRepository.findByMobilePhone((MobilePhone) emailOrMobilePhone); : accountRepository.findByMobilePhone((MobilePhone) emailOrMobilePhone))
.orElseThrow(() -> AccountLoginException.accountNotExistException("当前所登录的账号不存在"));
account.checkPassword(command.getOldPassword()); account.checkPassword(command.getOldPassword());
account.changePassword(command.getNewPassword(), command.getPasswordConfirmation()); account.changePassword(command.getNewPassword(), command.getPasswordConfirmation());
accountRepository.save(account); accountRepository.save(account);
@ -90,12 +91,12 @@ public class AccountContextService {
public void changePasswordByOtp(ChangePasswordByOtpCommand command) { public void changePasswordByOtp(ChangePasswordByOtpCommand command) {
var principal = command.getAccount(); var principal = command.getAccount();
Account account = switch (command.getPrincipalType()) { Optional<Account> account = switch (command.getPrincipalType()) {
case EMAIL -> accountRepository.findByEmail(Email.of(principal)); case EMAIL -> accountRepository.findByEmail(Email.of(principal));
case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal)); case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal));
default -> throw InvalidInputException.unsupportedPrincipalTypeException("输入邮箱地址或手机号"); default -> throw InvalidInputException.unsupportedPrincipalTypeException("输入邮箱地址或手机号");
}; };
Assert.notNull(account, () -> AccountLoginException.accountNotExistException()); account.orElseThrow(AccountLoginException::accountNotExistException);
mailAndSmsVerifyService.checkOtp(principal, command.getOtp()); mailAndSmsVerifyService.checkOtp(principal, command.getOtp());
} }

View File

@ -14,6 +14,7 @@ import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.system.application.exception.AccountRegisterException; import xyz.zhouxy.plusone.system.application.exception.AccountRegisterException;
import xyz.zhouxy.plusone.system.application.query.AccountQueries; import xyz.zhouxy.plusone.system.application.query.AccountQueries;
import xyz.zhouxy.plusone.system.application.query.params.AccountQueryParams; import xyz.zhouxy.plusone.system.application.query.params.AccountQueryParams;
@ -76,16 +77,16 @@ public class AccountManagementService {
public void deleteAccounts(List<Long> ids) { public void deleteAccounts(List<Long> ids) {
Account accountToDelete; Account accountToDelete;
for (var id : ids) { for (var id : ids) {
accountToDelete = accountRepository.find(id); accountToDelete = accountRepository.find(id)
AssertResult.nonNull(accountToDelete); .orElseThrow(() -> new DataNotExistException("该账号不存在"));
accountRepository.delete(accountToDelete); accountRepository.delete(accountToDelete);
} }
} }
public void updateAccountInfo(Long id, @Valid UpdateAccountCommand command) { public void updateAccountInfo(Long id, @Valid UpdateAccountCommand command) {
Assert.isTrue(Objects.equals(id, command.getId()), "参数错误: id 不匹配"); Assert.isTrue(Objects.equals(id, command.getId()), "参数错误: id 不匹配");
Account account = accountRepository.find(id); Account account = accountRepository.find(id)
AssertResult.nonNull(account, "该账号不存在"); .orElseThrow(() -> new DataNotExistException("该账号不存在"));
account.setAccountInfo(command.getNickname(), command.getAvatar(), command.getSex()); account.setAccountInfo(command.getNickname(), command.getAvatar(), command.getSex());
account.setUpdatedBy(adminAuthLogic.getLoginIdAsLong()); account.setUpdatedBy(adminAuthLogic.getLoginIdAsLong());
accountRepository.save(account); accountRepository.save(account);
@ -96,6 +97,13 @@ public class AccountManagementService {
return accountQueries.queryAccountOverviewPage(queryParams); return accountQueries.queryAccountOverviewPage(queryParams);
} }
/**
* {@link DataNotExistException}
*
* @param accountId id
* @return
* @throws DataNotExistException
*/
@Transactional(propagation = Propagation.SUPPORTS) @Transactional(propagation = Propagation.SUPPORTS)
public AccountDetails queryAccountDetails(@PathVariable("accountId") Long accountId) { public AccountDetails queryAccountDetails(@PathVariable("accountId") Long accountId) {
var accountDetails = accountQueries.queryAccountDetails(accountId); var accountDetails = accountQueries.queryAccountDetails(accountId);

View File

@ -44,14 +44,13 @@ public class AdminLoginService {
@ValidateDto @ValidateDto
public LoginInfoViewObject loginByPassword(LoginByPasswordCommand command) { public LoginInfoViewObject loginByPassword(LoginByPasswordCommand command) {
var principal = command.getPrincipal(); var principal = command.getPrincipal();
Account account = switch (command.getPrincipalType()) { Account account = (switch (command.getPrincipalType()) {
case USERNAME -> accountRepository.findByUsername(Username.of(principal)); case USERNAME -> accountRepository.findByUsername(Username.of(principal));
case EMAIL -> accountRepository.findByEmail(Email.of(principal)); case EMAIL -> accountRepository.findByEmail(Email.of(principal));
case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal)); case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal));
}; }).orElseThrow(AccountLoginException::accountNotExistException);
Assert.notNull(account, () -> AccountLoginException.accountNotExistException());
var isPasswordCorrect = account.checkPassword(command.getPassword()); var isPasswordCorrect = account.checkPassword(command.getPassword());
Assert.isTrue(isPasswordCorrect, () -> AccountLoginException.passwordErrorException()); Assert.isTrue(isPasswordCorrect, AccountLoginException::passwordErrorException);
adminAuthLogic.login(account.getId().orElseThrow(), command.isRememberMe()); adminAuthLogic.login(account.getId().orElseThrow(), command.isRememberMe());
var accountDetails = accountQueries.queryAccountDetails(account.getId().orElseThrow()); var accountDetails = accountQueries.queryAccountDetails(account.getId().orElseThrow());
@ -61,12 +60,11 @@ public class AdminLoginService {
@ValidateDto @ValidateDto
public LoginInfoViewObject loginByOtp(LoginByOtpCommand command) { public LoginInfoViewObject loginByOtp(LoginByOtpCommand command) {
var principal = command.getPrincipal(); var principal = command.getPrincipal();
Account account = switch (command.getPrincipalType()) { Account account = (switch (command.getPrincipalType()) {
case EMAIL -> accountRepository.findByEmail(Email.of(principal)); case EMAIL -> accountRepository.findByEmail(Email.of(principal));
case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal)); case MOBILE_PHONE -> accountRepository.findByMobilePhone(MobilePhone.of(principal));
default -> throw InvalidInputException.unsupportedPrincipalTypeException("输入邮箱地址或手机号"); default -> throw InvalidInputException.unsupportedPrincipalTypeException("输入邮箱地址或手机号");
}; }).orElseThrow(AccountLoginException::accountNotExistException);
Assert.notNull(account, () -> AccountLoginException.accountNotExistException());
mailAndSmsVerifyService.checkOtp(principal, command.getOtp()); mailAndSmsVerifyService.checkOtp(principal, command.getOtp());

View File

@ -10,6 +10,7 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.system.application.query.DictQueries; import xyz.zhouxy.plusone.system.application.query.DictQueries;
import xyz.zhouxy.plusone.system.application.query.params.DictQueryParams; import xyz.zhouxy.plusone.system.application.query.params.DictQueryParams;
import xyz.zhouxy.plusone.system.application.query.result.DictOverview; import xyz.zhouxy.plusone.system.application.query.result.DictOverview;
@ -45,21 +46,21 @@ public class DictManagementService {
public void deleteDicts(List<Long> ids) { public void deleteDicts(List<Long> ids) {
Dict dictToDelete; Dict dictToDelete;
for (Long id : ids) { for (Long id : ids) {
dictToDelete = dictRepository.find(id); dictToDelete = dictRepository.find(id).orElseThrow(DataNotExistException::new);
dictRepository.delete(dictToDelete); dictRepository.delete(dictToDelete);
} }
} }
public void updateDict(Long id, @Valid UpdateDictCommand command) { public void updateDict(Long id, @Valid UpdateDictCommand command) {
Assert.isTrue(Objects.equals(id, command.getId()), "id 不匹配"); Assert.isTrue(Objects.equals(id, command.getId()), "id 不匹配");
Dict dictToUpdate = dictRepository.find(command.getId()); Dict dictToUpdate = dictRepository.find(command.getId()).orElseThrow(DataNotExistException::new);
dictToUpdate.updateDict(command.getDictType(), command.getDictLabel(), command.getKeyLabelMap()); dictToUpdate.updateDict(command.getDictType(), command.getDictLabel(), command.getKeyLabelMap());
dictRepository.save(dictToUpdate); dictRepository.save(dictToUpdate);
} }
@Transactional(propagation = Propagation.SUPPORTS) @Transactional(propagation = Propagation.SUPPORTS)
public Dict findDictDetails(Long dictId) { public Dict findDictDetails(Long dictId) {
return dictRepository.find(dictId); return dictRepository.find(dictId).orElseThrow(DataNotExistException::new);
} }
@Transactional(propagation = Propagation.SUPPORTS) @Transactional(propagation = Propagation.SUPPORTS)

View File

@ -14,6 +14,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import xyz.zhouxy.plusone.domain.IWithOrderNumber; import xyz.zhouxy.plusone.domain.IWithOrderNumber;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.system.application.exception.UnsupportedMenuTypeException; import xyz.zhouxy.plusone.system.application.exception.UnsupportedMenuTypeException;
import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject; import xyz.zhouxy.plusone.system.application.query.result.MenuViewObject;
import xyz.zhouxy.plusone.system.application.service.command.CreateMenuCommand; import xyz.zhouxy.plusone.system.application.service.command.CreateMenuCommand;
@ -22,7 +23,6 @@ import xyz.zhouxy.plusone.system.domain.model.menu.Menu;
import xyz.zhouxy.plusone.system.domain.model.menu.MenuConstructor; import xyz.zhouxy.plusone.system.domain.model.menu.MenuConstructor;
import xyz.zhouxy.plusone.system.domain.model.menu.MenuRepository; import xyz.zhouxy.plusone.system.domain.model.menu.MenuRepository;
import xyz.zhouxy.plusone.system.domain.service.MenuService; import xyz.zhouxy.plusone.system.domain.service.MenuService;
import xyz.zhouxy.plusone.util.AssertResult;
/** /**
* *
@ -88,15 +88,16 @@ public class MenuManagementService {
// ==================== delete ==================== // ==================== delete ====================
public void deleteMenu(Long id) { public void deleteMenu(Long id) {
Menu menuToDelete = menuRepository.find(id); Menu menuToDelete = menuRepository.find(id)
AssertResult.nonNull(menuToDelete); .orElseThrow(DataNotExistException::new);
menuRepository.delete(menuToDelete); menuRepository.delete(menuToDelete);
} }
// ==================== update ==================== // ==================== update ====================
public void updateMenu(Long id, @Valid UpdateMenuCommand command) { public void updateMenu(Long id, @Valid UpdateMenuCommand command) {
Assert.isTrue(Objects.equals(id, command.getId()), "id 不匹配"); Assert.isTrue(Objects.equals(id, command.getId()), "id 不匹配");
Menu menuToUpdate = menuRepository.find(command.getId()); Menu menuToUpdate = menuRepository.find(command.getId())
.orElseThrow(DataNotExistException::new);
menuToUpdate.updateMenuInfo( menuToUpdate.updateMenuInfo(
command.getMenuType(), command.getMenuType(),
command.getParentId(), command.getParentId(),
@ -118,7 +119,7 @@ public class MenuManagementService {
@Transactional(propagation = Propagation.SUPPORTS) @Transactional(propagation = Propagation.SUPPORTS)
public MenuViewObject findById(Long id) { public MenuViewObject findById(Long id) {
var menu = menuRepository.find(id); var menu = menuRepository.find(id);
return MenuViewObject.of(menu); return MenuViewObject.of(menu.orElseThrow(DataNotExistException::new));
} }
@Transactional(propagation = Propagation.SUPPORTS) @Transactional(propagation = Propagation.SUPPORTS)

View File

@ -10,6 +10,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.system.application.query.RoleQueries; import xyz.zhouxy.plusone.system.application.query.RoleQueries;
import xyz.zhouxy.plusone.system.application.query.params.RoleQueryParams; import xyz.zhouxy.plusone.system.application.query.params.RoleQueryParams;
import xyz.zhouxy.plusone.system.application.query.result.RoleOverview; import xyz.zhouxy.plusone.system.application.query.result.RoleOverview;
@ -62,7 +63,7 @@ public class RoleManagementService {
public void updateRole(@Valid UpdateRoleCommand command) { public void updateRole(@Valid UpdateRoleCommand command) {
Long roleId = command.getId(); Long roleId = command.getId();
Role roleToUpdate = _roleRepository.find(roleId); Role roleToUpdate = _roleRepository.find(roleId).orElseThrow(DataNotExistException::new);
roleToUpdate.update( roleToUpdate.update(
command.getName(), command.getName(),
command.getIdentifier(), command.getIdentifier(),
@ -74,7 +75,7 @@ public class RoleManagementService {
} }
public void delete(Long id) { public void delete(Long id) {
Role role = _roleRepository.find(id); Role role = _roleRepository.find(id).orElseThrow(DataNotExistException::new);
_roleRepository.delete(role); _roleRepository.delete(role);
} }
@ -85,7 +86,7 @@ public class RoleManagementService {
@Transactional(propagation = Propagation.SUPPORTS) @Transactional(propagation = Propagation.SUPPORTS)
public Role findById(Long id) { public Role findById(Long id) {
return _roleRepository.find(id); return _roleRepository.find(id).orElseThrow(DataNotExistException::new);
} }
@Transactional(propagation = Propagation.SUPPORTS) @Transactional(propagation = Propagation.SUPPORTS)

View File

@ -27,7 +27,7 @@ public class AccountCreated extends DomainEvent {
public AccountCreated(Account account) { public AccountCreated(Account account) {
this.username = account.getUsername(); this.username = account.getUsername();
this.email = account.getEmail(); this.email = account.getEmail().orElse(null);
this.mobilePhone = account.getMobilePhone(); this.mobilePhone = account.getMobilePhone().orElse(null);
} }
} }

View File

@ -27,7 +27,7 @@ public class AccountLocked extends DomainEvent {
public AccountLocked(Account account) { public AccountLocked(Account account) {
this.username = account.getUsername(); this.username = account.getUsername();
this.email = account.getEmail(); this.email = account.getEmail().orElse(null);
this.mobilePhone = account.getMobilePhone(); this.mobilePhone = account.getMobilePhone().orElse(null);
} }
} }

View File

@ -27,8 +27,8 @@ public class AccountPasswordChanged extends DomainEvent {
public AccountPasswordChanged(Account account) { public AccountPasswordChanged(Account account) {
this.username = account.getUsername(); this.username = account.getUsername();
this.email = account.getEmail(); this.email = account.getEmail().orElse(null);
this.mobilePhone = account.getMobilePhone(); this.mobilePhone = account.getMobilePhone().orElse(null);
} }
} }

View File

@ -25,6 +25,6 @@ public class EmailChanged extends DomainEvent {
public EmailChanged(Account account) { public EmailChanged(Account account) {
this.username = account.getUsername(); this.username = account.getUsername();
this.email = account.getEmail(); this.email = account.getEmail().orElse(null);
} }
} }

View File

@ -25,6 +25,6 @@ public class MobilePhoneChanged extends DomainEvent {
public MobilePhoneChanged(Account account) { public MobilePhoneChanged(Account account) {
this.username = account.getUsername(); this.username = account.getUsername();
this.mobilePhone = account.getMobilePhone(); this.mobilePhone = account.getMobilePhone().orElse(null);
} }
} }

View File

@ -27,7 +27,7 @@ public class UsernameChanged extends DomainEvent {
public UsernameChanged(Account account) { public UsernameChanged(Account account) {
this.username = account.getUsername(); this.username = account.getUsername();
this.email = account.getEmail(); this.email = account.getEmail().orElse(null);
this.mobilePhone = account.getMobilePhone(); this.mobilePhone = account.getMobilePhone().orElse(null);
} }
} }

View File

@ -5,8 +5,6 @@ import java.util.HashSet;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString; import lombok.ToString;
import xyz.zhouxy.plusone.domain.AggregateRoot; import xyz.zhouxy.plusone.domain.AggregateRoot;
import xyz.zhouxy.plusone.domain.IWithVersion; import xyz.zhouxy.plusone.domain.IWithVersion;
@ -30,17 +28,17 @@ public class Account extends AggregateRoot<Long> implements IWithVersion {
// ===================== 字段 ==================== // ===================== 字段 ====================
private Long id; private Long id;
private @Getter Username username; private Username username;
private @Getter Email email; private Email email;
private @Getter MobilePhone mobilePhone; private MobilePhone mobilePhone;
private Password password; private Password password;
private @Getter AccountStatus status; private AccountStatus status;
private @Getter AccountInfo accountInfo; private AccountInfo accountInfo;
private Set<Long> roleRefs = new HashSet<>(); private Set<Long> roleRefs = new HashSet<>();
private @Getter Long createdBy; private Long createdBy;
private @Getter @Setter Long updatedBy; private Long updatedBy;
private @Getter long version; private long version;
public void setUsername(Username username) { public void setUsername(Username username) {
this.username = username; this.username = username;
@ -223,11 +221,48 @@ public class Account extends AggregateRoot<Long> implements IWithVersion {
return Optional.ofNullable(id); return Optional.ofNullable(id);
} }
public Set<Long> getRoleIds() { public Username getUsername() {
return Set.copyOf(this.roleRefs); return username;
}
public Optional<Email> getEmail() {
return Optional.ofNullable(email);
}
public Optional<MobilePhone> getMobilePhone() {
return Optional.ofNullable(mobilePhone);
} }
Password getPassword() { Password getPassword() {
return password; return password;
} }
public AccountStatus getStatus() {
return status;
}
public AccountInfo getAccountInfo() {
return accountInfo;
}
public Set<Long> getRoleIds() {
return Set.copyOf(roleRefs);
}
public Optional<Long> getCreatedBy() {
return Optional.ofNullable(createdBy);
}
public Optional<Long> getUpdatedBy() {
return Optional.ofNullable(updatedBy);
}
public void setUpdatedBy(long updatedBy) {
this.updatedBy = updatedBy;
}
@Override
public long getVersion() {
return this.version;
}
} }

View File

@ -3,6 +3,7 @@ package xyz.zhouxy.plusone.system.domain.model.account;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import lombok.Getter; import lombok.Getter;
import lombok.ToString; import lombok.ToString;
@ -13,13 +14,12 @@ import xyz.zhouxy.plusone.domain.IValueObject;
* *
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a> * @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/ */
@Getter
@ToString @ToString
public class AccountInfo implements IValueObject { public class AccountInfo implements IValueObject {
private final Nickname nickname; private final Nickname nickname;
private final URL avatar; private final URL avatar;
private final Sex sex; private final @Getter Sex sex;
private AccountInfo(Nickname nickname, URL avatar, Sex sex) { private AccountInfo(Nickname nickname, URL avatar, Sex sex) {
this.nickname = nickname; this.nickname = nickname;
@ -40,4 +40,12 @@ public class AccountInfo implements IValueObject {
} }
return new AccountInfo(Nickname.ofNullable(nickname), avatarURL, sex); return new AccountInfo(Nickname.ofNullable(nickname), avatarURL, sex);
} }
public Optional<Nickname> getNickname() {
return Optional.ofNullable(nickname);
}
public Optional<URL> getAvatar() {
return Optional.ofNullable(avatar);
}
} }

View File

@ -1,6 +1,7 @@
package xyz.zhouxy.plusone.system.domain.model.account; package xyz.zhouxy.plusone.system.domain.model.account;
import java.util.Collection; import java.util.Collection;
import java.util.Optional;
import xyz.zhouxy.plusone.domain.IRepository; import xyz.zhouxy.plusone.domain.IRepository;
@ -14,11 +15,11 @@ public interface AccountRepository extends IRepository<Account, Long> {
Collection<Account> findByRoleId(Long roleId); Collection<Account> findByRoleId(Long roleId);
Account findByEmail(Email email); Optional<Account> findByEmail(Email email);
Account findByMobilePhone(MobilePhone mobilePhone); Optional<Account> findByMobilePhone(MobilePhone mobilePhone);
Account findByUsername(Username username); Optional<Account> findByUsername(Username username);
boolean existsUsername(Username username); boolean existsUsername(Username username);

View File

@ -6,7 +6,6 @@ import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import lombok.ToString; import lombok.ToString;
import xyz.zhouxy.plusone.domain.AggregateRoot; import xyz.zhouxy.plusone.domain.AggregateRoot;
@ -115,7 +114,7 @@ public class Dict extends AggregateRoot<Long> implements IWithLabel, IWithVersio
} }
public Set<DictValue> getValues() { public Set<DictValue> getValues() {
return this.values.values().stream().collect(Collectors.toSet()); return Set.copyOf(this.values.values());
} }
@Override @Override

View File

@ -4,7 +4,7 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Collection; import java.util.Collection;
import java.util.Objects; import java.util.Optional;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -14,8 +14,8 @@ import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import static xyz.zhouxy.plusone.domain.ValidatableStringRecord.getValueOrNull;
import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport; import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport;
import xyz.zhouxy.plusone.util.AssertResult;
/** /**
* AccountRepository * AccountRepository
@ -41,11 +41,11 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
new MapSqlParameterSource() new MapSqlParameterSource()
.addValue("id", entity.getId().orElseThrow()) .addValue("id", entity.getId().orElseThrow())
.addValue("version", entity.getVersion())); .addValue("version", entity.getVersion()));
AssertResult.update(i, 1); assertUpdateOneRow(i);
} }
@Override @Override
protected final Account doFindById(@Nonnull Long id) { protected final Optional<Account> doFindById(@Nonnull Long id) {
return queryForObject(""" return queryForObject("""
SELECT SELECT
id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status, id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status,
@ -53,11 +53,11 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
FROM sys_account FROM sys_account
WHERE id = :id AND deleted = 0 WHERE id = :id AND deleted = 0
""", """,
new MapSqlParameterSource("id", id)); "id", id);
} }
@Override @Override
public Account findByEmail(Email email) { public Optional<Account> findByEmail(Email email) {
return queryForObject(""" return queryForObject("""
SELECT SELECT
id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status, id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status,
@ -65,11 +65,11 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
FROM sys_account FROM sys_account
WHERE email = :email AND deleted = 0 WHERE email = :email AND deleted = 0
""", """,
new MapSqlParameterSource("email", email.value())); "email", email.value());
} }
@Override @Override
public Account findByMobilePhone(MobilePhone mobilePhone) { public Optional<Account> findByMobilePhone(MobilePhone mobilePhone) {
return queryForObject(""" return queryForObject("""
SELECT SELECT
id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status, id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status,
@ -77,11 +77,11 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
FROM sys_account FROM sys_account
WHERE mobile_phone = :mobilePhone AND deleted = 0 WHERE mobile_phone = :mobilePhone AND deleted = 0
""", """,
new MapSqlParameterSource("mobilePhone", mobilePhone.value())); "mobilePhone", mobilePhone.value());
} }
@Override @Override
public Account findByUsername(Username username) { public Optional<Account> findByUsername(Username username) {
return queryForObject(""" return queryForObject("""
SELECT SELECT
id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status, id, email, mobile_phone, username, "password", salt, avatar, sex, nickname, status,
@ -89,31 +89,33 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
FROM sys_account FROM sys_account
WHERE username = :username AND deleted = 0 WHERE username = :username AND deleted = 0
""", """,
new MapSqlParameterSource("username", username.value())); "username", username.value());
} }
@Override @Override
public boolean exists(Long id) { public boolean exists(Long id) {
return queryExists("SELECT 1 FROM sys_account WHERE id = :id AND deleted = 0 LIMIT 1", return queryExists("SELECT EXISTS (SELECT 1 FROM sys_account WHERE id = :id AND deleted = 0 LIMIT 1)",
new MapSqlParameterSource("id", id)); "id", id);
} }
@Override @Override
public boolean existsUsername(Username username) { public boolean existsUsername(Username username) {
return queryExists("SELECT 1 FROM sys_account WHERE username = :username AND deleted = 0 LIMIT 1", return queryExists(
new MapSqlParameterSource("username", username.value())); "SELECT EXISTS (SELECT 1 FROM sys_account WHERE username = :username AND deleted = 0 LIMIT 1)",
"username", username.value());
} }
@Override @Override
public boolean existsEmail(Email email) { public boolean existsEmail(Email email) {
return queryExists("SELECT 1 FROM sys_account WHERE email = :email AND deleted = 0 LIMIT 1", return queryExists("SELECT EXISTS (SELECT 1 FROM sys_account WHERE email = :email AND deleted = 0 LIMIT 1)",
new MapSqlParameterSource("email", email.value())); "email", email.value());
} }
@Override @Override
public boolean existsMobilePhone(MobilePhone mobilePhone) { public boolean existsMobilePhone(MobilePhone mobilePhone) {
return queryExists("SELECT 1 FROM sys_account WHERE mobile_phone = :mobile_phone AND deleted = 0 LIMIT 1", return queryExists(
new MapSqlParameterSource("mobile_phone", mobilePhone.value())); "SELECT EXISTS (SELECT 1 FROM sys_account WHERE mobile_phone = :mobile_phone AND deleted = 0 LIMIT 1)",
"mobile_phone", mobilePhone.value());
} }
@Override @Override
@ -127,7 +129,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
LEFT JOIN sys_account_role ar ON a.id = ar.account_id LEFT JOIN sys_account_role ar ON a.id = ar.account_id
WHERE ar.role_id = :roleId AND a.deleted = 0 WHERE ar.role_id = :roleId AND a.deleted = 0
""", """,
new MapSqlParameterSource("roleId", roleId)); "roleId", roleId);
} }
@Override @Override
@ -142,9 +144,9 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
:createdBy, :createTime) :createdBy, :createTime)
""", """,
generateParamSource(id, entity)); generateParamSource(id, entity));
AssertResult.update(i, 1); assertUpdateOneRow(i);
this.accountRoleDAO.insertAccountRoleRefs(id, entity.getRoleIds()); this.accountRoleDAO.insertAccountRoleRefs(id, entity.getRoleIds());
return entity; return find(id).orElseThrow();
} }
@Override @Override
@ -166,9 +168,9 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
WHERE id = :id AND deleted = 0 AND "version" = :version WHERE id = :id AND deleted = 0 AND "version" = :version
""", """,
generateParamSource(entity)); generateParamSource(entity));
AssertResult.update(i, 1); assertUpdateOneRow(i);
this.accountRoleDAO.saveAccountRoleRefs(entity); this.accountRoleDAO.saveAccountRoleRefs(entity);
return entity; return find(entity.getId().orElseThrow()).orElseThrow();
} }
@Override @Override
@ -198,16 +200,14 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
AccountInfo accountInfo = entity.getAccountInfo(); AccountInfo accountInfo = entity.getAccountInfo();
return new MapSqlParameterSource() return new MapSqlParameterSource()
.addValue("id", id) .addValue("id", id)
.addValue("email", Objects.nonNull(entity.getEmail()) ? entity.getEmail().value() : null) .addValue("email", getValueOrNull(entity.getEmail()))
.addValue("mobilePhone", .addValue("mobilePhone", getValueOrNull(entity.getMobilePhone()))
Objects.nonNull(entity.getMobilePhone()) ? entity.getMobilePhone().value() : null)
.addValue("username", entity.getUsername().value()) .addValue("username", entity.getUsername().value())
.addValue("password", entity.getPassword().value()) .addValue("password", entity.getPassword().value())
.addValue("salt", entity.getPassword().getSalt()) .addValue("salt", entity.getPassword().getSalt())
.addValue("avatar", accountInfo.getAvatar().toString()) .addValue("avatar", accountInfo.getAvatar().toString())
.addValue("sex", accountInfo.getSex().getValue()) .addValue("sex", accountInfo.getSex().getValue())
.addValue("nickname", .addValue("nickname", getValueOrNull(accountInfo.getNickname()))
Objects.nonNull(accountInfo.getNickname()) ? accountInfo.getNickname().value() : null)
.addValue("status", entity.getStatus().getValue()) .addValue("status", entity.getStatus().getValue())
.addValue("createdBy", entity.getCreatedBy()) .addValue("createdBy", entity.getCreatedBy())
.addValue("createTime", now) .addValue("createTime", now)

View File

@ -1,47 +1,43 @@
package xyz.zhouxy.plusone.system.domain.model.account; package xyz.zhouxy.plusone.system.domain.model.account;
import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import xyz.zhouxy.plusone.util.AssertResult; import xyz.zhouxy.plusone.jdbc.PlusoneJdbcDaoSupport;
import xyz.zhouxy.plusone.util.NumberUtil;
class AccountRoleRefDAO { class AccountRoleRefDAO extends PlusoneJdbcDaoSupport {
private final NamedParameterJdbcTemplate jdbc;
AccountRoleRefDAO(NamedParameterJdbcTemplate jdbc) { AccountRoleRefDAO(@Nonnull NamedParameterJdbcTemplate jdbc) {
this.jdbc = jdbc; super(jdbc);
} }
Set<Long> selectRoleIdsByAccountId(Long accountId) { Set<Long> selectRoleIdsByAccountId(Long accountId) {
List<Long> roleRefs = this.jdbc.queryForList(""" return queryForStream("""
SELECT r.id FROM sys_role r RIGHT JOIN sys_account_role ar ON r.id = ar.role_id SELECT r.id FROM sys_role r RIGHT JOIN sys_account_role ar ON r.id = ar.role_id
WHERE r.deleted = 0 AND ar.account_id = :accountId; WHERE r.deleted = 0 AND ar.account_id = :accountId;
""", """,
new MapSqlParameterSource("accountId", accountId), "accountId", accountId,
Long.TYPE); Long.TYPE)
return new HashSet<>(roleRefs); .collect(Collectors.toSet());
} }
void clearAccountRoleRefs(Account entity) { void clearAccountRoleRefs(Account entity) {
var param = new MapSqlParameterSource("accountId", entity.getId().orElseThrow()); update("DELETE FROM sys_account_role WHERE account_id = :accountId", "accountId", entity.getId().orElseThrow());
this.jdbc.update("DELETE FROM sys_account_role WHERE account_id = :accountId", param);
} }
void insertAccountRoleRefs(Long accountId, Set<Long> roleRefs) { void insertAccountRoleRefs(Long accountId, Set<Long> roleRefs) {
String sql = "INSERT INTO sys_account_role (account_id, role_id) VALUES (:accountId, :roleId)"; int i = batchUpdate(
MapSqlParameterSource[] batchArgs = roleRefs "INSERT INTO sys_account_role (account_id, role_id) VALUES (:accountId, :roleId)",
.stream() roleRefs,
.map((Long roleId) -> new MapSqlParameterSource() (Long roleId) -> new MapSqlParameterSource()
.addValue("accountId", accountId) .addValue("accountId", accountId)
.addValue("roleId", roleId)) .addValue("roleId", roleId));
.toArray(MapSqlParameterSource[]::new); assertResultEquals(i, roleRefs.size());
int[] i = this.jdbc.batchUpdate(sql, batchArgs);
AssertResult.update(roleRefs.size(), NumberUtil.sum(i));
} }
void saveAccountRoleRefs(Account entity) { void saveAccountRoleRefs(Account entity) {

View File

@ -6,6 +6,7 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.Optional;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -15,6 +16,7 @@ import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport; import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport;
import xyz.zhouxy.plusone.util.AssertResult; import xyz.zhouxy.plusone.util.AssertResult;
@ -34,9 +36,9 @@ public class DictRepositoryImpl extends JdbcRepositorySupport<Dict, Long> implem
} }
@Override @Override
public Dict doFindById(@Nonnull Long id) { public Optional<Dict> doFindById(@Nonnull Long id) {
return queryForObject("SELECT id, dict_type, dict_label, \"version\" WHERE id = :id AND deleted = 0", return queryForObject("SELECT id, dict_type, dict_label, \"version\" WHERE id = :id AND deleted = 0",
new MapSqlParameterSource("id", id)); "id", id);
} }
@Override @Override
@ -47,9 +49,9 @@ public class DictRepositoryImpl extends JdbcRepositorySupport<Dict, Long> implem
VALUES (:dictType, :dictLabel, :createTime, :createdBy) VALUES (:dictType, :dictLabel, :createTime, :createdBy)
""", """,
generateParamSource(id, entity)); generateParamSource(id, entity));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
this.dictValueDAO.insertDictValues(id, entity); this.dictValueDAO.insertDictValues(id, entity);
return find(id); return find(id).orElseThrow(DataNotExistException::new);
} }
@Override @Override
@ -64,9 +66,9 @@ public class DictRepositoryImpl extends JdbcRepositorySupport<Dict, Long> implem
WHERE id = :id AND deleted = 0 AND "version" = :version WHERE id = :id AND deleted = 0 AND "version" = :version
""", """,
generateParamSource(entity)); generateParamSource(entity));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
this.dictValueDAO.updateDictValues(entity); this.dictValueDAO.updateDictValues(entity);
return find(entity.getId().orElseThrow()); return find(entity.getId().orElseThrow()).orElseThrow(DataNotExistException::new);
} }
@Override @Override
@ -76,12 +78,12 @@ public class DictRepositoryImpl extends JdbcRepositorySupport<Dict, Long> implem
WHERE id = :id AND deleted = 0 AND "version" = :version WHERE id = :id AND deleted = 0 AND "version" = :version
""", """,
generateParamSource(entity)); generateParamSource(entity));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
} }
@Override @Override
public boolean exists(Long id) { public boolean exists(Long id) {
return queryExists("SELECT 1 FROM sys_dict_type WHERE id = :id AND deleted = 0 LIMIT 1", return queryExists("SELECT EXISTS (SELECT 1 FROM sys_dict_type WHERE id = :id AND deleted = 0 LIMIT 1)",
new MapSqlParameterSource("id", id)); new MapSqlParameterSource("id", id));
} }

View File

@ -4,48 +4,45 @@ import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import xyz.zhouxy.plusone.util.AssertResult; import xyz.zhouxy.plusone.jdbc.PlusoneJdbcDaoSupport;
import xyz.zhouxy.plusone.util.NumberUtil;
class DictValueDAO { class DictValueDAO extends PlusoneJdbcDaoSupport {
private final NamedParameterJdbcTemplate jdbc;
DictValueDAO(NamedParameterJdbcTemplate jdbc) { DictValueDAO(@Nonnull NamedParameterJdbcTemplate jdbc) {
this.jdbc = jdbc; super(jdbc);
} }
void updateDictValues(Dict entity) { void updateDictValues(Dict entity) {
MapSqlParameterSource deleteParam = new MapSqlParameterSource("dictType", entity.getId().orElseThrow()); update("DELETE FROM sys_dict_value WHERE dict_type = :dictType",
this.jdbc.update("DELETE FROM sys_dict_value WHERE dict_type = :dictType", deleteParam); "dictType", entity.getId().orElseThrow());
int i = insertDictValues(entity.getId().orElseThrow(), entity); int i = insertDictValues(entity.getId().orElseThrow(), entity);
AssertResult.update(i, entity.count()); assertResultEquals(i, entity.count());
} }
int insertDictValues(Long dictId, Dict entity) { int insertDictValues(Long dictId, Dict entity) {
if (Objects.isNull(dictId) || Objects.isNull(entity) || CollectionUtils.isEmpty(entity.getValues())) { if (Objects.isNull(dictId) || Objects.isNull(entity) || CollectionUtils.isEmpty(entity.getValues())) {
return 0; return 0;
} }
int[] i = this.jdbc.batchUpdate( return batchUpdate(
"INSERT INTO sys_dict_value (dict_type, dict_key, label) VALUES (:dictType, :dictKey, :label)", "INSERT INTO sys_dict_value (dict_type, dict_key, label) VALUES (:dictType, :dictKey, :label)",
entity.getValues().stream() entity.getValues(),
.map(dictValue -> new MapSqlParameterSource() dictValue -> new MapSqlParameterSource()
.addValue("dictType", dictId) .addValue("dictType", dictId)
.addValue("dictKey", dictValue.getKey()) .addValue("dictKey", dictValue.getKey())
.addValue("label", dictValue.getLabel())) .addValue("label", dictValue.getLabel()));
.toArray(SqlParameterSource[]::new));
return NumberUtil.sum(i);
} }
Set<DictValue> selectDictValuesByDictId(long id) { Set<DictValue> selectDictValuesByDictId(long id) {
return this.jdbc.queryForStream(""" return queryForStream(
SELECT dict_key, label FROM sys_dict_value WHERE dict_type = :dictType "SELECT dict_key, label FROM sys_dict_value WHERE dict_type = :dictType",
""", new MapSqlParameterSource("dictType", id), "dictType", id,
(rs, rowNum) -> DictValue.of(rs.getInt("dict_key"), rs.getString("label"))) (rs, i) -> DictValue.of(rs.getInt("dict_key"), rs.getString("label")))
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }
} }

View File

@ -39,15 +39,14 @@ class ActionDAO extends JdbcEntityDaoSupport<Action, Long> {
.map(action -> action.getId().orElseThrow()) .map(action -> action.getId().orElseThrow())
.collect(Collectors.toSet()); .collect(Collectors.toSet());
if (!ids.isEmpty()) { if (!ids.isEmpty()) {
this.jdbc.update( update("UPDATE sys_action SET deleted = id WHERE resource = :resource AND id NOT IN (:ids) AND deleted = 0",
"UPDATE sys_action SET deleted = id WHERE resource = :resource AND id NOT IN (:ids) AND deleted = 0",
new MapSqlParameterSource() new MapSqlParameterSource()
.addValue("resource", menuId) .addValue("resource", menuId)
.addValue("ids", ids)); .addValue("ids", ids));
} }
// 更新存在的数据 // 更新存在的数据
this.jdbc.batchUpdate(""" batchUpdate("""
UPDATE sys_action UPDATE sys_action
SET resource = :resource, SET resource = :resource,
identifier = :identifier, identifier = :identifier,
@ -56,22 +55,18 @@ class ActionDAO extends JdbcEntityDaoSupport<Action, Long> {
updated_by = :updatedBy updated_by = :updatedBy
WHERE id = :id AND deleted = 0 WHERE id = :id AND deleted = 0
""", """,
actions.stream() actions.stream().filter(action -> action.getId().isPresent()),
.filter(action -> action.getId().isPresent()) action -> generateParamSource(menuId, action));
.map(action -> generateParamSource(menuId, action))
.toArray(MapSqlParameterSource[]::new));
// 插入新添加的数据 // 插入新添加的数据
this.jdbc.batchUpdate(""" batchUpdate("""
INSERT INTO sys_action INSERT INTO sys_action
(id, resource, identifier, "label", create_time, created_by) (id, resource, identifier, "label", create_time, created_by)
VALUES VALUES
(:id, :resource, :identifier, :label, :createTime, :createdBy) (:id, :resource, :identifier, :label, :createTime, :createdBy)
""", """,
actions.stream() actions.stream().filter(action -> action.getId().isEmpty()),
.filter(action -> action.getId().isEmpty()) action -> generateParamSource(menuId, IdUtil.getSnowflakeNextId(), action));
.map(action -> generateParamSource(menuId, IdUtil.getSnowflakeNextId(), action))
.toArray(MapSqlParameterSource[]::new));
} }
List<Action> selectActionsByMenuId(long menuId) { List<Action> selectActionsByMenuId(long menuId) {
@ -80,7 +75,8 @@ class ActionDAO extends JdbcEntityDaoSupport<Action, Long> {
FROM sys_action a FROM sys_action a
JOIN (SELECT id, resource FROM sys_menu WHERE id = :menuId AND deleted = 0) m ON a.resource = m.id JOIN (SELECT id, resource FROM sys_menu WHERE id = :menuId AND deleted = 0) m ON a.resource = m.id
WHERE a.deleted = 0 WHERE a.deleted = 0
""", new MapSqlParameterSource("menuId", menuId)); """,
"menuId", menuId);
} }
Collection<Action> selectActionsByIdIn(Collection<Long> actionIds) { Collection<Action> selectActionsByIdIn(Collection<Long> actionIds) {
@ -92,7 +88,8 @@ class ActionDAO extends JdbcEntityDaoSupport<Action, Long> {
FROM sys_action a FROM sys_action a
LEFT JOIN sys_menu m ON a.resource = m.id LEFT JOIN sys_menu m ON a.resource = m.id
WHERE a.id IN (:actionIds) AND a.deleted = 0 WHERE a.id IN (:actionIds) AND a.deleted = 0
""", new MapSqlParameterSource("actionIds", actionIds)); """,
"actionIds", actionIds);
} }
private SqlParameterSource generateParamSource(Long menuId, Action action) { private SqlParameterSource generateParamSource(Long menuId, Action action) {

View File

@ -8,6 +8,7 @@ import java.time.LocalDateTime;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -17,6 +18,7 @@ import org.springframework.stereotype.Repository;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import xyz.zhouxy.plusone.constant.EntityStatus; import xyz.zhouxy.plusone.constant.EntityStatus;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport; import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport;
import xyz.zhouxy.plusone.system.domain.model.menu.Menu.MenuType; import xyz.zhouxy.plusone.system.domain.model.menu.Menu.MenuType;
import xyz.zhouxy.plusone.util.AssertResult; import xyz.zhouxy.plusone.util.AssertResult;
@ -38,7 +40,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
} }
@Override @Override
protected final Menu doFindById(@Nonnull Long id) { protected final Optional<Menu> doFindById(@Nonnull Long id) {
return queryForObject(""" return queryForObject("""
SELECT SELECT
id, parent_id, "type", "name", "path", title, icon, hidden, order_number, status, remarks, id, parent_id, "type", "name", "path", title, icon, hidden, order_number, status, remarks,
@ -46,7 +48,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
FROM sys_menu FROM sys_menu
WHERE id = :id AND deleted = 0 WHERE id = :id AND deleted = 0
""", """,
new MapSqlParameterSource("id", id)); "id", id);
} }
@Override @Override
@ -62,9 +64,9 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
"""; """;
MapSqlParameterSource paramSource = generateParamSource(id, entity); MapSqlParameterSource paramSource = generateParamSource(id, entity);
int i = update(sql, paramSource); int i = update(sql, paramSource);
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
this.actionDAO.saveActions(id, entity.getActions()); this.actionDAO.saveActions(id, entity.getActions());
return entity; return find(id).orElseThrow(DataNotExistException::new);
} }
@Override @Override
@ -92,12 +94,12 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
// 更新菜单 // 更新菜单
int i = update(sql, generateParamSource(entity)); int i = update(sql, generateParamSource(entity));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
// 保存权限 // 保存权限
Long id = entity.getId().orElseThrow(); Long id = entity.getId().orElseThrow();
this.actionDAO.saveActions(id, entity.getActions()); this.actionDAO.saveActions(id, entity.getActions());
return entity; return find(entity.getId().orElseThrow()).orElseThrow(DataNotExistException::new);
} }
@Override @Override
@ -108,13 +110,13 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
""", """,
new MapSqlParameterSource("id", entity.getId().orElseThrow()) new MapSqlParameterSource("id", entity.getId().orElseThrow())
.addValue("version", entity.getVersion())); .addValue("version", entity.getVersion()));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
} }
@Override @Override
public boolean exists(Long id) { public boolean exists(Long id) {
return queryExists("SELECT 1 FROM sys_menu WHERE id = :id AND deleted = 0 LIMIT 1", return queryExists("SELECT EXISTS (SELECT 1 FROM sys_menu WHERE id = :id AND deleted = 0 LIMIT 1)",
new MapSqlParameterSource("id", id)); "id", id);
} }
@Override @Override
@ -129,7 +131,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
FROM sys_menu FROM sys_menu
WHERE id IN (:ids) AND deleted = 0 WHERE id IN (:ids) AND deleted = 0
""", """,
new MapSqlParameterSource("ids", ids)); "ids", ids);
} }
@Override @Override
@ -147,7 +149,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
LEFT JOIN sys_role_menu AS rm ON m.id = rm.menu_id LEFT JOIN sys_role_menu AS rm ON m.id = rm.menu_id
WHERE rm.role_id = :roleId AND r.deleted = 0 WHERE rm.role_id = :roleId AND r.deleted = 0
""", """,
new MapSqlParameterSource("roleId", roleId)); "roleId", roleId);
} }
@Override @Override

View File

@ -3,43 +3,38 @@ package xyz.zhouxy.plusone.system.domain.model.role;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import xyz.zhouxy.plusone.util.AssertResult; import xyz.zhouxy.plusone.jdbc.PlusoneJdbcDaoSupport;
import xyz.zhouxy.plusone.util.NumberUtil;
class RoleMenuRefDAO { class RoleMenuRefDAO extends PlusoneJdbcDaoSupport {
private final NamedParameterJdbcTemplate jdbc; public RoleMenuRefDAO(@Nonnull NamedParameterJdbcTemplate jdbc) {
super(jdbc);
public RoleMenuRefDAO(NamedParameterJdbcTemplate jdbc) {
this.jdbc = jdbc;
} }
Set<MenuRef> selectMenuRefsByRoleId(long roleId) { Set<MenuRef> selectMenuRefsByRoleId(long roleId) {
return this.jdbc.queryForList("SELECT menu_id FROM sys_role_menu WHERE role_id = :roleId", return queryForStream("SELECT menu_id FROM sys_role_menu WHERE role_id = :roleId",
new MapSqlParameterSource("roleId", roleId), "roleId", roleId,
Long.TYPE) Long.TYPE)
.stream()
.map(MenuRef::of) .map(MenuRef::of)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }
void clearRoleMenuRefs(Role entity) { void clearRoleMenuRefs(Role entity) {
MapSqlParameterSource param = new MapSqlParameterSource("roleId", entity.getId().orElseThrow()); update("DELETE FROM sys_role_menu WHERE role_id = :roleId", "roleId", entity.getId().orElseThrow());
this.jdbc.update("DELETE FROM sys_role_menu WHERE role_id = :roleId", param);
} }
void saveRoleMenuRefs(Long roleId, Role entity) { void saveRoleMenuRefs(Long roleId, Role entity) {
String sql = "INSERT INTO sys_role_menu(role_id, menu_id) VALUES (:roleId, :menuId)"; int i = batchUpdate(
MapSqlParameterSource[] batchArgs = entity.getMenus() "INSERT INTO sys_role_menu(role_id, menu_id) VALUES (:roleId, :menuId)",
.stream() entity.getMenus(),
.map(menuRef -> new MapSqlParameterSource() menuRef -> new MapSqlParameterSource()
.addValue("roleId", roleId) .addValue("roleId", roleId)
.addValue("menuId", menuRef.menuId())) .addValue("menuId", menuRef.menuId()));
.toArray(MapSqlParameterSource[]::new); assertResultEquals(i, entity.getMenus().size());
int[] i = this.jdbc.batchUpdate(sql, batchArgs);
AssertResult.update(entity.getMenus().size(), NumberUtil.sum(i));
} }
} }

View File

@ -3,43 +3,39 @@ package xyz.zhouxy.plusone.system.domain.model.role;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import xyz.zhouxy.plusone.util.AssertResult; import xyz.zhouxy.plusone.jdbc.PlusoneJdbcDaoSupport;
import xyz.zhouxy.plusone.util.NumberUtil;
class RolePermissionRefDAO { class RolePermissionRefDAO extends PlusoneJdbcDaoSupport {
private final NamedParameterJdbcTemplate jdbc;
public RolePermissionRefDAO(NamedParameterJdbcTemplate jdbc) { protected RolePermissionRefDAO(@Nonnull NamedParameterJdbcTemplate jdbc) {
this.jdbc = jdbc; super(jdbc);
} }
Set<ActionRef> selectPermissionRefsByRoleId(long roleId) { Set<ActionRef> selectPermissionRefsByRoleId(long roleId) {
return this.jdbc.queryForList("SELECT permission_id FROM sys_role_permission WHERE role_id = :roleId", return queryForStream("SELECT permission_id FROM sys_role_permission WHERE role_id = :roleId",
new MapSqlParameterSource("roleId", roleId), "roleId", roleId,
Long.TYPE) Long.TYPE)
.stream()
.map(ActionRef::of) .map(ActionRef::of)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }
void clearRolePermissionRefs(Role entity) { void clearRolePermissionRefs(Role entity) {
MapSqlParameterSource param = new MapSqlParameterSource("roleId", entity.getId().orElseThrow()); update("DELETE FROM sys_role_permission WHERE role_id = :roleId",
this.jdbc.update("DELETE FROM sys_role_permission WHERE role_id = :roleId", param); "roleId", entity.getId().orElseThrow());
} }
void saveRolePermissionRefs(Long roleId, Role entity) { void saveRolePermissionRefs(Long roleId, Role entity) {
String sql = "INSERT INTO sys_role_permission(role_id, permission_id) VALUES (:roleId, :permissionId)"; int i = batchUpdate(
SqlParameterSource[] batchArgs = entity.getPermissions() "INSERT INTO sys_role_permission(role_id, permission_id) VALUES (:roleId, :permissionId)",
.stream() entity.getPermissions(),
.map(menuRef -> new MapSqlParameterSource() actionRef -> new MapSqlParameterSource()
.addValue("roleId", roleId) .addValue("roleId", roleId)
.addValue("permissionId", menuRef.actionId())) .addValue("permissionId", actionRef.actionId()));
.toArray(MapSqlParameterSource[]::new); assertResultEquals(i, entity.getMenus().size());
int[] i = this.jdbc.batchUpdate(sql, batchArgs);
AssertResult.update(entity.getMenus().size(), NumberUtil.sum(i));
} }
} }

View File

@ -6,6 +6,7 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Collection; import java.util.Collection;
import java.util.Optional;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -15,6 +16,7 @@ import org.springframework.stereotype.Repository;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import xyz.zhouxy.plusone.constant.EntityStatus; import xyz.zhouxy.plusone.constant.EntityStatus;
import xyz.zhouxy.plusone.exception.DataNotExistException;
import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport; import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport;
import xyz.zhouxy.plusone.util.AssertResult; import xyz.zhouxy.plusone.util.AssertResult;
@ -36,12 +38,13 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
} }
@Override @Override
protected final Role doFindById(@Nonnull Long id) { protected final Optional<Role> doFindById(@Nonnull Long id) {
return queryForObject(""" return queryForObject("""
SELECT "id","name","identifier","status","remarks","version" SELECT "id","name","identifier","status","remarks","version"
FROM "sys_role" FROM "sys_role"
WHERE id = :id AND deleted = 0 WHERE id = :id AND deleted = 0
""", new MapSqlParameterSource("id", id)); """,
"id", id);
} }
@Override @Override
@ -52,13 +55,13 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
""", """,
new MapSqlParameterSource("id", entity.getId().orElseThrow()) new MapSqlParameterSource("id", entity.getId().orElseThrow())
.addValue("version", entity.getVersion())); .addValue("version", entity.getVersion()));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
} }
@Override @Override
public boolean exists(Long id) { public boolean exists(Long id) {
return queryExists("SELECT 1 FROM sys_role WHERE id = :id AND deleted = 0 LIMIT 1", return queryExists("SELECT EXISTS (SELECT 1 FROM sys_role WHERE id = :id AND deleted = 0 LIMIT 1)",
new MapSqlParameterSource("id", id)); "id", id);
} }
@Override @Override
@ -70,10 +73,10 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
VALUES VALUES
(:id, :name, :identifier, :status, :remarks, :createTime, :createdBy) (:id, :name, :identifier, :status, :remarks, :createTime, :createdBy)
""", generateParamSource(id, entity)); """, generateParamSource(id, entity));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
this.roleMenuRefDAO.saveRoleMenuRefs(id, entity); this.roleMenuRefDAO.saveRoleMenuRefs(id, entity);
this.rolePermissionRefDAO.saveRolePermissionRefs(id, entity); this.rolePermissionRefDAO.saveRolePermissionRefs(id, entity);
return entity; return find(id).orElseThrow(DataNotExistException::new);
} }
@Override @Override
@ -89,14 +92,14 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
"version" = "version" + 1 "version" = "version" + 1
WHERE id = :id AND deleted = 0 AND "version" = :version WHERE id = :id AND deleted = 0 AND "version" = :version
""", generateParamSource(entity)); """, generateParamSource(entity));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
Long id = entity.getId().orElseThrow(); Long id = entity.getId().orElseThrow();
this.roleMenuRefDAO.clearRoleMenuRefs(entity); this.roleMenuRefDAO.clearRoleMenuRefs(entity);
this.roleMenuRefDAO.saveRoleMenuRefs(id, entity); this.roleMenuRefDAO.saveRoleMenuRefs(id, entity);
this.rolePermissionRefDAO.clearRolePermissionRefs(entity); this.rolePermissionRefDAO.clearRolePermissionRefs(entity);
this.rolePermissionRefDAO.saveRolePermissionRefs(id, entity); this.rolePermissionRefDAO.saveRolePermissionRefs(id, entity);
return entity; return find(entity.getId().orElseThrow()).orElseThrow(DataNotExistException::new);
} }
@Override @Override
@ -106,7 +109,8 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
FROM sys_role AS r FROM sys_role AS r
LEFT JOIN sys_account_role AS ar ON r.id = ar.role_id LEFT JOIN sys_account_role AS ar ON r.id = ar.role_id
WHERE ar.account_id = :accountId AND r.deleted = 0 WHERE ar.account_id = :accountId AND r.deleted = 0
""", new MapSqlParameterSource("accountId", accountId)); """,
"accountId", accountId);
} }
@Override @Override

View File

@ -40,7 +40,7 @@
<commons-math3.version>3.6.1</commons-math3.version> <commons-math3.version>3.6.1</commons-math3.version>
<commons-pool2.version>2.11.1</commons-pool2.version> <commons-pool2.version>2.11.1</commons-pool2.version>
<tencentcloud-sdk.version>3.1.681</tencentcloud-sdk.version> <tencentcloud-sdk.version>3.1.681</tencentcloud-sdk.version>
<fastdfs.version>1.29-SNAPSHOT</fastdfs.version> <fastdfs.version>1.30-SNAPSHOT</fastdfs.version>
<okio.version>3.0.0</okio.version> <okio.version>3.0.0</okio.version>
<okhttp.version>4.10.0</okhttp.version> <okhttp.version>4.10.0</okhttp.version>
@ -197,6 +197,10 @@
<artifactId>jsr305</artifactId> <artifactId>jsr305</artifactId>
<version>3.0.2</version> <version>3.0.2</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
</dependencies> </dependencies>
<repositories> <repositories>