package com.gyee.wisdom.dao.sql.alarm;

import com.gyee.wisdom.common.data.DataUtil;
import com.gyee.wisdom.common.data.alarm.ElectricalTestingPointAI;
import com.gyee.wisdom.dao.alarm.ElectricalTestingPointAIDao;
import com.gyee.wisdom.dao.sql.alarm.Entity.ElectricalTestingPointAIEntity;
import com.gyee.wisdom.dao.sql.alarm.repository.ElectricalTestingPointAIRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;



@Component
public class JpaElectricalTestingPointAIDao implements ElectricalTestingPointAIDao {

    @Autowired
    private ElectricalTestingPointAIRepository ElectricalTestingPointAIRepository;

    @Override
    public List<ElectricalTestingPointAI> findByUniformcodeNameWindpowerstationid(String uniformcode, String name, String windpowerstationid) {

        return findTest(uniformcode,name,windpowerstationid);
    }

    public List<ElectricalTestingPointAI> findTest(String uniformcode, String name, String windpowerstationid) {
        Specification querySpecifi = new Specification<ElectricalTestingPointAIEntity>() {
            @Override
            public Predicate toPredicate(Root<ElectricalTestingPointAIEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                List<Predicate> predicates = new ArrayList<>();
                if (uniformcode != null && !uniformcode.trim().equals("")) {
                    predicates.add(criteriaBuilder.like(root.get("uniformCode"), "%" + uniformcode + "%" ));
                }
                if (name != null && !name.trim().equals("")) {
                    predicates.add(criteriaBuilder.like(root.get("name"),"%" + name + "%" ));
                }
                if (windpowerstationid != null && !windpowerstationid.trim().equals("")) {
                    predicates.add(criteriaBuilder.equal(root.get("windPowerStationId"), windpowerstationid));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };

        List<ElectricalTestingPointAIEntity> all = ElectricalTestingPointAIRepository.findAll(querySpecifi);
        return DataUtil.convertDataList(all);
    }



}
