package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.dto.ZccmClassCourseDto;
import com.hc.business.mapper.ZccmClassCourseMapper;
import com.hc.business.model.ZccmClassCourse;
import com.hc.business.service.ZccmClassCourseService;
import com.hc.core.utils.exception.ResponseException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.function.IntFunction;

/**
 * 班次课程表(ZccmClassCourse)表服务实现类
 *
 * @author java
 * @since 2020-11-24 11:30:09
 */

@Service("classCourseService")
public class ZccmClassCourseServiceImpl implements ZccmClassCourseService {

    private final ZccmClassCourseMapper classCourseMapper;

    public ZccmClassCourseServiceImpl(ZccmClassCourseMapper classCourseMapper) {
        this.classCourseMapper = classCourseMapper;
    }

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

    /**
     * 新增数据
     *
     * @param classCourse 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmClassCourse insert(ZccmClassCourse classCourse) {
        Integer classId = classCourse.getClassId();
        Integer courseId = classCourse.getCourseId();
        List<ZccmClassCourse> classCourses =
                this.classCourseMapper.queryByClassIdWithCourseId(classId, courseId);
        if (!CollectionUtils.isEmpty(classCourses)) {
            return classCourses.get(0);
        }
        classCourse.setCreateTime(System.currentTimeMillis());
        this.classCourseMapper.insert(classCourse);
        return classCourse;
    }

    /**
     * 修改数据
     *
     * @param classCourse 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmClassCourse update(ZccmClassCourse classCourse) {
        this.classCourseMapper.updateByPrimaryKeySelective(classCourse);
        return this.queryById(classCourse.getClassCourseId());
    }

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

    /**
     * 分页查询多条数据
     *
     * @param page 页码
     * @param size 查询条数
     * @return 对象列表
     */
    @Override
    public PageInfo queryAllByLimit(int page, int size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(this.classCourseMapper::selectAll);
    }

    @Override
    public PageInfo queryByClassId(Integer classId, Integer courseType,
                                   Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<ZccmClassCourseDto> classCourseDtoList
                = this.classCourseMapper.queryByClassId(classId, courseType);
        return new PageInfo(classCourseDtoList);
    }
}