package com.template.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.template.mapper.AlumniNewsMapper;
import com.template.model.pojo.AlumniNews;
import com.template.model.pojo.AlumniNews;
import com.template.mapper.AlumniNewsMapper;
import com.template.model.result.PageUtils;
import com.template.services.AlumniNewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2024-12-02
 */
@Service
public class AlumniNewsServiceImpl extends ServiceImpl<AlumniNewsMapper, AlumniNews> implements AlumniNewsService {
    @Autowired
    private AlumniNewsMapper alumniNewsMapper;

    @Override
    public int insertAlumniNews(AlumniNews au) {
        int result = alumniNewsMapper.insert(au);
        return result;
    }

    @Override
    public int updateAlumniNews(AlumniNews au) {
        int result = alumniNewsMapper.updateById(au);
        return result;
    }

    @Override
    public AlumniNews queryDataById(Integer id) {
        AlumniNews result = alumniNewsMapper.selectById(id);
        return result;
    }

    @Override
    public int deleteDataById(Integer id) {
        int result = alumniNewsMapper.deleteById(id);
        return result;
    }

    @Override
    public PageUtils<AlumniNews> queryPageList(int currentPage, int pageCount, String theme, String userName, String startTime, String endTime) {
        Page<AlumniNews> page = new Page<>(currentPage, pageCount);
        QueryWrapper<AlumniNews> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(theme), "theme", theme);
        queryWrapper.like(StringUtils.hasText(userName), "user_name", userName);
        if (StringUtils.hasText(startTime) && StringUtils.hasText(endTime)) {
            queryWrapper.between("create_time", startTime, endTime);
        }
        IPage<AlumniNews> result = alumniNewsMapper.selectPage(page, queryWrapper);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<AlumniNews> queryPageByCategoryId(int currentPage, int pageCount, Integer categoryId) {
        Page<AlumniNews> page = new Page<>(currentPage, pageCount);
        QueryWrapper<AlumniNews> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(categoryId != null && categoryId.intValue() > 0, "category_id", categoryId);
        IPage<AlumniNews> result = alumniNewsMapper.selectPage(page, queryWrapper);
        return new PageUtils(result);
    }

}
