package com.hc.business.service.impl;

import com.hc.business.dto.ZccmUserCourseDto;
import com.hc.business.mapper.ZccmUserCourseMapper;
import com.hc.business.model.ZccmUserCourse;
import com.hc.business.service.ZccmUserCourseService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 用户课程表(ZccmUserCourse)表服务实现类
 *
 * @author java
 * @since 2020-11-09 10:05:19
 */

@Service("userCourseService")
public class ZccmUserCourseServiceImpl implements ZccmUserCourseService {

    private final ZccmUserCourseMapper userCourseMapper;

    public ZccmUserCourseServiceImpl(ZccmUserCourseMapper userCourseMapper) {
        this.userCourseMapper = userCourseMapper;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param userCourseId 主键
     * @return 实例对象
     */
    @Override
    public ZccmUserCourse queryById(Integer userCourseId) {
        return this.userCourseMapper.selectByPrimaryKey(userCourseId);
    }

    /**
     * 新增数据
     *
     * @param userCourse 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmUserCourse insert(ZccmUserCourse userCourse) {
        List<ZccmUserCourse> userCourses =
                this.userCourseMapper.selectUserCourse(
                        userCourse.getUserId(),
                        userCourse.getClassId(),
                        userCourse.getCourseId());
        if (!CollectionUtils.isEmpty(userCourses)) {
            return userCourses.get(0);
        }
        this.userCourseMapper.insert(userCourse);
        return userCourse;
    }

    /**
     * 修改数据
     *
     * @param userCourse 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmUserCourse update(ZccmUserCourse userCourse) {
        this.userCourseMapper.updateByPrimaryKeySelective(userCourse);
        return this.queryById(userCourse.getUserCourseId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userCourseId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userCourseId) {
        return this.userCourseMapper.deleteByPrimaryKey(userCourseId) > 0;
    }

    /**
     * 分页查询多条数据,根据user_id查询该用户下所有的课程
     *
     * @param page 页码
     * @param size 查询条数
     * @return 对象列表
     */
    @Override
    public List<ZccmUserCourseDto> queryAllByLimit(Integer userId, Integer classId, int page, int size) {
        //  return PageHelper.startPage(page, size).doSelectPageInfo(this.userCourseMapper::selectAll);
        return userCourseMapper.selectUserCourseList(userId, classId);
    }

}