From c5fb9ebd55fb0c0b54490eca3181cc6e95bb8949 Mon Sep 17 00:00:00 2001 From: yaotongjia <3195739570@qq.com> Date: Wed, 1 Apr 2020 17:29:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A7=9A=E7=AB=A5=E4=BD=B3=EF=BC=9A4=E6=9C=881?= =?UTF-8?q?=E5=8F=B7=20=E5=9C=A8entity=E5=8C=85=E4=B8=8B=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E6=97=A5=E5=BF=97consume=E7=9A=84=E5=AE=9E=E4=BD=93?= =?UTF-8?q?=E7=B1=BB=20=E5=9C=A8service=E5=8C=85=E4=B8=8B=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E4=BA=86=E6=8E=A5=E5=8F=A3ConsumeService=E5=9C=A8?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=B8=AD=E7=BC=96=E5=86=99=E4=BA=86=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=9F=A5=E8=AF=A2=E5=92=8C=E5=88=A0=E9=99=A4=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95=20=E5=9C=A8impl=E5=8C=85=E4=B8=8B=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E4=BA=86=E6=8E=A5=E5=8F=A3=E5=AE=9E=E7=8E=B0=E7=B1=BB?= =?UTF-8?q?ConsumeServiceImpl=E5=AE=9E=E7=8E=B0=E4=BA=86=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=A0=E9=99=A4=E7=9A=84=E6=96=B9=E6=B3=95?= =?UTF-8?q?=20=E5=9C=A8controller=E5=8C=85=E4=B8=8B=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E4=BA=86ConsumeController=E7=BC=96=E5=86=99=E4=BA=86=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=A2=9E=E5=8A=A0=E5=88=A0=E9=99=A4=E7=9A=84=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ysd/controller/ConsumeController.java | 59 +++++++++++++++++ src/main/java/com/ysd/entity/Consume.java | 37 +++++++++++ .../com/ysd/repository/ConsumeRepository.java | 10 +++ .../java/com/ysd/service/ConsumeService.java | 28 +++++++++ .../ysd/service/impl/ConsumeServiceImpl.java | 63 +++++++++++++++++++ 5 files changed, 197 insertions(+) create mode 100644 src/main/java/com/ysd/controller/ConsumeController.java create mode 100644 src/main/java/com/ysd/entity/Consume.java create mode 100644 src/main/java/com/ysd/repository/ConsumeRepository.java create mode 100644 src/main/java/com/ysd/service/ConsumeService.java create mode 100644 src/main/java/com/ysd/service/impl/ConsumeServiceImpl.java diff --git a/src/main/java/com/ysd/controller/ConsumeController.java b/src/main/java/com/ysd/controller/ConsumeController.java new file mode 100644 index 0000000..b358ca2 --- /dev/null +++ b/src/main/java/com/ysd/controller/ConsumeController.java @@ -0,0 +1,59 @@ +package com.ysd.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.web.bind.annotation.DeleteMapping; +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.RestController; + +import com.ysd.entity.Consume; +import com.ysd.entity.Readroom; +import com.ysd.service.ConsumeService; +import com.ysd.util.FenyeUtil; + +@RestController +@RequestMapping("/consume") +public class ConsumeController { + + @Autowired + private ConsumeService consumeService; + @GetMapping + public FenyeUtil findByRcardPage(FenyeUtil fenye,Consume c,String rcard,Integer page,Integer limit){ + fenye.setT(c); + Pageable pageable = PageRequest.of(page-1, limit); + Page findByRcardPage = consumeService.findByRcardPage(c, rcard, pageable); + List content = findByRcardPage.getContent(); + long count = findByRcardPage.getTotalElements(); + fenye.setData(content); + fenye.setCount(count); + fenye.setCode(0); + fenye.setMsg(""); + return fenye; + } + + @PostMapping + public Consume addConsume(Consume c,Integer rid) { + Readroom readroom = new Readroom(); + readroom.setRid(rid); + c.setReadroom(readroom); + return consumeService.addConsume(c); + } + + @DeleteMapping + public Integer deleteConsume(Consume c,Integer cid) { + try { + c.setCid(cid); + consumeService.deleteConsume(c); + return 1; + } catch (Exception e) { + // TODO: handle exception + return 0; + } + } +} diff --git a/src/main/java/com/ysd/entity/Consume.java b/src/main/java/com/ysd/entity/Consume.java new file mode 100644 index 0000000..171e280 --- /dev/null +++ b/src/main/java/com/ysd/entity/Consume.java @@ -0,0 +1,37 @@ +package com.ysd.entity; + +import java.util.Date; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +import org.springframework.format.annotation.DateTimeFormat; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +@Data +@Entity +@AllArgsConstructor +@NoArgsConstructor +@Table(name = "consume") +public class Consume { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer cid; + private String rcard; + private Integer rstatus; + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date rinttime; + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date outtime; + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "rid") + private Readroom readroom; +} diff --git a/src/main/java/com/ysd/repository/ConsumeRepository.java b/src/main/java/com/ysd/repository/ConsumeRepository.java new file mode 100644 index 0000000..d056069 --- /dev/null +++ b/src/main/java/com/ysd/repository/ConsumeRepository.java @@ -0,0 +1,10 @@ +package com.ysd.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +import com.ysd.entity.Consume; + +public interface ConsumeRepository extends JpaRepository,JpaSpecificationExecutor { + +} diff --git a/src/main/java/com/ysd/service/ConsumeService.java b/src/main/java/com/ysd/service/ConsumeService.java new file mode 100644 index 0000000..2a15d43 --- /dev/null +++ b/src/main/java/com/ysd/service/ConsumeService.java @@ -0,0 +1,28 @@ +package com.ysd.service; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; + +import com.ysd.entity.Consume; + +public interface ConsumeService { + /** + * 根据卡号进行模糊查询 + * @param c 记录信息 + * @param rcard 卡号 + * @param pageable + * @return 结果集 + */ + Page findByRcardPage(Consume c,String rcard,Pageable pageable); + /** + * 添加 + * @param c 记录信息 + * @return 受影响的行 + */ + Consume addConsume(Consume c); + /** + * 删除 + * @param c 记录信息 + */ + void deleteConsume(Consume c); +} diff --git a/src/main/java/com/ysd/service/impl/ConsumeServiceImpl.java b/src/main/java/com/ysd/service/impl/ConsumeServiceImpl.java new file mode 100644 index 0000000..b433656 --- /dev/null +++ b/src/main/java/com/ysd/service/impl/ConsumeServiceImpl.java @@ -0,0 +1,63 @@ +package com.ysd.service.impl; + +import java.util.List; + +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Expression; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; +import javax.transaction.Transactional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.domain.Specification; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; + +import com.ysd.entity.Consume; +import com.ysd.repository.ConsumeRepository; +import com.ysd.service.ConsumeService; +@Service +public class ConsumeServiceImpl implements ConsumeService { + + @Autowired + private ConsumeRepository consumeRepository; + @Override + public Page findByRcardPage(Consume c,String rcard, Pageable pageable) { + // TODO Auto-generated method stub + return consumeRepository.findAll(get(rcard), pageable); + } + + private Specification get(String rcard) { + // TODO Auto-generated method stub + return new Specification() { + + @Override + public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) { + Predicate predicate = criteriaBuilder.conjunction(); + List> expression = predicate.getExpressions(); + if(!StringUtils.isEmpty(rcard)) { + expression.add(criteriaBuilder.like(root.get("rcard"), "%"+rcard+"%")); + } + return predicate; + } + }; + } + + @Transactional + @Override + public Consume addConsume(Consume c) { + // TODO Auto-generated method stub + return consumeRepository.save(c); + } + + @Override + public void deleteConsume(Consume c) { + // TODO Auto-generated method stub + consumeRepository.delete(c); + + } + +} -- Gitee