260 lines
9.1 KiB
Java
260 lines
9.1 KiB
Java
package com.peanut.modules.book.controller;
|
||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
||
import com.peanut.common.utils.PageUtils;
|
||
import com.peanut.common.utils.R;
|
||
import com.peanut.modules.book.dao.UserEbookBuyDao;
|
||
import com.peanut.modules.book.entity.*;
|
||
import com.peanut.modules.book.service.BookClockinCommentService;
|
||
import com.peanut.modules.book.service.BookClockinPunchService;
|
||
import com.peanut.modules.book.service.BookService;
|
||
import com.peanut.modules.book.service.MyUserService;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.web.bind.annotation.*;
|
||
import java.util.*;
|
||
|
||
@RestController
|
||
@RequestMapping("book/clockinPunch")
|
||
public class BookClockinPunchController {
|
||
@Autowired
|
||
private BookClockinPunchService bookClockinPunchService;
|
||
@Autowired
|
||
private BookService bookService;
|
||
@Autowired
|
||
private MyUserService myUserService;
|
||
@Autowired
|
||
private BookClockinCommentService bookClockinCommentService;
|
||
@Autowired
|
||
private UserEbookBuyDao userEbookBuyDao;
|
||
|
||
|
||
|
||
|
||
/**
|
||
* 列表 app post请求 /applistSS
|
||
*/
|
||
@RequestMapping("/applist")
|
||
public R applist(@RequestParam Map<String, Object> params) {
|
||
PageUtils page = bookClockinPunchService.queryPage(params);
|
||
return R.ok().put("page", page);
|
||
}
|
||
|
||
|
||
|
||
|
||
/**
|
||
* 计算打卡天数
|
||
*/
|
||
@RequestMapping("/clockindays")
|
||
public R applists(@RequestParam("bookId") String bookid,
|
||
@RequestParam("userId") String userid) {
|
||
ArrayList<Object> list = new ArrayList<>();
|
||
//查询用户id图书id 根据第一天打卡天数开始计算
|
||
BookClockinPunchEntity bookClockinEntity = bookClockinPunchService.getBaseMapper().selectOne(new QueryWrapper<BookClockinPunchEntity>()
|
||
.eq("book_id", bookid)
|
||
.eq("user_id", userid)
|
||
.eq("days",1)
|
||
);
|
||
List<BookClockinPunchEntity> bookClockinEntityList = bookClockinPunchService.getBaseMapper().selectList(new QueryWrapper<BookClockinPunchEntity>()
|
||
.eq("book_id", bookid)
|
||
.eq("user_id", userid)
|
||
);
|
||
for (BookClockinPunchEntity bookClock : bookClockinEntityList) {
|
||
Integer days = bookClock.getDays();
|
||
list.add(days);
|
||
}
|
||
if (bookClockinEntity != null) {
|
||
Date createTime = bookClockinEntity.getCreateTime();
|
||
long createTimeMillis = createTime.getTime();
|
||
//获取第一次打卡天数createTimeMillis毫秒数,时间戳减并除以毫秒数(24 * 60 * 60 * 1000)计算打卡总天数+1
|
||
int daysBetween = (int) (System.currentTimeMillis() - createTimeMillis) / (24 * 60 * 60 * 1000)+1;
|
||
return R.ok().put("daysBetween", daysBetween).put("dayslist", list);
|
||
|
||
} else if (bookClockinEntity == null) {
|
||
Date createTime = new Date();
|
||
long createTimeMillis = createTime.getTime();
|
||
int daysBetween = (int) (System.currentTimeMillis() - createTimeMillis) / (24 * 60 * 60 * 1000)+1;
|
||
return R.ok().put("daysBetween", daysBetween).put("dayslist",list );
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
return R.error("无信息记录");
|
||
}
|
||
|
||
/**
|
||
* 信息
|
||
*/
|
||
@RequestMapping("/info/{id}")
|
||
public R info(@PathVariable("id") Integer id) {
|
||
BookClockinPunchEntity bookClockinPunchEntity = bookClockinPunchService.getById(id);
|
||
|
||
return R.ok().put("bookClockinPunchEntity", bookClockinPunchEntity);
|
||
}
|
||
|
||
/**
|
||
* 保存
|
||
*/
|
||
@RequestMapping("/save")
|
||
public R save(@RequestBody BookClockinPunchEntity bookClockinPunchEntity) {
|
||
Integer bookid1 = bookClockinPunchEntity.getBookId();
|
||
Integer userId = bookClockinPunchEntity.getUserId();
|
||
Integer taskId = bookClockinPunchEntity.getTId();
|
||
BookEntity bookEntity = bookService.getBaseMapper().selectById(bookid1);
|
||
Boolean canListen = bookEntity.getCanListen();
|
||
BookClockinPunchEntity bookClock = bookClockinPunchService.getBaseMapper().selectOne(new QueryWrapper<BookClockinPunchEntity>()
|
||
.eq("book_id", bookid1)
|
||
.eq("user_id", userId)
|
||
.eq("t_id", taskId)
|
||
|
||
|
||
|
||
);
|
||
if (bookClock !=null) {
|
||
return R.error("您已经签到,请勿重复签到");
|
||
}
|
||
|
||
|
||
if(canListen == true){
|
||
boolean b = myUserService.bookAuthen(bookid1,userId);
|
||
if (b){
|
||
bookClockinPunchEntity.setDelFlag(0);
|
||
bookClockinPunchService.save(bookClockinPunchEntity);
|
||
return R.ok("签到成功");
|
||
}else {
|
||
return R.error("您还未购买此书,购买后即可签到");
|
||
|
||
}
|
||
|
||
}else {
|
||
return R.error("该书暂未开放打卡权限");
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* 获取用户打卡已购图书
|
||
* @param userId
|
||
* @return
|
||
*/
|
||
@RequestMapping("/myClockBooks")
|
||
public R myClockBooks(@RequestParam Integer userId,@RequestParam Integer limit,@RequestParam Integer page){
|
||
MPJLambdaWrapper<UserEbookBuyEntity> wrapper = new MPJLambdaWrapper<>();
|
||
wrapper.selectAll(BookEntity.class);
|
||
wrapper.leftJoin(BookEntity.class,BookEntity::getId,UserEbookBuyEntity::getBookId);
|
||
wrapper.eq(UserEbookBuyEntity::getUserId,userId);
|
||
wrapper.eq(BookEntity::getDelFlag,0);
|
||
wrapper.eq(BookEntity::getClockIn,1);
|
||
Page<BookEntity> userBookEntityPage = userEbookBuyDao.selectJoinPage(new Page<BookEntity>(page, limit), BookEntity.class, wrapper);
|
||
return R.ok().put("page",userBookEntityPage);
|
||
}
|
||
|
||
/**
|
||
* 获取打卡推荐图书
|
||
* @param userId
|
||
* @param limit
|
||
* @param page
|
||
* @return
|
||
*/
|
||
@RequestMapping("/getBestClockBooks")
|
||
public R getBestClockBooks(@RequestParam Integer userId,@RequestParam Integer limit,@RequestParam Integer page){
|
||
String not_ex_sql = "select 1 from user_ebook_buy where book.id = book_id and user_id = "+userId;
|
||
LambdaQueryWrapper<BookEntity> wrapper = new LambdaQueryWrapper<>();
|
||
wrapper.eq(BookEntity::getDelFlag,0);
|
||
wrapper.eq(BookEntity::getClockIn,1);
|
||
wrapper.notExists(not_ex_sql);
|
||
Page<BookEntity> bookEntityPage = bookService.getBaseMapper().selectPage(new Page<>(page, limit), wrapper);
|
||
return R.ok().put("page",bookEntityPage);
|
||
}
|
||
|
||
|
||
/**
|
||
* 修改
|
||
*/
|
||
@RequestMapping("/update")
|
||
public R update(@RequestBody BookClockinPunchEntity bookClockinPunchEntity) {
|
||
bookClockinPunchService.updateById(bookClockinPunchEntity);
|
||
return R.ok("提交成功");
|
||
}
|
||
|
||
|
||
/**
|
||
* 删除
|
||
*/
|
||
@RequestMapping("/delete")
|
||
public R delete(@RequestBody Integer[] ids) {
|
||
bookClockinPunchService.removeByIds(Arrays.asList(ids));
|
||
|
||
return R.ok();
|
||
}
|
||
|
||
|
||
/**
|
||
* 获取评论
|
||
* @param bookClockinPunchEntity
|
||
* @return
|
||
*/
|
||
@RequestMapping("/punchcoments")
|
||
public R punchcoments(@RequestBody BookClockinPunchEntity bookClockinPunchEntity) {
|
||
//图书Id
|
||
Integer bookId = bookClockinPunchEntity.getBookId();
|
||
//用户id
|
||
Integer userId = bookClockinPunchEntity.getUserId();
|
||
//天数
|
||
Integer days = bookClockinPunchEntity.getDays();
|
||
|
||
ArrayList<Object> list = new ArrayList<>();
|
||
List<BookClockinCommentEntity> bookClockinCommentEntities = bookClockinCommentService.getBaseMapper().selectList(new QueryWrapper<BookClockinCommentEntity>()
|
||
.eq("user_id", userId)
|
||
.eq("book_id", bookId)
|
||
.eq("task_id", days)
|
||
//pid等于1时为一级评论
|
||
// .eq("pid",1)
|
||
);
|
||
for (BookClockinCommentEntity bookClockinCommentEntity : bookClockinCommentEntities) {
|
||
HashMap<Object, Object> map = new HashMap<>();
|
||
String name = "";
|
||
String avatar="";
|
||
|
||
Integer userId1 = bookClockinCommentEntity.getUserId();
|
||
Integer bookId1 = bookClockinCommentEntity.getBookId();
|
||
String content = bookClockinCommentEntity.getContent();
|
||
String images = bookClockinCommentEntity.getImages();
|
||
Date createTime = bookClockinCommentEntity.getCreateTime();
|
||
|
||
|
||
|
||
List<MyUserEntity> id = myUserService.getBaseMapper().selectList(new QueryWrapper<MyUserEntity>().eq("id", userId));
|
||
for (MyUserEntity user : id) {
|
||
name = user.getNickname();
|
||
avatar = user.getAvatar();
|
||
}
|
||
map.put("userid", userId1);
|
||
map.put("name", name);
|
||
map.put("avatar", avatar);
|
||
map.put("bookid", bookId1);
|
||
map.put("content", content);
|
||
map.put("images", images);
|
||
map.put("createdate", createTime);
|
||
list.add(map);
|
||
|
||
}
|
||
|
||
Collections.reverse(list);
|
||
return R.ok().put("list", list);
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|