package com.jsc.oscm.service;

import com.jsc.oscm.dao.RulesParserRepository;
import com.jsc.oscm.entity.RulesParser;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

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

@Service
public class RulesParserService {

    @Resource
    RulesParserRepository rulesParserRepository;

    public RulesParser save(RulesParser rulesParser) {
        return rulesParserRepository.save(rulesParser);
    }

    public void delete(RulesParser rulesParser) {
        rulesParserRepository.delete(rulesParser);
    }

    public RulesParser findById(Integer id) {
        return rulesParserRepository.findById(id).orElse(null);
    }

    public List<RulesParser> findAll() {
        return rulesParserRepository.findAll();
    }

    public List<RulesParser> queryPageable(RulesParser rulesParser, Integer index, Integer size) {
        Example<RulesParser> example = Example.of(rulesParser);
        if (rulesParser.getParserName() != null) {
            ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("parserName", ExampleMatcher.GenericPropertyMatchers.contains());
            example = Example.of(rulesParser, matcher);
        }
        Pageable pageable = PageRequest.of(index, size, Sort.by(Sort.Order.desc("id")));
        return rulesParserRepository.findAll(example, pageable).getContent();
    }

    public long queryCount(RulesParser rulesParser) {
        Example<RulesParser> example = Example.of(rulesParser);
        if (rulesParser.getParserName() != null) {
            ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("parserName", ExampleMatcher.GenericPropertyMatchers.contains());
            example = Example.of(rulesParser, matcher);
        }
        return rulesParserRepository.count(example);
    }

}
