Merge remote-tracking branch 'origin/master'

This commit is contained in:
Tankaikai 2025-02-20 18:35:21 +08:00
commit 3dd6e2e189
6 changed files with 168 additions and 28 deletions

View File

@ -0,0 +1,45 @@
package com.czg.config;
import com.mybatisflex.core.dialect.impl.CommonsDialectImpl;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.exception.locale.LocalizedFormats;
import com.mybatisflex.core.query.CPI;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryTable;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.StringUtil;
import org.springframework.stereotype.Component;
import java.util.List;
import static com.mybatisflex.core.constant.SqlConsts.ASTERISK;
import static com.mybatisflex.core.constant.SqlConsts.WHERE;
/**
* 自定义sql解析器 备用
*/
//@Component
public class MyCommonsDialectImpl extends CommonsDialectImpl {
@Override
public String wrap(String keyword) {
return ASTERISK.equals(keyword) ? keyword : keywordWrap.wrap(keyword);
}
protected void buildWhereSql(StringBuilder sqlBuilder, QueryWrapper queryWrapper, List<QueryTable> queryTables, boolean allowNoCondition) {
QueryCondition whereQueryCondition = CPI.getWhereQueryCondition(queryWrapper);
if (whereQueryCondition != null) {
String whereSql = whereQueryCondition.toSql(queryTables, this);
if (StringUtil.hasText(whereSql)) {
sqlBuilder.append(WHERE).append(whereSql);
} else if (!allowNoCondition) {
throw FlexExceptions.wrap(LocalizedFormats.UPDATE_OR_DELETE_NOT_ALLOW);
}
} else {
// whereQueryCondition == null
if (!allowNoCondition) {
throw FlexExceptions.wrap(LocalizedFormats.UPDATE_OR_DELETE_NOT_ALLOW);
}
}
}
}

View File

@ -1,18 +0,0 @@
package com.czg.config;
import com.mybatisflex.core.mybatis.FlexConfiguration;
import com.mybatisflex.spring.boot.ConfigurationCustomizer;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.context.annotation.Configuration;
/**
* @author Administrator
*/
@Configuration
public class MyConfigurationCustomizer implements ConfigurationCustomizer {
@Override
public void customize(FlexConfiguration configuration) {
configuration.setLogImpl(StdOutImpl.class);
}
}

View File

@ -0,0 +1,36 @@
package com.czg.config;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.dialect.DbType;
import com.mybatisflex.core.dialect.DialectFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
/**
* 自定义sql方言
*
* @author Administrator
*/
@Configuration
@Slf4j(topic = "mybatis-flex-sql")
public class MybatisFlexConfig {
public MybatisFlexConfig() {
//开启审计功能
AuditManager.setAuditEnable(true);
//设置 SQL 审计收集器
AuditManager.setMessageCollector(auditMessage ->
log.info("{},{}ms", auditMessage.getFullSql()
, auditMessage.getElapsedTime())
);
}
// @Resource
private MyCommonsDialectImpl myCommonsDialect;
// @PostConstruct
public void init() {
DialectFactory.registerDialect(DbType.MYSQL, myCommonsDialect);
}
}

View File

