优化 添加通知栏

This commit is contained in:
liuchengqian 2023-03-09 09:47:35 +08:00
parent e3ab65e0dd
commit 96a1b9455e
6 changed files with 257 additions and 0 deletions

View File

@ -36,6 +36,7 @@ class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers("/global/configuration/**").permitAll() .antMatchers("/global/configuration/**").permitAll()
.antMatchers("/push/**").permitAll() .antMatchers("/push/**").permitAll()
.antMatchers("/queryFirePoint").permitAll() .antMatchers("/queryFirePoint").permitAll()
.antMatchers("/queryNotice").permitAll()//查询通知
.antMatchers(HttpMethod.POST, "/api/user/updateSysUser").permitAll() .antMatchers(HttpMethod.POST, "/api/user/updateSysUser").permitAll()
.antMatchers(HttpMethod.GET, "/selectGlobalConfigDict").permitAll() .antMatchers(HttpMethod.GET, "/selectGlobalConfigDict").permitAll()
.antMatchers(HttpMethod.GET, "/selectGlobalConfigValue").permitAll() .antMatchers(HttpMethod.GET, "/selectGlobalConfigValue").permitAll()

View File

@ -0,0 +1,31 @@
package com.xkrs.controller;
import com.xkrs.model.entity.NoticeEntity;
import com.xkrs.service.NoticeService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@RestController
public class NoticeController {
@Resource
private NoticeService noticeService;
/**
* 更新通知
*/
@PostMapping("/updateNotice")
public String updateNotice(@RequestHeader(value = "Authorization") String token, @RequestBody NoticeEntity updateQo) {
return noticeService.updateNotice(token, updateQo);
}
/**
* 查询通知
*/
@GetMapping("/queryNotice")
public String queryNotice() {
return noticeService.queryNotice();
}
}

View File

@ -0,0 +1,10 @@
package com.xkrs.dao;
import com.xkrs.model.entity.NoticeEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Component;
@Component
public interface NoticeDao extends JpaRepository<NoticeEntity, Long>, JpaSpecificationExecutor<NoticeEntity> {
}

View File

@ -0,0 +1,97 @@
package com.xkrs.model.entity;
import javax.persistence.*;
import java.io.Serializable;
/**
* 公告栏通知
*/
@Entity
@Table(name = "notice")
public class NoticeEntity implements Serializable {
/**
* 指定主键建立自增序列主键值取自序列
*/
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "notice_seq_gen")
@SequenceGenerator(name = "notice_seq_gen", sequenceName = "notice_id_seq", allocationSize = 1)
private Long id;
/**
* 标题
*/
private String title;
/**
* 内容
*/
@Column(length = 40960, columnDefinition = "varchar(40960)")
private String content;
/**
* 作者
*/
private String author;
/**
* 公告栏通知的状态
* 0正常
* 1禁用
*/
private Integer state;
public NoticeEntity() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
@Override
public String toString() {
return "NoticeEntity{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
", author='" + author + '\'' +
", state=" + state +
'}';
}
}

View File

@ -0,0 +1,20 @@
package com.xkrs.service;
import com.xkrs.model.entity.NoticeEntity;
/**
* 公告栏通知接口
*/
public interface NoticeService {
/**
* 更新通知
*/
String updateNotice(String token, NoticeEntity updateQo);
/**
* 查询通知
*/
String queryNotice();
}

View File

@ -0,0 +1,98 @@
package com.xkrs.service.impl;
import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.common.tool.TokenUtil;
import com.xkrs.dao.NoticeDao;
import com.xkrs.dao.SysUserDao;
import com.xkrs.model.entity.NoticeEntity;
import com.xkrs.model.entity.SysUserEntity;
import com.xkrs.service.NoticeService;
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.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
/**
* 公告栏通知接口
*/
@Service
public class NoticeServiceImpl implements NoticeService {
public static Logger log = LoggerFactory.getLogger(NoticeServiceImpl.class);
private final Locale locale = LocaleContextHolder.getLocale();
@Resource
private NoticeDao noticeDao;
@Resource
private SysUserDao sysUserDao;
/**
* 更新通知
*/
@Override
public String updateNotice(String token, NoticeEntity updateQo) {
if (TextUtils.isEmpty(token)) {
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "参数错误", locale);
}
// 验证token
String tokenUserName = TokenUtil.getTokenUserName(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);
}
/**
* 查询通知
*/
@Override
public String queryNotice() {
List<NoticeEntity> noticeList = noticeDao.findAll(Sort.by(Sort.Direction.DESC, "id"));
if (noticeList.size() > 0) {
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, noticeList.get(0), locale);
}
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, null, locale);
}
}