package com.casic.PgInterface.rs;

import com.casic.PgInterface.devTable.domain.PgPartition;
import com.casic.PgInterface.devTable.dto.PgPartitionDto;
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.text.SimpleDateFormat;
import java.util.*;

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

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

    @Resource
    private PgRoadManager pgRoadManager;

    /**
     * 新增分区
     */
    @POST
    @Path("addPartition")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addAlarm(@FormParam("parName") String parName,
                                        @FormParam("road") String road,
                                        @FormParam("pgPosition") String pgPosition,
                                        @FormParam("pgNum") String pgNum,
                                        @FormParam("pgType") String pgType,
                                        @FormParam("pgLength") String pgLength,
                                        @FormParam("buildDate") String buildDate,
                                        @FormParam("material") String material)throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        try {
            if (pgPartitionManager.getPartitionByParName(parName) != null) {
                result = "false";
                msg = "已存在分区:"+parName;
            } else {
                PgPartition pgPartition = new PgPartition();
                pgPartition.setParName(parName);
                pgPartition.setPgRoadId(pgRoadManager.getPgRoadByName(road).getId());
                pgPartition.setPgPosition(pgPosition);
                pgPartition.setPgNum(pgNum);
                pgPartition.setPgType(pgType);
                pgPartition.setPgLength(pgLength);
                pgPartition.setBuildDate(sdf.parse(buildDate));
                pgPartition.setMaterial(material);
                pgPartition.setActive(1);

                logger.info("成功保存");
                pgPartitionManager.save(pgPartition);
                result = "true";
                msg = "保存成功";
            }
        } catch (Exception e) {
            result = "false";
            msg = "保存失败";
            e.printStackTrace();
        }

        resultMap.put("result", result);
        resultMap.put("msg", msg);
        return resultMap;
    }
    /*
     *编辑分区
     */
    @POST
    @Path("editPartition")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> editPartition(@FormParam("id") String id,
                                             @FormParam("parName") String parName,
                                             @FormParam("road") String road,
                                             @FormParam("pgPosition") String pgPosition,
                                             @FormParam("pgNum") String pgNum,
                                             @FormParam("pgType") String pgType,
                                             @FormParam("pgLength") String pgLength,
                                             @FormParam("buildDate") String buildDate,
                                             @FormParam("material") String material) throws Exception {

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

        PgPartition partition = new PgPartition();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        try {
            if (pgPartitionManager.isExitPartition(parName,id)) {
                result = "false";
                msg = "已存在分区:"+parName;
            } else {
                PgPartition pgPartition = pgPartitionManager.get(Long.valueOf(id));
                pgPartition.setParName(parName);
                pgPartition.setPgRoadId(pgRoadManager.getPgRoadByName(road).getId());
                pgPartition.setPgPosition(pgPosition);
                pgPartition.setPgNum(pgNum);
                pgPartition.setPgType(pgType);
                pgPartition.setPgLength(pgLength);
                pgPartition.setBuildDate(sdf.parse(buildDate));
                pgPartition.setMaterial(material);
                pgPartition.setActive(1);

                pgPartitionManager.save(pgPartition);
                result = "true";
                msg = "编辑成功";
            }

        } catch (Exception e) {
            result = "false";
            msg = "编辑失败";
            e.printStackTrace();
        }

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


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

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

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

    /**
     * 获取分区信息
     */
    @POST
    @Path("getPartitionList")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getPartition() throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        List<PgPartitionDto> pgPartitionDtoList = new ArrayList<PgPartitionDto>();
        try {
            pgPartitionDtoList = pgPartitionManager.getPgPartitionDtoList();
            msg = "获取成功";
        } catch (Exception e) {
            msg = "获取失败";
            e.printStackTrace();
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgPartitionDtoList);
        return resultMap;
    }

    /**
     * 根据编号获取分区信息
     */
    @POST
    @Path("getPartitionListByName")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getPartitionListByName(@FormParam("parName") String parName) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        List<PgPartitionDto> pgPartitionDtoList = new ArrayList<PgPartitionDto>();
        try {
            pgPartitionDtoList = pgPartitionManager.getPartitionByName(parName);
            msg = "获取成功";
        } catch (Exception e) {
            msg = "获取失败";
            e.printStackTrace();
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgPartitionDtoList);
        return resultMap;
    }

}
