feat: 排行榜普通方法
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
package com.landaiqing.subject.infra.basic.config;
|
||||
|
||||
import com.landaiqing.subject.common.util.LoginUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.ibatis.executor.Executor;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.apache.ibatis.mapping.SqlCommandType;
|
||||
import org.apache.ibatis.plugin.*;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 填充createBy,createTime等公共字段的拦截器
|
||||
*
|
||||
* @author: landaiqing
|
||||
* @date: 2024/3/6
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
@Intercepts({@Signature(type = Executor.class, method = "update", args = {
|
||||
MappedStatement.class, Object.class
|
||||
})})
|
||||
public class MybatisInterceptor implements Interceptor {
|
||||
|
||||
@Override
|
||||
public Object intercept(Invocation invocation) throws Throwable {
|
||||
MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
|
||||
SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
|
||||
Object parameter = invocation.getArgs()[1];
|
||||
if (parameter == null) {
|
||||
return invocation.proceed();
|
||||
}
|
||||
//获取当前登录用户的id
|
||||
String loginId = LoginUtil.getLoginId();
|
||||
if (StringUtils.isBlank(loginId)) {
|
||||
return invocation.proceed();
|
||||
}
|
||||
if (SqlCommandType.INSERT == sqlCommandType || SqlCommandType.UPDATE == sqlCommandType) {
|
||||
replaceEntityProperty(parameter, loginId, sqlCommandType);
|
||||
}
|
||||
return invocation.proceed();
|
||||
}
|
||||
|
||||
private void replaceEntityProperty(Object parameter, String loginId, SqlCommandType sqlCommandType) {
|
||||
if (parameter instanceof Map) {
|
||||
replaceMap((Map) parameter, loginId, sqlCommandType);
|
||||
} else {
|
||||
replace(parameter, loginId, sqlCommandType);
|
||||
}
|
||||
}
|
||||
|
||||
private void replaceMap(Map parameter, String loginId, SqlCommandType sqlCommandType) {
|
||||
for (Object val : parameter.values()) {
|
||||
replace(val, loginId, sqlCommandType);
|
||||
}
|
||||
}
|
||||
|
||||
private void replace(Object parameter, String loginId, SqlCommandType sqlCommandType) {
|
||||
if (SqlCommandType.INSERT == sqlCommandType) {
|
||||
dealInsert(parameter, loginId);
|
||||
} else {
|
||||
dealUpdate(parameter, loginId);
|
||||
}
|
||||
}
|
||||
|
||||
private void dealUpdate(Object parameter, String loginId) {
|
||||
Field[] fields = getAllFields(parameter);
|
||||
for (Field field : fields) {
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
Object o = field.get(parameter);
|
||||
if (Objects.nonNull(o)) {
|
||||
field.setAccessible(false);
|
||||
continue;
|
||||
}
|
||||
if ("updateBy".equals(field.getName())) {
|
||||
field.set(parameter, loginId);
|
||||
field.setAccessible(false);
|
||||
} else if ("updateTime".equals(field.getName())) {
|
||||
field.set(parameter, new Date());
|
||||
field.setAccessible(false);
|
||||
} else {
|
||||
field.setAccessible(false);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("dealUpdate.error:{}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void dealInsert(Object parameter, String loginId) {
|
||||
Field[] fields = getAllFields(parameter);
|
||||
for (Field field : fields) {
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
Object o = field.get(parameter);
|
||||
if (Objects.nonNull(o)) {
|
||||
field.setAccessible(false);
|
||||
continue;
|
||||
}
|
||||
if ("isDeleted".equals(field.getName())) {
|
||||
field.set(parameter, 0);
|
||||
field.setAccessible(false);
|
||||
} else if ("createdBy".equals(field.getName())) {
|
||||
field.set(parameter, loginId);
|
||||
field.setAccessible(false);
|
||||
} else if ("createdTime".equals(field.getName())) {
|
||||
field.set(parameter, new Date());
|
||||
field.setAccessible(false);
|
||||
} else {
|
||||
field.setAccessible(false);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("dealInsert.error:{}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Field[] getAllFields(Object object) {
|
||||
Class<?> clazz = object.getClass();
|
||||
List<Field> fieldList = new ArrayList<>();
|
||||
while (clazz != null) {
|
||||
fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
|
||||
clazz = clazz.getSuperclass();
|
||||
}
|
||||
Field[] fields = new Field[fieldList.size()];
|
||||
fieldList.toArray(fields);
|
||||
return fields;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object plugin(Object target) {
|
||||
return Plugin.wrap(target, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProperties(Properties properties) {
|
||||
}
|
||||
|
||||
}
|
@@ -60,7 +60,10 @@ public class SubjectInfo implements Serializable {
|
||||
|
||||
private Integer isDeleted;
|
||||
|
||||
|
||||
/**
|
||||
* 题目数量
|
||||
*/
|
||||
private Integer subjectCount;
|
||||
|
||||
|
||||
}
|
||||
|
@@ -88,5 +88,7 @@ public interface SubjectInfoDao {
|
||||
@Param("labelId") Long labelId,
|
||||
@Param("start") int start,
|
||||
@Param("pageSize") Integer pageSize);
|
||||
|
||||
List<SubjectInfo> getContributeCount();
|
||||
}
|
||||
|
||||
|
@@ -48,4 +48,6 @@ public interface SubjectInfoService {
|
||||
int countByCondition(SubjectInfo subjectInfo, Long labelId, Long categoryId);
|
||||
|
||||
List<SubjectInfo> queryPage(SubjectInfo subjectInfo, Long categoryId, Long labelId, int start, Integer pageSize);
|
||||
|
||||
List<SubjectInfo> getContributeCount();
|
||||
}
|
||||
|
@@ -76,4 +76,9 @@ public class SubjectInfoServiceImpl implements SubjectInfoService {
|
||||
public List<SubjectInfo> queryPage(SubjectInfo subjectInfo, Long categoryId, Long labelId, int start, Integer pageSize) {
|
||||
return this.subjectInfoDao.queryPage(subjectInfo,categoryId,labelId,start,pageSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SubjectInfo> getContributeCount() {
|
||||
return this.subjectInfoDao.getContributeCount();
|
||||
}
|
||||
}
|
||||
|
@@ -16,4 +16,6 @@ public class UserInfo {
|
||||
private String userName;
|
||||
|
||||
private String nickName;
|
||||
|
||||
private String avatar;
|
||||
}
|
||||
|
@@ -34,6 +34,7 @@ public class UserRpc {
|
||||
AuthUserDTO data = result.getData();
|
||||
userInfo.setUserName(data.getUserName());
|
||||
userInfo.setNickName(data.getNickName());
|
||||
userInfo.setAvatar(data.getAvatar());
|
||||
return userInfo;
|
||||
}
|
||||
}
|
||||
|
@@ -168,6 +168,15 @@
|
||||
</if>
|
||||
limit #{start},#{pageSize}
|
||||
</select>
|
||||
<select id="getContributeCount" resultType="com.landaiqing.subject.infra.basic.entity.SubjectInfo">
|
||||
select count(1) as subjectCount,
|
||||
created_by as createdBy
|
||||
from subject_info
|
||||
where is_deleted = 0
|
||||
and created_by is not null
|
||||
group by created_by
|
||||
limit 0,5
|
||||
</select>
|
||||
|
||||
<!--新增所有列-->
|
||||
<insert id="insert" keyProperty="id" useGeneratedKeys="true">
|
||||
|
Reference in New Issue
Block a user