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.common.utils.CommonUtil;
import com.template.mapper.SmartDataClassMapper;
import com.template.model.pojo.SmartDataClass;
import com.template.model.pojo.SmartDataClass;
import com.template.mapper.SmartDataClassMapper;
import com.template.model.pojo.SmartDataSource;
import com.template.model.result.PageUtils;
import com.template.services.SmartDataClassService;
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.sql.SQLIntegrityConstraintViolationException;
import java.util.Map;

/**
 * <p>
 * 数据源类别 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2023-12-05
 */
@Service
public class SmartDataClassServiceImpl extends ServiceImpl<SmartDataClassMapper, SmartDataClass> implements SmartDataClassService {
    @Autowired
    private SmartDataClassMapper smartDataClassMapper;

    /**
     * 插入数据源类别
     *
     * @param smartDataClass 数据源类别对象
     * @return 返回
     */
    @Override
    public int insertSmartDataClass(SmartDataClass smartDataClass) {
        return smartDataClassMapper.insert(smartDataClass);
    }

    /**
     * 更新数据源类别
     *
     * @param smartDataClass 数据源类别对象
     * @return 返回
     */
    @Override
    public int updateSmartDataClass(SmartDataClass smartDataClass) {
        int result = smartDataClassMapper.updateById(smartDataClass);
        return result;
    }

    /**
     * 数据源类别分页查询
     *
     * @param currentPage 当前页
     * @param pageCount   每页显示数量
     * @param name        数据源类别名称
     * @return 返回
     */
    @Override
    public PageUtils<SmartDataClass> queryPageSmartDataClasss(int currentPage, int pageCount, String name) {
        Page<SmartDataClass> page = new Page<>(currentPage, pageCount);
        QueryWrapper<SmartDataClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name), "ds_cls_name", name);
        queryWrapper.orderByDesc("ds_cls_update_time");
        IPage<SmartDataClass> result = smartDataClassMapper.selectPage(page, queryWrapper);
        return new PageUtils<>(result);
    }

    /**
     * 删除数据源类别
     *
     * @param id 数据源类别id
     * @return 返回
     */
    @Override
    public int deleteSmartDataClassById(int id) {
        return smartDataClassMapper.deleteById(id);
    }

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