package com.casic.PgInterface.rs;

import com.casic.PgInterface.devTable.domain.PgRoad;
import com.casic.PgInterface.devTable.dto.PgPartitionDto;
import com.casic.PgInterface.devTable.dto.PgRoadDto;
import com.casic.PgInterface.devTable.manager.PgPartitionManager;
import com.casic.PgInterface.devTable.manager.PgRoadManager;
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;

@Component
@Path("pgRoad")
public class PgRoadRs {
    private static Logger logger = LoggerFactory.getLogger(PgRoad.class);

    @Resource
    private PgRoadManager pgRoadManager;

    @Resource
    private PgPartitionManager pgPartitionManager;

    /**
     * 获取道路信息
     */
    @POST
    @Path("getRoadList")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> getRoadList(){

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

        String msg="";
        List<PgRoadDto> pgRoadDtoList=new ArrayList<PgRoadDto>();
        try{
            pgRoadDtoList = pgRoadManager.getAllRoad();
            if (pgRoadDtoList.size() == 0)
                msg = "没有道路信息";
            else
                msg = "道路信息获取成功";
        }
        catch(Exception e){
            msg="道路信息获取失败";
            e.printStackTrace();
        }

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

        return resultMap;
    }

    /**
     * 新增道路信息
     */
    @POST
    @Path("addRoad")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> addRoad(@FormParam("roadName") String roadName)throws Exception{
        Map<String,Object> resultMap=new HashMap<String, Object>();
        String msg="";
        String result="";

        try{
            PgRoad pgRoad=new PgRoad();
            pgRoad.setRoadName(roadName);
            pgRoad.setActive(1);

            pgRoadManager.save(pgRoad);

            msg="新增道路成功";
            result="true";
        }
        catch (Exception e){
            msg="新增道路失败";
            result="true";
            e.printStackTrace();
        }

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

        return resultMap;
    }

    /**
     * 编辑道路信息
     */
    @POST
    @Path("editRoad")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> editRoad(@FormParam("id") String id,
                                       @FormParam("roadName") String roadName)throws Exception{

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

        String msg="";
        String result="";

        try {
            PgRoad pgRoad=pgRoadManager.get(Long.valueOf(id));

            if(pgRoad==null)
            {
                msg="不存在道路信息";
                result="false";
            }
            else{
                pgRoad.setRoadName(roadName);
                pgRoad.setActive(1);

                pgRoadManager.save(pgRoad);
                msg="道路信息保存成功";
                result="true";
            }
        }
        catch(Exception e){
            msg="道路信息编辑失败";
            result="false";
            e.printStackTrace();
        }
        resultMap.put("msg",msg);
        resultMap.put("result",result);

        return resultMap;
    }

    /**
     * 删除道路阈值
     */
    @POST
    @Path("deleteRoad")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> deleteRoad(@FormParam("id") String id) throws Exception {
        Map<String,Object> resultMap=new HashMap<String, Object>();

        String msg="";
        String result="";
        PgRoad pgRoad=null;

        try{
            pgRoad=pgRoadManager.get(Long.valueOf(id));
            if(pgRoad==null){
                msg="不存在道路信息";
                result="false";
            }
            else{
                pgRoad.setActive(0);
                pgRoadManager.save(pgRoad);
                msg="道路删除成功";
                result="true";
            }
        }
        catch (Exception e){
            msg="道路删除出错";
            result="false";
            e.printStackTrace();
        }

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

        return resultMap;
    }

    /**
     * 根据道路获取分区信息
     */
    @POST
    @Path("getParNameByRoadName")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> getParNameByRoadName(@FormParam("roadName") String roadName)throws Exception{
        Map<String,Object> resultMap=new HashMap<String, Object>();
        String msg="";
        String result="";

        List<PgPartitionDto> pgPartitionDtoList=null;
        try{
            PgRoad pgRoad=pgRoadManager.getPgRoadByName(roadName);
            if(pgRoad==null)
                msg="不存在道路信息";
            else
            {
                pgPartitionDtoList=pgPartitionManager.getPgPartitonDtoByRoad(pgRoad);
                if(pgPartitionDtoList==null||pgPartitionDtoList.size()==0)
                    msg="该道路没有分区信息";
                else
                    msg="分区信息获取成功";
            }
        }
        catch (Exception e)
        {
            msg="分区信息获取失败";
        }

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

        return resultMap;
    }

}