package com.casic.PgInterface.devTable.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.devTable.domain.PgEquipment;
import com.casic.PgInterface.devTable.dto.PgDeviceTypeStatisticDto;
import com.casic.PgInterface.devTable.dto.PgEquipmentDto;
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/9/3.
 */
@Service
public class PgEquipmentManager extends HibernateEntityDao<PgEquipment> {

    public PgEquipment getEquipmentByCode(String eqCode) {

        Criteria criteria = getSession().createCriteria(PgEquipment.class);
        criteria.add(Restrictions.eq("eqCode", eqCode));
        criteria.add(Restrictions.eq("active",1));

        List<PgEquipment> pgEquipmentList = (List<PgEquipment>)criteria.list();

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

    public List<PgEquipmentDto> getEquipmentDtiList()
    {
        Criteria criteria=createCriteria(PgEquipment.class);
        criteria.add(Restrictions.eq("active",1));

        List<PgEquipment> pgEquipmentList=criteria.list();
        List<PgEquipmentDto> pgEquipmentDtoList = new ArrayList<PgEquipmentDto>();
        for(PgEquipment pgEquipment:pgEquipmentList)
        {
            pgEquipmentDtoList.add(new PgEquipmentDto(pgEquipment));
        }

        return pgEquipmentDtoList;
    }


    //统计工器具耗材数量
    public List<PgDeviceTypeStatisticDto> getDeviceStatisticByDevType(String eqType){
        //String eqType1=eqType.equals("0")?"工器具":"耗材";

        String hql="select equipment.EQ_NAME,count(*) from PG_EQUIPMENT equipment where equipment.EQ_TYPE = '"
        +eqType+"' group by equipment.EQ_NAME";

        List<PgDeviceTypeStatisticDto> pgDeviceTypeStatisticDtoList=this.getSession().createSQLQuery(hql).list();

        return pgDeviceTypeStatisticDtoList;
    }

}
