feat: 排行榜普通方法

This commit is contained in:
2024-03-06 20:39:28 +08:00
parent 0c2026eb80
commit 2a1d171b5f
18 changed files with 254 additions and 17 deletions

View File

@@ -14,7 +14,10 @@ import com.landaiqing.subject.infra.basic.entity.SubjectInfoEs;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@@ -145,4 +148,25 @@ public class SubjectController {
return Result.fail("全文检索失败!");
}
}
/**
* @description: 获取题目贡献榜
* @param: []
* @return: com.landaiqing.subject.common.entity.Result<java.util.List < com.landaiqing.subject.application.dto.SubjectInfoDTO>>
* @author landaiqing
* @date: 2024/3/6 20:16
*/
@PostMapping("/getContributeList")
public Result<List<SubjectInfoDTO>> getContributeList() {
try {
List<SubjectInfoBO> boList = subjectInfoDomainService.getContributeList();
List<SubjectInfoDTO> dtoList = SubjectInfoDTOConverter.INSTANCE.convertBOToDTOList(boList);
return Result.ok(dtoList);
} catch (Exception e) {
log.error("SubjectController.getContributeList.error:{}", e.getMessage(), e);
return Result.fail("获取贡献榜失败!");
}
}
}

View File

@@ -1,8 +1,6 @@
package com.landaiqing.subject.application.convert;
import com.landaiqing.subject.application.dto.SubjectCategoryDTO;
import com.landaiqing.subject.application.dto.SubjectInfoDTO;
import com.landaiqing.subject.domain.entity.SubjectCategoryBO;
import com.landaiqing.subject.domain.entity.SubjectInfoBO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@@ -21,6 +19,7 @@ public interface SubjectInfoDTOConverter {
SubjectInfoBO convertDtoToBO(SubjectInfoDTO subjectInfoDTO);
SubjectInfoDTO convertBOToDTO(SubjectInfoBO subjectInfoBO);
List<SubjectInfoDTO> convertBOToDTOList(List<SubjectInfoBO> subjectInfoBO);
}

View File

@@ -4,7 +4,6 @@ import com.landaiqing.subject.common.entity.PageInfo;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
@@ -74,6 +73,18 @@ public class SubjectInfoDTO extends PageInfo implements Serializable {
private List<String> labelName;
private String keyWord;
/**
* 创建人昵称
*/
private String creatUser;
/**
* 创建人头像
*/
private String createUserAvatar;
/**
* 题目数量
*/
private Integer subjectCount;
}

View File

@@ -4,7 +4,6 @@ import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

View File

@@ -1,13 +1,12 @@
package com.landaiqing.subject.application.interceptor;
import com.landaiqing.subject.application.context.LoginContextHolder;
import com.landaiqing.subject.common.context.LoginContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
/**
* @Classname LoginInterceptor
@@ -22,7 +21,9 @@ public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String loginId = request.getHeader("loginId");
LoginContextHolder.set("loginId",loginId);
if (StringUtils.isNotBlank(loginId)) {
LoginContextHolder.set("loginId", loginId);
}
return true;
}

View File

@@ -1,10 +1,8 @@
package com.landaiqing.subject.application.context;
package com.landaiqing.subject.common.context;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* @Classname LoginContextHolder

View File

@@ -1,6 +1,6 @@
package com.landaiqing.subject.application.util;
package com.landaiqing.subject.common.util;
import com.landaiqing.subject.application.context.LoginContextHolder;
import com.landaiqing.subject.common.context.LoginContextHolder;
/**
* @Classname LoginUtil

View File

@@ -1,7 +1,6 @@
package com.landaiqing.subject.domain.entity;
import com.landaiqing.subject.common.entity.PageInfo;
import com.landaiqing.subject.common.entity.PageResult;
import lombok.Data;
import java.io.Serializable;
@@ -76,6 +75,19 @@ public class SubjectInfoBO extends PageInfo implements Serializable {
private String keyWord;
/**
* 创建人昵称
*/
private String creatUser;
/**
* 创建人头像
*/
private String createUserAvatar;
/**
* 题目数量
*/
private Integer subjectCount;
}

View File

@@ -2,7 +2,6 @@ package com.landaiqing.subject.domain.service;
import com.landaiqing.subject.common.entity.PageResult;
import com.landaiqing.subject.domain.entity.SubjectInfoBO;
import com.landaiqing.subject.domain.entity.SubjectLabelBO;
import com.landaiqing.subject.infra.basic.entity.SubjectInfoEs;
import java.util.List;
@@ -43,4 +42,6 @@ public interface SubjectInfoDomainService {
* @date: 2024/3/6 17:06
*/
PageResult<SubjectInfoEs> getSubjectPageBySearch(SubjectInfoBO subjectInfoBO);
List<SubjectInfoBO> getContributeList();
}

View File

@@ -18,11 +18,15 @@ import com.landaiqing.subject.infra.basic.service.SubjectEsService;
import com.landaiqing.subject.infra.basic.service.SubjectInfoService;
import com.landaiqing.subject.infra.basic.service.SubjectLabelService;
import com.landaiqing.subject.infra.basic.service.SubjectMappingService;
import com.landaiqing.subject.infra.entity.UserInfo;
import com.landaiqing.subject.infra.rpc.UserRpc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
@@ -44,6 +48,9 @@ public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {
@Resource
private SubjectEsService subjectEsService;
@Resource
private UserRpc userRpc;
/**
* @description: 新增标签
@@ -147,5 +154,23 @@ public class SubjectInfoDomainServiceImpl implements SubjectInfoDomainService {
}
@Override
public List<SubjectInfoBO> getContributeList() {
List<SubjectInfo> subjectInfoList= subjectInfoService.getContributeCount();
if(CollectionUtils.isEmpty(subjectInfoList)){
return Collections.emptyList();
}
List<SubjectInfoBO> boList=new LinkedList<>();
subjectInfoList.forEach(subjectInfo -> {
SubjectInfoBO subjectInfoBO = new SubjectInfoBO();
subjectInfoBO.setSubjectCount(subjectInfo.getSubjectCount());
UserInfo userInfo = userRpc.getUserInfo(subjectInfo.getCreatedBy());
subjectInfoBO.setCreatUser(userInfo.getNickName());
subjectInfoBO.setCreateUserAvatar(userInfo.getAvatar());
boList.add(subjectInfoBO);
});
return boList;
}
}

View File

@@ -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) {
}
}

View File

@@ -60,7 +60,10 @@ public class SubjectInfo implements Serializable {
private Integer isDeleted;
/**
* 题目数量
*/
private Integer subjectCount;
}

View File

@@ -88,5 +88,7 @@ public interface SubjectInfoDao {
@Param("labelId") Long labelId,
@Param("start") int start,
@Param("pageSize") Integer pageSize);
List<SubjectInfo> getContributeCount();
}

View File

@@ -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();
}

View File

@@ -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();
}
}

View File

@@ -16,4 +16,6 @@ public class UserInfo {
private String userName;
private String nickName;
private String avatar;
}

View File

@@ -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;
}
}

View File

@@ -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">