diff --git a/survey-common/src/main/java/com/xiaojusurvey/engine/common/constants/SurveyConstant.java b/survey-common/src/main/java/com/xiaojusurvey/engine/common/constants/SurveyConstant.java new file mode 100644 index 00000000..1541c0c2 --- /dev/null +++ b/survey-common/src/main/java/com/xiaojusurvey/engine/common/constants/SurveyConstant.java @@ -0,0 +1,14 @@ +package com.xiaojusurvey.engine.common.constants; + +/** + * Survey 常量 + * + * @author likui63@163.com + * @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"; +} diff --git a/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/survey/SurveyMeta.java b/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/survey/SurveyMeta.java index d6cd749c..dc5b97aa 100644 --- a/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/survey/SurveyMeta.java +++ b/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/survey/SurveyMeta.java @@ -57,5 +57,14 @@ public class SurveyMeta extends BaseEntity { */ private String owner; + /** + * 所有者ID + */ + private String ownerId; + + /** + * 空间 + */ + private String workspaceId; } diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/SurveyService.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/SurveyService.java index 94c8a217..eac3537a 100644 --- a/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/SurveyService.java +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/SurveyService.java @@ -2,7 +2,9 @@ package com.xiaojusurvey.engine.core.survey; 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 @@ -26,6 +28,12 @@ public interface SurveyService { */ boolean publishSurvey(String surveyId); + /** + * 获取问卷列表 + * @param param + * @return + */ + SurveyListVO getSurveyList(SurveyListParam param); + - } diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/dto/BaseQuery.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/dto/BaseQuery.java new file mode 100644 index 00000000..91936083 --- /dev/null +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/dto/BaseQuery.java @@ -0,0 +1,21 @@ +package com.xiaojusurvey.engine.core.survey.dto; + +import lombok.Data; + +import java.io.Serializable; + +/** + * 查询基础条件 + * + * @author likui63@163.com + * @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; + + +} diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/dto/FilterItem.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/dto/FilterItem.java new file mode 100644 index 00000000..8f3559ef --- /dev/null +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/dto/FilterItem.java @@ -0,0 +1,36 @@ +package com.xiaojusurvey.engine.core.survey.dto; + +import lombok.Data; + +/** + * TODO + * + * @author likui63@163.com + * @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; + } +} diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/dto/OrderItem.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/dto/OrderItem.java new file mode 100644 index 00000000..f2776205 --- /dev/null +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/dto/OrderItem.java @@ -0,0 +1,21 @@ +package com.xiaojusurvey.engine.core.survey.dto; + +import lombok.Data; + +/** + * 排序条件 + * + * @author likui63@163.com + * @Date 2024/8/1 20:59 + */ +@Data +public class OrderItem { + /** + * 字段名 + */ + private String field; + /** + * 升序降序 1-升序,-1-降序 + */ + private int value = 1; +} diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/impl/SurveyServiceImpl.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/impl/SurveyServiceImpl.java index cfd80387..b06f58f1 100644 --- a/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/impl/SurveyServiceImpl.java +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/impl/SurveyServiceImpl.java @@ -3,6 +3,7 @@ package com.xiaojusurvey.engine.core.survey.impl; 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; @@ -17,20 +18,29 @@ import com.xiaojusurvey.engine.core.survey.SurveyConfService; 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 @@ -175,5 +185,75 @@ public class SurveyServiceImpl implements SurveyService { return true; } + @Override + public SurveyListVO getSurveyList(SurveyListParam param) { + log.info("[getSurveyList]获取问卷列表 param={}", param); + Query query = buildQuery(param); + List 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 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 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 listAnd = new ArrayList(); + List 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; + } } diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/param/SurveyListParam.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/param/SurveyListParam.java new file mode 100644 index 00000000..0e5ee9da --- /dev/null +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/param/SurveyListParam.java @@ -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 likui63@163.com + * @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; + +} diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/vo/SurveyListVO.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/vo/SurveyListVO.java new file mode 100644 index 00000000..988bb31e --- /dev/null +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/vo/SurveyListVO.java @@ -0,0 +1,27 @@ +package com.xiaojusurvey.engine.core.survey.vo; + +import lombok.Data; + +import java.io.Serializable; +import java.util.List; + +/** + * 问卷列表 + * + * @author likui63@163.com + * @Date 2024/8/1 21:36 + */ +@Data +public class SurveyListVO implements Serializable { + + private static final long serialVersionUID = 2263256769125151536L; + /** + * 问卷列表 + */ + private List data; + + /** + * 总数量 + */ + private Long count; +} diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/vo/SurveyVO.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/vo/SurveyVO.java index 571c004b..410e93db 100644 --- a/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/vo/SurveyVO.java +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/vo/SurveyVO.java @@ -1,53 +1,56 @@ -//package com.xiaojusurvey.engine.core.survey.vo; -// -//import com.xiaojusurvey.engine.common.entity.Status; -// -//import javax.validation.constraints.NotBlank; -//import javax.validation.constraints.NotNull; -// -///** -// * 问卷信息 -// * -// * @author likui63@163.com -// * @Date 2024/7/28 21:57 -// */ -//public class SurveyVO { -// -// private String _id; -// -// private String name; -// /** -// * 问卷类型 -// */ -// @NotBlank(message = "问卷类型不能为空") -// @NotNull(message = "问卷类型不能为空") -// private String surveyType; -// -// /** -// * 问卷短链 -// */ -// private String surveyPath; -// /** -// * 问卷标题 -// */ -// private String title; -// /** -// * 当前状态 -// */ -// private Status curStatus; -// -// /** -// * 创建时间 -// */ -// private Long createDate; -// -// /** -// * 创建人 -// */ -// private String creator; -// /** -// * 所有者 -// */ -// private String owner; -// -//} +package com.xiaojusurvey.engine.core.survey.vo; + +import com.xiaojusurvey.engine.common.entity.Status; +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import java.io.Serializable; + +/** + * 问卷信息 + * + * @author likui63@163.com + * @Date 2024/7/28 21:57 + */ +@Data +public class SurveyVO implements Serializable { + + private static final long serialVersionUID = 6002962715728356197L; + + private String name; + /** + * 问卷类型 + */ + @NotBlank(message = "问卷类型不能为空") + @NotNull(message = "问卷类型不能为空") + private String surveyType; + + /** + * 问卷短链 + */ + private String surveyPath; + /** + * 问卷标题 + */ + private String title; + /** + * 当前状态 + */ + private Status curStatus; + + /** + * 创建时间 + */ + private Long createDate; + + /** + * 创建人 + */ + private String creator; + /** + * 所有者 + */ + private String owner; + +} diff --git a/survey-core/src/test/java/com/xiaojusurvey/engine/core/survey/impl/SurveyServiceImplTest.java b/survey-core/src/test/java/com/xiaojusurvey/engine/core/survey/impl/SurveyServiceImplTest.java index 8f8558ff..24b123b1 100644 --- a/survey-core/src/test/java/com/xiaojusurvey/engine/core/survey/impl/SurveyServiceImplTest.java +++ b/survey-core/src/test/java/com/xiaojusurvey/engine/core/survey/impl/SurveyServiceImplTest.java @@ -1,6 +1,7 @@ package com.xiaojusurvey.engine.core.survey.impl; +import com.xiaojusurvey.engine.common.entity.BaseEntity; import com.xiaojusurvey.engine.common.entity.Status; import com.xiaojusurvey.engine.common.entity.survey.SurveyConf; import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta; @@ -9,7 +10,10 @@ import com.xiaojusurvey.engine.common.enums.SurveyStatusEnum; import com.xiaojusurvey.engine.core.survey.SurveyConfService; import com.xiaojusurvey.engine.core.survey.SurveyHistoryService; import com.xiaojusurvey.engine.core.survey.SurveyPublishService; +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.util.WebUtils; import com.xiaojusurvey.engine.repository.MongoRepository; import org.junit.Assert; @@ -21,6 +25,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.Mockito; +import org.mockito.internal.matchers.Any; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.servlet.http.HttpServletRequest; @@ -55,10 +60,10 @@ public class SurveyServiceImplTest { SurveyMetaUpdateParam updateParam; SurveyMeta surveyMeta; - String surveyid = "111111111111111111"; + String surveyid = "111111111111111111"; @Before - public void before(){ + public void before() { surveyMeta = new SurveyMeta(); surveyMeta.setId(surveyid); surveyMeta.setTitle("title"); @@ -80,29 +85,29 @@ public class SurveyServiceImplTest { updateParam.setTitle("新时代"); updateParam.setRemark("这是一个测试"); - Mockito.when(mongoRepository.findOne(Mockito.any(),Mockito.any())).thenReturn(surveyMeta); + Mockito.when(mongoRepository.findOne(Mockito.any(), Mockito.any())).thenReturn(surveyMeta); - boolean flag = surveyService.updateMeta(updateParam); - Assert.assertEquals(true,flag); + boolean flag = surveyService.updateMeta(updateParam); + Assert.assertEquals(true, flag); } @Test public void deleteSurveyTest() { - Mockito.when(mongoRepository.findOne(Mockito.any(),Mockito.any())).thenReturn(surveyMeta); + Mockito.when(mongoRepository.findOne(Mockito.any(), Mockito.any())).thenReturn(surveyMeta); Mockito.when(surveyPublishService.delete(Mockito.any())).thenReturn(true); - boolean flag = surveyService.deleteSurvey(surveyid); - Assert.assertEquals(true,flag); + boolean flag = surveyService.deleteSurvey(surveyid); + Assert.assertEquals(true, flag); } @Test - public void publishSurveyTest(){ + public void publishSurveyTest() { SurveyConf conf = new SurveyConf(); Mockito.when(surveyConfService.getSurveyConfBySurveyId(Mockito.any())).thenReturn(conf); - Mockito.when(mongoRepository.findOne(Mockito.any(),Mockito.eq(SurveyMeta.class))).thenReturn(surveyMeta); + Mockito.when(mongoRepository.findOne(Mockito.any(), Mockito.eq(SurveyMeta.class))).thenReturn(surveyMeta); Mockito.when(mongoRepository.save(Mockito.any())).thenReturn(null); Mockito.when(surveyPublishService.save(Mockito.any())).thenReturn(true); Mockito.when(surveyHistoryService.addHistory(Mockito.any())).thenReturn(null); @@ -114,7 +119,29 @@ public class SurveyServiceImplTest { user.setUsername("ttt"); Mockito.when(req.getAttribute("user")).thenReturn(user); boolean falg = surveyService.publishSurvey(surveyid); - Assert.assertTrue("成功",falg == true); + Assert.assertTrue("成功", falg == true); + } + + + @Test + public void getListTest() { + SurveyListParam param = new SurveyListParam(); + param.setWorkspaceId("1"); + FilterItem item = new FilterItem(); +// item.setComparator("$regex"); + FilterItem.FilterCondition condition = new FilterItem.FilterCondition(); + condition.setComparator("$regex"); + condition.setField("title"); + condition.setValue("newTime3"); + item.setCondition(condition); + param.setFilter(new FilterItem[]{item}); + List list = new ArrayList<>(); + + Mockito.when(mongoRepository.page(Mockito.any(), Mockito.anyInt(), Mockito.anyInt(), Mockito.any())).thenReturn(list); + Mockito.when(mongoRepository.count(Mockito.any(), Mockito.any())).thenReturn(1L); + + SurveyListVO vo = surveyService.getSurveyList(param); + Assert.assertTrue("成功", vo.getCount() == 1); } diff --git a/survey-server/src/main/java/com/xiaojusurvey/engine/controller/SurveyController.java b/survey-server/src/main/java/com/xiaojusurvey/engine/controller/SurveyController.java index 239daf52..554c8a90 100644 --- a/survey-server/src/main/java/com/xiaojusurvey/engine/controller/SurveyController.java +++ b/survey-server/src/main/java/com/xiaojusurvey/engine/controller/SurveyController.java @@ -13,9 +13,11 @@ import com.xiaojusurvey.engine.core.reslut.IdResult; import com.xiaojusurvey.engine.core.survey.SurveyConfService; import com.xiaojusurvey.engine.core.survey.SurveyHistoryService; import com.xiaojusurvey.engine.core.survey.SurveyService; +import com.xiaojusurvey.engine.core.survey.param.SurveyListParam; import com.xiaojusurvey.engine.core.survey.param.SurveyMetaUpdateParam; import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoInVO; import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoOutVO; +import com.xiaojusurvey.engine.core.survey.vo.SurveyListVO; import lombok.extern.slf4j.Slf4j; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -146,4 +148,11 @@ public class SurveyController { } + @PostMapping("/getList") + public RpcResult getList(@RequestBody @Validated SurveyListParam param) { + log.info("[publishSurvey] 发布问卷,param={}", param); + SurveyListVO surveyListVO = surveyService.getSurveyList(param); + return RpcResultUtil.createSuccessResult(surveyListVO); + } + } diff --git a/survey-server/src/test/java/com/xiaojusurvey/engine/controller/SurveyControllerTest.java b/survey-server/src/test/java/com/xiaojusurvey/engine/controller/SurveyControllerTest.java index 9324914f..c8d428aa 100644 --- a/survey-server/src/test/java/com/xiaojusurvey/engine/controller/SurveyControllerTest.java +++ b/survey-server/src/test/java/com/xiaojusurvey/engine/controller/SurveyControllerTest.java @@ -9,8 +9,10 @@ import com.xiaojusurvey.engine.config.BannerDataConfig; import com.xiaojusurvey.engine.core.survey.SurveyConfService; import com.xiaojusurvey.engine.core.survey.SurveyHistoryService; import com.xiaojusurvey.engine.core.survey.SurveyService; +import com.xiaojusurvey.engine.core.survey.param.SurveyListParam; import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoInVO; import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoOutVO; +import com.xiaojusurvey.engine.core.survey.vo.SurveyListVO; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -97,6 +99,16 @@ public class SurveyControllerTest { Mockito.when(surveyService.publishSurvey(Mockito.any())).thenReturn(true); surveyController.publishSurvey("11111"); + } + + @Test + public void getListTest() { + SurveyListVO vo = new SurveyListVO(); + Mockito.when(surveyService.getSurveyList(Mockito.any())).thenReturn(vo); + + RpcResult rs = surveyController.getList(new SurveyListParam()); + + Assert.assertEquals(vo,rs.getData()); } }