package com.casic.PgInterface.patroler.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.patroler.domain.PgMaintainType;
import com.casic.PgInterface.patroler.dto.PgMaintainTypeDto;
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/8/21.
 */
@Service
public class PgMaintainTypeManager extends HibernateEntityDao<PgMaintainType> {

    public List<PgMaintainType> getMaintainType(){
        String sql="select distinct type from PG_MAINTAIN_TYPE";

        List<Object> objList=this.getSession().createSQLQuery(sql).list();

       List<PgMaintainType> pgMaintainTypes=new ArrayList<>();
        for(Object object : objList)
        {
            PgMaintainType pgMaintainType=new PgMaintainType();
            pgMaintainType.setType(String.valueOf(object));
            pgMaintainType.setActive(1);
            pgMaintainTypes.add(pgMaintainType);
        }

        return pgMaintainTypes;
    }

    public List<PgMaintainTypeDto> getMaintainTypeModel(String type) {

        Criteria criteria = createCriteria(PgMaintainType.class);

        criteria.add(Restrictions.eq("type", type));
        criteria.add(Restrictions.eq("active", 1));

        List<PgMaintainType> pgMaintainTypeList = criteria.list();
        List<PgMaintainTypeDto> pgMaintainTypeDtoList = new ArrayList<PgMaintainTypeDto>();
        for (PgMaintainType pgMaintainType : pgMaintainTypeList) {
            pgMaintainTypeDtoList.add(new PgMaintainTypeDto(pgMaintainType));
        }

        return pgMaintainTypeDtoList;
    }

    public PgMaintainType getMaintainTypeByMode(String model)
    {
        Criteria criteria = createCriteria(PgMaintainType.class);
        criteria.add(Restrictions.eq("model", model));
        criteria.add(Restrictions.eq("active", 1));
        criteria.addOrder(Order.desc("id"));

        List<PgMaintainType> pgMaintainTypeList = criteria.list();

        if(pgMaintainTypeList.size()!=0)
            return pgMaintainTypeList.get(0);
        else
            return null;
    }

}
