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.SmartSchoolMapper;
import com.template.model.pojo.SmartSchool;
import com.template.model.pojo.SmartSchool;
import com.template.mapper.SmartSchoolMapper;
import com.template.model.result.PageUtils;
import com.template.services.SmartSchoolService;
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 SmartSchoolServiceImpl extends ServiceImpl<SmartSchoolMapper, SmartSchool> implements SmartSchoolService {
    @Autowired
    private SmartSchoolMapper smartSchoolMapper;

    @Override
    public int insertSmartSchool(SmartSchool sa) {
        int result = smartSchoolMapper.insert(sa);
        return result;
    }

    @Override
    public int updateSmartSchool(SmartSchool sa) {
        int result = smartSchoolMapper.updateById(sa);
        return result;
    }

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

    @Override
    public int deleteSmartSchoolById(int id) {
        int result = smartSchoolMapper.deleteById(id);
        return result;
    }

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

    @Override
    public SmartSchool getSmartSchool() {

        SmartSchool newOne = this.getOne(new LambdaQueryWrapper<SmartSchool>()
                .orderBy(true, false, SmartSchool::getCreateTime)
                .last("limit 1")
        );

        return newOne == null ? new SmartSchool() : newOne;
    }
}
