package com.casic.PgInterface.patroler.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.patroler.domain.PgRole;
import com.casic.PgInterface.patroler.dto.PgRoleDto;
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 PgRoleManager extends HibernateEntityDao<PgRole> {

    public PgRole getPgRoleByRole(String role) {
        try {
            Criteria criteria = createCriteria(PgRole.class);
            criteria.add(Restrictions.eq("role", role));
            criteria.add(Restrictions.eq("active", 1));
            List<PgRole> pgRoleList = criteria.list();

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

    public List<PgRoleDto> getPgRoleDtoList() {
        try {
            Criteria criteria = createCriteria(PgRole.class);
            criteria.add(Restrictions.eq("active", 1));

            List<PgRole> pgRoleList=criteria.list();
            List<PgRoleDto> pgRoleDtoList=new ArrayList<PgRoleDto>();
            for(PgRole pgRole:pgRoleList)
            {
                pgRoleDtoList.add(new PgRoleDto(pgRole));
            }
            return pgRoleDtoList;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

}
