package com.casic.PgInterface.patroler.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.patroler.domain.PgAlarmRule;
import com.casic.PgInterface.patroler.dto.PgAlarmRuleDto;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yxw on 2017/12/14.
 */
@Service
public class PgAlarmRuleManager extends HibernateEntityDao<PgAlarmRule> {

    //根据设备获取设备报警阈值
    public List<PgAlarmRuleDto>  getAlarmRuleDto() {
        Criteria criteria=createCriteria(PgAlarmRule.class);
        criteria.addOrder(Order.desc("id"));

        List<PgAlarmRule> pgAlarmRuleList=criteria.list();
        List<PgAlarmRuleDto> pgAlarmRuleDtoList = new ArrayList<PgAlarmRuleDto>();
        for (PgAlarmRule pgAlarmRule : pgAlarmRuleList) {
            pgAlarmRuleDtoList.add(new PgAlarmRuleDto(pgAlarmRule));
        }
        return pgAlarmRuleDtoList;

    }

    public PgAlarmRule getAlarmRuleBydevType(String devType) {
        Criteria criteria=createCriteria(PgAlarmRule.class);
        criteria.add(Restrictions.eq("devType", devType));

        List<PgAlarmRule> pgAlarmRuleList=criteria.list();

        if(pgAlarmRuleList==null||pgAlarmRuleList.size()==0)
            return null;
        else
            return pgAlarmRuleList.get(0);
    }

}
