package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.mapper.MsgsMapper;
import com.hc.business.model.Msgs;
import com.hc.business.service.MsgsService;
import org.springframework.stereotype.Service;

/**
 * (Msgs)表服务实现类
 *
 * @author makejava
 * @since 2024-02-29 18:06:59
 */

@Service
public class MsgsServiceImpl implements MsgsService {

    private final MsgsMapper msgsMapper;

    public MsgsServiceImpl(MsgsMapper msgsMapper) {
        this.msgsMapper = msgsMapper;
    }

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

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

    /**
     * 修改数据
     *
     * @param msgs 实例对象
     * @return 实例对象
     */
    @Override
    public Msgs update(Msgs msgs) {
        this.msgsMapper.updateByPrimaryKeySelective(msgs);
        return this.queryById(msgs.getMsgId());
    }

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

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