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.model.pojo.*;
import com.template.mapper.SmartGradeMapper;
import com.template.model.result.PageUtils;
import com.template.services.SmartGradeService;
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 SmartGradeServiceImpl extends ServiceImpl<SmartGradeMapper, SmartGrade> implements SmartGradeService {

    @Autowired
    private SmartGradeMapper smartGradeMapper;

    @Override
    public int updateSmartGrade(SmartGrade sg) {
        int result = smartGradeMapper.updateById(sg);
        return result;
    }

    @Override
    public List<SmartGrade> getSmartGrades() {
        List<SmartGrade> result = smartGradeMapper.selectList(null);
        return result;
    }

    @Override
    public int insertSmartGrade(SmartGrade sg) {
        int result = smartGradeMapper.insert(sg);
        return result;
    }

    @Override
    public int existSmartGrade(Integer gradeNo, String gradeName) {
        QueryWrapper<SmartGrade> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(gradeNo != null, "grade_no", gradeNo);
        queryWrapper.eq(StringUtils.hasText(gradeName), "name", gradeName);
        int existCount = smartGradeMapper.selectCount(queryWrapper);
        return existCount;
    }

    @Override
    public SmartGrade existSmartGradeData(Integer gradeNo, String gradeName) {
        QueryWrapper<SmartGrade> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(gradeNo != null, "grade_no", gradeNo);
        queryWrapper.eq(StringUtils.hasText(gradeName), "name", gradeName);
        SmartGrade result = smartGradeMapper.selectOne(queryWrapper);
        return result;
    }

    @Override
    public SmartGrade querySmartGradeByBaisheng(String bsGradeNo) {
        QueryWrapper<SmartGrade> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(bsGradeNo), "bs_grade_no", bsGradeNo);
        SmartGrade result = smartGradeMapper.selectOne(queryWrapper);
        return result;
    }

    @Override
    public SmartGrade querySmartGradeById(Integer id) {
        SmartGrade result = smartGradeMapper.selectById(id);
        return result;
    }

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

}
