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.SmartClassMapper;
import com.template.model.pojo.SmartClass;
import com.template.mapper.SmartClassMapper;
import com.template.model.pojo.SmartClass;
import com.template.model.pojo.SmartGrade;
import com.template.model.pojo.SmartSchool;
import com.template.model.result.PageUtils;
import com.template.services.SmartClassService;
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;

import java.util.List;

/**
 * <p>
 * 楼栋表 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2023-12-25
 */
@Service
public class SmartClassServiceImpl extends ServiceImpl<SmartClassMapper, SmartClass> implements SmartClassService {
    @Autowired
    private SmartClassMapper smartClassMapper;

    @Override
    public int updateSmartClass(SmartClass sc) {
        int result = smartClassMapper.updateById(sc);
        return result;
    }

    @Override
    public SmartClass querySmartClassById(Integer id) {
        SmartClass result = smartClassMapper.selectById(id);
        return result;
    }

    @Override
    public List<SmartClass> getSmartClasss() {
        List<SmartClass> result = smartClassMapper.selectList(null);
        return result;
    }

    @Override
    public int existSmartClass(String className, Integer gradeId) {
        //查重
        QueryWrapper<SmartClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(gradeId != null, "grade_id", gradeId);
        queryWrapper.eq(StringUtils.hasText(className), "name", className);
        int existCount = smartClassMapper.selectCount(queryWrapper);
        return existCount;
    }

    @Override
    public int insertSmartClass(SmartClass sc) {
        int result = smartClassMapper.insert(sc);
        return result;
    }

    @Override
    public SmartClass getSmartClassById(int id) {
        SmartClass result = smartClassMapper.selectById(id);
        return result;
    }

    @Override
    public List<SmartClass> getSmartClassByGradeID(Integer gradeId) {
        QueryWrapper<SmartClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(gradeId != null, "grade_id", gradeId);
        List<SmartClass> result = smartClassMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public SmartClass existSmartClassData(Integer gradeId, String name, Integer classNo) {
        QueryWrapper<SmartClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(gradeId != null, "grade_id", gradeId);
        queryWrapper.eq(classNo != null, "class_no", classNo);
        queryWrapper.eq(StringUtils.hasText(name), "name", name);
        SmartClass result = smartClassMapper.selectOne(queryWrapper);
        return result;
    }

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