diff --git a/src/main/java/com/xkrs/common/config/WebSecurityConfig.java b/src/main/java/com/xkrs/common/config/WebSecurityConfig.java index 1ba07b0..374cd60 100644 --- a/src/main/java/com/xkrs/common/config/WebSecurityConfig.java +++ b/src/main/java/com/xkrs/common/config/WebSecurityConfig.java @@ -36,6 +36,7 @@ class WebSecurityConfig extends WebSecurityConfigurerAdapter { .antMatchers("/global/configuration/**").permitAll() .antMatchers("/push/**").permitAll() .antMatchers("/queryFirePoint").permitAll() + .antMatchers("/queryNotice").permitAll()//查询通知 .antMatchers(HttpMethod.POST, "/api/user/updateSysUser").permitAll() .antMatchers(HttpMethod.GET, "/selectGlobalConfigDict").permitAll() .antMatchers(HttpMethod.GET, "/selectGlobalConfigValue").permitAll() diff --git a/src/main/java/com/xkrs/controller/NoticeController.java b/src/main/java/com/xkrs/controller/NoticeController.java new file mode 100644 index 0000000..e5bf4d6 --- /dev/null +++ b/src/main/java/com/xkrs/controller/NoticeController.java @@ -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(); + } + +} diff --git a/src/main/java/com/xkrs/dao/NoticeDao.java b/src/main/java/com/xkrs/dao/NoticeDao.java new file mode 100644 index 0000000..51dc1b5 --- /dev/null +++ b/src/main/java/com/xkrs/dao/NoticeDao.java @@ -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, JpaSpecificationExecutor { +} diff --git a/src/main/java/com/xkrs/model/entity/NoticeEntity.java b/src/main/java/com/xkrs/model/entity/NoticeEntity.java new file mode 100644 index 0000000..aa8cafc --- /dev/null +++ b/src/main/java/com/xkrs/model/entity/NoticeEntity.java @@ -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 + + '}'; + } +} diff --git a/src/main/java/com/xkrs/service/NoticeService.java b/src/main/java/com/xkrs/service/NoticeService.java new file mode 100644 index 0000000..51a0796 --- /dev/null +++ b/src/main/java/com/xkrs/service/NoticeService.java @@ -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(); + +} diff --git a/src/main/java/com/xkrs/service/impl/NoticeServiceImpl.java b/src/main/java/com/xkrs/service/impl/NoticeServiceImpl.java new file mode 100644 index 0000000..672a5fb --- /dev/null +++ b/src/main/java/com/xkrs/service/impl/NoticeServiceImpl.java @@ -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 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 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); + } + +}