提交 00b27522 authored 作者: xc's avatar xc

分页

上级 72939942
package com.zjty.efs.bus.Dao; package com.zjty.efs.bus.Dao;
import com.zjty.efs.bus.entity.Attention; import com.zjty.efs.bus.entity.Attention;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface AttentionDao extends JpaRepository<Attention, Integer> { public interface AttentionDao extends JpaRepository<Attention, Integer> {
Page<Attention> findOutByUserId(int sender, Pageable pageable);
} }
...@@ -13,5 +13,5 @@ import java.util.List; ...@@ -13,5 +13,5 @@ import java.util.List;
public interface NoticeDao extends JpaRepository<Notice , Integer> { public interface NoticeDao extends JpaRepository<Notice , Integer> {
Notice findById(int id); Notice findById(int id);
Page<Notice> findOutBySenderAndType(int sender, int type, Pageable pageable); Page<Notice> findOutBySenderAndType(int sender, int type, Pageable pageable);
List<Notice> findOutBySenderAndStatusAndType(int sender, int status, int type);
} }
package com.zjty.efs.bus.controller; package com.zjty.efs.bus.controller;
import com.zjty.efs.bus.entity.Attention; import com.zjty.efs.bus.entity.Attention;
import com.zjty.efs.bus.entity.Paging;
import com.zjty.efs.misc.config.AutoDocument; import com.zjty.efs.misc.config.AutoDocument;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
...@@ -28,8 +29,8 @@ public class AttentionController { ...@@ -28,8 +29,8 @@ public class AttentionController {
@ApiImplicitParam(name = "pageSize", value = "条数", dataType = "int", paramType = "query", required = true) @ApiImplicitParam(name = "pageSize", value = "条数", dataType = "int", paramType = "query", required = true)
}) })
@PostMapping("/getAttention") @PostMapping("/getAttention")
public ResponseEntity<Page<Attention>> test(@RequestParam int userId, int pageNum, int pageSize){ public ResponseEntity<Paging<Attention>> test(@RequestParam int userId, int pageNum, int pageSize){
return ResponseEntity.ok(new PageImpl<>(new ArrayList<>())); return ResponseEntity.ok(new Paging<>());
} }
} }
package com.zjty.efs.bus.controller; package com.zjty.efs.bus.controller;
import com.alibaba.fastjson.JSONObject;
import com.zjty.efs.bus.entity.Notice; import com.zjty.efs.bus.entity.Notice;
import com.zjty.efs.bus.entity.Paging;
import com.zjty.efs.bus.service.NoticeService; import com.zjty.efs.bus.service.NoticeService;
import com.zjty.efs.misc.config.AutoDocument; import com.zjty.efs.misc.config.AutoDocument;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
...@@ -9,15 +9,10 @@ import io.swagger.annotations.ApiImplicitParam; ...@@ -9,15 +9,10 @@ import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@AutoDocument @AutoDocument
...@@ -68,13 +63,13 @@ public class NoticeController { ...@@ -68,13 +63,13 @@ public class NoticeController {
@ApiImplicitParam(name = "type", value = "通知类型", paramType = "query", required = true, example = "0", dataType = "int"), @ApiImplicitParam(name = "type", value = "通知类型", paramType = "query", required = true, example = "0", dataType = "int"),
@ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query", required = true, dataType = "int"), @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query", required = true, dataType = "int"),
@ApiImplicitParam(name = "pageSize", value = "条数", paramType = "query", required = true, dataType = "int")}) @ApiImplicitParam(name = "pageSize", value = "条数", paramType = "query", required = true, dataType = "int")})
public ResponseEntity<Page<Notice>> getNoticeList(@RequestParam int user, int type, int pageNum, int pageSize){ public ResponseEntity<Paging<Notice>> getNoticeList(@RequestParam int user, int type, int pageNum, int pageSize){
Page<Notice> data = noticeService.getReceiveList(user, type, pageNum, pageSize); Paging<Notice> data = noticeService.getReceiveList(user, type, pageNum, pageSize);
return ResponseEntity.ok(data); return ResponseEntity.ok(data);
} }
@PutMapping("/updateStatus") @PutMapping("/updateStatus")
@ApiOperation(value = "修改通知为已读") @ApiOperation(value = "修改通知为已读",notes = "status 未查看 0,已查看 1")
@ApiImplicitParams({@ApiImplicitParam(name = "notices", value = "通知列表", paramType = "body", required = true, @ApiImplicitParams({@ApiImplicitParam(name = "notices", value = "通知列表", paramType = "body", required = true,
allowMultiple = true, dataType = "Notice")}) allowMultiple = true, dataType = "Notice")})
public ResponseEntity updateStatus(@RequestBody List<Notice> notices){ public ResponseEntity updateStatus(@RequestBody List<Notice> notices){
......
...@@ -23,7 +23,7 @@ public class Attention { ...@@ -23,7 +23,7 @@ public class Attention {
*/ */
@ApiModelProperty(value = "id", example = "1") @ApiModelProperty(value = "id", example = "1")
@Id @Id
@GeneratedValue @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id") @Column(name = "id")
private int id; private int id;
......
...@@ -29,7 +29,7 @@ public class Notice { ...@@ -29,7 +29,7 @@ public class Notice {
*/ */
@ApiModelProperty(value = "id", example = "1") @ApiModelProperty(value = "id", example = "1")
@Id @Id
@GeneratedValue @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id") @Column(name = "id")
private int id; private int id;
...@@ -62,7 +62,7 @@ public class Notice { ...@@ -62,7 +62,7 @@ public class Notice {
private String message; private String message;
/** /**
* 通知状态 已查看 0,未查看 1,已下载 2 * 通知状态 未查看 0,已查看 1,已下载 2
*/ */
@ApiModelProperty(value = "status", example = "0", dataType = "int" ,name = "通知状态", @ApiModelProperty(value = "status", example = "0", dataType = "int" ,name = "通知状态",
notes = "未查看 0,已查看 1,已下载 2,默认 0") notes = "未查看 0,已查看 1,已下载 2,默认 0")
...@@ -72,8 +72,8 @@ public class Notice { ...@@ -72,8 +72,8 @@ public class Notice {
/** /**
* 通知类型 接受 0,发送 1,草稿 2 * 通知类型 接受 0,发送 1,草稿 2
*/ */
@ApiModelProperty(value = "type", example = "0", dataType = "int", name = "通知类型", @ApiModelProperty(value = "type", example = "1", dataType = "int", name = "通知类型",
notes = "接收 0,发送 1,草稿 2,默认 ") notes = "接收 0,发送 1,草稿 2,默认 1")
@Column(name = "type") @Column(name = "type")
private int type; private int type;
......
package com.zjty.efs.bus.entity;
import com.zjty.efs.misc.config.AutoDocument;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@AutoDocument
@ApiModel(value = "分页")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Paging<T> {
@ApiModelProperty(value = "total", example = "12", dataType = "int", name = "总数")
private int total;
@ApiModelProperty(value = "pageSize", example = "10", dataType = "int", name = "每页条数")
private int pageSize;
@ApiModelProperty(value = "pageNum", example = "1", dataType = "int", name = "页码", notes = "从1开始")
private int pageNum;
@ApiModelProperty(value = "data", example = "")
List<T> data;
}
package com.zjty.efs.bus.service; package com.zjty.efs.bus.service;
import com.zjty.efs.bus.entity.Attention; import com.zjty.efs.bus.entity.Attention;
import com.zjty.efs.bus.entity.Paging;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
public interface AttentionService { public interface AttentionService {
Page<Attention> getAttentionList(int userId, int pageNum, int pageSize); Paging<Attention> getAttentionList(int userId, int pageNum, int pageSize);
} }
package com.zjty.efs.bus.service; package com.zjty.efs.bus.service;
import com.alibaba.fastjson.JSONObject;
import com.zjty.efs.bus.entity.Notice; import com.zjty.efs.bus.entity.Notice;
import org.springframework.data.domain.Page; import com.zjty.efs.bus.entity.Paging;
import java.util.List; import java.util.List;
...@@ -37,7 +36,7 @@ public interface NoticeService { ...@@ -37,7 +36,7 @@ public interface NoticeService {
* *
* @return 通知json对象 * @return 通知json对象
*/ */
Page<Notice> getReceiveList(int user, int type, int pageNum, int pageSize); Paging<Notice> getReceiveList(int user, int type, int pageNum, int pageSize);
/** /**
* 修改通知为已读 * 修改通知为已读
...@@ -46,4 +45,14 @@ public interface NoticeService { ...@@ -46,4 +45,14 @@ public interface NoticeService {
boolean updateStatus(List<Notice> notices); boolean updateStatus(List<Notice> notices);
/**
* 获取当前用户未读的接收通知
* @param userId 当前用户
* @param status 0 未读
* @param type 0 接收通知
* @return
*/
List<Notice> findBySender(int userId, int status, int type);
} }
package com.zjty.efs.bus.service.impl; package com.zjty.efs.bus.service.impl;
import com.zjty.efs.bus.Dao.AttentionDao;
import com.zjty.efs.bus.entity.Attention; import com.zjty.efs.bus.entity.Attention;
import com.zjty.efs.bus.entity.Paging;
import com.zjty.efs.bus.service.AttentionService; import com.zjty.efs.bus.service.AttentionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
public class AttentionServiceImpl implements AttentionService { public class AttentionServiceImpl implements AttentionService {
@Autowired
AttentionDao attentionDao;
@Override @Override
public Page<Attention> getAttentionList(int userId, int pageNum, int pageSize) { public Paging<Attention> getAttentionList(int userId, int pageNum, int pageSize) {
return null; try {
Pageable pageable = PageRequest.of(pageNum - 1, pageSize, Sort.Direction.DESC,"updateTime");
Page<Attention> p = attentionDao.findOutByUserId(userId, pageable);
Paging<Attention> paging= new Paging<>();
paging.setTotal((int)p.getTotalElements());
paging.setPageNum(pageNum);
paging.setPageSize(pageSize);
paging.setData(p.getContent());
return paging;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
public boolean addAttention(int noticeId){
return true;
} }
} }
package com.zjty.efs.bus.service.impl; package com.zjty.efs.bus.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.zjty.efs.bus.Dao.NoticeDao; import com.zjty.efs.bus.Dao.NoticeDao;
import com.zjty.efs.bus.entity.Notice; import com.zjty.efs.bus.entity.Notice;
import com.zjty.efs.bus.entity.Paging;
import com.zjty.efs.bus.service.NoticeService; import com.zjty.efs.bus.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
...@@ -12,21 +12,29 @@ import org.springframework.data.domain.Pageable; ...@@ -12,21 +12,29 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@Service @Service
public class NoticeServiceImpl implements NoticeService { public class NoticeServiceImpl implements NoticeService {
@Autowired @Autowired
NoticeDao noticeDao; NoticeDao noticeDao;
@Override @Override
public boolean addNotice(Notice notice) { public boolean addNotice(Notice notice) {
try{ try{
noticeDao.save(notice); noticeDao.save(notice);
notice.setType(0);
noticeDao.save(notice); Notice notice1 = new Notice();
notice1.setType(0);
notice1.setTitle(notice.getTitle());
notice1.setAddressee(notice.getAddressee());
notice1.setDeadline(notice.getDeadline());
notice1.setFileList(notice.getFileList());
notice1.setMessage(notice.getMessage());
notice1.setNote(notice.getNote());
notice1.setSender(notice.getSender());
notice1.setStatus(notice.getStatus());
notice1.setUpdateTime(notice.getUpdateTime());
noticeDao.save(notice1);
return true; return true;
}catch (Exception e){ }catch (Exception e){
...@@ -51,7 +59,10 @@ public class NoticeServiceImpl implements NoticeService { ...@@ -51,7 +59,10 @@ public class NoticeServiceImpl implements NoticeService {
@Override @Override
public Notice getNotice(int id) { public Notice getNotice(int id) {
try { try {
return noticeDao.findById(id); Notice notice = noticeDao.findById(id);
notice.setStatus(1);
noticeDao.save(notice);
return notice;
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
return null; return null;
...@@ -60,11 +71,16 @@ public class NoticeServiceImpl implements NoticeService { ...@@ -60,11 +71,16 @@ public class NoticeServiceImpl implements NoticeService {
} }
@Override @Override
public Page<Notice> getReceiveList(int user, int type, int pageNum, int pageSize) { public Paging<Notice> getReceiveList(int user, int type, int pageNum, int pageSize) {
Pageable pageable = PageRequest.of(pageNum - 1, pageSize, Sort.Direction.DESC,"updateTime"); Pageable pageable = PageRequest.of(pageNum - 1, pageSize, Sort.Direction.DESC,"updateTime");
Page<Notice> p = noticeDao.findOutBySenderAndType(user, type, pageable); Page<Notice> p = noticeDao.findOutBySenderAndType(user, type, pageable);
return p; Paging<Notice> paging = new Paging<>();
paging.setData(p.getContent());
paging.setPageSize(pageSize);
paging.setPageNum(pageNum);
paging.setTotal((int)p.getTotalElements());
return paging;
} }
@Override @Override
...@@ -79,4 +95,16 @@ public class NoticeServiceImpl implements NoticeService { ...@@ -79,4 +95,16 @@ public class NoticeServiceImpl implements NoticeService {
return false; return false;
} }
} }
@Override
public List<Notice> findBySender(int userId, int status, int type) {
try{
return noticeDao.findOutBySenderAndStatusAndType(userId, status, type);
}catch (Exception e){
e.printStackTrace();
return null;
}
}
} }
...@@ -4,7 +4,7 @@ spring.datasource.url=jdbc:mysql://localhost:3306/efs-notice?useSSL=false&server ...@@ -4,7 +4,7 @@ spring.datasource.url=jdbc:mysql://localhost:3306/efs-notice?useSSL=false&server
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root spring.datasource.username=root
spring.datasource.password=root spring.datasource.password=root
spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.format_sql=false
spring.main.allow-bean-definition-overriding=true spring.main.allow-bean-definition-overriding=true
spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true spring.jpa.show-sql=true
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论