AuthUserCourseCommentBiz.java 2.8 KB
Newer Older
F
fengyw 已提交
1 2
package com.roncoo.education.course.service.auth.biz;

F
fengyw 已提交
3
import cn.hutool.core.collection.CollUtil;
F
fengyw 已提交
4 5
import com.roncoo.education.common.config.ThreadContext;
import com.roncoo.education.common.core.base.Page;
F
fengyw 已提交
6
import com.roncoo.education.common.core.base.PageUtil;
F
fengyw 已提交
7 8 9
import com.roncoo.education.common.core.base.Result;
import com.roncoo.education.common.core.tools.BeanUtil;
import com.roncoo.education.common.service.BaseBiz;
F
fengyw 已提交
10
import com.roncoo.education.course.dao.CourseDao;
F
fengyw 已提交
11
import com.roncoo.education.course.dao.UserCourseCommentDao;
F
fengyw 已提交
12
import com.roncoo.education.course.dao.impl.mapper.entity.Course;
F
fengyw 已提交
13
import com.roncoo.education.course.dao.impl.mapper.entity.UserCourseComment;
F
fengyw 已提交
14
import com.roncoo.education.course.dao.impl.mapper.entity.UserCourseCommentExample;
F
fengyw 已提交
15 16 17
import com.roncoo.education.course.service.auth.req.AuthUserCourseCommentPageReq;
import com.roncoo.education.course.service.auth.req.AuthUserCourseCommentReq;
import com.roncoo.education.course.service.auth.resp.AuthUserCourseCommentResp;
F
fengyw 已提交
18
import com.roncoo.education.course.service.biz.resp.CourseResp;
F
fengyw 已提交
19 20 21 22
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
F
fengyw 已提交
23 24 25
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
F
fengyw 已提交
26 27 28 29 30 31 32 33 34 35 36 37

/**
 * AUTH-课程评论
 *
 * @author wujing
 */
@Component
@RequiredArgsConstructor
public class AuthUserCourseCommentBiz extends BaseBiz {

    @NotNull
    private final UserCourseCommentDao dao;
F
fengyw 已提交
38 39
    @NotNull
    private final CourseDao courseDao;
F
fengyw 已提交
40 41

    public Result<Page<AuthUserCourseCommentResp>> listForPage(AuthUserCourseCommentPageReq req) {
F
fengyw 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54
        UserCourseCommentExample example = new UserCourseCommentExample();
        example.createCriteria().andUserIdEqualTo(ThreadContext.userId());
        example.setOrderByClause("id desc");

        Page<AuthUserCourseCommentResp> resp = PageUtil.transform(dao.page(req.getPageCurrent(), req.getPageSize(), example), AuthUserCourseCommentResp.class);
        if (CollUtil.isNotEmpty(resp.getList())) {
            List<Long> courseIds = resp.getList().stream().map(AuthUserCourseCommentResp::getCourseId).collect(Collectors.toList());
            Map<Long, Course> courseMap = courseDao.listByIds(courseIds).stream().collect(Collectors.toMap(Course::getId, item -> item));
            for (AuthUserCourseCommentResp res : resp.getList()) {
                res.setCourseResp(BeanUtil.copyProperties(courseMap.get(res.getCourseId()), CourseResp.class));
            }
        }
        return Result.success(resp);
F
fengyw 已提交
55 56 57 58 59 60 61 62 63 64
    }

    public Result<String> add(AuthUserCourseCommentReq req) {
        UserCourseComment userCourseComment = BeanUtil.copyProperties(req, UserCourseComment.class);
        userCourseComment.setUserId(ThreadContext.userId());
        dao.save(userCourseComment);
        return Result.success("评论成功");
    }

}