package com.casic.PgInterface.devTable.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.devTable.domain.PgPartition;
import com.casic.PgInterface.devTable.domain.PgRoad;
import com.casic.PgInterface.devTable.domain.PipeGallery;
import com.casic.PgInterface.devTable.dto.PgPartitionDto;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yxw on 2017/11/4.
 */
@Service
public class PgPartitionManager extends HibernateEntityDao<PgPartition> {

    @Resource
    PgRoadManager pgRoadManager;

    public PgPartition getPartitionByParName(String parName) {
        Criteria criteria = getSession().createCriteria(PgPartition.class);
        criteria.add(Restrictions.eq("parName", parName));
        criteria.add(Restrictions.eq("active",1));

        List<PgPartition> pgPartitionList = criteria.list();

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

    public boolean isExitPartition(String parName,String id) {
        Criteria criteria = getSession().createCriteria(PgPartition.class);
        criteria.add(Restrictions.eq("parName", parName));
        criteria.add(Restrictions.eq("active",1));

        List<PgPartition> pgPartitionList = criteria.list();

        boolean reResult=false;
        if (pgPartitionList != null)
        {
            for(PgPartition pgPartition:pgPartitionList)
                if(pgPartition.getId()!=Long.valueOf(id))
                    reResult= true;
        }
        else
            reResult=false;

        return reResult;
    }

    public List<PgPartitionDto> getPartitionByName(String parName) {
        Criteria criteria = getSession().createCriteria(PipeGallery.class);
        criteria.add(Restrictions.eq("parName", parName));
        criteria.add(Restrictions.eq("active",1));

        List<PgPartition> pgPartitionList = criteria.list();

        List<PgPartitionDto> pgPartitionDtoList=new ArrayList<PgPartitionDto>();
        for(PgPartition pgPartition:pgPartitionList) {
            pgPartitionDtoList.add(new PgPartitionDto(pgPartition,pgRoadManager));
        }

        return pgPartitionDtoList;
    }

    public List<PgPartitionDto> getPgPartitionDtoList(){
        Criteria criteria=getSession().createCriteria(PgPartition.class);
        criteria.add(Restrictions.eq("active",1));
        criteria.addOrder(Order.desc("id"));

        List<PgPartition> pgPartitionList=criteria.list();
        if(pgPartitionList.size()==0||pgPartitionList==null)
            return null;
        List<PgPartitionDto> pgPartitionDtoList=new ArrayList<PgPartitionDto>();
        for(PgPartition pgPartition:pgPartitionList) {
            pgPartitionDtoList.add(new PgPartitionDto(pgPartition,pgRoadManager));
        }
        return pgPartitionDtoList;
    }

    public List<PgPartitionDto> getPgPartitonDtoByRoad(PgRoad pgRoad){
        Criteria criteria = getSession().createCriteria(PgPartition.class);
        criteria.add(Restrictions.eq("pgRoadId", pgRoad.getId()));
        criteria.add(Restrictions.eq("active",1));

        List<PgPartition> pgPartitionList = criteria.list();
        List<PgPartitionDto> pgPartitionDtoList=new ArrayList<PgPartitionDto>();
        for(PgPartition pgPartition:pgPartitionList){
            pgPartitionDtoList.add(new PgPartitionDto(pgPartition,pgRoadManager));
        }
        return pgPartitionDtoList;
    }

}