mirror of
https://github.com/201206030/novel-plus.git
synced 2025-06-24 04:46:37 +00:00
用户购买功能实现
This commit is contained in:
@ -116,7 +116,11 @@ public class BookController extends BaseController{
|
||||
public ResultBean queryBookIndexAbout(Long bookId,Long lastBookIndexId) {
|
||||
Map<String,Object> data = new HashMap<>(2);
|
||||
data.put("bookIndexCount",bookService.queryIndexCount(bookId));
|
||||
data.put("lastBookContent",bookService.queryBookContent(lastBookIndexId).getContent().substring(0,42));
|
||||
String lastBookContent = bookService.queryBookContent(lastBookIndexId).getContent();
|
||||
if(lastBookContent.length()>42){
|
||||
lastBookContent=lastBookContent.substring(0,42);
|
||||
}
|
||||
data.put("lastBookContent",lastBookContent);
|
||||
return ResultBean.ok(data);
|
||||
}
|
||||
|
||||
@ -161,4 +165,6 @@ public class BookController extends BaseController{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import com.java2nb.novel.entity.*;
|
||||
import com.java2nb.novel.service.AuthorService;
|
||||
import com.java2nb.novel.service.BookService;
|
||||
import com.java2nb.novel.service.NewsService;
|
||||
import com.java2nb.novel.service.UserService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -34,6 +35,8 @@ public class PageController extends BaseController{
|
||||
|
||||
private final AuthorService authorService;
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
|
||||
@RequestMapping("{url}.html")
|
||||
public String module(@PathVariable("url") String url) {
|
||||
@ -121,7 +124,7 @@ public class PageController extends BaseController{
|
||||
* 内容页
|
||||
* */
|
||||
@RequestMapping("/book/{bookId}/{bookIndexId}.html")
|
||||
public String indexList(@PathVariable("bookId") Long bookId,@PathVariable("bookIndexId") Long bookIndexId, Model model) {
|
||||
public String indexList(@PathVariable("bookId") Long bookId,@PathVariable("bookIndexId") Long bookIndexId, HttpServletRequest request,Model model) {
|
||||
//查询书籍
|
||||
Book book = bookService.queryBookDetail(bookId);
|
||||
model.addAttribute("book",book);
|
||||
@ -137,6 +140,23 @@ public class PageController extends BaseController{
|
||||
//查询内容
|
||||
BookContent bookContent = bookService.queryBookContent(bookIndex.getId());
|
||||
model.addAttribute("bookContent",bookContent);
|
||||
//判断该目录是否收费
|
||||
if(bookIndex.getIsVip() == 1){
|
||||
UserDetails user = getUserDetails(request);
|
||||
if(user == null){
|
||||
//未登录
|
||||
return "redirect:/user/login.html?originUrl="+request.getRequestURI();
|
||||
}
|
||||
//收费,判断用户是否购买过该目录
|
||||
boolean isBuy = userService.queryIsBuyBookIndex(user.getId(),bookIndexId);
|
||||
if(!isBuy){
|
||||
//没有购买过,需要购买
|
||||
bookContent.setContent(null);
|
||||
model.addAttribute("needBuy",true);
|
||||
return "book/book_content";
|
||||
}
|
||||
}
|
||||
model.addAttribute("needBuy",false);
|
||||
return ThreadLocalUtil.getTemplateDir()+"book/book_content";
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,7 @@ import com.java2nb.novel.core.cache.CacheService;
|
||||
import com.java2nb.novel.core.enums.ResponseStatus;
|
||||
import com.java2nb.novel.core.utils.RandomValidateCodeUtil;
|
||||
import com.java2nb.novel.entity.User;
|
||||
import com.java2nb.novel.entity.UserBuyRecord;
|
||||
import com.java2nb.novel.form.UserForm;
|
||||
import com.java2nb.novel.service.BookService;
|
||||
import com.java2nb.novel.service.UserService;
|
||||
@ -264,6 +265,21 @@ public class UserController extends BaseController {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 购买小说章节
|
||||
* */
|
||||
@PostMapping("buyBookIndex")
|
||||
public ResultBean buyBookIndex(UserBuyRecord buyRecord, HttpServletRequest request) {
|
||||
UserDetails userDetails = getUserDetails(request);
|
||||
if (userDetails == null) {
|
||||
return ResultBean.fail(ResponseStatus.NO_LOGIN);
|
||||
}
|
||||
userService.buyBookIndex(userDetails.getId(),buyRecord);
|
||||
return ResultBean.ok();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package com.java2nb.novel.service;
|
||||
|
||||
|
||||
import com.java2nb.novel.core.bean.UserDetails;
|
||||
import com.java2nb.novel.entity.UserBuyRecord;
|
||||
import com.java2nb.novel.form.UserForm;
|
||||
import com.java2nb.novel.vo.BookReadHistoryVO;
|
||||
import com.java2nb.novel.vo.BookShelfVO;
|
||||
@ -122,4 +123,19 @@ public interface UserService {
|
||||
* @param userId 用户ID
|
||||
* @param amount 增加的余额 */
|
||||
void addAmount(Long userId, int amount);
|
||||
|
||||
/**
|
||||
* 判断用户是否购买过该小说章节
|
||||
* @param userId 用户ID
|
||||
* @param bookIndexId 章节目录ID
|
||||
* @return true:购买过,false:没购买
|
||||
* */
|
||||
boolean queryIsBuyBookIndex(Long userId, Long bookIndexId);
|
||||
|
||||
/**
|
||||
* 购买小说章节
|
||||
* @param userId 用户ID
|
||||
* @param buyRecord 购买信息
|
||||
* */
|
||||
void buyBookIndex(Long userId, UserBuyRecord buyRecord);
|
||||
}
|
||||
|
@ -3,14 +3,12 @@ package com.java2nb.novel.service.impl;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.java2nb.novel.core.bean.UserDetails;
|
||||
import com.java2nb.novel.core.utils.BeanUtil;
|
||||
import com.java2nb.novel.entity.*;
|
||||
import com.java2nb.novel.entity.User;
|
||||
import com.java2nb.novel.form.UserForm;
|
||||
import com.java2nb.novel.service.UserService;
|
||||
import com.java2nb.novel.core.enums.ResponseStatus;
|
||||
import com.java2nb.novel.core.exception.BusinessException;
|
||||
import com.java2nb.novel.entity.User;
|
||||
import com.java2nb.novel.entity.UserBookshelf;
|
||||
import com.java2nb.novel.entity.UserFeedback;
|
||||
import com.java2nb.novel.entity.UserReadHistory;
|
||||
import com.java2nb.novel.mapper.*;
|
||||
import com.java2nb.novel.vo.BookReadHistoryVO;
|
||||
import com.java2nb.novel.vo.BookShelfVO;
|
||||
@ -31,6 +29,8 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static com.java2nb.novel.mapper.BookDynamicSqlSupport.book;
|
||||
import static com.java2nb.novel.mapper.BookDynamicSqlSupport.id;
|
||||
import static com.java2nb.novel.mapper.UserBookshelfDynamicSqlSupport.userBookshelf;
|
||||
import static com.java2nb.novel.mapper.UserDynamicSqlSupport.*;
|
||||
import static com.java2nb.novel.mapper.UserFeedbackDynamicSqlSupport.userFeedback;
|
||||
@ -54,6 +54,8 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
private final UserFeedbackMapper userFeedbackMapper;
|
||||
|
||||
private final UserBuyRecordMapper userBuyRecordMapper;
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
@ -266,5 +268,40 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean queryIsBuyBookIndex(Long userId, Long bookIndexId) {
|
||||
|
||||
return userBuyRecordMapper.count(c ->
|
||||
c.where(UserBuyRecordDynamicSqlSupport.userId, isEqualTo(userId))
|
||||
.and(UserBuyRecordDynamicSqlSupport.bookIndexId,isEqualTo(bookIndexId))) > 0;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public void buyBookIndex(Long userId, UserBuyRecord buyRecord) {
|
||||
//查询用户余额
|
||||
long balance = userInfo(userId).getAccountBalance();
|
||||
if(balance<10){
|
||||
//余额不足
|
||||
throw new BusinessException(ResponseStatus.USER_NO_BALANCE);
|
||||
}
|
||||
buyRecord.setUserId(userId);
|
||||
buyRecord.setCreateTime(new Date());
|
||||
buyRecord.setBuyAmount(10);
|
||||
//生成购买记录
|
||||
userBuyRecordMapper.insertSelective(buyRecord);
|
||||
|
||||
//减少用户余额
|
||||
userMapper.update(update(user)
|
||||
.set(UserDynamicSqlSupport.accountBalance)
|
||||
.equalTo(balance-10)
|
||||
.where(id,isEqualTo(userId))
|
||||
.build()
|
||||
.render(RenderingStrategies.MYBATIS3));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user