package com.peanut.modules.book.controller; import cn.hutool.core.date.DateUnit; import cn.hutool.core.date.DateUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; 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.*; import io.swagger.models.auth.In; 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; @Autowired UserBookClockService userBookClockService; /** * 列表 app post请求 /applistSS */ @RequestMapping("/applist") public R applist(@RequestParam Map params) { PageUtils page = bookClockinPunchService.queryPage(params); return R.ok().put("page", page); } /** * 信息 */ @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() .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 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 userBookEntityPage = userEbookBuyDao.selectJoinPage(new Page(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 wrapper = new LambdaQueryWrapper<>(); wrapper.eq(BookEntity::getDelFlag, 0); wrapper.eq(BookEntity::getClockIn, 1); wrapper.notExists(not_ex_sql); Page 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 list = new ArrayList<>(); List bookClockinCommentEntities = bookClockinCommentService.getBaseMapper().selectList(new QueryWrapper() .eq("user_id", userId) .eq("book_id", bookId) .eq("task_id", days) //pid等于1时为一级评论 // .eq("pid",1) ); for (BookClockinCommentEntity bookClockinCommentEntity : bookClockinCommentEntities) { HashMap 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 id = myUserService.getBaseMapper().selectList(new QueryWrapper().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); } }