package com.casic.PgInterface.devTable.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.devTable.domain.PgAlarmType;
import com.casic.PgInterface.devTable.dto.PgAlarmTypeDto;
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/11/8.
 */
@Service
public class PgAlarmTypeManager extends HibernateEntityDao<PgAlarmType> {

    //获取当前报警类型列表
    public List<PgAlarmTypeDto> getAllAlarmTypeDto() {
        Criteria criteria = createCriteria(PgAlarmType.class);
        criteria.add(Restrictions.eq("active", 1));
        criteria.addOrder(Order.desc("id"));

        List<PgAlarmType> pgAlarmTypeList = criteria.list();
        List<PgAlarmTypeDto> pgAlarmTypeDtoList = new ArrayList<PgAlarmTypeDto>();
        for (PgAlarmType pgAlarmType : pgAlarmTypeList) {
            pgAlarmTypeDtoList.add(new PgAlarmTypeDto(pgAlarmType));
        }

        return pgAlarmTypeDtoList;
    }

    public List<PgAlarmType> getAllAlarmType() {
        Criteria criteria = createCriteria(PgAlarmType.class);
        criteria.add(Restrictions.eq("active", 1));
        criteria.addOrder(Order.desc("id"));

        List<PgAlarmType> pgAlarmTypeList = criteria.list();

        return pgAlarmTypeList;
    }


    public PgAlarmType getAlarmTypeByAlarmType(String alarmType) {
        Criteria criteria = createCriteria(PgAlarmType.class);
        criteria.add(Restrictions.eq("alarmType", alarmType));
        criteria.add(Restrictions.eq("active", 1));
        criteria.addOrder(Order.desc("id"));

        List<PgAlarmType> pgAlarmTypeList = criteria.list();

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


}
