package com.casic.PgInterface.devTable.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.devTable.domain.PgDeviceType;
import com.casic.PgInterface.devTable.dto.PgDeviceTypeDto;
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 2018/1/8.
 */
@Service
public class PgDeviceTypeManager  extends HibernateEntityDao<PgDeviceType> {

    public List<PgDeviceTypeDto> getAllDeviceType() {

        Criteria criteria = createCriteria(PgDeviceType.class);
        criteria.add(Restrictions.eq("active", 1));
        criteria.addOrder(Order.desc("id"));

        List<PgDeviceType> pgDeviceTypeList = criteria.list();
        List<PgDeviceTypeDto> pgDeviceTypeDtoList = new ArrayList<PgDeviceTypeDto>();
        for (PgDeviceType pgDeviceType : pgDeviceTypeList) {
            pgDeviceTypeDtoList.add(new PgDeviceTypeDto(pgDeviceType));
        }

        return pgDeviceTypeDtoList;
    }

    public PgDeviceType getDeviceTypeByDeType(String devType) {

        Criteria criteria = createCriteria(PgDeviceType.class);
        criteria.add(Restrictions.eq("active", 1));
        criteria.add(Restrictions.eq("devType", devType));

        List<PgDeviceType> pgDeviceTypeList = criteria.list();
        if (pgDeviceTypeList.size() == 0)
            return null;
        else
            return pgDeviceTypeList.get(0);
    }

}
