diff --git a/src/main/java/com/mobile_education_platform/controller/NoticeController.java b/src/main/java/com/mobile_education_platform/controller/NoticeController.java index 8897e54e35893c109b162d610fe54c13cccce5af..5f67dafad8ff537499b59ce8eb4067b45b45d1a7 100644 --- a/src/main/java/com/mobile_education_platform/controller/NoticeController.java +++ b/src/main/java/com/mobile_education_platform/controller/NoticeController.java @@ -1,8 +1,11 @@ package com.mobile_education_platform.controller; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.hazelcast.internal.json.Json; import com.mobile_education_platform.common.R; import com.mobile_education_platform.pojo.*; import com.mobile_education_platform.pojo.Class; @@ -61,7 +64,19 @@ public class NoticeController { @Autowired public LoginService loginService; + /** + * 功能: 对系统消息进行查询,通过userNumber和学期来进行筛选,最终返回与其所关联的班级的通知 + * @author: ChenZiKang + * @Created: 2021/10/28 8:09 + * @param id userNumber + * @param schoolSemester 学期 + * @return R + */ @ApiOperation(value = "系统消息-查询接口", notes = "查询本人参与的逻辑删除字段不为0的班级的通知,以学期为条件来排序查询。") + @ApiImplicitParams({ + @ApiImplicitParam(name = "用户ID",value = "id",paramType = "string"), + @ApiImplicitParam(name = "学期",value = "schoolSemester",paramType = "string") + }) @RequestMapping(value = "/notices/{schoolSemester}/{id}", method = RequestMethod.GET) public R inNotice(@PathVariable("id") String id, @PathVariable("schoolSemester") String schoolSemester) { @@ -134,33 +149,60 @@ public class NoticeController { return R.error("查询失败!请检查资料是否关联!"); } + /** + * 功能: 当教师发送通知后,对通知所关联的班级(支持批量)进行学生群发 + * @author: ChenZiKang + * @Created: 2021/10/28 8:13 + * @param notice 通知 + * @param classIds 班级ID + * @return R + */ @ApiOperation(value = "发送通知-添加接口") @RequestMapping(value = "/notice/{classId}", method = RequestMethod.POST) public R addNotice(@RequestBody Notice notice, - @PathVariable("classId") String classId) { + List classIds) throws IOException { boolean saveNotice = noticeService.save(notice); if (!saveNotice) { return R.error("保存通知失败!请检查是否有误!"); } - ClassNotice classNotice = new ClassNotice(); - classNotice.setNoticeId(notice.getId()); - classNotice.setClassId(classId); - boolean saveClassNotice = classNoticeService.save(classNotice); - if (!saveClassNotice) { - return R.error("请检查班级是否有误!"); - } - List studentClassList = studentClassService.listByClassId(classId); - List studentIds = new ArrayList<>(); - for (StudentClass studentClass : studentClassList) { - studentIds.add(studentClass.getStudentId()); + for(String classId:classIds){ + ClassNotice classNotice = new ClassNotice(); + classNotice.setNoticeId(notice.getId()); + classNotice.setClassId(classId); + boolean saveClassNotice = classNoticeService.save(classNotice); + if (!saveClassNotice) { + return R.error("请检查班级是否有误!"); + } + List studentClassList = studentClassService.listByClassId(classId); + List studentIds = new ArrayList<>(); + for (StudentClass studentClass : studentClassList) { + studentIds.add(studentClass.getStudentId()); + } + //调用群发功能,需要根据学生们的student_id来获取wechat_id进行单发(通知只支持一对一发送) + List studentWechatIds= userService.listWechatIdByUserNumber(studentIds); + for (String wechatId:studentWechatIds){ + publishMsg(wechatId); + } } - //调用后台 - //****** return R.ok("新发布通知已完成群发。"); } + /** + * 功能: 根据班级ID进行通知的查询,并进行分页 + * @author: ChenZiKang + * @Created: 2021/10/28 8:19 + * @param classId 班级ID + * @param pageNum 分页的页数 + * @param pageCount 分页的每页通知数量 + * @return R + */ @ApiOperation(value = "发送通知-查询接口") - @RequestMapping(value = "/notice/{id}/", method = RequestMethod.GET) + @ApiImplicitParams({ + @ApiImplicitParam(name = "班级ID",value = "classId",paramType = "string"), + @ApiImplicitParam(name = "页数总数",value = "pageNum",paramType = "integer"), + @ApiImplicitParam(name = "每页中通知数量",value = "pageCount",paramType = "integer") + }) + @RequestMapping(value = "/notice/{pageNum}/{pageCount}/{classId}", method = RequestMethod.GET) public R selectNotice(@PathVariable("classId") String classId, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageCount") Integer pageCount) { @@ -179,8 +221,15 @@ public class NoticeController { return R.ok("查询成功!",data); } + /** + * 功能: 根据通知ID进行逻辑删除 + * @author: ChenZiKang + * @Created: 2021/10/28 8:22 + * @param id 通知ID + * @return R + */ @ApiOperation(value = "发送通知-删除接口") - @ApiImplicitParam(name = "待删除的通知ID", value = "id", required = true, paramType = "string") + @ApiImplicitParam(name = "待删除的通知ID", value = "id",paramType = "string") @RequestMapping(value = "/notice/{id}", method = RequestMethod.DELETE) public R deleteNotice(@PathVariable("id") String id) { Notice notice = noticeService.getById(id); @@ -202,6 +251,14 @@ public class NoticeController { return data; } + /** + * 功能: 对单个用户进行消息的发送(小程序不支持群发) + * @author: ChenZiKang + * @Created: 2021/10/28 8:23 + * @param openId 用户的识别凭证 + * @return R + * @throws IOException + */ @ApiOperation("订阅消息群发") @ApiImplicitParam(name = "用户openId",value = "openId",paramType = "string") @RequestMapping(value = "/publishMsg",method = RequestMethod.GET) @@ -222,7 +279,17 @@ public class NoticeController { dataList.add(new TemplateDataVO(new Date().toString())); dataList.add(new TemplateDataVO("该教师发布了新通知,请及时查看。")); msgVo.setParams(dataList); - String s = noticeService.sendSubscribeMessage(msgVo); + String result = noticeService.sendSubscribeMessage(msgVo); + JSONObject sendResult= JSON.parseObject(result); + Integer errcode = (Integer) sendResult.get("errcode"); + switch (errcode){ + case 40003: + return R.error("openid不正确!"); + case 43101: + return R.error("用户取消了订阅!"); + default: + break; + } return R.ok("发布成功!"); } } diff --git a/src/main/java/com/mobile_education_platform/service/NoticeService.java b/src/main/java/com/mobile_education_platform/service/NoticeService.java index b05fe171a6a3c51db054bcb5fe86546b16d25cc8..32ce288301fdc01e908e50aa8d9cabce88eb5b55 100644 --- a/src/main/java/com/mobile_education_platform/service/NoticeService.java +++ b/src/main/java/com/mobile_education_platform/service/NoticeService.java @@ -39,5 +39,13 @@ public interface NoticeService extends IService { */ Page getNoticePage(int pageNum,int pageCount,String classId); + /** + * 功能: 根据传入的通知请求封装对象发送请求来群发通知 + * @author: 陈梓康 + * @Created: 2021/10/27 18:20 + * @param vo 通知请求封装对象 + * @return 请求是否成功的字符串(可以转为JSON) + * @throws IOException + */ String sendSubscribeMessage(WxMsgVO vo) throws IOException; } diff --git a/src/main/java/com/mobile_education_platform/service/UserService.java b/src/main/java/com/mobile_education_platform/service/UserService.java index 894d1e2bd3916dd72f41b3e13e9c7ed9a9fa436f..49d2a0c41e5e08a9a8cde6fc92ab6cca2df5d8bb 100644 --- a/src/main/java/com/mobile_education_platform/service/UserService.java +++ b/src/main/java/com/mobile_education_platform/service/UserService.java @@ -4,6 +4,7 @@ import com.mobile_education_platform.pojo.User; import com.baomidou.mybatisplus.extension.service.IService; import com.mobile_education_platform.pojo.vo.UserVO; +import java.util.List; import java.util.Map; /** @@ -51,4 +52,13 @@ public interface UserService extends IService { * @Date: 2021/10/27 9:01 **/ Boolean hasUserInfo(Integer role,String userNumber); + + /** + * 功能: 根据userNumber的List来获取WechatId的List + * @author: 陈梓康 + * @Created: 2021/10/27 18:46 + * @param userNumbers userNumber的List + * @return WechatId的List + */ + List listWechatIdByUserNumber(List userNumbers); } diff --git a/src/main/java/com/mobile_education_platform/service/impl/NoticeServiceImpl.java b/src/main/java/com/mobile_education_platform/service/impl/NoticeServiceImpl.java index 41a6b1982e11c591e249e0ce45a5c0e716f596b2..abf29355b3b4895d8ff9e7876c1bd2f4d832e74e 100644 --- a/src/main/java/com/mobile_education_platform/service/impl/NoticeServiceImpl.java +++ b/src/main/java/com/mobile_education_platform/service/impl/NoticeServiceImpl.java @@ -65,6 +65,14 @@ public class NoticeServiceImpl extends ServiceImpl impleme return baseMapper.selectPage(page,queryWrapper); } + /** + * 功能: 根据传入的通知请求封装对象发送请求来群发通知 + * @author: 陈梓康 + * @Created: 2021/10/27 18:20 + * @param vo 通知请求封装对象 + * @return 请求是否成功的字符串(可以转为JSON) + * @throws IOException + */ @Override public String sendSubscribeMessage(WxMsgVO vo) throws IOException { String info=""; diff --git a/src/main/java/com/mobile_education_platform/service/impl/UserServiceImpl.java b/src/main/java/com/mobile_education_platform/service/impl/UserServiceImpl.java index e68efe4fc3b4baa7fd12580fc54d5612f6df23ff..460fe53ce428da963222d3e1a100244e15617a19 100644 --- a/src/main/java/com/mobile_education_platform/service/impl/UserServiceImpl.java +++ b/src/main/java/com/mobile_education_platform/service/impl/UserServiceImpl.java @@ -4,10 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.mobile_education_platform.mapper.ParentMapper; import com.mobile_education_platform.mapper.StudentMapper; import com.mobile_education_platform.mapper.TeacherMapper; -import com.mobile_education_platform.pojo.Parent; -import com.mobile_education_platform.pojo.Student; -import com.mobile_education_platform.pojo.Teacher; -import com.mobile_education_platform.pojo.User; +import com.mobile_education_platform.pojo.*; import com.mobile_education_platform.mapper.UserMapper; import com.mobile_education_platform.pojo.vo.UserVO; import com.mobile_education_platform.service.UserService; @@ -15,9 +12,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; +import java.util.*; /** *

@@ -177,4 +172,23 @@ public class UserServiceImpl extends ServiceImpl implements Us throw new RuntimeException("用户身份异常,请联系管理员"); } } + + /** + * 功能: 根据usernumber的List来获取WechatId的List + * @author: 陈梓康 + * @Created: 2021/10/27 18:46 + * @param userNumbers usernumber的List + * @return WechatId的List + */ + @Override + public List listWechatIdByUserNumber(List userNumbers) { + List wechatId=new ArrayList<>(); + for(String userNumber:userNumbers){ + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("user_number",userNumber); + User user = baseMapper.selectOne(queryWrapper); + wechatId.add(user.getWechatId()); + } + return wechatId; + } } diff --git a/src/test/java/com/mobile_education_platform/MobileEducationPlatformApplicationTests.java b/src/test/java/com/mobile_education_platform/MobileEducationPlatformApplicationTests.java index 1cf187f3ca1d7daea86ae1ed68e9af60fbc69407..74c2450be8f675fcb6a4e4b52689975b74127a28 100644 --- a/src/test/java/com/mobile_education_platform/MobileEducationPlatformApplicationTests.java +++ b/src/test/java/com/mobile_education_platform/MobileEducationPlatformApplicationTests.java @@ -1,13 +1,31 @@ package com.mobile_education_platform; +import com.mobile_education_platform.pojo.User; +import com.mobile_education_platform.service.UserService; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import java.util.ArrayList; +import java.util.List; + @SpringBootTest class MobileEducationPlatformApplicationTests { + @Autowired + public UserService userService; + @Test void contextLoads() { + List userNumber=new ArrayList<>(); + userNumber.add("012020005"); + userNumber.add("012020008"); + userNumber.add("012020009"); + userNumber.add("012020010"); + List strings = userService.listWechatIdByUserNumber(userNumber); + for(String usernumber:strings){ + System.out.println(usernumber); + } } }