package com.jsc.oscm.service;

import com.jsc.oscm.dao.TargetSensitiveRepository;
import com.jsc.oscm.entity.TargetSensitive;
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.Collections;
import java.util.List;

@Service
public class TargetSensitiveService {

    @Resource
    TargetSensitiveRepository targetSensitiveRepository;

    public TargetSensitive save(TargetSensitive targetSensitive) {
        return targetSensitiveRepository.save(targetSensitive);
    }

    public boolean delete(List<String> ids) {
        for (String id : ids) {
            TargetSensitive targetSensitive = queryById(Integer.valueOf(id));
            targetSensitive.setDelFlag("1");
            save(targetSensitive);
        }
        return true;
    }

    public boolean delete(Integer id) {
        TargetSensitive targetSensitive = queryById(id);
        targetSensitive.setDelFlag("1");
        save(targetSensitive);
        return true;
    }

    public TargetSensitive queryById(Integer id) {
        return targetSensitiveRepository.getOne(id);
    }

    public List<TargetSensitive> queryPageable(TargetSensitive targetSensitive, Integer pageIndex, Integer pageSize) {
        Pageable page = PageRequest.of(pageIndex, pageSize, Sort.by(Sort.Order.desc("id")));
        Example<TargetSensitive> example = Example.of(targetSensitive);
        return targetSensitiveRepository.findAll(example, page).getContent();
    }

    public List<TargetSensitive> query(TargetSensitive targetSensitive) {
        Example<TargetSensitive> example = Example.of(targetSensitive);
        List<TargetSensitive> resultList = targetSensitiveRepository.findAll(example);
        Collections.shuffle(resultList);
        return resultList;
    }

    public Long queryCount(TargetSensitive targetSensitive) {
        Example<TargetSensitive> example = Example.of(targetSensitive);
        return targetSensitiveRepository.count(example);
    }

    public List<Object> getCountryList() {
        return targetSensitiveRepository.getCountryList();
    }
}