@ -1,6 +1,7 @@
package com.czg.utils;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.constant.SqlConnector;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.query.*;
import com.mybatisflex.core.table.TableInfo;
@ -10,12 +11,14 @@ import com.mybatisflex.core.util.LambdaGetter;
import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.util.function.Consumer;
import static com.mybatisflex.core.util.LambdaUtil.getFieldName;
/**
* 关联查询的queryWrapper
*
* @author Administrator
*/
public class JoinQueryWrapper extends QueryWrapper {
@ -55,6 +58,59 @@ public class JoinQueryWrapper extends QueryWrapper {
return tableInfo.getTableName() + "." + StrUtil.toUnderlineCase(getFieldName(column));
}
public static <T> QueryColumn column(LambdaGetter<T> getter) {
SerializedLambda lambda = getSerializedLambda(getter);
Class<?> entityClass = getImplClass0(lambda);
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entityClass);
String fieldName = getFieldName(getter);
QueryColumn columnByProperty = tableInfo.getQueryColumnByProperty(fieldName);
columnByProperty.setName(tableInfo.getTableName() + "." + StrUtil.toUnderlineCase(fieldName));
return columnByProperty;
}
@Override
public JoinQueryWrapper and(Consumer<QueryWrapper> consumer, boolean condition) {
if (!condition) {
return this;
}
JoinQueryWrapper newWrapper = new JoinQueryWrapper();
consumer.accept(newWrapper);
QueryCondition whereQueryCondition = newWrapper.whereQueryCondition;
if (whereQueryCondition != null) {
and(new Brackets(whereQueryCondition));
}
return this;
}
@Override
public JoinQueryWrapper or(Consumer<QueryWrapper> consumer, boolean condition) {
if (!condition) {
return this;
}
JoinQueryWrapper newWrapper = new JoinQueryWrapper();
consumer.accept(newWrapper);
QueryCondition whereQueryCondition = newWrapper.whereQueryCondition;
if (whereQueryCondition != null) {
or(new Brackets(whereQueryCondition));
}
return this;
}
@Override
public JoinQueryWrapper or(Consumer<QueryWrapper> consumer) {
return or(consumer, true);
}
@Override
public JoinQueryWrapper and(Consumer<QueryWrapper> consumer) {
return and(consumer, true);
}
@Override
public QueryWrapper and(QueryCondition queryCondition) {
return addWhereQueryCondition(queryCondition, SqlConnector.AND);
}
@Override
public <T> JoinQueryWrapper eq(LambdaGetter<T> column, Object value) {
eq(getColum(column), value);
@ -90,4 +146,12 @@ public class JoinQueryWrapper extends QueryWrapper {
orderBy(getColum(column), asc);
return this;
}
@Override
public <T> JoinQueryWrapper like(LambdaGetter<T> column, Object value) {
like(getColum(column), value);
return this;
}
}

View File

@ -18,6 +18,7 @@ import com.czg.sa.StpKit;
import com.czg.service.RedisService;
import com.czg.service.account.mapper.ShopUserMapper;
import com.czg.utils.AssertUtil;
import com.czg.utils.JoinQueryWrapper;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
@ -56,9 +57,15 @@ public class ShopUserServiceImpl extends ServiceImpl<ShopUserMapper, ShopUser> i
@Override
public Page<ShopUser> getPage(String key, Integer isVip) {
QueryWrapper queryWrapper = new QueryWrapper().eq(ShopUser::getShopId, StpKit.USER.getShopId());
JoinQueryWrapper queryWrapper = new JoinQueryWrapper().eq(ShopUser::getShopId, StpKit.USER.getShopId());
if (StrUtil.isNotBlank(key)) {
queryWrapper.and(column(UserInfo::getNickName).like(key).or(column(UserInfo::getPhone).like(key)));
queryWrapper.and(q -> {
q.like(UserInfo::getNickName, key).or(r -> {
r.like(UserInfo::getPhone, key);
});
});
// queryWrapper.and(JoinQueryWrapper.column(UserInfo::getNickName).like(key).or(JoinQueryWrapper.column(UserInfo::getPhone).like(key)));
}
if (isVip != null) {

View File

@ -37,18 +37,24 @@
where a.user_id = #{userInfoId}
</select>
<select id="selectAssetsSummary" resultType="com.czg.account.dto.user.userinfo.UserInfoAssetsSummaryDTO">
select sum(IFNULL(b.amount, 0)) as amount, sum(IFNULL(b.account_points, 0)) as points, sum(IFNULL(c.over_num, 0)) as couponNum
select sum(IFNULL(b.amount, 0)) as amount,
sum(IFNULL(b.account_points, 0)) as points,
sum(IFNULL(c.id, 0)) as couponNum
from tb_user_info as a
left join tb_shop_user as b on a.id = b.user_id
left join tb_shop_activate_in_record as c on c.shop_id = b.shop_id
where a.id=#{userId}
left join tb_shop_activate_coupon_record as c
on c.shop_id = b.shop_id and c.vip_user_id = b.id and c.`status` = 'used'
where a.id = #{userId}
</select>
<select id="selectPageByKeyAndIsVip" resultType="com.czg.account.entity.ShopUser">
select tb_shop_user.* from tb_user_info
left join tb_shop_user on tb_user_info.id=tb_shop_user.user_id ${qwSql} limit ${pageOffset}, ${pageSize}
select tb_shop_user.*
from tb_user_info
left join tb_shop_user on tb_user_info.id = tb_shop_user.user_id ${qwSql}
limit ${pageOffset}, ${pageSize}
</select>
<select id="selectPageByKeyAndIsVip_COUNT" resultType="java.lang.Long">
select count(1) from tb_user_info
left join tb_shop_user on tb_user_info.id=tb_shop_user.user_id ${qwSql}
select count(1)
from tb_user_info
left join tb_shop_user on tb_user_info.id = tb_shop_user.user_id ${qwSql}
</select>
</mapper>