优化
This commit is contained in:
parent
424a234582
commit
e14ca02cb6
@ -1,17 +1,30 @@
|
||||
package com.xkrs.straw.service.impl;
|
||||
|
||||
import com.xkrs.common.encapsulation.PromptMessageEnum;
|
||||
import com.xkrs.straw.dao.NoticeDao;
|
||||
import com.xkrs.straw.dao.NoticeRelaDao;
|
||||
import com.xkrs.straw.dao.SysUserDao;
|
||||
import com.xkrs.straw.model.entity.NoticeEntity;
|
||||
import com.xkrs.straw.model.entity.NoticeRelaEntity;
|
||||
import com.xkrs.straw.model.entity.SysUserEntity;
|
||||
import com.xkrs.straw.service.NoticeService;
|
||||
import com.xkrs.utils.TokenUtil;
|
||||
import org.apache.hc.core5.util.TextUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
|
||||
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
|
||||
|
||||
/**
|
||||
* 公告栏通知接口
|
||||
@ -37,7 +50,13 @@ public class NoticeServiceImpl implements NoticeService {
|
||||
*/
|
||||
@Override
|
||||
public String insertNotice(String token, NoticeEntity insertQo) {
|
||||
return null;
|
||||
NoticeEntity noticeEntity = new NoticeEntity();
|
||||
noticeEntity.setTitle(insertQo.getTitle());
|
||||
noticeEntity.setContent(insertQo.getContent());
|
||||
noticeEntity.setAuthor(insertQo.getAuthor());
|
||||
noticeEntity.setState(insertQo.getState());
|
||||
noticeDao.save(noticeEntity);
|
||||
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "添加成功", locale);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -45,7 +64,29 @@ public class NoticeServiceImpl implements NoticeService {
|
||||
*/
|
||||
@Override
|
||||
public String deleteNotice(String token, Long id) {
|
||||
return null;
|
||||
if (TextUtils.isEmpty(token) || id == null) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "参数错误", locale);
|
||||
}
|
||||
// 验证token
|
||||
String tokenUserName = TokenUtil.obtainUserNameByToken(token);
|
||||
SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
|
||||
if (sysUserEntity == null) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale);
|
||||
}
|
||||
if (!sysUserEntity.getAccountType().equals("管理员")) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "权限错误", locale);
|
||||
}
|
||||
noticeDao.deleteById(id);
|
||||
Specification<NoticeRelaEntity> specification = (root, criteriaQuery, criteriaBuilder) -> {
|
||||
List<Predicate> predicateList = new ArrayList<>();
|
||||
predicateList.add(criteriaBuilder.equal(root.get("noticeId").as(Long.class), id));
|
||||
return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
|
||||
};
|
||||
List<NoticeRelaEntity> list = noticeRelaDao.findAll(specification);
|
||||
if (list.size() > 0) {
|
||||
noticeRelaDao.deleteAll(list);
|
||||
}
|
||||
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "删除成功", locale);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -53,7 +94,45 @@ public class NoticeServiceImpl implements NoticeService {
|
||||
*/
|
||||
@Override
|
||||
public String updateNotice(String token, NoticeEntity updateQo) {
|
||||
return null;
|
||||
if (TextUtils.isEmpty(token)) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "参数错误", locale);
|
||||
}
|
||||
// 验证token
|
||||
String tokenUserName = TokenUtil.obtainUserNameByToken(token);
|
||||
SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
|
||||
if (sysUserEntity == null) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale);
|
||||
}
|
||||
if (!sysUserEntity.getAccountType().equals("管理员")) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "权限错误", locale);
|
||||
}
|
||||
Long id = updateQo.getId();
|
||||
String title = updateQo.getTitle();
|
||||
String content = updateQo.getContent();
|
||||
String author = updateQo.getAuthor();
|
||||
Integer state = updateQo.getState();
|
||||
if (id == null) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "参数错误", locale);
|
||||
}
|
||||
Optional<NoticeEntity> targetOptional = noticeDao.findById(id);
|
||||
if (targetOptional.isEmpty()) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "通知不存在", locale);
|
||||
}
|
||||
NoticeEntity target = targetOptional.get();
|
||||
if (!TextUtils.isEmpty(title)) {
|
||||
target.setTitle(title);
|
||||
}
|
||||
if (!TextUtils.isEmpty(content)) {
|
||||
target.setContent(content);
|
||||
}
|
||||
if (!TextUtils.isEmpty(author)) {
|
||||
target.setAuthor(author);
|
||||
}
|
||||
if (state != null) {
|
||||
target.setState(state);
|
||||
}
|
||||
noticeDao.save(target);
|
||||
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "更新成功", locale);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -61,7 +140,48 @@ public class NoticeServiceImpl implements NoticeService {
|
||||
*/
|
||||
@Override
|
||||
public String queryNotice(String token) {
|
||||
return null;
|
||||
if (TextUtils.isEmpty(token)) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "参数错误", locale);
|
||||
}
|
||||
// 验证token
|
||||
String tokenUserName = TokenUtil.obtainUserNameByToken(token);
|
||||
SysUserEntity sysUserEntity = sysUserDao.selectByUserName(tokenUserName);
|
||||
if (sysUserEntity == null) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.USER_LOGIN_ERROR, "您还没有注册登录,请先注册登录", locale);
|
||||
}
|
||||
Specification<NoticeRelaEntity> specification = (root, criteriaQuery, criteriaBuilder) -> {
|
||||
List<Predicate> predicateList = new ArrayList<>();
|
||||
predicateList.add(criteriaBuilder.equal(root.get("userId").as(Long.class), sysUserEntity.getId()));
|
||||
predicateList.add(criteriaBuilder.equal(root.get("state").as(Integer.class), 1));
|
||||
return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
|
||||
};
|
||||
//该用户的已读公告关联表
|
||||
List<NoticeRelaEntity> readNoticeRelaList = noticeRelaDao.findAll(specification);
|
||||
//系统的全部公告
|
||||
List<NoticeEntity> noticeList = noticeDao.findAll(Sort.by(Sort.Direction.DESC, "id"));
|
||||
List<NoticeEntity> resultList = new ArrayList<>();
|
||||
for (NoticeEntity notice : noticeList) {
|
||||
Long noticeId = notice.getId();
|
||||
if (!ifRead(readNoticeRelaList, noticeId)) {
|
||||
resultList.add(notice);
|
||||
}
|
||||
}
|
||||
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, resultList, locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断noticeId对应的公告栏通知是否已读
|
||||
*/
|
||||
private boolean ifRead(List<NoticeRelaEntity> readNoticeRelaList, Long noticeId) {
|
||||
if (readNoticeRelaList == null || readNoticeRelaList.size() == 0) {
|
||||
return false;
|
||||
}
|
||||
for (NoticeRelaEntity readNoticeRela : readNoticeRelaList) {
|
||||
if (readNoticeRela.getNoticeId().longValue() == noticeId.longValue()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user