package com.casic.PgInterface.rs;

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.devTable.manager.PgPipeLineManager;
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;

    /**
     * 新增管线
     */
    @POST
    @Path("addPipeLine")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addLog( @FormParam("lineType") String lineType,
                                      @FormParam("secRow") String secRow,
                                      @FormParam("secColumn") String secColumn,
                                      @FormParam("pileAcuList") String pileAcuList) throws Exception {

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

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

        try {
               resultMap = pgPipeLineManager.addPipeLine(lineType,secRow,secColumn,pileAcuList);
               resultMap.put("msg","提交成功");
               resultMap.put("result","true");

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result","false");
            resultMap.put("msg","提交失败");
            return  resultMap;
        }

        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("getPipeLineByCode")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getPipeLineByName(@FormParam("lineCode") String lineCode) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String msg = "";
        PgPipeLineDto pgPipeLineDto=null;
        try {
            PgPipeLine pgPipeLine = pgPipeLineManager.getPipeLineBylineName(lineCode);
            if(pgPipeLine==null)
                msg="不存在："+lineCode;
            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 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;
    }

}