package com.casic.PgInterface.rs;

import com.casic.PgInterface.intelPlan.domain.PgPile;
import com.casic.PgInterface.intelPlan.dto.PgPileDTO;
import com.casic.PgInterface.intelPlan.manager.PgPileManager;
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("pgPile")
public class PgPileRs {

    @Resource
    private PgPileManager pgPileManager;

    /*
    新增桩号
     */
    @POST
    @Path("addPile")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addPile(@FormParam("pileCode") String pileCode)throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String result = "";
        String msg = "";
        String code="0";

        try {
            PgPile pgPile=new PgPile();
            pgPile.setPileCode(pileCode);
            pgPile.setActive(true);

            pgPileManager.save(pgPile);
            code="200";
            result="true";
            msg="保存成功";
        } catch (Exception e) {
            result = "false";
            msg = "保存失败";
            e.printStackTrace();
        }
        resultMap.put("code",code);
        resultMap.put("msg", msg);
        resultMap.put("result", result);

        return resultMap;
    }

    /*
    获取桩号列表
     */
    @POST
    @Path("getPileList")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> getPileList()throws Exception{

        Map<String,Object> resultMap=new HashMap<>();
        List<PgPileDTO> pgPileDTOList=new ArrayList<>();
        String msg="";
        String code="0";
        try{
            pgPileDTOList=pgPileManager.getPgPileDTOList();
            code="200";
            msg="获取成功";
        }
        catch(Exception ex){
            pgPileDTOList=null;
            msg="获取成失败";
            ex.printStackTrace();
        }
        resultMap.put("code",code);
        resultMap.put("msg",msg);
        resultMap.put("result",pgPileDTOList);

        return resultMap;
    }

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

        String result="";
        String msg="";
        String code="0";
        try{
            PgPile pgPile=pgPileManager.get(id);
            if(pgPile==null){
                result="false";
                msg="不存在桩号";
            }
            else
            {
                pgPile.setActive(false);
                pgPileManager.save(pgPile);
                code="200";
                result="true";
                msg="桩号删除成功";
            }
        }
        catch(Exception ex){
            result="false";
            msg="桩号删除失败";
            ex.printStackTrace();
        }
        resultMap.put("code",code);
        resultMap.put("msg",msg);
        resultMap.put("result",result);

        return resultMap;
    }

}