package cn.reghao.autodop.dmaster.machine.db.query;

import cn.reghao.autodop.dmaster.machine.model.po.MachineHost;
import cn.reghao.autodop.dmaster.machine.model.po.MachineInfo;
import cn.reghao.autodop.dmaster.machine.db.repository.MachineHostRepository;
import cn.reghao.autodop.dmaster.machine.db.repository.MachineInfoRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2021-06-17 22:52:32
 */
@Deprecated
@Service
public class MachineQuery {
    private MachineInfoRepository infoRepository;
    private MachineHostRepository hostRepository;

    public MachineQuery(MachineInfoRepository infoRepository, MachineHostRepository hostRepository) {
        this.infoRepository = infoRepository;
        this.hostRepository = hostRepository;
    }

    public MachineHost query(String machineId) {
        Specification<MachineHost> specification = (root, query, cb) -> {
            Join<MachineHost, MachineInfo> innerJoin = root.join("machineInfo", JoinType.INNER);
            Predicate predicate = cb.equal(innerJoin.get("machineId"), machineId);
            //
            return cb.and(predicate);
        };
        List<MachineHost> list = hostRepository.findAll(specification);
        return list.isEmpty() ? null : list.get(0);
    }

    public List<MachineHost> queryByMatchAll(Map<String, String> kv) {
        Specification<MachineHost> specification = matchSpec(kv);
        return hostRepository.findAll(specification);
    }

    public Page<MachineHost> queryByMatchAll(Map<String, String> kv, PageRequest pageRequest) {
        Specification<MachineHost> specification = matchSpec(kv);
        return hostRepository.findAll(specification, pageRequest);
    }

    public List<MachineHost> queryByMachineIpv4(String machineIpv4) {
        Specification<MachineHost> specification = innerJoinMatchSpec(machineIpv4);
        return hostRepository.findAll(specification);
    }

    private Specification<MachineHost> matchSpec(Map<String, String> kv) {
        return (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            kv.forEach((name, value) -> {
                predicates.add(cb.equal(root.get(name), value));
            });
            return cb.and(predicates.toArray(new Predicate[0]));
        };
    }

    private Specification<MachineHost> innerJoinMatchSpec(String machineIpv4) {
        return (root, query, cb) -> {
            Join<MachineHost, MachineInfo> innerJoin = root.join("machineInfo", JoinType.INNER);
            // MachineInfo 中的 NetworkInfo 集合
            //ListJoin<MachineInfo, NetworkInfo> listJoin = innerJoin.joinList("networkInfos");
            return cb.and();
        };
    }
}
