package com.casic.PgInterface.rs;

import com.casic.PgInterface.construction.domain.PgConstructionType;
import com.casic.PgInterface.construction.dto.PgConstructionTypeDto;
import com.casic.PgInterface.construction.manager.PgConstructionTypeManager;
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;

/**
 * Created by yxw on 2018/4/10.
 */
@Component
@Path("pgConstructionType")
public class PgConstructionTypeRs {

    @Resource
    private PgConstructionTypeManager pgConstructionTypeManager;

    /**
     * 获取项目状态
     */
    @POST
    @Path("getConstructionType")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getConstructionType() throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        List<PgConstructionTypeDto> pgConstructionTypeDtoList = new ArrayList<PgConstructionTypeDto>();
        try {
            pgConstructionTypeDtoList = pgConstructionTypeManager.getAllConstructionTypeDto();
            if (pgConstructionTypeDtoList.size() == 0)
                msg = "没有项目类型信息";
            else
                msg = "项目类型获取成功";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "项目类型信息获取失败";
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgConstructionTypeDtoList);
        return resultMap;
    }

    /**
     * 新增项目状态
     */
    @POST
    @Path("addConstructionType")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addConstructionType(@FormParam("typeName") String typeName) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";

        try {
            if (pgConstructionTypeManager.getConstructionTypeByType(typeName) != null) {
                result = "false";
                msg = "已存项目状态";
            } else {
                PgConstructionType pgConstructionType = new PgConstructionType();
                pgConstructionType.setTypeName(typeName);
                pgConstructionType.setActive(1);

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

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

    /**
     * 编辑项目状态
     */
    @POST
    @Path("editConstruction")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> editConstruction(@FormParam("id") String id,
                                             @FormParam("typeName") String typeName)throws Exception{
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";

        try{
            if(pgConstructionTypeManager.getConstructionTypeByType(typeName)!=null)
            {
                result = "false";
                msg = "已存项目状态";
            }
            else {
                PgConstructionType pgConstructionType = pgConstructionTypeManager.get(Long.valueOf(id));
                if(pgConstructionType==null)
                {
                    result = "false";
                    msg = "不存在项目状态";
                }
                else {
                    pgConstructionType.setTypeName(typeName);

                    pgConstructionTypeManager.save(pgConstructionType);
                    result = "true";
                    msg = "编辑成功";
                }
            }
        }
        catch(Exception e)
        {
            result = "false";
            msg = "编辑失败";
            e.printStackTrace();
        }

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

        return resultMap;
    }


    /**
     * 删除项目状态
     */
    @POST
    @Path("deleteConstructionType")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> deleteConstructionType(@FormParam("id") String id)throws Exception {

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

        try {
            PgConstructionType pgConstructionType=pgConstructionTypeManager.get(Long.valueOf(id));
            if(pgConstructionType==null) {
                msg = "不存项目状态";
                result="false";
            }
            else
            {
                pgConstructionType.setActive(0);
                pgConstructionTypeManager.save(pgConstructionType);
                msg="项目状态删除成功";
                result="true";
            }
        }catch(Exception e){
            e.printStackTrace();
            msg="项目状态删除失败";
            result="false";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",result);

        return resultMap;
    }


}
