package com.casic.PgInterface.rs;

import com.casic.PgInterface.devTable.domain.PgRoad;
import com.casic.PgInterface.devTable.manager.PgRoadManager;
import com.casic.PgInterface.intelPlan.domain.PgPileAcu;
import com.casic.PgInterface.intelPlan.manager.PgPileAcuManager;
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.HashMap;
import java.util.Map;

@Component
@Path("pgPileAcu")
public class PgPileAcuRs {

    @Resource
    private PgPileAcuManager pgPileAcuManager;

    @Resource
    private PgRoadManager pgRoadManager;

    /*
    新增桩号
    */
    @POST
    @Path("addPileAcu")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addPileAcu(@FormParam("acuCode") String acuCode,
                                          @FormParam("length") String length,
                                          @FormParam("road") String road)throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String result = "";
        String msg = "";
        PgRoad pgRoad=pgRoadManager.getPgRoadByName(road);
        try {
            PgPileAcu pgPileAcu=new PgPileAcu();
            pgPileAcu.setAcuCode(acuCode);
            pgPileAcu.setLength(Long.valueOf(length));
            if(pgRoad==null)
                pgPileAcu.setRoadId(null);
            else
                pgPileAcu.setRoadId(pgRoad.getId());
            pgPileAcu.setActive(true);

            pgPileAcuManager.save(pgPileAcu);
            result="true";
            msg="保存成功";
        } catch (Exception e) {
            result = "false";
            msg = "保存失败";
            e.printStackTrace();
        }

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

    /*
    删除桩号
    */
    @POST
    @Path("deletePileAcu")
    @Produces(MediaType.APPLICATION_JSON)
    public  Map<String,Object> deletePileAcu(@FormParam("id") Long id)throws Exception{
        Map<String, Object> resultMap=new HashMap<>();

        String result="";
        String msg="";
        try{
            PgPileAcu pgPileAcu=pgPileAcuManager.get(id);
            if(pgPileAcu==null){
                result="false";
                msg="不存在桩号分区";
            }
            else
            {
                pgPileAcu.setActive(false);
                pgPileAcuManager.save(pgPileAcu);

                result="true";
                msg="桩号删除成功";
            }
        }
        catch(Exception ex){
            result="false";
            msg="桩号删除失败";
            ex.printStackTrace();
        }

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

        return resultMap;
    }
}
