阿里巴巴规范改造

This commit is contained in:
xiongxiaoyang
2019-12-09 16:17:59 +08:00
parent 32847bc874
commit 289f1e1670
24 changed files with 785 additions and 2018 deletions

View File

@ -1,20 +1,19 @@
package xyz.zinglizingli.books.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.Charsets;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.http.*;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import tk.mybatis.orderbyhelper.OrderByHelper;
import xyz.zinglizingli.books.constant.CacheKeyConstans;
import xyz.zinglizingli.books.mapper.*;
@ -23,33 +22,31 @@ import xyz.zinglizingli.books.util.UUIDUtils;
import xyz.zinglizingli.common.cache.CommonCacheUtil;
import xyz.zinglizingli.common.utils.RestTemplateUtil;
import java.io.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author XXY
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class BookService {
@Autowired
private BookMapper bookMapper;
private final BookMapper bookMapper;
@Autowired
private BookIndexMapper bookIndexMapper;
private final BookIndexMapper bookIndexMapper;
@Autowired
private BookContentMapper bookContentMapper;
private final BookContentMapper bookContentMapper;
@Autowired
private ScreenBulletMapper screenBulletMapper;
private final ScreenBulletMapper screenBulletMapper;
@Autowired
private UserRefBookMapper userRefBookMapper;
private final UserRefBookMapper userRefBookMapper;
@Autowired
private CommonCacheUtil cacheUtil;
RestTemplate isoRestTemplate = RestTemplateUtil.getInstance("iso-8859-1");
private final CommonCacheUtil cacheUtil;
@Value("${pic.save.type}")
private Byte picSaveType;
@ -58,16 +55,11 @@ public class BookService {
private String picSavePath;
private Logger log = LoggerFactory.getLogger(BookService.class);
/**
* 保存章节目录和内容
* */
public void saveBookAndIndexAndContent(Book book, List<BookIndex> bookIndex, List<BookContent> bookContent){
//一次最多只允许插入20条记录,否则影响服务器响应,如果没有插入所有更新,则更新时间设为昨天
/*if(bookIndex.size()>100){
book.setUpdateTime(new Date(book.getUpdateTime().getTime()-1000*60*60*24));
}
*/
boolean isUpdate = false;
Long bookId = -1L;
@ -81,12 +73,12 @@ public class BookService {
bookId = books.get(0).getId();
book.setId(bookId);
String picSrc = book.getPicUrl();
if(picSaveType == 2 && org.apache.commons.lang3.StringUtils.isNotBlank(picSrc)){
if(picSaveType == 2 && StringUtils.isNotBlank(picSrc)){
try {
HttpHeaders headers = new HttpHeaders();
HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
ResponseEntity<Resource> resEntity = isoRestTemplate.exchange(picSrc, HttpMethod.GET, requestEntity, Resource.class);
InputStream input = resEntity.getBody().getInputStream();
ResponseEntity<Resource> resEntity = RestTemplateUtil.getInstance(Charsets.ISO_8859_1).exchange(picSrc, HttpMethod.GET, requestEntity, Resource.class);
InputStream input = Objects.requireNonNull(resEntity.getBody()).getInputStream();
Date currentDate = new Date();
picSrc = "/localPic/" + DateUtils.formatDate(currentDate, "yyyy") + "/" + DateUtils.formatDate(currentDate, "MM") + "/" + DateUtils.formatDate(currentDate, "dd")
+ UUIDUtils.getUUID32()
@ -127,12 +119,7 @@ public class BookService {
}
if (bookId >= 0) {
//查询目录已存在数量
/* BookIndexExample bookIndexExample = new BookIndexExample();
bookIndexExample.createCriteria().andBookIdEqualTo(bookId);
int indexCount = bookIndexMapper.countByExample(bookIndexExample);*/
BookIndex lastIndex = null;
List<BookIndex> newBookIndexList = new ArrayList<>();
List<BookContent> newContentList = new ArrayList<>();
for (int i = 0; i < bookIndex.size(); i++) {
@ -141,11 +128,9 @@ public class BookService {
BookIndex bookIndexItem = bookIndex.get(i);
bookIndexItem.setBookId(bookId);
bookContentItem.setBookId(bookId);
//bookContentItem.setIndexId(bookIndexItem.getId());暂时使用bookId和IndexNum查询content
bookContentItem.setIndexNum(bookIndexItem.getIndexNum());
newBookIndexList.add(bookIndexItem);
newContentList.add(bookContentItem);
lastIndex = bookIndexItem;
}
//一次最多只允许插入20条记录,否则影响服务器响应
if (isUpdate && i % 20 == 0 && newBookIndexList.size() > 0) {
@ -174,15 +159,21 @@ public class BookService {
}
@Transactional
/**
* 批量插入章节目录表和章节内容表
* */
@Transactional(rollbackFor = Exception.class)
public void insertIndexListAndContentList(List<BookIndex> newBookIndexList, List<BookContent> newContentList) {
bookIndexMapper.insertBatch(newBookIndexList);
bookContentMapper.insertBatch(newContentList);
}
/**
* 生成随机访问次数
* */
private Long generateVisiteCount(Float score) {
int baseNum = (int) (Math.pow(score * 10, (int) (score - 5)) / 2);
int baseNum = (int)(score * 100);
return Long.parseLong(baseNum + new Random().nextInt(1000) + "");
}
@ -202,12 +193,13 @@ public class BookService {
OrderByHelper.orderBy(sortBy + " " + sort);
}
List<Book> books = bookMapper.search(userId, ids, keyword, catId, softCat, softTag, bookStatus);
return books;
return bookMapper.search(userId, ids, keyword, catId, softCat, softTag, bookStatus);
}
/**
* 获取分类名
* */
public String getCatNameById(Integer catid) {
String catName = "其他";
@ -257,11 +249,17 @@ public class BookService {
return catName;
}
/**
* 查询书籍的基础数据
* */
public Book queryBaseInfo(Long bookId) {
return bookMapper.selectByPrimaryKey(bookId);
}
/**
* 查询最新更新的书籍列表
* */
public List<BookIndex> queryNewIndexList(Long bookId) {
PageHelper.startPage(1, 15);
BookIndexExample example = new BookIndexExample();
@ -271,6 +269,9 @@ public class BookService {
}
/**
* 查询书籍目录列表
* */
public List<BookIndex> queryAllIndexList(Long bookId) {
BookIndexExample example = new BookIndexExample();
example.createCriteria().andBookIdEqualTo(bookId);
@ -278,6 +279,9 @@ public class BookService {
return bookIndexMapper.selectByExample(example);
}
/**
* 查询书籍章节内容
* */
public BookContent queryBookContent(Long bookId, Integer indexNum) {
BookContent content = (BookContent) cacheUtil.getObject(CacheKeyConstans.BOOK_CONTENT_KEY_PREFIX + "_" + bookId + "_" + indexNum);
if (content == null) {
@ -285,11 +289,6 @@ public class BookService {
example.createCriteria().andBookIdEqualTo(bookId).andIndexNumEqualTo(indexNum);
List<BookContent> bookContents = bookContentMapper.selectByExample(example);
content = bookContents.size() > 0 ? bookContents.get(0) : null;
/*try {
content.setContent(chargeBookContent(content.getContent()));
} catch (IOException e) {
log.error(e.getMessage(), e);
}*/
cacheUtil.setObject(CacheKeyConstans.BOOK_CONTENT_KEY_PREFIX + "_" + bookId + "_" + indexNum, content, 60 * 60 * 24);
}
@ -297,6 +296,9 @@ public class BookService {
}
/**
* 增加访问次数
* */
public void addVisitCount(Long bookId, String userId, Integer indexNum) {
bookMapper.addVisitCount(bookId);
@ -307,13 +309,23 @@ public class BookService {
}
/**
* 查询章节名
* */
public String queryIndexNameByBookIdAndIndexNum(Long bookId, Integer indexNum) {
BookIndexExample example = new BookIndexExample();
example.createCriteria().andBookIdEqualTo(bookId).andIndexNumEqualTo(indexNum);
return bookIndexMapper.selectByExample(example).get(0).getIndexName();
List<BookIndex> indexList = bookIndexMapper.selectByExample(example);
if(indexList != null && indexList.size() > 0 ) {
return indexList.get(0).getIndexName();
}
return null;
}
/**
* 查询最大和最小章节号
* */
public List<Integer> queryMaxAndMinIndexNum(Long bookId) {
List<Integer> result = new ArrayList<>();
BookIndexExample example = new BookIndexExample();
@ -330,7 +342,7 @@ public class BookService {
/**
* 查询该书籍目录数量
*/
public List<Integer> queryIndexCountByBookNameAndBAuthor(String bookName, String author) {
public List<Integer> queryIndexCountByBookNameAndAuthor(String bookName, String author) {
List<Integer> result = new ArrayList<>();
BookExample example = new BookExample();
example.createCriteria().andBookNameEqualTo(bookName).andAuthorEqualTo(author);
@ -353,33 +365,11 @@ public class BookService {
}
public Book queryRandomBook() {
return bookMapper.queryRandomBook();
}
public Map<String, Object> queryNewstBook() {
final String SENDIDS = "sendWeiboIds";
Set<Long> sendIds = (Set<Long>) cacheUtil.getObject(SENDIDS);
if (sendIds == null) {
sendIds = new HashSet<>();
}
String newstIndexName = null;
Book book = null;
book = bookMapper.queryNewstBook(sendIds);
Map<String, Object> data = new HashMap<>();
if (book != null && book.getId() != null) {
newstIndexName = bookIndexMapper.queryNewstIndexName(book.getId());
if (!StringUtils.isEmpty(newstIndexName)) {
sendIds.add(book.getId());
cacheUtil.setObject(SENDIDS, sendIds, 60 * 60 * 24 * 2);
data.put("book", book);
data.put("newstIndexName", newstIndexName);
}
}
return data;
}
/**
* 查询轻小说分类名
* */
public String getSoftCatNameById(Integer softCat) {
String catName = "其他";
@ -434,6 +424,9 @@ public class BookService {
}
/**
* 查询漫画分类名
* */
public String getMhCatNameById(Integer softCat) {
String catName = "其他";
@ -456,6 +449,9 @@ public class BookService {
}
/**
* 保存弹幕
* */
public void sendBullet(Long contentId, String bullet) {
ScreenBullet screenBullet = new ScreenBullet();
@ -466,6 +462,9 @@ public class BookService {
screenBulletMapper.insertSelective(screenBullet);
}
/**
* 查询弹幕
* */
public List<ScreenBullet> queryBullet(Long contentId) {
ScreenBulletExample example = new ScreenBulletExample();
@ -475,19 +474,19 @@ public class BookService {
return screenBulletMapper.selectByExample(example);
}
public String queryIndexList(Long bookId, int count) {
BookIndexExample example = new BookIndexExample();
example.createCriteria().andBookIdEqualTo(bookId).andIndexNumEqualTo(count);
return bookIndexMapper.selectByExample(example).get(0).getIndexName();
}
/**
* 查询章节内容
* */
public String queryContentList(Long bookId, int count) {
BookContentExample example = new BookContentExample();
example.createCriteria().andBookIdEqualTo(bookId).andIndexNumEqualTo(count);
return bookContentMapper.selectByExample(example).get(0).getContent();
}
/**
* 查询章节数
* */
public int countIndex(Long bookId) {
BookIndexExample example = new BookIndexExample();
example.createCriteria().andBookIdEqualTo(bookId);
@ -495,6 +494,9 @@ public class BookService {
}
/**
* 查询完本书籍
* */
public List<String> queryEndBookIdList() {
return bookMapper.queryEndBookIdList();
}
@ -503,6 +505,9 @@ public class BookService {
/**
* 查询前一章节和后一章节号
* */
public List<Integer> queryPreAndNextIndexNum(Long bookId, Integer indexNum) {
List<Integer> result = new ArrayList<>();
BookIndexExample example = new BookIndexExample();

View File

@ -1,8 +1,8 @@
package xyz.zinglizingli.books.service;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
@ -14,36 +14,52 @@ import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.UnsupportedEncodingException;
/**
* @author XXY
*/
@Service
@RequiredArgsConstructor
public class MailService {
private final Logger logger = LoggerFactory.getLogger(MailService.class);
/**
* 使用@Value注入application.properties中指定的用户名
* */
@Value("${spring.mail.username}")
//使用@Value注入application.properties中指定的用户名
private String from;
String nickName = "精品小说楼";
private String nickName = "精品小说楼";
@Autowired
//用于发送文件
private JavaMailSender mailSender;
/**
* 用于发送文件
* */
private final JavaMailSender mailSender;
/**
* 发送简单邮件
* */
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);//收信人
message.setSubject(subject);//主题
message.setText(content);//内容
message.setFrom(from);//发信人
//收信人
message.setTo(to);
//主题
message.setSubject(subject);
//内容
message.setText(content);
//发信人
message.setFrom(from);
mailSender.send(message);
}
/**
* 发送html邮件
* */
public void sendHtmlMail(String to, String subject, String content){
logger.info("发送HTML邮件开始{},{},{}", to, subject, content);
@ -57,7 +73,8 @@ public class MailService {
helper.setFrom(new InternetAddress(from, nickName, "UTF-8"));
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);//true代表支持html
//true代表支持html
helper.setText(content, true);
mailSender.send(message);
logger.info("发送HTMLto"+to+"邮件成功");
} catch (Exception e) {
@ -65,6 +82,9 @@ public class MailService {
}
}
/**
* 发送带附件的邮件
* */
public void sendAttachmentMail(String to, String subject, String content, String filePath) {
logger.info("发送带附件邮件开始:{},{},{},{}", to, subject, content, filePath);
@ -80,7 +100,8 @@ public class MailService {
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = file.getFilename();
helper.addAttachment(fileName, file);//添加附件,可多次调用该方法添加多个附件
//添加附件,可多次调用该方法添加多个附件
helper.addAttachment(fileName, file);
mailSender.send(message);
logger.info("发送带附件邮件成功");
} catch (MessagingException e) {
@ -103,7 +124,8 @@ public class MailService {
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);//重复使用添加多个图片
//重复使用添加多个图片
helper.addInline(rscId, res);
mailSender.send(message);
logger.info("发送带图片邮件成功");
} catch (Exception e) {

View File

@ -1,7 +1,7 @@
package xyz.zinglizingli.books.service;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import xyz.zinglizingli.books.mapper.UserMapper;
import xyz.zinglizingli.books.mapper.UserRefBookMapper;
@ -13,27 +13,38 @@ import xyz.zinglizingli.books.util.MD5Util;
import java.util.List;
/**
* @author XXY
*/
@RequiredArgsConstructor
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
private final UserMapper userMapper;
@Autowired
private UserRefBookMapper userRefBookMapper;
private final UserRefBookMapper userRefBookMapper;
/**
* 判断登录名是否存在
* */
public boolean isExistLoginName(String loginName) {
UserExample example = new UserExample();
example.createCriteria().andLoginNameEqualTo(loginName);
return userMapper.countByExample(example)>0?true:false;
return userMapper.countByExample(example)>0;
}
/**
* 注册
* */
public void regist(User user) {
user.setPassword(MD5Util.MD5Encode(user.getPassword(),"utf-8"));
userMapper.insertSelective(user);
}
/**
* 登陆
* */
public void login(User user) {
UserExample example = new UserExample();
example.createCriteria().andLoginNameEqualTo(user.getLoginName())
@ -48,6 +59,9 @@ public class UserService {
}
/**
* 加入书架
* */
public void addToCollect(Long bookId, long userId) {
UserRefBook userRefBook = new UserRefBook();
userRefBook.setBookId(bookId);
@ -59,20 +73,29 @@ public class UserService {
}
/**
* 判断是否加入书架
* */
public boolean isCollect(Long bookId, long userId) {
UserRefBookExample example = new UserRefBookExample();
example.createCriteria().andBookIdEqualTo(bookId).andUserIdEqualTo(userId);
return userRefBookMapper.countByExample(example)>0?true:false;
return userRefBookMapper.countByExample(example)>0;
}
/**
* 取消加入书架
* */
public void cancelToCollect(Long bookId, long userId) {
UserRefBookExample example = new UserRefBookExample();
example.createCriteria().andBookIdEqualTo(bookId).andUserIdEqualTo(userId);
userRefBookMapper.deleteByExample(example);
}
/**
* 加入或取消书架
* */
public void collectOrCancelBook(Long userid, Long bookId) {
boolean collect = isCollect(bookId, userid);
@ -84,6 +107,9 @@ public class UserService {
}
}
/**
* 查询用户章节阅读记录
* */
public Integer queryBookIndexNumber(String userId, Long bookId) {
return userRefBookMapper.queryBookIndexNumber(userId,bookId);
}