package io.renren.modules.generator.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.datasource.annotation.DataSource;
import io.renren.modules.generator.dao.CategoryMapper;
import io.renren.modules.generator.entity.CategoryEntity;
import io.renren.modules.generator.service.CategoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;

@Service
@DataSource("slave2")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryEntity> implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String categoryName = (String)params.get("categoryName");
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
                        .like(StringUtils.isNotBlank(categoryName),"name", categoryName)
        );
        return new PageUtils(page);

    }

    @Override
    public void deleteBatch(Long[] categoryIds) {
        this.removeByIds(Arrays.asList(categoryIds));
    }

    @Override
    public Long getByName(String classificationtag) {

        return categoryMapper.getByName(classificationtag);
    }
}
