完成Register && Gird

This commit is contained in:
2019-06-03 18:22:02 +08:00
parent 9b5d2ec4e5
commit 7473c0e36a
36 changed files with 2051 additions and 943 deletions
@@ -1,18 +1,20 @@
package net.alawn.bill.controller;
import lombok.extern.slf4j.Slf4j;
import net.alawn.bill.entity.dto.NewUser;
import net.alawn.bill.security.AuthUser;
import net.alawn.bill.security.JwtAuthenticationRequest;
import net.alawn.bill.security.JwtAuthenticationResponse;
import net.alawn.bill.security.JwtTokenUtil;
import net.alawn.bill.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@@ -21,6 +23,7 @@ import javax.servlet.http.HttpServletRequest;
* @Description:
* @Date: Create in 09:59 2019-05-23
*/
@Slf4j
@RestController
public class AuthController {
@@ -30,6 +33,9 @@ public class AuthController {
@Autowired
private AuthService authService;
@Autowired
private JwtTokenUtil jwtTokenUtil;
@RequestMapping(value = "/auth", method = RequestMethod.POST)
public ResponseEntity<?> createAuthenticationToken(
@RequestBody JwtAuthenticationRequest authenticationRequest) throws AuthenticationException {
@@ -55,4 +61,12 @@ public class AuthController {
public NewUser register(@RequestBody NewUser addedUser) throws AuthenticationException, HttpRequestMethodNotSupportedException {
return authService.register(addedUser);
}
@RequestMapping(value = "/auth/valid", method = RequestMethod.POST)
public boolean valid(@RequestParam String token) throws AuthenticationException, HttpRequestMethodNotSupportedException {
log.info("token:" + token);
// AuthUser authUser = (AuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
boolean rs = jwtTokenUtil.isTokenExpired(token);
return !rs;
}
}
@@ -11,9 +11,12 @@ import net.alawn.bill.security.AuthUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
/**
@@ -27,6 +30,7 @@ import java.util.List;
@RequestMapping(value = "/bill")
@RestController
public class BillController {
final static double feeRate = 0.08;
@Autowired
BookRepo bookRepo;
@@ -38,22 +42,31 @@ public class BillController {
UserRepo userRepo;
@RequestMapping(value = "/books",method = RequestMethod.POST)
@Transactional(rollbackFor=Exception.class)
public ResponseEntity<Book> newBook(@RequestBody NewBill newBill){
AuthUser authUser = (AuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
long userId = authUser.getId();
log.info("newBill:" + newBill);
Book book = newBill.toBook();
book.setUser(new User(userId));
log.info("book:" + book);
/** 入库 */
bookRepo.save(book);
log.info("book:" + book);
bookRepo.flush();
/** 更新总金额 */
bookRepo.sumAmount(book.getId());
log.info("book:" + book);
// book = bookRepo.findById(book.getId()).get();
book = bookRepo.findById(book.getId()).get();
log.info("book:" + book);
log.info("book:" + book.getTotalAmount());
/** 费率计算 */
book.setFee(book.getTotalAmount().multiply(BigDecimal.valueOf(feeRate)));
bookRepo.save(book);
bookRepo.flush();
return ResponseEntity.ok(book);
}
@@ -62,9 +75,8 @@ public class BillController {
AuthUser authUser = (AuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
long userId = authUser.getId();
List<Book> books = bookRepo.findByUserId(userId);
List<Book> books = bookRepo.findByUserIdOrderByCreateTimeDesc(userId);
return ResponseEntity.ok(books);
}
}
@@ -80,7 +80,7 @@ public class TestController {
main.setApplyDate(new Date());
main.setAcceptDate(new Date());
main.setState(State.APPLIED);
main.setState(State.Applied);
main.setUser(user);
main.setPost(post);
main.addDetail(detail1);
@@ -24,7 +24,7 @@ public class NewBill {
Book book = new Book();
book.setApplyDate(new Date());
book.setPost(new Post(postId));
book.setState(State.APPLIED);
book.setState(State.Applied);
// List<Item> items = new ArrayList<>();
// for(BillItem rq: itemRQs){
@@ -9,11 +9,13 @@ import net.alawn.bill.entity.po.User;
public class NewUser {
private String username;
private String fullname;
private String password;
public User build() {
return User.builder()
.username(username)
.fullname(fullname)
.password(password)
.build();
}
@@ -50,6 +50,7 @@ public class Book extends EntityId {
private Date applyDate ;
private Date acceptDate ;
private BigDecimal totalAmount ;
private BigDecimal fee ;
public void addItem(Item item) {
item.setBook(this);
@@ -1,5 +1,6 @@
package net.alawn.bill.entity.po;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.*;
@@ -47,6 +48,8 @@ public class Item extends EntityId {
@NotFound(action = NotFoundAction.IGNORE)
private Merchant merchant;
// @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
private Date dating;
private BigDecimal amount;
@@ -40,6 +40,10 @@ public class User extends EntityId {
/** 用户名称 */
private String username;
/** 用户全名 */
private String fullname;
/** 密钥 */
@JsonIgnore
private String password;
@@ -8,9 +8,11 @@ package net.alawn.bill.enums;
public enum State {
APPLIED ( "已申请"),
ACCEPTED ( "已受理"),
POSTED ( "已快递");
Applied ( "已申请"),
Rejected ( "已受理"),
Accepted ( "已受理"),
Posted ( "已快递"),
Finished ( "已完成");
private String desc;
@@ -19,9 +19,9 @@ import java.util.List;
@Repository
public interface BookRepo extends JpaRepository<Book, Long> {
List<Book> findByUserId(long userId);
List<Book> findByUserIdOrderByCreateTimeDesc(long userId);
@Modifying
@Modifying(clearAutomatically = true)
@Query(value = "update t_bill_books set total_amount = (select sum(amount) from t_bill_items where book_id = ?1) where id = ?1",nativeQuery = true)
void sumAmount(long bookId);
}