package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.mapper.ZccmUserAdviseMapper;
import com.hc.business.model.ZccmUserAdvise;
import com.hc.business.service.ZccmUserAdviseService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 用户建议表
 * (ZccmUserAdvise)表服务实现类
 *
 * @author java
 * @since 2020-11-16 14:20:04
 */

@Service("userAdviseService")
public class ZccmUserAdviseServiceImpl implements ZccmUserAdviseService {

    private final ZccmUserAdviseMapper userAdviseMapper;

    public ZccmUserAdviseServiceImpl(ZccmUserAdviseMapper userAdviseMapper) {
        this.userAdviseMapper = userAdviseMapper;
    }

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

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

    /**
     * 修改数据
     *
     * @param userAdvise 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmUserAdvise update(ZccmUserAdvise userAdvise) {
        this.userAdviseMapper.updateByPrimaryKeySelective(userAdvise);
        return this.queryById(userAdvise.getAdviceId());
    }

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

    /**
     * 分页查询多条数据
     *
     * @param page 页码
     * @param size 查询条数
     * @return 对象列表
     */
    @Override
    public PageInfo queryAllByLimit(Integer adviceType, int page, int size) {
        PageHelper.startPage(page, size);
        if (Objects.isNull(adviceType)) {
            List<ZccmUserAdvise> userAdvises = this.userAdviseMapper.queryAll();
            return new PageInfo<>(userAdvises);
        }
        List<ZccmUserAdvise> userAdvises = this.userAdviseMapper.queryAllByAdviceType(adviceType);
        return new PageInfo<>(userAdvises);
    }

    /**
     * 依据用户获取建议
     *
     * @param userId 用户id
     * @return 实例对象数组
     */
    @Override
    public PageInfo queryAllByUserId(Integer userId, int page, int size) {
        PageHelper.startPage(page, size);
        List<ZccmUserAdvise> userAdvises = this.userAdviseMapper.queryAllByUserId(userId);
        return new PageInfo(userAdvises);
    }

}