From 02f18587bd8860b305e2c688e20465be166bb48c Mon Sep 17 00:00:00 2001 From: huanghongfa <huanghongfa123456> Date: 星期四, 22 七月 2021 15:14:03 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/test' into test --- springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComActQuestnaireServiceImpl.java | 202 ++++++++++++++++++++++++++++++++++++-------------- 1 files changed, 144 insertions(+), 58 deletions(-) diff --git a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComActQuestnaireServiceImpl.java b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComActQuestnaireServiceImpl.java index b9e3c05..498e8f2 100644 --- a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComActQuestnaireServiceImpl.java +++ b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComActQuestnaireServiceImpl.java @@ -1,7 +1,9 @@ package com.panzhihua.service_community.service.impl; import java.util.*; +import java.util.stream.Collectors; +import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; @@ -14,16 +16,12 @@ import com.panzhihua.common.model.dtos.community.PageQuestnaireDTO; import com.panzhihua.common.model.dtos.community.PageQuestnaireSubDetailsDTO; import com.panzhihua.common.model.dtos.community.QuestnaireAnswersDTO; +import com.panzhihua.common.model.vos.LoginUserInfoVO; import com.panzhihua.common.model.vos.R; import com.panzhihua.common.model.vos.community.questnaire.*; -import com.panzhihua.service_community.dao.ComActQuestnaireAnswerContentDAO; -import com.panzhihua.service_community.dao.ComActQuestnaireDAO; -import com.panzhihua.service_community.dao.ComActQuestnaireSubDAO; -import com.panzhihua.service_community.dao.ComActQuestnaireSubSelectionDAO; -import com.panzhihua.service_community.model.dos.ComActQuestnaireAnswerContentDO; -import com.panzhihua.service_community.model.dos.ComActQuestnaireDO; -import com.panzhihua.service_community.model.dos.ComActQuestnaireSubDO; -import com.panzhihua.service_community.model.dos.ComActQuestnaireSubSelectionDO; +import com.panzhihua.common.service.user.UserService; +import com.panzhihua.service_community.dao.*; +import com.panzhihua.service_community.model.dos.*; import com.panzhihua.service_community.service.ComActQuestnaireService; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; @@ -31,6 +29,7 @@ import com.panzhihua.common.model.vos.community.ComActQuestnaireSubSelectionVO; import com.panzhihua.common.model.vos.community.ComActQuestnaireSubVO; import com.panzhihua.common.model.vos.community.ComActQuestnaireVO; +import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; @@ -45,6 +44,8 @@ @Resource ComActQuestnaireSubDAO questnaireSubDAO; @Resource + ComActDynUserDAO userDAO; + @Resource ComActQuestnaireSubSelectionDAO questnaireSubSelectionDAO; @Resource ComActQuestnaireAnswerContentDAO questnaireAnswerContentDAO; @@ -52,8 +53,13 @@ ComActQuestnaireSubServiceImpl questnaireSubService; @Resource ComActQuestnaireSubSelectionServiceImpl subSelectionService; + @Resource + ComActQuestnaireAnswerContentServiceImpl answerContentService; + @Resource + UserService userService; @Override + @Transactional(rollbackFor = Exception.class) public R addQuestnaire(QuestnaireVO questnaireVO, Long communityId, Long userId) { ComActQuestnaireDO comActQuestnaireDO = new ComActQuestnaireDO(); comActQuestnaireDO.setTitle(questnaireVO.getQueTitle()); @@ -67,48 +73,68 @@ comActQuestnaireDO.setForParty(questnaireVO.getForParty()); comActQuestnaireDO.setForVolunteer(questnaireVO.getForVolunteer()); int questId = questnaireDAO.insert(comActQuestnaireDO); - questnaireVO.getQuestnaiteSubVOS().forEach(sub -> { - ComActQuestnaireSubDO comActQuestnaireSubDO = new ComActQuestnaireSubDO(); - comActQuestnaireSubDO.setType(sub.getType()); - comActQuestnaireSubDO.setContent(sub.getContent()); - comActQuestnaireSubDO.setSort(sub.getSort()); - comActQuestnaireSubDO.setQueId((long) questId); - comActQuestnaireSubDO.setCreateBy(userId); - int subId = questnaireSubDAO.insert(comActQuestnaireSubDO); - ArrayList<ComActQuestnaireSubSelectionDO> subSelectionList = Lists.newArrayList(); - sub.getQuestnaiteSubSelectionVOS().forEach(subSelect -> { - ComActQuestnaireSubSelectionDO subSelectionDO = new ComActQuestnaireSubSelectionDO(); - subSelectionDO.setQueSubId((long) subId); - subSelectionDO.setOptionName(subSelect.getOptionName()); - subSelectionDO.setSort(subSelect.getSort()); - subSelectionDO.setContent(subSelect.getContent()); - subSelectionDO.setCreateBy(userId); - subSelectionList.add(subSelectionDO); + if(questId!=1){ + return R.fail("创建失败"); + } + if(questnaireVO.getQuestnaiteSubVOS()!=null) { + questnaireVO.getQuestnaiteSubVOS().forEach(sub -> { + ComActQuestnaireSubDO comActQuestnaireSubDO = new ComActQuestnaireSubDO(); + comActQuestnaireSubDO.setType(sub.getType()); + comActQuestnaireSubDO.setContent(sub.getContent()); + comActQuestnaireSubDO.setSort(sub.getSort()); + comActQuestnaireSubDO.setQueId((long) comActQuestnaireDO.getId()); + comActQuestnaireSubDO.setCreateBy(userId); + int subId = questnaireSubDAO.insert(comActQuestnaireSubDO); + + ArrayList<ComActQuestnaireSubSelectionDO> subSelectionList = Lists.newArrayList(); + if(sub.getQuestnaiteSubSelectionVOS()!=null) { + List<QuestnaiteSubSelectionVO> l = sub.getQuestnaiteSubSelectionVOS(); + for(int i=0; i<l.size(); i++){ + QuestnaiteSubSelectionVO subSelect = l.get(i); + ComActQuestnaireSubSelectionDO subSelectionDO = new ComActQuestnaireSubSelectionDO(); + subSelectionDO.setQueSubId(comActQuestnaireSubDO.getId()); + if(subSelect.getType()==1){ + //添加选项名称 + subSelectionDO.setOptionName(subSelect.getOptionName()); + }else { + subSelectionDO.setOptionName(String.valueOf(Character.toChars('A' + i))); + } + subSelectionDO.setSort(subSelect.getSort()); + subSelectionDO.setContent(subSelect.getContent()); + subSelectionDO.setCreateBy(userId); + subSelectionDO.setType(subSelect.getType()); + subSelectionList.add(subSelectionDO); + } + } + subSelectionService.saveBatch(subSelectionList); }); - subSelectionService.saveBatch(subSelectionList); - }); + } return R.ok(); } @Override public R pageQuestnaire(PageQuestnaireDTO pageQuestnaireDTO, Long communityId) { - Page userPage = new Page(pageQuestnaireDTO.getPageNum(), pageQuestnaireDTO.getPageSize()); - LambdaQueryWrapper<ComActQuestnaireDO> lambdaQueryWrapper = Wrappers.lambdaQuery(); - lambdaQueryWrapper.eq(ComActQuestnaireDO::getCommunityId, communityId); - //过滤隐藏字段 - lambdaQueryWrapper.eq(ComActQuestnaireDO::getIsHide, 0); - if (pageQuestnaireDTO.getTitle() != null) { - lambdaQueryWrapper.like(ComActQuestnaireDO::getTitle, pageQuestnaireDTO.getTitle()); + Page page = new Page<>(); + Long pageNum = pageQuestnaireDTO.getPageNum(); + Long pageSize = pageQuestnaireDTO.getPageSize(); + if (null == pageNum || 0 == pageNum) { + pageNum = 1l; } - if (pageQuestnaireDTO.getTitle() != null) { - lambdaQueryWrapper.eq(ComActQuestnaireDO::getState, pageQuestnaireDTO.getState()); + if (null == pageSize || 0 == pageSize) { + pageSize = 10l; } - if (pageQuestnaireDTO.getEndTime() != null && pageQuestnaireDTO.getStartTime() != null) { - lambdaQueryWrapper.ge(ComActQuestnaireDO::getCreateAt, pageQuestnaireDTO.getStartTime()); - lambdaQueryWrapper.lt(ComActQuestnaireDO::getCreateAt, pageQuestnaireDTO.getEndTime()); - } - IPage<ComActQuestnaireDO> doPager = questnaireDAO.selectPage(userPage, lambdaQueryWrapper.orderByDesc(ComActQuestnaireDO::getCreateAt)); - return R.ok(doPager); + page.setSize(pageSize); + page.setCurrent(pageNum); + IPage<QuestnaireListVo> voPage = questnaireDAO.selectPageList(page,pageQuestnaireDTO,communityId); + voPage.getRecords().forEach(record -> { + Integer userAnswerCount = questnaireAnswerContentDAO.selectUserCountByQuestnaireId(record.getId(), pageQuestnaireDTO.getUserId()); + if(userAnswerCount>0){ + record.setAnswer(true); + }else{ + record.setAnswer(false); + } + }); + return R.ok(voPage); } @Override @@ -119,8 +145,8 @@ if (questnaireDO == null) { return R.fail(500, "id有误!"); } - questnaireDO.setTitle(questnaireDO.getTitle()); - questnaireDO.setQueDescribe(questnaireDO.getQueDescribe()); + questnaireDO.setTitle(questnaireVO.getQueTitle()); + questnaireDO.setQueDescribe(questnaireVO.getQueDescribe()); //保存问卷调查 questnaireDAO.updateById(questnaireDO); questnaireVO.getQuestnaiteSubVOS().forEach(sub -> { @@ -145,6 +171,8 @@ }); }); + }else{ + return R.fail("参数有误"); } return R.ok(); } @@ -176,8 +204,12 @@ } @Override - public R statQuestnaire(Long questId){ + public R statQuestnaire(Long questId) { ComActQuestnaireDO comActQuestnaireDO = questnaireDAO.selectOne(new QueryWrapper<ComActQuestnaireDO>().lambda().eq(ComActQuestnaireDO::getId, questId).eq(ComActQuestnaireDO::getIsHide, 0)); + + if(comActQuestnaireDO==null){ + return R.fail("问卷已删除!"); + } QuestnaireAnswerStatVO usersAnswerStatVO = new QuestnaireAnswerStatVO(); //问卷信息 @@ -193,7 +225,7 @@ List<UsersSubAnswerStatVO> usersSubAnswerStatVOList = new ArrayList<>(); - comActQuestnaireSubDOList.forEach(comActQuestnaireSubDO->{ + comActQuestnaireSubDOList.forEach(comActQuestnaireSubDO -> { UsersSubAnswerStatVO usersSubAnswerStatVO = new UsersSubAnswerStatVO(); ComActQuestnaireSubVO comActQuestnaireSubVO = new ComActQuestnaireSubVO(); @@ -203,13 +235,16 @@ List<UserSubAnswerSelectionStatVO> userSubAnswerSelectionStatVOList = new ArrayList<>(); usersSubAnswerStatVO.setTotal(0); - usearSubAnswerSelectionStatVOList.forEach(stat ->{ - if(stat.getSubId()==comActQuestnaireSubDO.getId()){ + usearSubAnswerSelectionStatVOList.forEach(stat -> { + if (stat.getSubId().equals(comActQuestnaireSubDO.getId()) + && stat.getTotal()>0) { userSubAnswerSelectionStatVOList.add(stat); } }); usersSubAnswerStatVO.setUsersAnswer(userSubAnswerSelectionStatVOList); - Integer count = questnaireAnswerContentDAO.selectCount(new QueryWrapper<ComActQuestnaireAnswerContentDO>().select(" DISTINCT sub_id, user_id ").lambda().eq(ComActQuestnaireAnswerContentDO::getSubId,comActQuestnaireSubDO.getId())); + Integer count = questnaireAnswerContentDAO.selectCount(new QueryWrapper<ComActQuestnaireAnswerContentDO>() + .select(" DISTINCT sub_id, user_id ").lambda() + .eq(ComActQuestnaireAnswerContentDO::getSubId, comActQuestnaireSubDO.getId())); usersSubAnswerStatVO.setTotal(count); usersSubAnswerStatVOList.add(usersSubAnswerStatVO); }); @@ -225,8 +260,8 @@ @Override public R answerInfoQuestnaire(Long userId, Long questId) { ComActQuestnaireDO comActQuestnaireDO = questnaireDAO.selectOne(new QueryWrapper<ComActQuestnaireDO>().lambda().eq(ComActQuestnaireDO::getId, questId).eq(ComActQuestnaireDO::getIsHide, 0)); - if(comActQuestnaireDO==null){ - return R.fail("问卷不存在!"); + if (comActQuestnaireDO == null) { + return R.fail("问卷不存在!"); } UsersAnswerVO usersAnswerVO = new UsersAnswerVO(); @@ -238,7 +273,7 @@ list.sort(Comparator.comparing(ComActQuestnaireSubDO::getSort)); List<UsersSubAnswerVO> usersSubAnswerVOList = new ArrayList<>(); - list.forEach(comActQuestnaireSubDO ->{ + list.forEach(comActQuestnaireSubDO -> { UsersSubAnswerVO usersSubAnswerVO = new UsersSubAnswerVO(); //问卷调查题目 ComActQuestnaireSubVO comActQuestnaireSubVO = new ComActQuestnaireSubVO(); @@ -259,7 +294,7 @@ }); usersSubAnswerVO.setQuestSelectionList(comActQuestnaireSubSelectionVOs); //用户回答 - if(userId!=null) { + if (userId != null) { List<ComActQuestnaireAnswerContentDO> questnaireAnswerContentDOList = questnaireAnswerContentDAO.selectList(new QueryWrapper<ComActQuestnaireAnswerContentDO>().lambda().eq(ComActQuestnaireAnswerContentDO::getUserId, userId) .eq(ComActQuestnaireAnswerContentDO::getSubId, subId)); @@ -289,7 +324,7 @@ */ List<ComActQuestnaireSubDO> list = questnaireSubDAO.selectList(new QueryWrapper<ComActQuestnaireSubDO>().lambda().eq(ComActQuestnaireSubDO::getQueId, questId)); List<ComActQuestnaireSubVO> listSubVo = new ArrayList<>(); - list.forEach(subDo->{ + list.forEach(subDo -> { ComActQuestnaireSubVO comActQuestnaireSubVO = new ComActQuestnaireSubVO(); BeanUtils.copyProperties(subDo, comActQuestnaireSubVO); listSubVo.add(comActQuestnaireSubVO); @@ -300,7 +335,7 @@ */ List<ComActQuestnaireAnswerContentDO> questnaireAnswerContentDOList = questnaireAnswerContentDAO.selectListByQuestnaire(questId); List<ComActQuestnaireAnswerContentVO> vos = new ArrayList<>(); - questnaireAnswerContentDOList.forEach(dos ->{ + questnaireAnswerContentDOList.forEach(dos -> { ComActQuestnaireAnswerContentVO vo = new ComActQuestnaireAnswerContentVO(); BeanUtils.copyProperties(dos, vo); vos.add(vo); @@ -313,16 +348,67 @@ @Override public R qaQeustDetails(PageQuestnaireSubDetailsDTO pageQuestnaireSubDetailsDTO) { Page userPage = new Page(pageQuestnaireSubDetailsDTO.getPageNum(), pageQuestnaireSubDetailsDTO.getPageSize()); - IPage<ComActQuestnaireAnswerContentDO> doPager = - questnaireAnswerContentDAO.selectPage(userPage, new QueryWrapper<ComActQuestnaireAnswerContentDO>() - .orderBy(true, true, "create_at").lambda().eq(ComActQuestnaireAnswerContentDO::getSubId, pageQuestnaireSubDetailsDTO.getSubId())); + IPage<ComActQuestnaireAnswerContentVO> doPager = + questnaireAnswerContentDAO.selectVoByPage(userPage, pageQuestnaireSubDetailsDTO); return R.ok(doPager); } @Override public R pageQuestnaireAnswer(PageQuestnaireAnswerDTO pageQuestnaireAnswerDTO) { + if(pageQuestnaireAnswerDTO.getPageNum()==null||pageQuestnaireAnswerDTO.getPageNum().longValue()==0){ + pageQuestnaireAnswerDTO.setPageNum(1l); + } + if(pageQuestnaireAnswerDTO.getPageSize()==null||pageQuestnaireAnswerDTO.getPageSize().longValue()==0){ + pageQuestnaireAnswerDTO.setPageSize(10l); + } Page userPage = new Page(pageQuestnaireAnswerDTO.getPageNum(), pageQuestnaireAnswerDTO.getPageSize()); IPage<ComActQuestnaireAnswerContentVO> doPager = questnaireAnswerContentDAO.selectAnswersByPage(userPage, pageQuestnaireAnswerDTO); return R.ok(doPager); } + + @Override + public R usersAnsweQuestnaire(UsersAnswerQuestnaireVO answerQuestnaireVO, Long userId) { + if (answerQuestnaireVO == null || answerQuestnaireVO.getAnswerContentVOList() == null) { + return R.fail(500, "数据为空!"); + } + ComActQuestnaireDO questnaireDO = questnaireDAO.selectById(answerQuestnaireVO.getQuestId()); + if (questnaireDO == null) { + return R.fail(500, "调查问卷不存在!"); + } + R r = userService.getUserInfoByUserId(userId+""); + if(!R.isOk(r)){ + return R.fail("用户信息错误"); + } + LoginUserInfoVO loginUserInfoVO = JSONObject.parseObject(JSONObject.toJSONString(r.getData()), LoginUserInfoVO.class); + Long userCommunityId = loginUserInfoVO.getCommunityId(); + if(questnaireDO.getCommunityId()!=null && questnaireDO.getCommunityId().longValue()!=userCommunityId.longValue()){ + return R.fail(500, "您目前所属社区无法填写当前社区的调查问卷,请切换社区后重试"); + } + LambdaQueryWrapper<ComActQuestnaireSubDO> subQuery = new LambdaQueryWrapper<ComActQuestnaireSubDO>(); + subQuery.eq(ComActQuestnaireSubDO::getQueId,answerQuestnaireVO.getQuestId()); + List<ComActQuestnaireSubDO> subDOS = questnaireSubDAO.selectList(subQuery); + List<Long> subIds = subDOS.stream().map(ComActQuestnaireSubDO::getId).collect(Collectors.toList()); + List<UserAnswerContentVO> answerContentVOList = answerQuestnaireVO.getAnswerContentVOList(); + List<ComActQuestnaireAnswerContentDO> answerContentDOList = Lists.newArrayList(); + for (UserAnswerContentVO answerContent:answerContentVOList) { + ComActQuestnaireAnswerContentDO answerContentDO = new ComActQuestnaireAnswerContentDO(); + boolean b = subIds.stream().anyMatch(a -> a.equals(answerContent.getSubId())); + if (!b) { + throw new ServiceException("500", "调查问卷题目不存在!"); + } + BeanUtils.copyProperties(answerContent,answerContentDO); + answerContentDO.setUserId(userId); + answerContentDOList.add(answerContentDO); + } + boolean batchAddSuccess = answerContentService.saveBatch(answerContentDOList); + if(batchAddSuccess) { + /** + * 更新回答用户数量 + */ + Integer joinCount = questnaireDO.getJoinCount(); + questnaireDO.setJoinCount(joinCount != null ? joinCount + 1 : 1); + questnaireDAO.updateById(questnaireDO); + } + return R.ok(); + } } -- Gitblit v1.7.1