package com.hc.gpbusiness.service.impl;

import com.github.pagehelper.PageHelper;
import com.hc.gpbusiness.mapper.WebUserMapper;
import com.hc.gpbusiness.model.WebUser;
import com.hc.gpbusiness.service.WebUserService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;

/**
 * 用户表(User)表服务实现类
 *
 * @author java
 * @since 2020-07-29 16:23:12
 */

@Service("userService")
public class WebUserServiceImpl implements WebUserService {

    private final WebUserMapper userMapper;

    public WebUserServiceImpl(WebUserMapper userMapper) {
        this.userMapper = userMapper;
    }

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

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

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public WebUser update(WebUser user) {
        user.setUpdateTime(System.currentTimeMillis());
        this.userMapper.updateByPrimaryKeySelective(user);
        return this.queryById(user.getUserId());
    }

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

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