package com.jsc.oscm.service;

import com.jsc.oscm.dao.MetaDictRepository;
import com.jsc.oscm.entity.MetaDict;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MetaDictService {

    @Resource
    MetaDictRepository metaDictRepository;

    public List<MetaDict> queryDictByColumn(String columnName) {
        List<MetaDict> metaDictList;
        MetaDict exampleDict = new MetaDict();
        exampleDict.setColumnName(columnName);
        Example<MetaDict> example = Example.of(exampleDict);
        metaDictList = metaDictRepository.findAll(example);
        return metaDictList;
    }

    public MetaDict save(MetaDict metaDict) {
        return metaDictRepository.save(metaDict);
    }

    public void delete(MetaDict metaDict) {
        metaDictRepository.delete(metaDict);
    }

    public void deleteAll(List<MetaDict> metaDictList) {
        metaDictRepository.deleteAll(metaDictList);
    }

    public List<MetaDict> queryPageable(MetaDict metaDict, int pageIndex, int pageSize) {
        Pageable pageable = PageRequest.of(pageIndex, pageSize, Sort.by(Sort.Order.desc("id")));
        Example<MetaDict> example = Example.of(metaDict);
        return metaDictRepository.findAll(example, pageable).getContent();
    }

    public List<MetaDict> queryAll(MetaDict metaDict) {
        Example<MetaDict> example = Example.of(metaDict);
        return metaDictRepository.findAll(example);
    }

    public Long queryCount(MetaDict metaDict) {
        Example<MetaDict> example = Example.of(metaDict);
        return metaDictRepository.count(example);
    }

    public List<Object> getTableNameList() {
        return metaDictRepository.getTableNameList();
    }

    public List<Object> getColumnNameList() {
        return metaDictRepository.getColumnNameList();
    }

}
