package com.casic.PgInterface.devTable.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.devTable.domain.PgPipeLine;
import com.casic.PgInterface.devTable.dto.PgPipeLineDto;
import com.casic.PgInterface.devTable.dto.PgPipeLineLengthStatic;
import com.casic.PgInterface.devTable.dto.PgPipeLineTypeDto;
import com.casic.PgInterface.devTable.dto.PgPipeLineTypeNumDto;
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 2017/10/30.
 */
@Service
public class PgPipeLineManager extends HibernateEntityDao<PgPipeLine> {

    public PgPipeLine getPipeLineBylineName(String lineName) {
        Criteria criteria=createCriteria(PgPipeLine.class);
        criteria.add(Restrictions.eq("lineName", lineName));

        List<PgPipeLine> pgPipeLineList =criteria.list();
        if(pgPipeLineList.size()==0)
            return null;
        else
            return pgPipeLineList.get(0);
    }

    public List<PgPipeLineDto> getPipeLineByLineType(String lineType) {
        Criteria criteria = createCriteria(PgPipeLine.class);
        criteria.add(Restrictions.eq("lineType", lineType));

        List<PgPipeLine> pgPipeLineList = criteria.list();
        List<PgPipeLineDto> pgPipeLineDtoList = new ArrayList<PgPipeLineDto>();
        for (PgPipeLine pgPipeLine : pgPipeLineList) {
            pgPipeLineDtoList.add(new PgPipeLineDto(pgPipeLine));
        }
        return pgPipeLineDtoList;
    }

    public List<PgPipeLineLengthStatic> getPipeLengthByName() {
        String sql = "select LINE_TYPE,SUM(PIPE_LENGTH) from PG_PIPELINE group by LINE_TYPE ";

        List<Object[]> objectList = this.getSession().createSQLQuery(sql).list();
        List<PgPipeLineLengthStatic> pgPipeLineLengthStaticList = new ArrayList<PgPipeLineLengthStatic>();

        for(Object[] objectArray : objectList)
        {
            PgPipeLineLengthStatic pgPipeLineLengthStatic=new PgPipeLineLengthStatic();
            pgPipeLineLengthStatic.setLineType(String.valueOf(objectArray[0]));
            pgPipeLineLengthStatic.setLength(String.valueOf(objectArray[1]));
            pgPipeLineLengthStaticList.add(pgPipeLineLengthStatic);
        }
        return pgPipeLineLengthStaticList;
    }

    public  List<PgPipeLineTypeDto> getPipeType() {
        String sql = "select distinct LINE_TYPE from PG_PIPELINE";

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

        List<PgPipeLineTypeDto> pgPipeLineTypeDtoList=new ArrayList<PgPipeLineTypeDto>();

        for(Object ob : objectList)
        {
            PgPipeLineTypeDto pgPipeLineTypeDto=new PgPipeLineTypeDto();
            pgPipeLineTypeDto.setPipeLineType(String.valueOf(ob));
            pgPipeLineTypeDtoList.add(pgPipeLineTypeDto);
        }
        return pgPipeLineTypeDtoList;
    }

    public List<PgPipeLineTypeNumDto> getPipeTypeNum(){
        String sql="select LINE_TYPE,count(*) from pg_pipeline group by line_type";

        List<Object[]> objectList = this.getSession().createSQLQuery(sql).list();
        List<PgPipeLineTypeNumDto> pgPipeLineTypeNumDtoList=new ArrayList<PgPipeLineTypeNumDto>();

        for(Object[] ob:objectList){
            PgPipeLineTypeNumDto pgPipeLineTypeNumDto=new PgPipeLineTypeNumDto();

            pgPipeLineTypeNumDto.setPipeLineType(String.valueOf(ob[0]));
            pgPipeLineTypeNumDto.setNum(String.valueOf(ob[1]));

            pgPipeLineTypeNumDtoList.add(pgPipeLineTypeNumDto);
        }

        return pgPipeLineTypeNumDtoList;
    }

}