package com.casic.PgInterface.devTable.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.core.util.StringUtils;
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 com.casic.PgInterface.intelPlan.domain.PgPileAcu;
import com.casic.PgInterface.intelPlan.manager.PgLineHoleManager;
import com.casic.PgInterface.intelPlan.manager.PgPileAcuManager;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.ws.rs.FormParam;
import java.util.*;

/**
 * Created by yxw on 2017/10/30.
 */
@Service
public class PgPipeLineManager extends HibernateEntityDao<PgPipeLine> {

    @Resource
    private PgPileAcuManager pgPileAcuManager;

    @Resource
    private PgLineHoleManager pgLineHoleManager;

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

        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);
        if(StringUtils.isNotBlank(lineType))
            criteria.add(Restrictions.eq("lineType", lineType));

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

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

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

        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;
    }

    public Map<String,Object> addPipeLine(String lineType,String secRow, String secColumn, String pileAcuList){
        Map<String,Object> resultMap=new HashMap<>();
        String msg="";
        String result="";

        List<String> pileAcuListStr = Arrays.asList(pileAcuList.split(","));
        List<PgPileAcu> pgPileAcuList=pgPileAcuManager.getPileAcuListByPileAcuStr(pileAcuListStr);
        Long acuLength=pgPileAcuManager.getAcuListLength(pgPileAcuList);
        try{
            PgPipeLine pgPipeLine=new PgPipeLine();
            pgPipeLine.setLineType(lineType);
            pgPipeLine.setLength(acuLength);
            pgPipeLine.setActive(1);

            //新增管线
            pgPileAcuManager.save(pgPipeLine);

            pgLineHoleManager.addPgLIneHole(pgPileAcuList,lineType,secRow,secColumn);
            msg="管线保存成功";
            result="true";
        }
        catch(Exception ex){
            ex.printStackTrace();
            msg="管线保存失败";
            result="false";
        }
        resultMap.put("result",result);
        resultMap.put("msg",msg);

        return resultMap;
    }

}