package com.hc.gpbusiness.service.impl;

import com.github.pagehelper.PageHelper;
import com.hc.gpbusiness.dto.WebCourseDto;
import com.hc.gpbusiness.mapper.WebCourseMapper;
import com.hc.gpbusiness.mapper.WebCourseTypeMapper;
import com.hc.gpbusiness.model.WebCourse;
import com.hc.gpbusiness.model.WebCourseType;
import com.hc.gpbusiness.service.WebCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程表(WebCourse)表服务实现类
 *
 * @author java
 * @since 2020-07-29 16:23:06
 */

@Service("courseService")
public class WebCourseServiceImpl implements WebCourseService {

    private final WebCourseMapper courseMapper;

    private final WebCourseTypeMapper courseTypeMapper;

    public WebCourseServiceImpl(WebCourseMapper courseMapper,WebCourseTypeMapper courseTypeMapper) {
        this.courseMapper = courseMapper;
        this.courseTypeMapper = courseTypeMapper;
    }
    /**
     * 通过ID查询单条数据
     *
     * @param courseId 主键
     * @return 实例对象
     */
    @Override
    public WebCourseDto queryModelById(Integer courseId) {
        return copy(this.courseMapper.selectByPrimaryKey(courseId));
    }

    @Override
    public WebCourse queryById(Integer courseId) {
        return this.courseMapper.selectByPrimaryKey(courseId);
    }


    /**
     * 新增数据
     *
     * @param course 实例对象
     * @return 实例对象
     */
    @Override
    public WebCourse insert(WebCourse course) {
        course.setCreateTime(System.currentTimeMillis());
        this.courseMapper.insert(course);
        return course;
    }

    /**
     * 修改数据
     *
     * @param course 实例对象
     * @return 实例对象
     */
    @Override
    public WebCourse update(WebCourse course) {
        course.setUpdateTime(System.currentTimeMillis());
        this.courseMapper.updateByPrimaryKeySelective(course);
        return this.queryById(course.getCourseId());
    }

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

    /**
     * 分页查询多条数据
     *
     * @param page     页码
     * @param size     查询条数
     * @param moduleId 板块id
     * @return 对象列表
     */
    @Override
    public PageInfo queryAllByLimit(int page, int size, Integer moduleId) {
        PageInfo info = PageHelper.startPage(page, size).setOrderBy("create_time desc").doSelectPageInfo(() -> this.courseMapper.queryByModuleId(moduleId));
        info.setList(copyList(info.getList()));
        return info;
    }

    public List<WebCourseDto> copyList(List<WebCourse> records) {
        List<WebCourseDto> WebCourseDtoList = new ArrayList<>();
        for (WebCourse record : records) {
            WebCourseDtoList.add(copy(record));
        }
        return WebCourseDtoList;
    }

    public WebCourseDto copy(WebCourse entity) {
        WebCourseDto newsDto = new WebCourseDto();
        BeanUtils.copyProperties(entity, newsDto);

        WebCourseType type = courseTypeMapper.selectByPrimaryKey(entity.getTypeId());
        if (type != null) {
            newsDto.setTypeName(type.getTypeName());
        }

        return newsDto;
    }
}