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.SmartDepartmentMapper;
import com.template.model.pojo.SmartDepartment;
import com.template.model.pojo.SmartDepartment;
import com.template.mapper.SmartDepartmentMapper;
import com.template.model.result.PageUtils;
import com.template.services.SmartDepartmentService;
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-04
 */
@Service
public class SmartDepartmentServiceImpl extends ServiceImpl<SmartDepartmentMapper, SmartDepartment> implements SmartDepartmentService {
    @Autowired
    private SmartDepartmentMapper smartDepartmentMapper;

    @Override
    public int insertSmartDepartment(SmartDepartment sa) {
        int result = smartDepartmentMapper.insert(sa);
        return result;
    }

    @Override
    public int updateSmartDepartment(SmartDepartment sa) {
        int result = smartDepartmentMapper.updateById(sa);
        return result;
    }

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

    @Override
    public int deleteSmartDepartmentById(int id) {
        int result = smartDepartmentMapper.deleteById(id);
        return result;
    }

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

    @Override
    public SmartDepartment getSmartByName(String name) {
        QueryWrapper<SmartDepartment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(name),"name", name);

        SmartDepartment result = smartDepartmentMapper.selectOne(queryWrapper);

        return result;
    }

    @Override
    public List<SmartDepartment> getSmartsByName(String name) {
        QueryWrapper<SmartDepartment> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name),"name", name);

        List<SmartDepartment> result = smartDepartmentMapper.selectList(queryWrapper);

        return result;
    }

    @Override
    public int existDataByName(String name) {
        QueryWrapper<SmartDepartment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(name),"name", name);

        int existCount = smartDepartmentMapper.selectCount(queryWrapper);

        return existCount;
    }
}
