package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.dto.ZccmUserStatusClassDto;
import com.hc.business.mapper.ZccmUserStatusMapper;
import com.hc.business.model.ZccmUserStatus;
import com.hc.business.service.ZccmUserStatusService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 用户班级学习进度表
 * (ZccmUserStatus)表服务实现类
 *
 * @author java
 * @since 2020-12-06 11:26:45
 */

@Service("userStatusService")
public class ZccmUserStatusServiceImpl implements ZccmUserStatusService {

    private final ZccmUserStatusMapper userStatusMapper;

    public ZccmUserStatusServiceImpl(ZccmUserStatusMapper userStatusMapper) {
        this.userStatusMapper = userStatusMapper;
    }

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

    /**
     * 新增数据
     *
     * @param userStatus 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmUserStatus insert(ZccmUserStatus userStatus) {
        List<ZccmUserStatus> userStatuses
                = this.userStatusMapper.queryByClassIdAndUserId(userStatus.getClassId(),
                userStatus.getUserId());
        if (!CollectionUtils.isEmpty(userStatuses)) {
            return userStatuses.get(0);
        }
        this.userStatusMapper.insert(userStatus);
        return userStatus;
    }

    /**
     * 修改数据
     *
     * @param userStatus 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmUserStatus update(ZccmUserStatus userStatus) {
        this.userStatusMapper.updateByPrimaryKeySelective(userStatus);
        return this.queryById(userStatus.getStatusId());
    }

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

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

    @Override
    public List<ZccmUserStatusClassDto> queryDtoByUserId(Integer userId) {
        return this.userStatusMapper.queryDtoByUserId(userId);
    }

    @Override
    public PageInfo queryByClassId(Integer classId, int page, int size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(() -> this.userStatusMapper.queryByClassId(classId));
    }


}