小说章节定价规则自定义

This commit is contained in:
xiongxiaoyang
2020-11-22 17:15:20 +08:00
parent cd11854eff
commit 31aa3192fd
11 changed files with 140 additions and 66 deletions

View File

@ -282,6 +282,7 @@ public class UserController extends BaseController {
if (userDetails == null) {
return ResultBean.fail(ResponseStatus.NO_LOGIN);
}
buyRecord.setBuyAmount(bookService.queryBookIndex(buyRecord.getBookIndexId()).getBookPrice());
userService.buyBookIndex(userDetails.getId(),buyRecord);
return ResultBean.ok();
}

View File

@ -0,0 +1,23 @@
package com.java2nb.novel.core.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
/**
* 章节费用配置
* @author cd
*/
@Component
@Data
@ConfigurationProperties(prefix = "book.price")
public class BookPriceConfig {
private BigDecimal wordCount;
private BigDecimal value;
}

View File

@ -5,6 +5,7 @@ import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java2nb.novel.core.cache.CacheKey;
import com.java2nb.novel.core.cache.CacheService;
import com.java2nb.novel.core.config.BookPriceConfig;
import com.java2nb.novel.core.enums.ResponseStatus;
import com.java2nb.novel.core.exception.BusinessException;
import com.java2nb.novel.core.utils.*;
@ -88,6 +89,8 @@ public class BookServiceImpl implements BookService {
private final FileService fileService;
private final BookPriceConfig bookPriceConfig;
@SneakyThrows
@Override
@ -256,7 +259,7 @@ public class BookServiceImpl implements BookService {
@Override
public BookIndex queryBookIndex(Long bookIndexId) {
SelectStatementProvider selectStatement = select(BookIndexDynamicSqlSupport.id, BookIndexDynamicSqlSupport.bookId, BookIndexDynamicSqlSupport.indexNum, BookIndexDynamicSqlSupport.indexName, BookIndexDynamicSqlSupport.wordCount, BookIndexDynamicSqlSupport.updateTime, BookIndexDynamicSqlSupport.isVip)
SelectStatementProvider selectStatement = select(BookIndexDynamicSqlSupport.id, BookIndexDynamicSqlSupport.bookId, BookIndexDynamicSqlSupport.indexNum, BookIndexDynamicSqlSupport.indexName, BookIndexDynamicSqlSupport.wordCount,BookIndexDynamicSqlSupport.bookPrice, BookIndexDynamicSqlSupport.updateTime, BookIndexDynamicSqlSupport.isVip)
.from(bookIndex)
.where(BookIndexDynamicSqlSupport.id, isEqualTo(bookIndexId))
.build()
@ -560,6 +563,10 @@ public class BookServiceImpl implements BookService {
.and(BookDynamicSqlSupport.authorId, isEqualTo(authorId))
.build()
.render(RenderingStrategies.MYBATIS3));
//计算价格
int bookPrice = new BigDecimal(wordCount).divide(bookPriceConfig.getWordCount()).multiply(bookPriceConfig.getValue()).intValue();
//更新小说目录表
int indexNum = 0;
if (book.getLastIndexId() != null) {
@ -572,6 +579,7 @@ public class BookServiceImpl implements BookService {
lastBookIndex.setIndexNum(indexNum);
lastBookIndex.setBookId(bookId);
lastBookIndex.setIsVip(isVip);
lastBookIndex.setBookPrice(bookPrice);
lastBookIndex.setCreateTime(currentDate);
lastBookIndex.setUpdateTime(currentDate);
bookIndexMapper.insertSelective(lastBookIndex);
@ -788,6 +796,10 @@ public class BookServiceImpl implements BookService {
Date currentDate = new Date();
int wordCount = content.length();
//计算价格
int bookPrice = new BigDecimal(wordCount).divide(bookPriceConfig.getWordCount()).multiply(bookPriceConfig.getValue()).intValue();
//更新小说目录表
int update = bookIndexMapper.update(
update(BookIndexDynamicSqlSupport.bookIndex)
@ -795,6 +807,8 @@ public class BookServiceImpl implements BookService {
.equalTo(indexName)
.set(BookIndexDynamicSqlSupport.wordCount)
.equalTo(wordCount)
.set(BookIndexDynamicSqlSupport.bookPrice)
.equalTo(bookPrice)
.set(BookIndexDynamicSqlSupport.updateTime)
.equalTo(currentDate)
.where(BookIndexDynamicSqlSupport.id, isEqualTo(indexId))

View File

@ -284,20 +284,19 @@ public class UserServiceImpl implements UserService {
public void buyBookIndex(Long userId, UserBuyRecord buyRecord) {
//查询用户余额
long balance = userInfo(userId).getAccountBalance();
if(balance<10){
if(balance<buyRecord.getBuyAmount()){
//余额不足
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)
.equalTo(balance-buyRecord.getBuyAmount())
.where(id,isEqualTo(userId))
.build()
.render(RenderingStrategies.MYBATIS3));

View File

@ -57,3 +57,11 @@ author:
share-proportion: 0.7
#兑换比率(人民币)
exchange-proportion: 0.01
#小说章节定价规则
book:
price:
#字数
word-count: 1000
#价值(屋币)
value: 5

View File

@ -116,7 +116,7 @@
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAX4WEeZOhVYVmRzc9paH9JSy/08L4LdXX0bnEtd0tA2crFh4MPja1O/9L0Y0B5Q7Mkw2OnJlkYmOh3/iyqfPkkn99UiP/cCDJ38/2cPKg8P57VHEmkKJr8/tJbwExBtkfhGxxoA1kMIQUaw59BH5iPe">
</div>
<ul class="order_list">
<li>价格:<span class="red">10屋币(1元=100屋币)</span></li>
<li>价格:<span class="red" th:text="${bookIndex.bookPrice}+'屋币(1元=100屋币)'"></span></li>
<li id="panelPay" class="btns"><a class="btn_red" href="javascript:buyBookIndex()" >购买</a></li>
</ul>

View File

@ -238,7 +238,7 @@
<div class="Readarea ReadAjax_content screen_container"
style="color: rgb(0, 0, 0); font-size: 10px;background-color: #fff" th:if="${needBuy}">
<h5>此章为VIP章节需要订阅后才能继续阅读</h5>
价格:<span style="color: red">10屋币(1元=100屋币)</span><br/>
价格:<span style="color: red" th:text="${bookIndex.bookPrice}+'屋币(1元=100屋币)'"></span><br/>
<a href="javascript:buyBookIndex()" type="button" class="layui-btn layui-btn-sm layui-btn-radius">购买</a>
</div>