package com.casic.PgInterface.construction.manager;

import com.casic.PgInterface.construction.domain.PgConstructionType;
import com.casic.PgInterface.construction.dto.PgConstructionTypeDto;
import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
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/4/10.
 */
@Service
public class PgConstructionTypeManager extends HibernateEntityDao<PgConstructionType> {

    public PgConstructionType getConstructionTypeByType(String typeName)
    {
        Criteria criteria=createCriteria(PgConstructionType.class);
        criteria.add(Restrictions.eq("typeName",typeName));
        criteria.add(Restrictions.eq("active",1));

        List<PgConstructionType> pgConstructionTypeList=criteria.list();
        if(pgConstructionTypeList!=null&&pgConstructionTypeList.size()!=0)
            return pgConstructionTypeList.get(0);
        else
            return null;
    }

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

        List<PgConstructionType> pgConstructionTypeList=criteria.list();
        List<PgConstructionTypeDto> pgConstructionTypeDtoList=new ArrayList<PgConstructionTypeDto>();
        for(PgConstructionType pgConstructionType : pgConstructionTypeList)
        {
            pgConstructionTypeDtoList.add(new PgConstructionTypeDto(pgConstructionType));
        }

        return pgConstructionTypeDtoList;
    }

}
