package com.casic.PgInterface.patroler.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.patroler.domain.PgAuthority;
import com.casic.PgInterface.patroler.dto.PgAuthorityDto;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

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

/**
 * Created by yxw on 2018/3/30.
 */
@Service
public class PgAuthorityManager extends HibernateEntityDao<PgAuthority>{

    public PgAuthority getAuthorityByAuthority(String authority) {
        try {
            Criteria criteria = createCriteria(PgAuthority.class);
            criteria.add(Restrictions.eq("authority", authority));
            criteria.add(Restrictions.eq("active", 1));
            List<PgAuthority> pgAuthorityList = criteria.list();

            if (pgAuthorityList == null || pgAuthorityList.size() == 0)
                return null;
            else
                return pgAuthorityList.get(0);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<PgAuthorityDto> getAuthorityList()
    {
        try {
            Criteria criteria = createCriteria(PgAuthority.class);
            criteria.add(Restrictions.eq("active", 1));

            List<PgAuthority> pgAuthorityList = criteria.list();
            List<PgAuthorityDto> pgAuthorityDtoList=new ArrayList<PgAuthorityDto>();
            for(PgAuthority pgAuthority : pgAuthorityList)
            {
                pgAuthorityDtoList.add(new PgAuthorityDto(pgAuthority));
            }

            return pgAuthorityDtoList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
