package com.casic.PgInterface.intelPlan.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.devTable.domain.PgRoad;
import com.casic.PgInterface.devTable.dto.PgPipeLineLengthStatic;
import com.casic.PgInterface.devTable.manager.PgRoadManager;
import com.casic.PgInterface.intelPlan.domain.PgLineHole;
import com.casic.PgInterface.intelPlan.domain.PgPileAcu;
import com.casic.PgInterface.intelPlan.dto.PgLineHoleDTO;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

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

@Service
public class PgLineHoleManager extends HibernateEntityDao<PgLineHole> {

    @Resource
    private PgPileAcuManager pgPileAcuManager;

    @Resource
    private PgRoadManager pgRoadManager;

    public List<PgLineHole> getPgLineHole(String lineName,Long pileAcuId){
        Criteria criteria=createCriteria(PgLineHole.class);

        criteria.add(Restrictions.eq("lineName",lineName));
        criteria.add(Restrictions.eq("pileAcuId",pileAcuId));
        criteria.add(Restrictions.eq("active",true));

        List<PgLineHole> pgLineHoleList=criteria.list();

        if(pgLineHoleList==null||pgLineHoleList.size()==0)
            return null;
        else
            return pgLineHoleList;
    }

    public boolean isExistLine(List<PgLineHole> pgLineHoleList,int row,int column){
        for(PgLineHole pgLineHole:pgLineHoleList){
            if(pgLineHole.getSecRow()==row&&pgLineHole.getSecColumn()==column)
                return true;
        }
        return false;
    }

    public List<PgLineHoleDTO> getCurrentHoleByAcu(Long pileAcuId){
        Criteria criteria=createCriteria(PgLineHole.class);

        criteria.add(Restrictions.eq("pileAcuId",pileAcuId));
        criteria.add(Restrictions.eq("active",true));

        List<PgLineHole> pgLineHoleList=criteria.list();
        List<PgLineHoleDTO> pgLineHoleDTOList=new ArrayList<>();
        if(pgLineHoleList!=null&&pgLineHoleList.size()!=0)
            pgLineHoleDTOList=PgLineHoleDTO.ConvertToDTOList(pgLineHoleList,pgPileAcuManager,pgRoadManager);
        return pgLineHoleDTOList;
    }

    public void addPgLIneHole(List<PgPileAcu> pgPileAcuList,String lineName,String lineCode,String secRow,String secColumn,int lineState){
        for(PgPileAcu pgPileAcu:pgPileAcuList){
            PgLineHole pgLineHole=new PgLineHole();
            pgLineHole.setLineName(lineName);
            pgLineHole.setLineCode(lineCode);
            pgLineHole.setPileAcuId(pgPileAcu.getId());
            pgLineHole.setSecColumn(Long.valueOf(secColumn));
            pgLineHole.setSecRow(Long.valueOf(secRow));
            pgLineHole.setLineState(lineState);
            pgLineHole.setActive(true);

            this.save(pgLineHole);
        }
    }

    public List<PgLineHole> getPgLineHoleByLineCode(String lineCode){

        List<PgLineHole> pgLineHoleList=this.findBy("lineCode",lineCode);

        return pgLineHoleList;
    }

    public List<PgPipeLineLengthStatic> getLineLengthStatic(String roadName){
        List<PgPipeLineLengthStatic> pgPipeLineLengthStaticList=new ArrayList<>();
        String sql="select t.linename,sum(p.length) from PG_LINEHOLE t,pg_pileacu p,pg_road r where t.pileacuid=p.id and " +
                "p.id in (select id from pg_road where roadname = '"+roadName+"') group by linename";
        List<Object[]> objects=this.getSession().createSQLQuery(sql).list();

        for(Object[] objects1:objects){
        PgPipeLineLengthStatic pgPipeLineLengthStatic=new PgPipeLineLengthStatic();
        pgPipeLineLengthStatic.setLineType(objects1[0].toString());
        pgPipeLineLengthStatic.setLength(objects1[1].toString());

        pgPipeLineLengthStaticList.add(pgPipeLineLengthStatic);
        }

        return pgPipeLineLengthStaticList;
    }

}