package com.template.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.SmartScreenshotMapper;
import com.template.model.pojo.SmartSchool;
import com.template.model.pojo.SmartScreenshot;
import com.template.mapper.SmartScreenshotMapper;
import com.template.model.pojo.SmartScreenshot;
import com.template.model.result.PageUtils;
import com.template.services.SmartScreenshotService;
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 2023-12-04
 */
@Service
public class SmartScreenshotServiceImpl extends ServiceImpl<SmartScreenshotMapper, SmartScreenshot> implements SmartScreenshotService {
    @Autowired
    private SmartScreenshotMapper smartScreenshotMapper;

    @Override
    public int insertSmartScreenshot(SmartScreenshot sa) {
        int result = smartScreenshotMapper.insert(sa);
        return result;
    }

    @Override
    public int updateSmartScreenshot(SmartScreenshot sa) {
        int result = smartScreenshotMapper.updateById(sa);
        return result;
    }

    @Override
    public PageUtils<SmartScreenshot> queryPageSmartScreenshots(int currentPage, int pageCount, String name) {
        Page<SmartScreenshot> page = new Page<>(currentPage, pageCount);
        QueryWrapper<SmartScreenshot> queryWrapper = new QueryWrapper<>();
        //queryWrapper.like(StringUtils.hasText(name), "name", name);
        IPage<SmartScreenshot> result = smartScreenshotMapper.selectPage(page,queryWrapper);
        return new PageUtils<>(result);
    }

    @Override
    public int deleteSmartScreenshotById(int id) {
        int result = smartScreenshotMapper.deleteById(id);
        return result;
    }

    @Override
    public SmartScreenshot getSmartById(int id) {
        SmartScreenshot result = smartScreenshotMapper.selectById(id);
        return result;
    }

    @Override
    public SmartScreenshot getSmartByTimes(int times) {
        QueryWrapper<SmartScreenshot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(times > 0, "screenshot_num", times);
        SmartScreenshot result = smartScreenshotMapper.selectOne(queryWrapper);
        return result;
    }

    @Override
    public SmartScreenshot getSmartLastOneByTimes(int times) {
        SmartScreenshot result = this.getOne(new LambdaQueryWrapper<SmartScreenshot>()
                .gt(times > 0 ,SmartScreenshot::getScreenshotNum, times)
                .orderBy(true, false, SmartScreenshot::getScreenshotNum)
                .last("limit 1")
        );
        return result;
    }
}
