diff --git a/novel-common/src/main/java/com/java2nb/novel/entity/BookIndex.java b/novel-common/src/main/java/com/java2nb/novel/entity/BookIndex.java index 273c998..cbbd033 100644 --- a/novel-common/src/main/java/com/java2nb/novel/entity/BookIndex.java +++ b/novel-common/src/main/java/com/java2nb/novel/entity/BookIndex.java @@ -22,6 +22,9 @@ public class BookIndex { @Generated("org.mybatis.generator.api.MyBatisGenerator") private Byte isVip; + @Generated("org.mybatis.generator.api.MyBatisGenerator") + private Integer bookPrice; + @Generated("org.mybatis.generator.api.MyBatisGenerator") private Date createTime; @@ -88,6 +91,16 @@ public class BookIndex { this.isVip = isVip; } + @Generated("org.mybatis.generator.api.MyBatisGenerator") + public Integer getBookPrice() { + return bookPrice; + } + + @Generated("org.mybatis.generator.api.MyBatisGenerator") + public void setBookPrice(Integer bookPrice) { + this.bookPrice = bookPrice; + } + @Generated("org.mybatis.generator.api.MyBatisGenerator") public Date getCreateTime() { return createTime; diff --git a/novel-common/src/main/java/com/java2nb/novel/mapper/BookIndexDynamicSqlSupport.java b/novel-common/src/main/java/com/java2nb/novel/mapper/BookIndexDynamicSqlSupport.java index a6a1155..1c2ac2a 100644 --- a/novel-common/src/main/java/com/java2nb/novel/mapper/BookIndexDynamicSqlSupport.java +++ b/novel-common/src/main/java/com/java2nb/novel/mapper/BookIndexDynamicSqlSupport.java @@ -28,6 +28,9 @@ public final class BookIndexDynamicSqlSupport { @Generated("org.mybatis.generator.api.MyBatisGenerator") public static final SqlColumn<Byte> isVip = bookIndex.isVip; + @Generated("org.mybatis.generator.api.MyBatisGenerator") + public static final SqlColumn<Integer> bookPrice = bookIndex.bookPrice; + @Generated("org.mybatis.generator.api.MyBatisGenerator") public static final SqlColumn<Date> createTime = bookIndex.createTime; @@ -48,6 +51,8 @@ public final class BookIndexDynamicSqlSupport { public final SqlColumn<Byte> isVip = column("is_vip", JDBCType.TINYINT); + public final SqlColumn<Integer> bookPrice = column("book_price", JDBCType.INTEGER); + public final SqlColumn<Date> createTime = column("create_time", JDBCType.TIMESTAMP); public final SqlColumn<Date> updateTime = column("update_time", JDBCType.TIMESTAMP); diff --git a/novel-common/src/main/java/com/java2nb/novel/mapper/BookIndexMapper.java b/novel-common/src/main/java/com/java2nb/novel/mapper/BookIndexMapper.java index 2423a4b..7870bf7 100644 --- a/novel-common/src/main/java/com/java2nb/novel/mapper/BookIndexMapper.java +++ b/novel-common/src/main/java/com/java2nb/novel/mapper/BookIndexMapper.java @@ -4,10 +4,12 @@ import static com.java2nb.novel.mapper.BookIndexDynamicSqlSupport.*; import static org.mybatis.dynamic.sql.SqlBuilder.*; import com.java2nb.novel.entity.BookIndex; + import java.util.Collection; import java.util.List; import java.util.Optional; import javax.annotation.Generated; + import org.apache.ibatis.annotations.DeleteProvider; import org.apache.ibatis.annotations.InsertProvider; import org.apache.ibatis.annotations.Mapper; @@ -35,45 +37,46 @@ import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils; @Mapper public interface BookIndexMapper { @Generated("org.mybatis.generator.api.MyBatisGenerator") - BasicColumn[] selectList = BasicColumn.columnList(id, bookId, indexNum, indexName, wordCount, isVip, createTime, updateTime); + BasicColumn[] selectList = BasicColumn.columnList(id, bookId, indexNum, indexName, wordCount, isVip, bookPrice, createTime, updateTime); @Generated("org.mybatis.generator.api.MyBatisGenerator") - @SelectProvider(type=SqlProviderAdapter.class, method="select") + @SelectProvider(type = SqlProviderAdapter.class, method = "select") long count(SelectStatementProvider selectStatement); @Generated("org.mybatis.generator.api.MyBatisGenerator") - @DeleteProvider(type=SqlProviderAdapter.class, method="delete") + @DeleteProvider(type = SqlProviderAdapter.class, method = "delete") int delete(DeleteStatementProvider deleteStatement); @Generated("org.mybatis.generator.api.MyBatisGenerator") - @InsertProvider(type=SqlProviderAdapter.class, method="insert") + @InsertProvider(type = SqlProviderAdapter.class, method = "insert") int insert(InsertStatementProvider<BookIndex> insertStatement); @Generated("org.mybatis.generator.api.MyBatisGenerator") - @InsertProvider(type=SqlProviderAdapter.class, method="insertMultiple") + @InsertProvider(type = SqlProviderAdapter.class, method = "insertMultiple") int insertMultiple(MultiRowInsertStatementProvider<BookIndex> multipleInsertStatement); @Generated("org.mybatis.generator.api.MyBatisGenerator") - @SelectProvider(type=SqlProviderAdapter.class, method="select") + @SelectProvider(type = SqlProviderAdapter.class, method = "select") @ResultMap("BookIndexResult") Optional<BookIndex> selectOne(SelectStatementProvider selectStatement); @Generated("org.mybatis.generator.api.MyBatisGenerator") - @SelectProvider(type=SqlProviderAdapter.class, method="select") - @Results(id="BookIndexResult", value = { - @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true), - @Result(column="book_id", property="bookId", jdbcType=JdbcType.BIGINT), - @Result(column="index_num", property="indexNum", jdbcType=JdbcType.INTEGER), - @Result(column="index_name", property="indexName", jdbcType=JdbcType.VARCHAR), - @Result(column="word_count", property="wordCount", jdbcType=JdbcType.INTEGER), - @Result(column="is_vip", property="isVip", jdbcType=JdbcType.TINYINT), - @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP), - @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP) + @SelectProvider(type = SqlProviderAdapter.class, method = "select") + @Results(id = "BookIndexResult", value = { + @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true), + @Result(column = "book_id", property = "bookId", jdbcType = JdbcType.BIGINT), + @Result(column = "index_num", property = "indexNum", jdbcType = JdbcType.INTEGER), + @Result(column = "index_name", property = "indexName", jdbcType = JdbcType.VARCHAR), + @Result(column = "word_count", property = "wordCount", jdbcType = JdbcType.INTEGER), + @Result(column = "is_vip", property = "isVip", jdbcType = JdbcType.TINYINT), + @Result(column = "book_price", property = "bookPrice", jdbcType = JdbcType.INTEGER), + @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP), + @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.TIMESTAMP) }) List<BookIndex> selectMany(SelectStatementProvider selectStatement); @Generated("org.mybatis.generator.api.MyBatisGenerator") - @UpdateProvider(type=SqlProviderAdapter.class, method="update") + @UpdateProvider(type = SqlProviderAdapter.class, method = "update") int update(UpdateStatementProvider updateStatement); @Generated("org.mybatis.generator.api.MyBatisGenerator") @@ -88,50 +91,53 @@ public interface BookIndexMapper { @Generated("org.mybatis.generator.api.MyBatisGenerator") default int deleteByPrimaryKey(Long id_) { - return delete(c -> - c.where(id, isEqualTo(id_)) + return delete(c -> + c.where(id, isEqualTo(id_)) ); } @Generated("org.mybatis.generator.api.MyBatisGenerator") default int insert(BookIndex record) { return MyBatis3Utils.insert(this::insert, record, bookIndex, c -> - c.map(id).toProperty("id") - .map(bookId).toProperty("bookId") - .map(indexNum).toProperty("indexNum") - .map(indexName).toProperty("indexName") - .map(wordCount).toProperty("wordCount") - .map(isVip).toProperty("isVip") - .map(createTime).toProperty("createTime") - .map(updateTime).toProperty("updateTime") + c.map(id).toProperty("id") + .map(bookId).toProperty("bookId") + .map(indexNum).toProperty("indexNum") + .map(indexName).toProperty("indexName") + .map(wordCount).toProperty("wordCount") + .map(isVip).toProperty("isVip") + .map(bookPrice).toProperty("bookPrice") + .map(createTime).toProperty("createTime") + .map(updateTime).toProperty("updateTime") ); } @Generated("org.mybatis.generator.api.MyBatisGenerator") default int insertMultiple(Collection<BookIndex> records) { return MyBatis3Utils.insertMultiple(this::insertMultiple, records, bookIndex, c -> - c.map(id).toProperty("id") - .map(bookId).toProperty("bookId") - .map(indexNum).toProperty("indexNum") - .map(indexName).toProperty("indexName") - .map(wordCount).toProperty("wordCount") - .map(isVip).toProperty("isVip") - .map(createTime).toProperty("createTime") - .map(updateTime).toProperty("updateTime") + c.map(id).toProperty("id") + .map(bookId).toProperty("bookId") + .map(indexNum).toProperty("indexNum") + .map(indexName).toProperty("indexName") + .map(wordCount).toProperty("wordCount") + .map(isVip).toProperty("isVip") + .map(bookPrice).toProperty("bookPrice") + .map(createTime).toProperty("createTime") + .map(updateTime).toProperty("updateTime") ); } @Generated("org.mybatis.generator.api.MyBatisGenerator") default int insertSelective(BookIndex record) { return MyBatis3Utils.insert(this::insert, record, bookIndex, c -> - c.map(id).toPropertyWhenPresent("id", record::getId) - .map(bookId).toPropertyWhenPresent("bookId", record::getBookId) - .map(indexNum).toPropertyWhenPresent("indexNum", record::getIndexNum) - .map(indexName).toPropertyWhenPresent("indexName", record::getIndexName) - .map(wordCount).toPropertyWhenPresent("wordCount", record::getWordCount) - .map(isVip).toPropertyWhenPresent("isVip", record::getIsVip) - .map(createTime).toPropertyWhenPresent("createTime", record::getCreateTime) - .map(updateTime).toPropertyWhenPresent("updateTime", record::getUpdateTime) + c.map(id).toPropertyWhenPresent("id", record::getId) + .map(bookId).toPropertyWhenPresent("bookId", record::getBookId) + .map(indexNum).toPropertyWhenPresent("indexNum", record::getIndexNum) + .map(indexName).toPropertyWhenPresent("indexName", record::getIndexName) + .map(wordCount).toPropertyWhenPresent("wordCount", record::getWordCount) + .map(isVip).toPropertyWhenPresent("isVip", record::getIsVip) + .map(bookPrice).toPropertyWhenPresent("bookPrice", record::getBookPrice) + .map(createTime).toPropertyWhenPresent("createTime", record::getCreateTime) + .map(updateTime).toPropertyWhenPresent("updateTime", record::getUpdateTime) ); } @@ -153,7 +159,7 @@ public interface BookIndexMapper { @Generated("org.mybatis.generator.api.MyBatisGenerator") default Optional<BookIndex> selectByPrimaryKey(Long id_) { return selectOne(c -> - c.where(id, isEqualTo(id_)) + c.where(id, isEqualTo(id_)) ); } @@ -170,6 +176,7 @@ public interface BookIndexMapper { .set(indexName).equalTo(record::getIndexName) .set(wordCount).equalTo(record::getWordCount) .set(isVip).equalTo(record::getIsVip) + .set(bookPrice).equalTo(record::getBookPrice) .set(createTime).equalTo(record::getCreateTime) .set(updateTime).equalTo(record::getUpdateTime); } @@ -182,6 +189,7 @@ public interface BookIndexMapper { .set(indexName).equalToWhenPresent(record::getIndexName) .set(wordCount).equalToWhenPresent(record::getWordCount) .set(isVip).equalToWhenPresent(record::getIsVip) + .set(bookPrice).equalToWhenPresent(record::getBookPrice) .set(createTime).equalToWhenPresent(record::getCreateTime) .set(updateTime).equalToWhenPresent(record::getUpdateTime); } @@ -189,28 +197,30 @@ public interface BookIndexMapper { @Generated("org.mybatis.generator.api.MyBatisGenerator") default int updateByPrimaryKey(BookIndex record) { return update(c -> - c.set(bookId).equalTo(record::getBookId) - .set(indexNum).equalTo(record::getIndexNum) - .set(indexName).equalTo(record::getIndexName) - .set(wordCount).equalTo(record::getWordCount) - .set(isVip).equalTo(record::getIsVip) - .set(createTime).equalTo(record::getCreateTime) - .set(updateTime).equalTo(record::getUpdateTime) - .where(id, isEqualTo(record::getId)) + c.set(bookId).equalTo(record::getBookId) + .set(indexNum).equalTo(record::getIndexNum) + .set(indexName).equalTo(record::getIndexName) + .set(wordCount).equalTo(record::getWordCount) + .set(isVip).equalTo(record::getIsVip) + .set(bookPrice).equalTo(record::getBookPrice) + .set(createTime).equalTo(record::getCreateTime) + .set(updateTime).equalTo(record::getUpdateTime) + .where(id, isEqualTo(record::getId)) ); } @Generated("org.mybatis.generator.api.MyBatisGenerator") default int updateByPrimaryKeySelective(BookIndex record) { return update(c -> - c.set(bookId).equalToWhenPresent(record::getBookId) - .set(indexNum).equalToWhenPresent(record::getIndexNum) - .set(indexName).equalToWhenPresent(record::getIndexName) - .set(wordCount).equalToWhenPresent(record::getWordCount) - .set(isVip).equalToWhenPresent(record::getIsVip) - .set(createTime).equalToWhenPresent(record::getCreateTime) - .set(updateTime).equalToWhenPresent(record::getUpdateTime) - .where(id, isEqualTo(record::getId)) + c.set(bookId).equalToWhenPresent(record::getBookId) + .set(indexNum).equalToWhenPresent(record::getIndexNum) + .set(indexName).equalToWhenPresent(record::getIndexName) + .set(wordCount).equalToWhenPresent(record::getWordCount) + .set(isVip).equalToWhenPresent(record::getIsVip) + .set(bookPrice).equalToWhenPresent(record::getBookPrice) + .set(createTime).equalToWhenPresent(record::getCreateTime) + .set(updateTime).equalToWhenPresent(record::getUpdateTime) + .where(id, isEqualTo(record::getId)) ); } } \ No newline at end of file diff --git a/novel-front/src/main/java/com/java2nb/novel/controller/UserController.java b/novel-front/src/main/java/com/java2nb/novel/controller/UserController.java index b37c5ca..1e509e6 100644 --- a/novel-front/src/main/java/com/java2nb/novel/controller/UserController.java +++ b/novel-front/src/main/java/com/java2nb/novel/controller/UserController.java @@ -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(); } diff --git a/novel-front/src/main/java/com/java2nb/novel/core/config/BookPriceConfig.java b/novel-front/src/main/java/com/java2nb/novel/core/config/BookPriceConfig.java new file mode 100644 index 0000000..6a2c7fa --- /dev/null +++ b/novel-front/src/main/java/com/java2nb/novel/core/config/BookPriceConfig.java @@ -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; + + +} diff --git a/novel-front/src/main/java/com/java2nb/novel/service/impl/BookServiceImpl.java b/novel-front/src/main/java/com/java2nb/novel/service/impl/BookServiceImpl.java index fc6dd08..ac2383a 100644 --- a/novel-front/src/main/java/com/java2nb/novel/service/impl/BookServiceImpl.java +++ b/novel-front/src/main/java/com/java2nb/novel/service/impl/BookServiceImpl.java @@ -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)) diff --git a/novel-front/src/main/java/com/java2nb/novel/service/impl/UserServiceImpl.java b/novel-front/src/main/java/com/java2nb/novel/service/impl/UserServiceImpl.java index 99ae5f7..da8281f 100644 --- a/novel-front/src/main/java/com/java2nb/novel/service/impl/UserServiceImpl.java +++ b/novel-front/src/main/java/com/java2nb/novel/service/impl/UserServiceImpl.java @@ -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)); diff --git a/novel-front/src/main/resources/application.yml b/novel-front/src/main/resources/application.yml index c805afc..6129ef3 100644 --- a/novel-front/src/main/resources/application.yml +++ b/novel-front/src/main/resources/application.yml @@ -57,3 +57,11 @@ author: share-proportion: 0.7 #兑换比率(人民币) exchange-proportion: 0.01 + +#小说章节定价规则 +book: + price: + #字数 + word-count: 1000 + #价值(屋币) + value: 5 \ No newline at end of file diff --git a/novel-front/src/main/resources/templates/book/book_content.html b/novel-front/src/main/resources/templates/book/book_content.html index bc13e82..e19822b 100644 --- a/novel-front/src/main/resources/templates/book/book_content.html +++ b/novel-front/src/main/resources/templates/book/book_content.html @@ -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> diff --git a/novel-front/src/main/resources/templates/mobile/book/book_content.html b/novel-front/src/main/resources/templates/mobile/book/book_content.html index abfcbc2..0181b2c 100644 --- a/novel-front/src/main/resources/templates/mobile/book/book_content.html +++ b/novel-front/src/main/resources/templates/mobile/book/book_content.html @@ -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> diff --git a/sql/20201122.sql b/sql/20201122.sql new file mode 100644 index 0000000..b723efa --- /dev/null +++ b/sql/20201122.sql @@ -0,0 +1 @@ +alter table book_index add column `book_price` int(3) DEFAULT 0 COMMENT '章节费用(屋币)' after `is_vip`; \ No newline at end of file