package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.dto.LiveClassDto;
import com.hc.business.mapper.ClassConfigMapper;
import com.hc.business.mapper.LiveClassMapper;
import com.hc.business.mapper.LiveMapper;
import com.hc.business.model.LiveClass;
import com.hc.business.service.LiveClassService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
 * 直播间班级配置表(LiveClass)表服务实现类
 *
 * @author makejava
 * @since 2024-01-20 10:54:50
 */

@Service("liveClassService")
public class LiveClassServiceImpl implements LiveClassService {

    private final LiveClassMapper liveClassMapper;
    private final LiveMapper liveMapper;
    private final ClassConfigMapper classConfigMapper;

    public LiveClassServiceImpl(LiveClassMapper liveClassMapper,LiveMapper liveMapper,ClassConfigMapper classConfigMapper) {

        this.liveClassMapper = liveClassMapper;
        this.liveMapper = liveMapper;
        this.classConfigMapper = classConfigMapper;
    }

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

    /**
     * 新增数据
     *
     * @param liveClass 实例对象
     * @return 实例对象
     */
    @Override
    public LiveClass insert(LiveClass liveClass) {
        this.liveClassMapper.insert(liveClass);
        return liveClass;
    }

    /**
     * 修改数据
     *
     * @param liveClass 实例对象
     * @return 实例对象
     */
    @Override
    public LiveClass update(LiveClass liveClass) {
        this.liveClassMapper.updateByPrimaryKeySelective(liveClass);
        return this.queryById(liveClass.getLiveClassId());
    }

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

    /**
     * 通过直播查询她关联的培训班
    * */
    @Override
    public List<LiveClassDto> queryByLiveId(Integer liveId) {
        List<LiveClass> list = this.liveClassMapper.queryByLiveId(liveId);
        List<LiveClassDto> dtoList = copyList(list);
        return dtoList;
    }


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


    private List<LiveClassDto> copyList(List<LiveClass> records) {
        List<LiveClassDto> list = new ArrayList<>();
        for (LiveClass record : records) {
            list.add(copy(record));
        }
        return list;
    }

    private LiveClassDto copy(LiveClass record) {
        LiveClassDto recordDto = new LiveClassDto();
        BeanUtils.copyProperties(record, recordDto);

        recordDto.setLiveModel(liveMapper.selectByPrimaryKey(record.getLiveId()));
        recordDto.setClassConfigModel(classConfigMapper.selectByPrimaryKey(record.getClassId()));

        return recordDto;
    }
}
