package com.casic.PgInterface.rs;

import com.casic.PgInterface.devTable.domain.PgPartition;
import com.casic.PgInterface.devTable.domain.PipeGallery;
import com.casic.PgInterface.devTable.dto.PipeGalleryDto;
import com.casic.PgInterface.devTable.dto.PipeGalleryStatic;
import com.casic.PgInterface.devTable.manager.PgPartitionManager;
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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yxw on 2017/11/6.
 */
@Component
@Path("pipeGallery")
public class PipeGalleryRs {

    private static Logger logger = LoggerFactory.getLogger(PgAlarmRs.class);
    @Resource
    private PipeGalleryManager pipeGalleryManager;
    @Resource
    private PgPartitionManager pgPartitionManager;

    /**
     * 新增管廊
     */
    @POST
    @Path("addPipeGallery")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addPipeGallery(@FormParam("pgName") String pgName,
                                              @FormParam("parLength") String parLength,
                                              @FormParam("parName") String parName) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";
        PipeGallery pipeGallery = new PipeGallery();
        try {
            PgPartition pgPartition=pgPartitionManager.getPartitionByParName(parName);
            if(pgPartition==null) {
                result = "false";
                msg = "不存在分区:"+parName;
            }
            else {
                if (pipeGalleryManager.getPipeGalleryByPgName(pgName,pgPartition) != null) {
                    result = "false";
                    msg = "已存在相同管廊";
                } else {
                    pipeGallery.setPgName(pgName);
                    pipeGallery.setPgLength(Double.valueOf(parLength));
                    pipeGallery.setPgPartitionId(pgPartition);
                    pipeGallery.setActive(1);
                    logger.info("成功保存");
                    pipeGalleryManager.save(pipeGallery);
                    result = "true";
                    msg = "保存成功";
                }
            }
        } catch (Exception e) {
            result = "false";
            msg = "保存失败";
            e.printStackTrace();
        }

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

    /**
     *获取分区下的管廊仓列表
     */
    @POST
    @Path("getPipeGallerys")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getPipeGalleryList(@FormParam("parName") String parName)throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg="";
        List<PipeGalleryDto> pipeGalleryDtoList=new ArrayList<PipeGalleryDto>();
        try {
            pipeGalleryDtoList=pipeGalleryManager.getPipeGalleryByParName(parName);
            if(pipeGalleryDtoList.size()==0)
                msg="没有分区信息";
            else
                msg="分区信息获取成功";
        }catch(Exception e){
            e.printStackTrace();
            msg="分区信息获取失败";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",pipeGalleryDtoList);
        return resultMap;
    }

    /*
    *编辑管廊信息
     */
    @POST
    @Path("editPipeGallery")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> editPipeGallery(@FormParam("id") String id,
                                               @FormParam("pgName") String pgName,
                                               @FormParam("parLength") String parLength,
                                               @FormParam("parName") String parName) throws Exception {

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

        PipeGallery pipeGallery=new PipeGallery();
        try {
            PgPartition pgPartition=pgPartitionManager.getPartitionByParName(parName);
            if (pgPartition == null) {
                result = "false";
                msg = "不存在该分区信息";
            } else {
                /*
                if (pipeGalleryManager.getPipeGalleryByPgName(pgName, pgPartition) != null) {
                    result = "false";
                    msg = "已存在相同管廊";
                } else {
                */
                pipeGallery = pipeGalleryManager.get(Long.valueOf(id));

                pipeGallery.setPgName(pgName);
                pipeGallery.setPgLength(Long.valueOf(parLength));
                pipeGallery.setPgPartitionId(pgPartition);
                pipeGallery.setActive(1);

                pipeGalleryManager.save(pipeGallery);
                result = "true";
                msg = "编辑成功";
                //}
            }
        } catch (Exception e) {
            result = "false";
            msg = "编辑失败";
            e.printStackTrace();
        }
        resultMap.put("result",result);
        resultMap.put("msg",msg);
        return resultMap;
    }

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

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

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

    /**
     * 获取管廊仓体的长度
     */
    @POST
    @Path("getPgLengthStatic")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> getPgLengthStatic()
    {
        Map<String,Object> resultMap=new HashMap<String, Object>();
        List<PipeGalleryStatic> pipeGalleryStaticList=new ArrayList<PipeGalleryStatic>();
        String msg="";

        try{
            pipeGalleryStaticList=pipeGalleryManager.getPgLengthByName();

            msg="仓体长度获取成功";
        }
        catch(Exception e)
        {
            e.printStackTrace();
            msg="仓体长度获取失败";
        }

        resultMap.put("msg",msg);
        resultMap.put("result",pipeGalleryStaticList);

        return resultMap;
    }
}