package com.ruoyi.project.system.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.domain.TabBlog;
import com.ruoyi.project.system.domain.TabBlogExample;
import com.ruoyi.project.system.mapper.TabBlogMapper;
import com.ruoyi.project.system.service.TabBlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TabBlogServiceImpl implements TabBlogService {
    @Autowired
    private TabBlogMapper tabBlogMapper;

    @Override
    public int add(TabBlog tabBlog) {
        tabBlog.setCreateBy(SecurityUtils.getUsername());
        tabBlog.setCreateTime(new Date());
        int i = tabBlogMapper.insertSelective(tabBlog);
        return i;
    }

    @Override
    public int update(TabBlog tabBlog) {
        int i = tabBlogMapper.updateByPrimaryKeySelective(tabBlog);
        return i;
    }

    @Override
    public List<TabBlog> list(TabBlog tabBlog) {
        TabBlogExample example=new TabBlogExample();
        TabBlogExample.Criteria criteria = example.createCriteria();
        String title = tabBlog.getTitle();
        //标题查询
        if(StringUtils.isNotNull(title)){
            criteria.andTitleEqualTo(title);

        }
        //是否发布
        Integer status = tabBlog.getStatus();
        if(StringUtils.isNotNull(status)) {
            criteria.andStatusEqualTo(status);
        }
        //分类选着
        String type = tabBlog.getType();
        if(StringUtils.isNotNull(type)) {
            criteria.andTypeEqualTo(type);
        }
        return tabBlogMapper.selectByExample(example);
    }

    @Override
    public TabBlog get(Integer id) {
        return tabBlogMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(List<Integer> ids) {
        TabBlogExample example=new TabBlogExample();
        TabBlogExample.Criteria criteria = example.createCriteria();
        criteria.andBIdIn(ids);
        return tabBlogMapper.deleteByExample(example);
    }
}
