diff --git a/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/survey/SurveyConf.java b/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/survey/SurveyConf.java new file mode 100644 index 00000000..a851f07c --- /dev/null +++ b/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/survey/SurveyConf.java @@ -0,0 +1,25 @@ +package com.xiaojusurvey.engine.common.entity.survey; + +import com.xiaojusurvey.engine.common.entity.BaseEntity; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import org.springframework.data.mongodb.core.mapping.Document; + +import java.util.Map; + +/** + * @Author: maple + * @CreateTime: 2024/6/7 21:27 + * @Description: 问卷配置 + */ +@Document("surveyConf") +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +@Data +public class SurveyConf extends BaseEntity { + + private String pageId; + + private Map code; +} diff --git a/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/survey/SurveyHistory.java b/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/survey/SurveyHistory.java new file mode 100644 index 00000000..523161de --- /dev/null +++ b/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/survey/SurveyHistory.java @@ -0,0 +1,29 @@ +package com.xiaojusurvey.engine.common.entity.survey; + +import com.xiaojusurvey.engine.common.entity.BaseEntity; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import org.springframework.data.mongodb.core.mapping.Document; + +import java.util.Map; + +/** + * @Author: maple + * @CreateTime: 2024/6/7 21:28 + * @Description: 问卷历史记录 + */ +@Document("surveyHistory") +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +@Data +public class SurveyHistory extends BaseEntity { + + private String pageId; + + private String type; + + private Map schema; + + private Map operator; +} diff --git a/survey-common/src/main/java/com/xiaojusurvey/engine/common/enums/HistoryTypeEnum.java b/survey-common/src/main/java/com/xiaojusurvey/engine/common/enums/HistoryTypeEnum.java new file mode 100644 index 00000000..0e043b9c --- /dev/null +++ b/survey-common/src/main/java/com/xiaojusurvey/engine/common/enums/HistoryTypeEnum.java @@ -0,0 +1,25 @@ +package com.xiaojusurvey.engine.common.enums; + +import lombok.Getter; + +/** + * @Author: maple + * @CreateTime: 2024/6/10 21:28 + * @Description: 历史记录枚举值 + */ +@Getter +public enum HistoryTypeEnum { + + //保存历史 + DAILY_HIS("dailyHis"), + + //发布历史 + PUBLISH_HIS("publishHis"); + + private final String historyType; + + HistoryTypeEnum(String historyType) { + this.historyType = historyType; + } + +} diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/SurveyConfService.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/SurveyConfService.java new file mode 100644 index 00000000..35cdf3a6 --- /dev/null +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/SurveyConfService.java @@ -0,0 +1,10 @@ +package com.xiaojusurvey.engine.core.survey; + +import com.xiaojusurvey.engine.common.entity.survey.SurveyConf; + +public interface SurveyConfService { + + void saveSurveyConfig(SurveyConf surveyConf); + + SurveyConf getSurveyConfBySurveyId(String surveyId); +} diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/SurveyHistoryService.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/SurveyHistoryService.java new file mode 100644 index 00000000..a1a982e3 --- /dev/null +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/SurveyHistoryService.java @@ -0,0 +1,12 @@ +package com.xiaojusurvey.engine.core.survey; + +import com.xiaojusurvey.engine.common.entity.survey.SurveyHistory; + +import java.util.List; + +public interface SurveyHistoryService { + + SurveyHistory addHistory(SurveyHistory surveyHistory); + + List getHistoryList(String surveyId, String historyType); +} 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 92c6533b..18cf6501 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 @@ -10,4 +10,6 @@ import com.xiaojusurvey.engine.core.reslut.IdResult; */ public interface SurveyService { IdResult createSurvey(SurveyMeta surveyMeta); + + SurveyMeta getSurveyMeta(String surveyId); } diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/impl/SurveyConfServiceImpl.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/impl/SurveyConfServiceImpl.java new file mode 100644 index 00000000..3e78a326 --- /dev/null +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/impl/SurveyConfServiceImpl.java @@ -0,0 +1,42 @@ +package com.xiaojusurvey.engine.core.survey.impl; + +import com.xiaojusurvey.engine.common.constants.RespErrorCode; +import com.xiaojusurvey.engine.common.entity.survey.SurveyConf; +import com.xiaojusurvey.engine.common.exception.ServiceException; +import com.xiaojusurvey.engine.core.survey.SurveyConfService; +import com.xiaojusurvey.engine.repository.MongoRepository; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +/** + * @Author: maple + * @CreateTime: 2024/6/9 13:52 + * @Description: + */ +@Service +public class SurveyConfServiceImpl implements SurveyConfService { + + @Resource + private MongoRepository mongoRepository; + + @Override + public void saveSurveyConfig(SurveyConf surveyConf) { + SurveyConf codeInfo = this.getSurveyConfBySurveyId(surveyConf.getPageId()); + if (null == codeInfo) { + throw new ServiceException(RespErrorCode.SURVEY_NOT_FOUND.getMessage(), RespErrorCode.SURVEY_NOT_FOUND.getCode()); + } + codeInfo.setCode(surveyConf.getCode()); + codeInfo.setUpdateDate(System.currentTimeMillis()); + mongoRepository.save(codeInfo); + } + + @Override + public SurveyConf getSurveyConfBySurveyId(String surveyId) { + Criteria criteria = Criteria.where("pageId").is(surveyId); + Query query = new Query(criteria); + return this.mongoRepository.findOne(query, SurveyConf.class); + } +} diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/impl/SurveyHistoryServiceImpl.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/impl/SurveyHistoryServiceImpl.java new file mode 100644 index 00000000..490dd7ca --- /dev/null +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/impl/SurveyHistoryServiceImpl.java @@ -0,0 +1,45 @@ +package com.xiaojusurvey.engine.core.survey.impl; + +import com.xiaojusurvey.engine.common.entity.survey.SurveyHistory; +import com.xiaojusurvey.engine.core.survey.SurveyHistoryService; +import com.xiaojusurvey.engine.repository.MongoRepository; +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.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +/** + * @Author: maple + * @CreateTime: 2024/6/10 21:29 + * @Description: 问卷历史记录服务 + */ +@Service +public class SurveyHistoryServiceImpl implements SurveyHistoryService { + + @Resource + private MongoRepository mongoRepository; + + @Override + public SurveyHistory addHistory(SurveyHistory surveyHistory) { + return mongoRepository.save(surveyHistory); + } + + @Override + public List getHistoryList(String surveyId, String historyType) { + // 组合查询条件 + Query query = new Query(); + query.addCriteria(Criteria.where("pageId").is(surveyId)) + .addCriteria(Criteria.where("type").is(historyType)); + + // 设置查询选项 + query.limit(100); + query.with(Sort.by(Sort.Direction.DESC, "createDate")); + + // 选择查询的字段 + query.fields().include("createDate").include("operator").include("type").include("_id"); + return mongoRepository.page(query, 0, 100, SurveyHistory.class); + } +} 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 7cb4241f..8c9ac985 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 @@ -4,6 +4,8 @@ import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta; import com.xiaojusurvey.engine.core.reslut.IdResult; import com.xiaojusurvey.engine.core.survey.SurveyService; import com.xiaojusurvey.engine.repository.MongoRepository; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -18,6 +20,7 @@ public class SurveyServiceImpl implements SurveyService { @Resource private MongoRepository mongoRepository; + /** * 创建问卷 */ @@ -27,4 +30,11 @@ public class SurveyServiceImpl implements SurveyService { idResult.setId(mongoRepository.save(surveyMeta).getId()); return idResult; } + + @Override + public SurveyMeta getSurveyMeta(String surveyId) { + Query query = new Query(); + query.addCriteria(Criteria.where("_id").is(surveyId)); + return mongoRepository.findOne(query, SurveyMeta.class); + } } diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/vo/SurveyHistoryOutVO.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/vo/SurveyHistoryOutVO.java new file mode 100644 index 00000000..aec29572 --- /dev/null +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/vo/SurveyHistoryOutVO.java @@ -0,0 +1,22 @@ +package com.xiaojusurvey.engine.core.survey.vo; + +import lombok.Data; + +import java.util.Map; + +/** + * @Author: maple + * @CreateTime: 2024/6/15 20:16 + * @Description: + */ +@Data +public class SurveyHistoryOutVO { + + private String id; + + private String type; + + private Long createDate; + + private Map operator; +} diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/vo/SurveyInfoInVO.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/vo/SurveyInfoInVO.java new file mode 100644 index 00000000..36c03d2d --- /dev/null +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/vo/SurveyInfoInVO.java @@ -0,0 +1,25 @@ +package com.xiaojusurvey.engine.core.survey.vo; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import java.util.Map; + +/** + * @Author: maple + * @CreateTime: 2024/6/9 13:40 + * @Description: + */ +@Data +public class SurveyInfoInVO { + + @NotBlank(message = "问卷id不能为空", groups = {UpdateConf.class}) + private String surveyId; + + @NotNull(message = "问卷数据不能为空", groups = {UpdateConf.class}) + private Map configData; + + public interface UpdateConf { + } +} diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/vo/SurveyInfoOutVO.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/vo/SurveyInfoOutVO.java new file mode 100644 index 00000000..d07c914f --- /dev/null +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/survey/vo/SurveyInfoOutVO.java @@ -0,0 +1,22 @@ +package com.xiaojusurvey.engine.core.survey.vo; + +import com.xiaojusurvey.engine.common.entity.survey.SurveyConf; +import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @Author: maple + * @CreateTime: 2024/6/9 14:16 + * @Description: + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class SurveyInfoOutVO { + + private SurveyMeta surveyMetaRes; + + private SurveyConf surveyConfRes; +} diff --git a/survey-server/src/main/java/com/xiaojusurvey/engine/config/BannerDataConfig.java b/survey-server/src/main/java/com/xiaojusurvey/engine/config/BannerDataConfig.java new file mode 100644 index 00000000..0e7d3e1d --- /dev/null +++ b/survey-server/src/main/java/com/xiaojusurvey/engine/config/BannerDataConfig.java @@ -0,0 +1,40 @@ +package com.xiaojusurvey.engine.config; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.Resource; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; + +/** + * @Author: maple + * @CreateTime: 2024/6/9 14:23 + * @Description: + */ +@Getter +@Configuration +@Slf4j +public class BannerDataConfig implements InitializingBean { + + @Value("classpath:banner.json") + private Resource bannerResource; + + private JSONObject bannerData; + + @Override + public void afterPropertiesSet() throws Exception { + try { + String bannerContent = new String(Files.readAllBytes(bannerResource.getFile().toPath()), StandardCharsets.UTF_8); + bannerData = JSON.parseObject(bannerContent); + } catch (Exception e) { + log.error("Initializing BannerDataConfig fail, e:", e); + } + } + +} 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 f99b0f62..72490929 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 @@ -1,16 +1,28 @@ package com.xiaojusurvey.engine.controller; +import com.xiaojusurvey.engine.common.constants.RespErrorCode; +import com.xiaojusurvey.engine.common.entity.survey.SurveyConf; +import com.xiaojusurvey.engine.common.entity.survey.SurveyHistory; import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta; -import com.xiaojusurvey.engine.core.reslut.IdResult; +import com.xiaojusurvey.engine.common.entity.user.User; +import com.xiaojusurvey.engine.common.enums.HistoryTypeEnum; import com.xiaojusurvey.engine.common.rpc.RpcResult; import com.xiaojusurvey.engine.common.util.RpcResultUtil; +import com.xiaojusurvey.engine.config.BannerDataConfig; +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.vo.SurveyInfoInVO; +import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoOutVO; import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import javax.validation.constraints.NotBlank; +import java.util.HashMap; +import java.util.Map; /** * @Author: LYF @@ -23,6 +35,15 @@ public class SurveyController { @Resource private SurveyService surveyService; + @Resource + private SurveyConfService surveyConfService; + + @Resource + private SurveyHistoryService surveyHistoryService; + + @Resource + private BannerDataConfig bannerDataConfig; + /** * 创建问卷 */ @@ -30,4 +51,51 @@ public class SurveyController { public RpcResult createSurvey(@Validated @RequestBody SurveyMeta surveyMeta) { return RpcResultUtil.createSuccessResult(surveyService.createSurvey(surveyMeta)); } + + /** + * 获取banner数据 + */ + @GetMapping("/getBannerData") + public RpcResult getBannerData() { + return RpcResultUtil.createSuccessResult(bannerDataConfig.getBannerData()); + } + + /** + * 更新问卷 + */ + @PostMapping("/updateConf") + public RpcResult updateConf(HttpServletRequest request, + @RequestBody @Validated(SurveyInfoInVO.UpdateConf.class) SurveyInfoInVO infoInVO) { + SurveyConf surveyConf = new SurveyConf(); + surveyConf.setCode(infoInVO.getConfigData()); + surveyConf.setPageId(infoInVO.getSurveyId()); + surveyConfService.saveSurveyConfig(surveyConf); + + User user = (User) request.getAttribute("user"); + SurveyHistory surveyHistory = new SurveyHistory(); + surveyHistory.setPageId(infoInVO.getSurveyId()); + surveyHistory.setType(HistoryTypeEnum.DAILY_HIS.getHistoryType()); + surveyHistory.setSchema(infoInVO.getConfigData()); + surveyHistory.setCreateDate(System.currentTimeMillis()); + surveyHistory.setUpdateDate(System.currentTimeMillis()); + Map operator = new HashMap<>(2); + operator.put("_id", user.getId()); + operator.put("username", user.getUsername()); + surveyHistory.setOperator(operator); + surveyHistoryService.addHistory(surveyHistory); + return RpcResultUtil.createSuccessResult(true); + } + + /** + * 获取问卷 + */ + @GetMapping("/getSurvey") + public RpcResult getSurvey(@RequestParam("surveyId") @NotBlank String surveyId) { + SurveyMeta surveyMeta = surveyService.getSurveyMeta(surveyId); + if (null == surveyMeta) { + return RpcResultUtil.createFailedResult(RespErrorCode.SURVEY_NOT_FOUND.getCode(), RespErrorCode.SURVEY_NOT_FOUND.getMessage()); + } + SurveyConf surveyConf = surveyConfService.getSurveyConfBySurveyId(surveyId); + return RpcResultUtil.createSuccessResult(new SurveyInfoOutVO(surveyMeta, surveyConf)); + } } diff --git a/survey-server/src/main/java/com/xiaojusurvey/engine/controller/SurveyHistoryController.java b/survey-server/src/main/java/com/xiaojusurvey/engine/controller/SurveyHistoryController.java new file mode 100644 index 00000000..c153fcbe --- /dev/null +++ b/survey-server/src/main/java/com/xiaojusurvey/engine/controller/SurveyHistoryController.java @@ -0,0 +1,49 @@ +package com.xiaojusurvey.engine.controller; + +import com.xiaojusurvey.engine.common.entity.survey.SurveyHistory; +import com.xiaojusurvey.engine.common.rpc.RpcResult; +import com.xiaojusurvey.engine.common.util.RpcResultUtil; +import com.xiaojusurvey.engine.core.survey.SurveyHistoryService; +import com.xiaojusurvey.engine.core.survey.vo.SurveyHistoryOutVO; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +/** + * @Author: maple + * @CreateTime: 2024/6/10 21:10 + * @Description: + */ +@RequestMapping("/api/surveyHistory") +@RestController +public class SurveyHistoryController { + + @Resource + private SurveyHistoryService surveyHistoryService; + + /** + * 获取问卷历史列表 + */ + @GetMapping("/getList") + public RpcResult> getList(@RequestParam("surveyId") String surveyId, @RequestParam("historyType") String historyType) { + List historyList = surveyHistoryService.getHistoryList(surveyId, historyType); + if (null == historyList || historyList.isEmpty()) { + return RpcResultUtil.createSuccessResult(new ArrayList<>()); + } + + List resultList = historyList.stream().map(e -> { + SurveyHistoryOutVO outVO = new SurveyHistoryOutVO(); + outVO.setId(e.getId()); + outVO.setOperator(e.getOperator()); + outVO.setCreateDate(e.getCreateDate()); + return outVO; + }).collect(Collectors.toList()); + return RpcResultUtil.createSuccessResult(resultList); + } +} diff --git a/survey-server/src/main/java/com/xiaojusurvey/engine/interceptor/LoginInterceptor.java b/survey-server/src/main/java/com/xiaojusurvey/engine/interceptor/LoginInterceptor.java index fae723e1..b09e7565 100644 --- a/survey-server/src/main/java/com/xiaojusurvey/engine/interceptor/LoginInterceptor.java +++ b/survey-server/src/main/java/com/xiaojusurvey/engine/interceptor/LoginInterceptor.java @@ -47,6 +47,7 @@ public class LoginInterceptor implements HandlerInterceptor { throw new ServiceException(RespErrorCode.USER_CREDENTIALS_ERROR.getMessage(), RespErrorCode.USER_CREDENTIALS_ERROR.getCode()); } User user = userService.loadUserByUsernameAndPassword(username, password); + request.setAttribute("user", user); return HandlerInterceptor.super.preHandle(request, response, handler); } } diff --git a/survey-server/src/main/resources/banner.json b/survey-server/src/main/resources/banner.json new file mode 100644 index 00000000..e1cd290d --- /dev/null +++ b/survey-server/src/main/resources/banner.json @@ -0,0 +1,175 @@ +{ + "temp": { + "key": "default", + "name": "全部", + "list": [{ + "src": "/imgs/skin/17e06b7604a007e1d3e1453b9ddadc3c.webp", + "title": "1" + }] + }, + "scenery": { + "key": "scenery", + "name": "风景", + "list": [{ + "src": "/imgs/skin/SyiLRcukyE1558430525760.webp", + "title": "1" + }, { + "src": "/imgs/skin/sqYig4AcWr1558430525663.webp", + "title": "2" + }, { + "src": "/imgs/skin/ElNeqJT2I21558430526165.webp", + "title": "3" + }, { + "src": "/imgs/skin/CxQkSU6AY21558430526163.webp", + "title": "4" + }, { + "src": "/imgs/skin/VTUwbp6vY61558430527320.webp", + "title": "5" + }, { + "src": "/imgs/skin/SHs0K703Yn1558430527218.webp", + "title": "6" + }, { + "src": "/imgs/skin/oVTedX9V4s1558430527671.webp", + "title": "7" + }] + }, + "business": { + "key": "business", + "name": "商务", + "list": [{ + "src": "/imgs/skin/3ABKqvDaVn1558514860472.webp", + "title": "1" + }, { + "src": "/imgs/skin/OewuaQmWoq1558514860285.webp", + "title": "2" + }, { + "src": "/imgs/skin/HuVqqtbFjs1558514860570.webp", + "title": "3" + }, { + "src": "/imgs/skin/icSlqsr0uZ1558514860875.webp", + "title": "4" + }, { + "src": "/imgs/skin/Qu9rg33wmq1558514861015.webp", + "title": "5" + }, { + "src": "/imgs/skin/145gBCRtNP1558514861211.webp", + "title": "6" + }, { + "src": "/imgs/skin/ykWLFV0QWj1558514861444.webp", + "title": "7" + }] + }, + "activity": { + "key": "activity", + "name": "节日", + "list": [{ + "src": "/imgs/skin/Ixx8hqiwwk1660979120801.webp", + "title": "1" + }] + }, + "creative": { + "key": "creative", + "name": "创意", + "list": [{ + "src": "/imgs/skin/PLwNH1rAie1558430219772.webp", + "title": "1" + }, { + "src": "/imgs/skin/NnXsAOhBNm1558430219312.webp", + "title": "2" + }, { + "src": "/imgs/skin/ujpUoWqhw31558430220124.webp", + "title": "3" + }, { + "src": "/imgs/skin/5OCvbjqJQm1558430220362.webp", + "title": "4" + }, { + "src": "/imgs/skin/0k7Jg7In8I1558430221154.webp", + "title": "5" + }, { + "src": "/imgs/skin/UH0A8DbTai1558430221033.webp", + "title": "6" + }, { + "src": "/imgs/skin/FRIzPC6ZtN1558430221344.webp", + "title": "7" + }] + }, + "transportation": { + "key": "transportation", + "name": "交通", + "list": [{ + "src": "/imgs/skin/XYKqJZuMig1558430904735.webp", + "title": "1" + }, { + "src": "/imgs/skin/GnPatsr48Z1558430904680.webp", + "title": "2" + }, { + "src": "/imgs/skin/UqIvVvEXAK1558430905204.webp", + "title": "3" + }, { + "src": "/imgs/skin/PUssufh5uI1558430905104.webp", + "title": "4" + }, { + "src": "/imgs/skin/O409pRTDlW1558430905738.webp", + "title": "5" + }, { + "src": "/imgs/skin/A9FzlbYXqI1558430905739.webp", + "title": "6" + }, { + "src": "/imgs/skin/HN9YGctDeF1558430906686.webp", + "title": "7" + }] + }, + "delicacy": { + "key": "delicacy", + "name": "美食", + "list": [{ + "src": "/imgs/skin/lE6PSclCcU1558434536703.webp", + "title": "1" + }, { + "src": "/imgs/skin/OnSdbm7u6n1558434536641.webp", + "title": "2" + }, { + "src": "/imgs/skin/N9Z2ZuyO731558434537314.webp", + "title": "3" + }, { + "src": "/imgs/skin/YP9PoW8pX51558434537301.webp", + "title": "4" + }, { + "src": "/imgs/skin/zUtDv378bg1558434538351.webp", + "title": "5" + }, { + "src": "/imgs/skin/gY1JljCow21558434538303.webp", + "title": "6" + }, { + "src": "/imgs/skin/oOjHPbABdd1558434538864.webp", + "title": "7" + }] + }, + + "campus": { + "key": "campus", + "name": "校园", + "list": [{ + "src": "/imgs/skin/4aWi5JxG471558514268698.webp", + "title": "1" + }, { + "src": "/imgs/skin/j8C2OBP7WK1558514268563.webp", + "title": "2" + }, { + "src": "/imgs/skin/q3uJoQhYsR1558514268877.webp", + "title": "3" + }, { + "src": "/imgs/skin/W5PPlNsmsr1558514269088.webp", + "title": "4" + }, { + "src": "/imgs/skin/6xQk1IAmKt1558514269874.webp", + "title": "5" + }, { + "src": "/imgs/skin/XQE2iyF0rj1558514269935.webp", + "title": "6" + }, { + "src": "/imgs/skin/POHlQiSwPR1558514270379.webp", + "title": "7" + }] + } +} \ No newline at end of file diff --git a/survey-server/src/test/java/com/xiaojusurvey/engine/controller/AuthTest.java b/survey-server/src/test/java/com/xiaojusurvey/engine/controller/AuthTest.java deleted file mode 100644 index 72cc5324..00000000 --- a/survey-server/src/test/java/com/xiaojusurvey/engine/controller/AuthTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.xiaojusurvey.engine.controller; - -import com.xiaojusurvey.engine.SurveyApplication; -import com.xiaojusurvey.engine.core.auth.vo.CaptchaVo; -import com.xiaojusurvey.engine.common.rpc.RpcResult; -import com.xiaojusurvey.engine.core.auth.param.UserParam; -import com.xiaojusurvey.engine.core.auth.vo.UserVo; -import com.xiaojusurvey.engine.core.auth.util.JwtTokenUtil; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import javax.annotation.Resource; - -/** - * 问卷单元测试 - */ -@RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest(classes = SurveyApplication.class) -public class AuthTest { - - @Resource - private JwtTokenUtil jwtTokenUtil; - - @Resource - AuthController authController; - - @Test - public void captcha() { - RpcResult captcha = authController.captcha(); - - System.out.println(captcha); - } - - @Test - public void register() { - UserParam userParam = new UserParam(); - userParam.setUsername("admin"); - userParam.setPassword("admin"); - userParam.setCaptchaId("666169681618c21f3f6b90cd"); - userParam.setCaptcha("3377"); - RpcResult register = authController.register(userParam); - System.out.println(register); - } - - @Test - public void login() { - //eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiZXhwIjoxNzE3Njg4ODcyLCJqdGkiOiI4MDYwMDhhOC0xODVkLTQ1YjgtYmNhMy04YzRiNmRhN2IyNjcifQ.Sq2WMKsdOTqo-Xbw_g_FjQWv1pwRwWwUAJIDON9c2jA - UserParam userParam = new UserParam(); - userParam.setUsername("admin"); - userParam.setPassword("admin"); - userParam.setCaptchaId("66617239de72dc5238d1c32e"); - userParam.setCaptcha("1167"); - RpcResult register = authController.login(userParam); - System.out.println(register); - - } - -} 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 new file mode 100644 index 00000000..5cb1aad7 --- /dev/null +++ b/survey-server/src/test/java/com/xiaojusurvey/engine/controller/SurveyControllerTest.java @@ -0,0 +1,93 @@ +package com.xiaojusurvey.engine.controller; + +import com.alibaba.fastjson.JSONObject; +import com.xiaojusurvey.engine.common.entity.survey.SurveyHistory; +import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta; +import com.xiaojusurvey.engine.common.entity.user.User; +import com.xiaojusurvey.engine.common.rpc.RpcResult; +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.vo.SurveyInfoInVO; +import com.xiaojusurvey.engine.core.survey.vo.SurveyInfoOutVO; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import javax.servlet.http.HttpServletRequest; + + +/** + * @Author: maple + * @CreateTime: 2024/6/15 20:40 + * @Description: + */ +@RunWith(SpringJUnit4ClassRunner.class) +public class SurveyControllerTest { + + @InjectMocks + SurveyController surveyController; + + @Mock + private SurveyService surveyService; + + @Mock + private SurveyConfService surveyConfService; + + @Mock + private SurveyHistoryService surveyHistoryService; + + @Mock + private BannerDataConfig bannerDataConfig; + + private HttpServletRequest httpServletRequest; + + @Before + public void initBean() { + httpServletRequest = new MockHttpServletRequest(); + User user = new User(); + user.setId("123"); + user.setUsername("maple"); + httpServletRequest.setAttribute("user", user); + } + + @Test + public void getBannerData() { + Mockito.when(bannerDataConfig.getBannerData()).thenReturn(new JSONObject()); + RpcResult bannerData = surveyController.getBannerData(); + Assert.assertTrue(bannerData.getSuccess()); + Assert.assertEquals(new Integer(200), bannerData.getCode()); + } + + @Test + public void updateConf() { + Mockito.doNothing().when(surveyConfService).saveSurveyConfig(Mockito.any()); + Mockito.when(surveyHistoryService.addHistory(Mockito.any())).thenReturn(new SurveyHistory()); + RpcResult updateConf = surveyController.updateConf(httpServletRequest, new SurveyInfoInVO()); + Assert.assertTrue(updateConf.getSuccess()); + Assert.assertEquals(new Integer(200), updateConf.getCode()); + } + + @Test + public void getSurvey() { + Mockito.when(surveyService.getSurveyMeta(Mockito.anyString())).thenReturn(null); + Mockito.when(surveyHistoryService.addHistory(Mockito.any())).thenReturn(new SurveyHistory()); + RpcResult surveyResult = surveyController.getSurvey("112133"); + Assert.assertFalse(surveyResult.getSuccess()); + Assert.assertEquals(new Integer(3004), surveyResult.getCode()); + + Mockito.when(surveyService.getSurveyMeta(Mockito.anyString())).thenReturn(new SurveyMeta()); + Mockito.when(surveyHistoryService.addHistory(Mockito.any())).thenReturn(new SurveyHistory()); + RpcResult surveyResult2 = surveyController.getSurvey("112133"); + Assert.assertTrue(surveyResult2.getSuccess()); + Assert.assertEquals(new Integer(200), surveyResult2.getCode()); + } + +} diff --git a/survey-server/src/test/java/com/xiaojusurvey/engine/controller/SurveyHistoryControllerTest.java b/survey-server/src/test/java/com/xiaojusurvey/engine/controller/SurveyHistoryControllerTest.java new file mode 100644 index 00000000..32b28bb2 --- /dev/null +++ b/survey-server/src/test/java/com/xiaojusurvey/engine/controller/SurveyHistoryControllerTest.java @@ -0,0 +1,52 @@ +package com.xiaojusurvey.engine.controller; + +import com.xiaojusurvey.engine.common.entity.survey.SurveyHistory; +import com.xiaojusurvey.engine.common.rpc.RpcResult; +import com.xiaojusurvey.engine.core.survey.SurveyHistoryService; +import com.xiaojusurvey.engine.core.survey.vo.SurveyHistoryOutVO; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * @Author: maple + * @CreateTime: 2024/6/15 20:30 + * @Description: + */ +@RunWith(SpringJUnit4ClassRunner.class) +public class SurveyHistoryControllerTest { + + @InjectMocks + SurveyHistoryController surveyHistoryController; + + @Mock + SurveyHistoryService surveyHistoryService; + + @Test + public void getList() { + Mockito.when(surveyHistoryService.getHistoryList(Mockito.any(), Mockito.any())).thenReturn(new ArrayList<>()); + RpcResult> result = surveyHistoryController.getList("1", "dailyHis"); + Assert.assertTrue(result.getSuccess()); + Assert.assertEquals(new Integer(200), result.getCode()); + + List historyList = new ArrayList<>(); + SurveyHistory history = new SurveyHistory(); + history.setId("111"); + history.setCreateDate(System.currentTimeMillis()); + history.setOperator(new HashMap<>()); + historyList.add(history); + Mockito.when(surveyHistoryService.getHistoryList(Mockito.any(), Mockito.any())).thenReturn(historyList); + RpcResult> resultList = surveyHistoryController.getList("1", "dailyHis"); + Assert.assertTrue(resultList.getSuccess()); + Assert.assertEquals(new Integer(200), resultList.getCode()); + Assert.assertFalse(resultList.getData().isEmpty()); + } +} diff --git a/survey-server/src/test/java/com/xiaojusurvey/engine/controller/SurveyMetaTest.java b/survey-server/src/test/java/com/xiaojusurvey/engine/controller/SurveyMetaTest.java deleted file mode 100644 index 948e7de5..00000000 --- a/survey-server/src/test/java/com/xiaojusurvey/engine/controller/SurveyMetaTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.xiaojusurvey.engine.controller; - -import com.xiaojusurvey.engine.SurveyApplication; -import com.xiaojusurvey.engine.common.entity.survey.SurveyMeta; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import javax.annotation.Resource; - -/** - * 问卷单元测试 - */ -@RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest(classes = SurveyApplication.class) -public class SurveyMetaTest { - - @Resource - private SurveyController surveyController; - - - @Test - public void createSurvey() { - SurveyMeta surveyMeta = new SurveyMeta(); - surveyMeta.setTitle("测试问卷"); - surveyMeta.setSurveyType("1"); - surveyMeta.setCreateMethod("1"); - surveyMeta.setCreateFrom("1"); - surveyController.createSurvey(surveyMeta); - } -}