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.PgSection;
import com.casic.PgInterface.intelPlan.dto.PgLineType;
import com.casic.PgInterface.intelPlan.manager.PgSectionManager;
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("pgSection")
public class PgSectionRs {

    @Resource
    private PgSectionManager pgSectionManager;

    @Resource
    private PgRoadManager pgRoadManager;

    @POST
    @Path("addPgSection")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> addPgSection(@FormParam("road") String road,
                                           @FormParam("lineName") String lineName,
                                           @FormParam("secRow") String secRow,
                                           @FormParam("secColumn") String secColumn){
        Map<String,Object> resultMap=new HashMap<>();
        String msg="";
        String result="";
        String code="0";
        PgRoad pgRoad=pgRoadManager.getPgRoadByName(road);

        try{
            PgSection pgSection=new PgSection();

            if(pgRoad==null)
                pgSection.setRoadId(null);
            else
                pgSection.setRoadId(pgRoad.getId());
            pgSection.setLineName(lineName);
            pgSection.setSecRow(Long.valueOf(secRow));
            pgSection.setSecColumn(Long.valueOf(secColumn));
            pgSection.setActive(true);

            pgSectionManager.save(pgSection);
            msg="截面信息保存成功";
            result="true";
            code="200";
        }
        catch(Exception ex){
            msg="界面信息保存失败";
            result="false";
            ex.printStackTrace();
        }
        resultMap.put("code",code);
        resultMap.put("msg",msg);
        resultMap.put("result",result);

        return resultMap;
    }

    @POST
    @Path("deletePgSection")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> deletePgSection(@FormParam("id") Long id){

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

        String msg="";
        String result="";
        String code="0";

        try{
            PgSection pgSection=pgSectionManager.get(id);

            pgSection.setActive(false);
            pgSectionManager.save(pgSection);

            code="200";
            msg="截面删除成功";
            result="true";
        }
        catch(Exception ex){
            msg="截面删除失败";
            result="false";
            ex.printStackTrace();
        }
        resultMap.put("code",code);
        resultMap.put("msg",msg);
        resultMap.put("result",result);

        return resultMap;
    }


    @POST
    @Path("getLineType")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> getLineType(){
        Map<String,Object> resultMap=new HashMap<>();
        List<PgLineType> pgLineTypeList=new ArrayList<>();
        String msg="";
        String code="0";

        try{
            pgLineTypeList=pgSectionManager.getLineType();
            msg="获取成功";
            code="200";
        }
        catch(Exception ex){
            ex.printStackTrace();
            msg="获取失败";
        }

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

        return resultMap;
    }
}