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.PgLineHole;
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 getPipeLineBylineCode(String lineCode) {
        List<PgPipeLine> pgPipeLineList =this.findBy("lineCode",lineCode);

        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 lineCode,String lineType,String secRow, String secColumn,
                                          int lineState,String pileAcuList){
        Map<String,Object> resultMap=new HashMap<>();
        String msg="";
        String result="";
        String code="0";

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

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

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

        return resultMap;
    }

    public Map<String,Object> editLineState(String lineCode,int lineState){

        Map<String,Object> resultMap=new HashMap<>();

        try {

            PgPipeLine pgPipeLine = getPipeLineBylineCode(lineCode);
            List<PgLineHole> pgLineHoleList = pgLineHoleManager.getPgLineHoleByLineCode(lineCode);
            if (pgPipeLine == null || pgLineHoleList == null || pgLineHoleList.size() == 0) {
                resultMap.put("code","0");
                resultMap.put("msg","不存在编号为"+lineCode+"的管线信息");
            } else {
                pgPipeLine.setLineState(lineState);
                this.save(pgPipeLine);

                for (PgLineHole pgLineHole : pgLineHoleList) {
                    pgLineHole.setLineState(lineState);
                    pgLineHoleManager.save(pgLineHole);
                }

                resultMap.put("code", "200");
                resultMap.put("msg", "管线状态修改失败");
            }
        }
        catch (Exception ex){
            resultMap.put("code","0");
            resultMap.put("msg","管线状态修改失败");
            ex.printStackTrace();
        }

        return resultMap;
    }

}