Skip to content

Commit

Permalink
[Feature] 新增问卷列表查询接口及相关单元测试 (#377)
Browse files Browse the repository at this point in the history
* feat: 1,添加修改问卷,删除问卷接口,修改创建问卷 部分逻辑
2,添加修改问卷,删除问卷接口单元测试

* feat: 1,添加修改问卷,删除问卷接口,修改创建问卷 部分逻辑
2,添加修改问卷,删除问卷接口单元测试

* style:代码格式化调整

* style:代码格式化调整

* feat:新增发布问卷接口,同时修改了创建问卷接口

* feat:新增问卷列表查询接口及相关单元测试

* feat:修改变量名

---------

Co-authored-by: kui <[email protected]>
  • Loading branch information
likui63 and kui authored Aug 5, 2024
1 parent c2ccdca commit 92f11f8
Show file tree
Hide file tree
Showing 13 changed files with 369 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.xiaojusurvey.engine.common.constants;

/**
* Survey 常量
*
* @author [email protected]
* @Date 2024/8/3 12:24
*/
public class SurveyConstant {

public static final String OPT_OR = "$or";
public static final String OPT_NE = "$ne";
public static final String OPT_REGEX = "$regex";
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,14 @@ public class SurveyMeta extends BaseEntity {
*/
private String owner;

/**
* 所有者ID
*/
private String ownerId;

/**
* 空间
*/
private String workspaceId;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta;
import com.xiaojusurvey.engine.core.reslut.IdResult;
import com.xiaojusurvey.engine.core.survey.param.SurveyListParam;
import com.xiaojusurvey.engine.core.survey.param.SurveyMetaUpdateParam;
import com.xiaojusurvey.engine.core.survey.vo.SurveyListVO;

/**
* @Author: LYF
Expand All @@ -26,6 +28,12 @@ public interface SurveyService {
*/
boolean publishSurvey(String surveyId);

/**
* 获取问卷列表
* @param param
* @return
*/
SurveyListVO getSurveyList(SurveyListParam param);



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.xiaojusurvey.engine.core.survey.dto;

import lombok.Data;

import java.io.Serializable;

/**
* 查询基础条件
*
* @author [email protected]
* @Date 2024/8/1 21:14
*/
@Data
public class BaseQuery implements Serializable {

private static final long serialVersionUID = 3378327499951251208L;
private int pageSize = 10;
private int curPage = 1;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.xiaojusurvey.engine.core.survey.dto;

import lombok.Data;

/**
* TODO
*
* @author [email protected]
* @Date 2024/8/1 20:59
*/
@Data
public class FilterItem {
/**
* 比较符
*/
private String comparator;

private FilterCondition condition;


@Data
public static class FilterCondition {
/**
* 字段名
*/
private String field;
/**
* 比较符
*/
private String comparator;
/**
* 筛选条件
*/
private String value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.xiaojusurvey.engine.core.survey.dto;

import lombok.Data;

/**
* 排序条件
*
* @author [email protected]
* @Date 2024/8/1 20:59
*/
@Data
public class OrderItem {
/**
* 字段名
*/
private String field;
/**
* 升序降序 1-升序,-1-降序
*/
private int value = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;

import com.xiaojusurvey.engine.common.constants.RespErrorCode;
import com.xiaojusurvey.engine.common.constants.SurveyConstant;
import com.xiaojusurvey.engine.common.entity.Status;
import com.xiaojusurvey.engine.common.entity.survey.SurveyConf;
import com.xiaojusurvey.engine.common.entity.survey.SurveyHistory;
Expand All @@ -17,20 +18,29 @@
import com.xiaojusurvey.engine.core.survey.SurveyHistoryService;
import com.xiaojusurvey.engine.core.survey.SurveyPublishService;
import com.xiaojusurvey.engine.core.survey.SurveyService;
import com.xiaojusurvey.engine.core.survey.dto.FilterItem;
import com.xiaojusurvey.engine.core.survey.param.SurveyListParam;
import com.xiaojusurvey.engine.core.survey.param.SurveyMetaUpdateParam;
import com.xiaojusurvey.engine.core.survey.vo.SurveyListVO;
import com.xiaojusurvey.engine.core.survey.vo.SurveyVO;
import com.xiaojusurvey.engine.core.util.WebUtils;
import com.xiaojusurvey.engine.repository.MongoRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @Author: LYF
Expand Down Expand Up @@ -175,5 +185,75 @@ public boolean publishSurvey(String surveyId) {
return true;
}

@Override
public SurveyListVO getSurveyList(SurveyListParam param) {
log.info("[getSurveyList]获取问卷列表 param={}", param);
Query query = buildQuery(param);
List<SurveyMeta> list = mongoRepository.page(query, param.getCurPage() - 1, param.getPageSize(), SurveyMeta.class);
Long count = mongoRepository.count(query, SurveyMeta.class);
SurveyListVO vo = new SurveyListVO();
vo.setCount(count);
List<SurveyVO> data = new ArrayList<>();
if (!CollectionUtils.isEmpty(list)) {
data = list.stream().map(r -> {
SurveyVO surveyVO = new SurveyVO();
BeanUtils.copyProperties(r, surveyVO);
return surveyVO;
}).collect(Collectors.toList());
}
vo.setData(data);
return vo;
}


private Query buildQuery(SurveyListParam param) {
Query query = new Query();
if (param.getOrder() != null) {
List<Sort.Order> orders = new ArrayList<>();
Arrays.stream(param.getOrder()).forEach(r -> {
if (r.getValue() == 1) {
orders.add(new Sort.Order(Sort.Direction.ASC, r.getField()));
} else {
orders.add(new Sort.Order(Sort.Direction.DESC, r.getField()));
}
});
query.with(Sort.by(orders));
}
Criteria criteria = new Criteria();
List<Criteria> listAnd = new ArrayList();
List<Criteria> listOr = new ArrayList();
if (StringUtils.hasLength(param.getWorkspaceId())) {
listAnd.add(Criteria.where("workspaceId").is(param.getWorkspaceId()));
}
if (StringUtils.hasLength(param.getUsername())) {
listAnd.add(Criteria.where("owner").is(param.getUsername()));
}
if (StringUtils.hasLength(param.getUserId())) {
listAnd.add(Criteria.where("ownerId").is(param.getUserId()));
}
if (param.getFilter() != null) {
Arrays.stream(param.getFilter()).forEach(r -> {
FilterItem.FilterCondition ff = r.getCondition();
Criteria crt = null;
if (SurveyConstant.OPT_NE.equals(ff.getComparator())) {
crt = Criteria.where(ff.getField()).ne(ff.getValue());
} else if (SurveyConstant.OPT_REGEX.equals(ff.getComparator())) {
crt = Criteria.where(ff.getField()).regex(ff.getValue());
}
if (StringUtils.hasLength(r.getComparator()) && SurveyConstant.OPT_OR.equals(r.getComparator())) {
listOr.add(crt);
} else {
listOr.add(crt);
}
});
}
if (!listAnd.isEmpty()) {
criteria.andOperator(listAnd);
}
if (!listOr.isEmpty()) {
criteria = criteria.orOperator(listOr);
}
query.addCriteria(criteria);
return query;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.xiaojusurvey.engine.core.survey.param;

import com.xiaojusurvey.engine.core.survey.dto.BaseQuery;
import com.xiaojusurvey.engine.core.survey.dto.FilterItem;
import com.xiaojusurvey.engine.core.survey.dto.OrderItem;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import java.io.Serializable;

/**
* 问卷查询
*
* @author [email protected]
* @Date 2024/8/1 21:05
*/
@Data
public class SurveyListParam extends BaseQuery implements Serializable {

private static final long serialVersionUID = -6294820920444326423L;
private FilterItem[] filter;

private OrderItem[] order;

@NotBlank(message = "空间id不能为空")
private String workspaceId;
/**
* 所有者
*/
private String username;

/**
* 所有者Id
*/
private String userId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.xiaojusurvey.engine.core.survey.vo;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
* 问卷列表
*
* @author [email protected]
* @Date 2024/8/1 21:36
*/
@Data
public class SurveyListVO implements Serializable {

private static final long serialVersionUID = 2263256769125151536L;
/**
* 问卷列表
*/
private List<SurveyVO> data;

/**
* 总数量
*/
private Long count;
}
Loading

0 comments on commit 92f11f8

Please sign in to comment.