package com.qxueyou.scc.controller; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.qxueyou.scc.admin.classes.dao.ClassDAO; import com.qxueyou.scc.admin.classes.model.ClsClass; import com.qxueyou.scc.admin.classes.service.IClassService; import com.qxueyou.scc.admin.notice.dao.NoticeDAO; import com.qxueyou.scc.admin.notice.service.INoticeService; import com.qxueyou.scc.base.model.Pager; import com.qxueyou.scc.base.model.Result; import com.qxueyou.scc.base.util.ClientUtils; import com.qxueyou.scc.base.util.CollectionUtils; import com.qxueyou.scc.base.util.QBeanUtils; import com.qxueyou.scc.notice.model.Notice; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; @Api(tags = "公告接口-教师端") @RestController @RequestMapping(value = "/admin/notice") public class NoticeController { @Autowired IClassService classService; @Autowired INoticeService noticeService; @Autowired NoticeDAO dao; @Autowired ClassDAO classDao; /** * 显示 公告列表 * * @param keyword * @param pageNum * @param pageSize * @return */ @ApiOperation(value = "公告列表", notes = "获取班级列表及班级所在公告列表") @GetMapping(value = "list") public Result list(String keyword, Integer pageNum, Integer pageSize) { if (!ClientUtils.isAdmin()) { // 获取班级列表及班级所在公告列表 List noticeIds = new ArrayList(10); List clsLst = classService.getClassLstByTeacherId(); for (ClsClass cls : clsLst) { noticeIds.addAll(dao.getClassNotices(cls.getClassId())); } List notices = noticeService.readByStatus(noticeIds, Notice.STATUS_ISSUED); List> lst = QBeanUtils.listBean2ListMap(notices, CollectionUtils.newStringMap( "noticeId", "noticeId", "title", "name", "noticeTypeName", "type", "issuedTime", "releaseTime")); if(lst == null) { return new Result(true, "success", CollectionUtils.newObjectMap("noticeCount", 0, "noticeLst", new ArrayList<>())); } for (Map map : lst) { map.put("isView", dao.isView((String) map.get("noticeId"), ClientUtils.getUserId())); map.put("classCount", dao.getLiveClasses((String) map.get("noticeId")).size()); map.put("noticeTypeName", this.getTypeName((String)map.get("type"))); } return new Result(true, "success", CollectionUtils.newObjectMap("noticeCount", lst.size(), "noticeLst", lst)); } else { return new Result(true, "success", CollectionUtils.newObjectMap("noticeLst", Collections.EMPTY_LIST)); } } /** * 显示 公告管理列表 * @param keyword * @param pageNum * @param pageSize * @return */ @ApiOperation(value = "公告管理列表", notes = "") @GetMapping(value = "managerLst") public Result managerLst(String keyword, Pager pager, @RequestParam(defaultValue = "desc") String sort) { String keyword_ = StringUtils.isBlank(keyword) ? "" : keyword; List notices = noticeService.list(keyword_, pager.getPageNum(), pager.getPageSize(), ClientUtils.getUserId(), sort); int count = noticeService.listCount(keyword_, ClientUtils.getUserId()); List> lst = QBeanUtils.listBean2ListMap(notices, CollectionUtils.newStringMap( "noticeId", "noticeId", "title", "name", "status", "status", "noticeTypeName", "type", "issuedTime", "releaseTime" )); for (Map map : lst) { List classIds = dao.getLiveClasses((String) map.get("noticeId")); map.put("classCount", classIds.size()); map.put("studentCount", classIds.size() > 0?classDao.getStudentCount(classIds):0); map.put("readCount", dao.getReadCount((String) map.get("noticeId"))); map.put("noticeTypeName", this.getTypeName((String)map.get("type"))); } return new Result(true, "success", CollectionUtils.newObjectMap("noticeCount", count, "noticeLst", lst )); } private String getTypeName(String key) { Map typeMap = new HashMap<>(); typeMap.put("homework", "作业公告"); typeMap.put("exam", "考试公告"); typeMap.put("other", "其他公告"); typeMap.put("class", "上课公告"); return typeMap.get(key); } /** * 发布公告/多个id已逗号隔开 * * @param noticeIds 公告ids * @return */ @ApiOperation(value = "发布公告", notes = "多个id已逗号隔开") @ApiImplicitParams({ @ApiImplicitParam(name = "noticeIds", value = "多个id已逗号隔开", required = true, paramType="query", dataType = "String") }) @PostMapping(value = "release") public Result release(String noticeIds) { return noticeService.doIssue(noticeIds.split(",")); } /** * 取消发布/多个id已逗号隔开 * * @param noticeIds 公告ids * @return */ @ApiOperation(value = "取消发布", notes = "多个id已逗号隔开") @ApiImplicitParams({ @ApiImplicitParam(name = "noticeIds", value = "多个id已逗号隔开", required = true, paramType="query", dataType = "String"), }) @PostMapping(value = "cancel") public Result cancel(String noticeIds) { return noticeService.doCancel(noticeIds.split(",")); } /** * 删除 公告/多个id已逗号隔开 * * @param noticeIds 公告ids * @return */ @ApiOperation(value = "删除公告", notes = "多个id已逗号隔开") @ApiImplicitParams({ @ApiImplicitParam(name = "noticeIds", value = "多个id已逗号隔开", required = true, paramType="query", dataType = "String"), }) @PostMapping(value = "delete") public Result delete(String noticeIds) { return noticeService.delete(noticeIds.split(",")); } /** * 新增/更新 * * @param noticeId * @param name * @param content * @param type * @param classIds * @param pathUrl * @return */ @ApiOperation(value = "新增/更新公告", notes = "noticeId存在则更新,否则新增") @ApiImplicitParams({ @ApiImplicitParam(name = "name", value = "公告标题", required = true, paramType = "query", dataType = "String"), @ApiImplicitParam(name = "content", value = "公告内容", required = true, paramType = "query", dataType = "String"), @ApiImplicitParam(name = "type", value = "公告类型名称", required = true, paramType = "query", dataType = "String"), @ApiImplicitParam(name = "classIds", value = "关联的班级id,逗号组装", required = true, paramType = "query", dataType = "String") }) @PostMapping(value = "addOrUpdate") public Result addOrUpdate(String noticeId, String name, String content, String type, String classIds, String pathUrl) { if (StringUtils.isEmpty(noticeId)) { return noticeService.add(name, content, type, CollectionUtils.newList(String.class, classIds.split(","))); } else { return noticeService.update(noticeId, name, content, type, CollectionUtils.newList(String.class, classIds.split(","))); } } /** * 获取公告详情 * * @param noticeId * @return */ @ApiOperation(value = "获取公告详情", notes = "") @ApiImplicitParams({ @ApiImplicitParam(name = "noticeId", value = "公告id", required = true, paramType = "query", dataType = "String") }) @GetMapping(value = "detail") public Result detail(String noticeId) { Notice notice = noticeService.read(noticeId); List classIds = dao.getLiveClasses(noticeId); String [] classNames = null; if(classIds!=null && classIds.size()>0){ classNames= classService.queryClassNamesByIds(classIds.toArray(new String [classIds.size()])); } return new Result(true, "success", CollectionUtils.newObjectMap( "noticeName", notice.getTitle(), "type", notice.getNoticeTypeName(), "createTime", notice.getCreateTime(), "remark", notice.getContent(), "fullPath", notice.getImgPath(), "creator", notice.getCreator(), "classIds", classIds, "classNames", classNames )); } @ApiOperation(value = "获取班级列表", notes = "") @GetMapping(value = "findClassLst") public Result findClassLst() { if (ClientUtils.isAdmin()) { List> clsLst = classService.getAllClassLst().getDataT("classLst"); return new Result(true, "操作成功", QBeanUtils.listBean2ListMap(clsLst, CollectionUtils.newStringMap( "classId", "classId", "className", "className" ))); } else { List clsLst = classService.getClassLstByTeacherId(); return new Result(true, "操作成功", QBeanUtils.listBean2ListMap(clsLst, CollectionUtils.newStringMap( "classId", "classId", "name", "className" ))); } } @ApiOperation(value = "更新公告查看次数", notes = "") @ApiImplicitParams({ @ApiImplicitParam(name = "noticeId", value = "公告id", required = false, paramType = "query", dataType = "String") }) @GetMapping(value = "view") public Result view(String noticeId) { dao.view(noticeId, ClientUtils.getUserId()); return Result.SUCCESS; } }