package com.casic.PgInterface.rs;

import com.casic.PgInterface.devTable.domain.PgPartition;
import com.casic.PgInterface.devTable.domain.PgPipeLine;
import com.casic.PgInterface.devTable.domain.PipeGallery;
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.devTable.manager.PgPartitionManager;
import com.casic.PgInterface.devTable.manager.PgPipeLineManager;
import com.casic.PgInterface.devTable.manager.PipeGalleryManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yxw on 2017/11/8.
 */
@Component
@Path("pgPipeLine")
public class PgPipeLineRs {

    private static Logger logger = LoggerFactory.getLogger(PgPipeLineRs.class);

    @Resource
    private PgPipeLineManager pgPipeLineManager;

    @Resource
    private PipeGalleryManager pipeGalleryManager;

    @Resource
    private PgPartitionManager pgPartitionManager;

    /**
     * 新增管线
     */
    @POST
    @Path("addPipeLine")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addLog(@FormParam("lineName") String lineName,
                                      @FormParam("lineType") String lineType,
                                      @FormParam("ownerUnit") String ownerUnit,
                                      @FormParam("enterDate") String enterDate,
                                      @FormParam("layerNum") String layerNum,
                                      @FormParam("diameter") String diameter,
                                      @FormParam("pipeLength") String pipeLength,
                                      @FormParam("pgName") String pgName,
                                      @FormParam("parName") String parName) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

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

