AdminUserStudyBiz.java 5.7 KB
Newer Older
F
fengyw 已提交
1 2
package com.roncoo.education.course.service.admin.biz;

F
fengyw 已提交
3 4
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
F
fengyw 已提交
5 6 7 8
import com.roncoo.education.common.core.base.Page;
import com.roncoo.education.common.core.base.PageUtil;
import com.roncoo.education.common.core.base.Result;
import com.roncoo.education.common.core.tools.BeanUtil;
F
fengyw 已提交
9 10 11 12 13 14 15 16
import com.roncoo.education.common.service.BaseBiz;
import com.roncoo.education.course.dao.CourseChapterDao;
import com.roncoo.education.course.dao.CourseChapterPeriodDao;
import com.roncoo.education.course.dao.UserStudyDao;
import com.roncoo.education.course.dao.impl.mapper.entity.CourseChapter;
import com.roncoo.education.course.dao.impl.mapper.entity.CourseChapterExample;
import com.roncoo.education.course.dao.impl.mapper.entity.CourseChapterPeriod;
import com.roncoo.education.course.dao.impl.mapper.entity.UserStudy;
F
fengyw 已提交
17 18 19 20
import com.roncoo.education.course.service.admin.req.AdminUserStudyEditReq;
import com.roncoo.education.course.service.admin.req.AdminUserStudyPageReq;
import com.roncoo.education.course.service.admin.req.AdminUserStudySaveReq;
import com.roncoo.education.course.service.admin.resp.AdminUserStudyPageResp;
F
fengyw 已提交
21
import com.roncoo.education.course.service.admin.resp.AdminUserStudyPeriodPageResp;
F
fengyw 已提交
22 23 24 25 26
import com.roncoo.education.course.service.admin.resp.AdminUserStudyViewResp;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
F
fengyw 已提交
27 28 29
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
F
fengyw 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42

/**
 * ADMIN-课程用户学习日志
 *
 * @author wujing
 */
@Component
@RequiredArgsConstructor
public class AdminUserStudyBiz extends BaseBiz {

    @NotNull
    private final UserStudyDao dao;

F
fengyw 已提交
43 44 45 46 47
    @NotNull
    private final CourseChapterDao courseChapterDao;
    @NotNull
    private final CourseChapterPeriodDao courseChapterPeriodDao;

F
fengyw 已提交
48 49 50 51 52 53 54
    /**
     * 课程用户学习日志分页
     *
     * @param req 课程用户学习日志分页查询参数
     * @return 课程用户学习日志分页查询结果
     */
    public Result<Page<AdminUserStudyPageResp>> page(AdminUserStudyPageReq req) {
F
fengyw 已提交
55 56 57 58 59 60 61
        CourseChapterExample example = new CourseChapterExample();
        CourseChapterExample.Criteria c = example.createCriteria();
        if (ObjectUtil.isNotEmpty(req.getCourseId())) {
            c.andCourseIdEqualTo(req.getCourseId());
        }
        example.setOrderByClause("sort asc, id desc");
        Page<CourseChapter> page = courseChapterDao.page(req.getPageCurrent(), req.getPageSize(), example);
F
fengyw 已提交
62
        Page<AdminUserStudyPageResp> respPage = PageUtil.transform(page, AdminUserStudyPageResp.class);
F
fengyw 已提交
63 64 65 66 67 68
        if (CollUtil.isNotEmpty(respPage.getList())) {
            List<Long> chapterIds = respPage.getList().stream().map(AdminUserStudyPageResp::getId).collect(Collectors.toList());
            // 课时
            List<CourseChapterPeriod> periodList = courseChapterPeriodDao.listByChapterIds(chapterIds);
            if (CollUtil.isNotEmpty(periodList)) {
                // 记录
F
fengyw 已提交
69
                List<UserStudy> userStudyList = dao.listByUserIdAndCourseId(req.getUserId(), req.getCourseId());
F
fengyw 已提交
70 71 72 73 74 75
                Map<Long, UserStudy> userStudyMap = userStudyList.stream().collect(Collectors.toMap(item -> item.getPeriodId(), item -> item));
                Map<Long, List<CourseChapterPeriod>> periodMap = periodList.stream().collect(Collectors.groupingBy(CourseChapterPeriod::getChapterId, Collectors.toList()));

                for (AdminUserStudyPageResp resp : respPage.getList()) {
                    resp.setUserStudyPeriodPageRespList(BeanUtil.copyProperties(periodMap.get(resp.getId()), AdminUserStudyPeriodPageResp.class));
                    for (AdminUserStudyPeriodPageResp period : resp.getUserStudyPeriodPageRespList()) {
F
fengyw 已提交
76
                        UserStudy userStudy = userStudyMap.get(period.getId());
F
fengyw 已提交
77 78 79 80 81
                        if (ObjectUtil.isNotEmpty(userStudy)) {
                            period.setProgress(userStudy.getProgress());
                            period.setGmtCreate(userStudy.getGmtCreate());
                            period.setGmtModified(userStudy.getGmtModified());
                        }
F
fengyw 已提交
82 83 84 85
                    }
                }
            }
        }
F
fengyw 已提交
86 87 88 89 90 91 92 93 94 95 96 97
        return Result.success(respPage);
    }

    /**
     * 课程用户学习日志添加
     *
     * @param req 课程用户学习日志
     * @return 添加结果
     */
    public Result<String> save(AdminUserStudySaveReq req) {
        UserStudy record = BeanUtil.copyProperties(req, UserStudy.class);
        if (dao.save(record) > 0) {
F
fengyw 已提交
98
            return Result.success("操作成功");
F
fengyw 已提交
99
        }
F
fengyw 已提交
100
        return Result.error("操作失败");
F
fengyw 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    }

    /**
     * 课程用户学习日志查看
     *
     * @param id 主键ID
     * @return 课程用户学习日志
     */
    public Result<AdminUserStudyViewResp> view(Long id) {
        return Result.success(BeanUtil.copyProperties(dao.getById(id), AdminUserStudyViewResp.class));
    }

    /**
     * 课程用户学习日志修改
     *
     * @param req 课程用户学习日志修改对象
     * @return 修改结果
     */
    public Result<String> edit(AdminUserStudyEditReq req) {
        UserStudy record = BeanUtil.copyProperties(req, UserStudy.class);
        if (dao.updateById(record) > 0) {
F
fengyw 已提交
122
            return Result.success("操作成功");
F
fengyw 已提交
123
        }
F
fengyw 已提交
124
        return Result.error("操作失败");
F
fengyw 已提交
125 126 127 128 129 130 131 132 133 134
    }

    /**
     * 课程用户学习日志删除
     *
     * @param id ID主键
     * @return 删除结果
     */
    public Result<String> delete(Long id) {
        if (dao.deleteById(id) > 0) {
F
fengyw 已提交
135
            return Result.success("操作成功");
F
fengyw 已提交
136
        }
F
fengyw 已提交
137
        return Result.error("操作失败");
F
fengyw 已提交
138 139
    }
}