        PgPartition pgPartition = pgPartitionManager.getPartitionByParName(parName);
        PipeGallery pipeGallery = pipeGalleryManager.getPipeGalleryByPgName(pgName,pgPartition);
        if (pgPartition == null) {
            result = "false";
            msg = "不存在管廊仓和分区信息:"+parName+","+pgName;
        }
        else {
            try {
                if(pgPipeLineManager.getPipeLineBylineName(lineName)!=null)
                {
                    result = "false";
                    msg = "已存在同名管线:"+lineName;
                }
                else {
                    PgPipeLine pgPipeLine=new PgPipeLine();

                    pgPipeLine.setLineName(lineName);
                    pgPipeLine.setLineType(lineType);
                    pgPipeLine.setOwnerUnit(ownerUnit);
                    pgPipeLine.setLayerNum(layerNum);
                    pgPipeLine.setEnterDate(sdf.parse(enterDate));
                    pgPipeLine.setDiameter(diameter);
                    pgPipeLine.setPipeLength(Double.valueOf(pipeLength));
                    pgPipeLine.setPipeGalleryId(pipeGallery);
                    pgPipeLine.setActive(1);

                    pgPipeLineManager.save(pgPipeLine);
                    result = "true";
                    msg = "保存成功";
                }
            } catch (Exception e) {
                result = "false";
                msg = "保存失败";
                e.printStackTrace();
            }
        }
        resultMap.put("result", result);
        resultMap.put("msg", msg);
        return resultMap;
    }

    /**
     * 编辑管线
     */
    @POST
    @Path("editPipeLine")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> editPipeLine(@FormParam("id") String id,
                                            @FormParam("lineName") String lineName,
                                            @FormParam("lineType") String lineType,
                                            @FormParam("ownerUnit") String ownerUnit,
                                            @FormParam("enterDate") String enterDate,
                                            @FormParam("layerNum") String layerNum,
                                            @FormParam("diameter") String diameter,
                                            @FormParam("pipeLength") String pipeLength,
                                            @FormParam("pgName") String pgName,
                                            @FormParam("parName") String parName) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

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

        PgPartition pgPartition = pgPartitionManager.getPartitionByParName(parName);
        PipeGallery pipeGallery = pipeGalleryManager.getPipeGalleryByPgName(pgName,pgPartition);
        if (pgPartition == null) {
            result = "false";
            msg = "不存在管廊仓和分区信息:"+parName+","+pgName;
        } else {
            try {
                /*
                if(pgPipeLineManager.getPipeLineBylineName(lineName)!=null)
                {
                    result = "false";
                    msg = "已存在同名管线";
                }
                else {
                */
                PgPipeLine pgPipeLine = pgPipeLineManager.get(Long.valueOf(id));

                pgPipeLine.setLineName(lineName);
                pgPipeLine.setLineType(lineType);
                pgPipeLine.setOwnerUnit(ownerUnit);
                pgPipeLine.setEnterDate(sdf.parse(enterDate));
                pgPipeLine.setLayerNum(layerNum);
                pgPipeLine.setDiameter(diameter);
                pgPipeLine.setPipeLength(Double.valueOf(pipeLength));
                pgPipeLine.setPipeGalleryId(pipeGallery);
                pgPipeLine.setActive(1);

                pgPipeLineManager.save(pgPipeLine);
                result = "true";
                msg = "编辑成功";
                //}
            } catch (Exception e) {
                result = "false";
                msg = "编辑失败";
                e.printStackTrace();
            }
        }

        resultMap.put("result",result);
        resultMap.put("msg",msg);
        return resultMap;
    }

    /**
     * 删除管线
     */
    @POST
    @Path("deletePipeLine")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> deletePipeLine(@FormParam("id") String id)throws Exception {

        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg="";
        String result="false";

        try {
            PgPipeLine pgPipeLine=pgPipeLineManager.get(Long.valueOf(id));
            if(pgPipeLine==null) {
                msg = "不存在该管线信息";
                result="false";
            }
            else
            {
                pgPipeLine.setActive(0);
                pgPipeLineManager.save(pgPipeLine);
                msg="管线删除成功";
                result="true";
            }
        }catch(Exception e){
            e.printStackTrace();
            msg="管线删除失败";
            result="false";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",result);
        return resultMap;
    }

    /*
    * 根据管线类型获取管线列表
     */
    @POST
    @Path("getPipeLineByLineType")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getPipeLineByLineType(@FormParam("lineType") String lineType) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        List<PgPipeLineDto> pgPipeLineDtoList=new ArrayList<PgPipeLineDto>();
        try {
            pgPipeLineDtoList = pgPipeLineManager.getPipeLineByLineType(lineType);
            msg = "获取成功";
        } catch (Exception e) {
            msg = "获取失败";
            e.printStackTrace();
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgPipeLineDtoList);
        return resultMap;
    }

    /**
     * 根据管线名称获取管线信息
     */
    @POST
    @Path("getPipeLineByName")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getPipeLineByName(@FormParam("lineName") String lineName) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        PgPipeLineDto pgPipeLineDto=null;
        try {
            PgPipeLine pgPipeLine = pgPipeLineManager.getPipeLineBylineName(lineName);
            if(pgPipeLine==null)
                msg="不存在："+lineName;
            else {
                pgPipeLineDto = new PgPipeLineDto(pgPipeLine);
                msg = "获取成功";
            }
        } catch (Exception e) {
            msg = "获取失败";
            e.printStackTrace();
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgPipeLineDto);
        return resultMap;
    }

    /**
     * 根据管线类型获取管线长度
     */
    @POST
    @Path("getPipeLengthStatic")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> getPipeLengthStatic()throws Exception{
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        List<PgPipeLineLengthStatic> pgPipeLineLengthStaticList=new ArrayList<PgPipeLineLengthStatic>();
        try {
            pgPipeLineLengthStaticList = pgPipeLineManager.getPipeLengthByName();
            msg="管线长度获取成功";
        }catch(Exception e)
        {
            e.printStackTrace();
            msg="管线长度统计失败";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",pgPipeLineLengthStaticList);

        return resultMap;
    }

    /**
     * 根据管线名称获取管线信息
     */
    @POST
    @Path("getPipeLineType")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getPipeLineType() throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        List<PgPipeLineTypeDto> pgPipeLineTypeDtoList=new ArrayList<PgPipeLineTypeDto>();
        try {
            pgPipeLineTypeDtoList = pgPipeLineManager.getPipeType();
            msg = "获取成功";
        } catch (Exception e) {
            msg = "获取失败";
            e.printStackTrace();
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgPipeLineTypeDtoList);
        return resultMap;
    }

    /**
     * 根据管线名称获取管线数量
     */
    @POST
    @Path("getPipeNumStatic")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> getPipeNumStatic() throws Exception{
        Map<String,Object> resultMap=new HashMap<String, Object>();

        String msg="";
        List<PgPipeLineTypeNumDto> pgPipeLineTypeNumDtoList=new ArrayList<PgPipeLineTypeNumDto>();

        try{
            pgPipeLineTypeNumDtoList=pgPipeLineManager.getPipeTypeNum();

            if(pgPipeLineTypeNumDtoList==null)
                msg="不存在管线类型占比";
            else
                msg="管线占比获取成功";
        }
        catch (Exception e){
            msg="管线占比数量获取失败";
            e.printStackTrace();
        }
        resultMap.put("msg",msg);
        resultMap.put("result",pgPipeLineTypeNumDtoList);

        return resultMap;
    }

}