package com.engine.organization.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.organization.util.response.ReturnResult;
import com.engine.organization.wrapper.OrgChartWrapper;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @className: OrgChartController
 * @author: dengjp
 * @date: 2022/7/7
 * @description: 组织架构图
 **/
public class OrgChartController {
    public OrgChartWrapper getOrgChartWrapper(User user) {
        return ServiceUtil.getService(OrgChartWrapper.class, user);
    }

    /**
     * 获取组织维度信息
     */
    @GET
    @Path("/getCondition")
    @Produces(MediaType.APPLICATION_JSON)
    public String getCondition(@Context HttpServletRequest request, @Context HttpServletResponse
            response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            //实例化Service 并调用业务类处理
            apidatas = getOrgChartWrapper(user).getOptionCondition(ParamUtil.request2Map(request), user);
        } catch (Exception e) {
            //异常处理
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        //数据转换
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 组织架构数据
     */
    @GET
    @Path("/companyData")
    @Produces(MediaType.APPLICATION_JSON)
    public String getCompanyData(@Context HttpServletRequest request, @Context HttpServletResponse
            response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            //实例化Service 并调用业务类处理
            apidatas = getOrgChartWrapper(user).getCompanyData(ParamUtil.request2Map(request), user);
        } catch (Exception e) {
            //异常处理
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        //数据转换
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 组织透视图
     */
    @GET
    @Path("/userData")
    @Produces(MediaType.APPLICATION_JSON)
    public String getUserData(@Context HttpServletRequest request, @Context HttpServletResponse
            response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            //实例化Service 并调用业务类处理
            apidatas = getOrgChartWrapper(user).getUserData(ParamUtil.request2Map(request), user);
        } catch (Exception e) {
            //异常处理
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        //数据转换
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 异步人员数据
     */
    @GET
    @Path("/asyncUserData")
    @Produces(MediaType.APPLICATION_JSON)
    public String asyncUserData(@Context HttpServletRequest request, @Context HttpServletResponse
            response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            //实例化Service 并调用业务类处理
            apidatas = getOrgChartWrapper(user).asyncUserData(ParamUtil.request2Map(request), user);
        } catch (Exception e) {
            //异常处理
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        //数据转换
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 异步组织数据
     */
    @GET
    @Path("/asyncCompanyData")
    @Produces(MediaType.APPLICATION_JSON)
    public String asyncCompanyData(@Context HttpServletRequest request, @Context HttpServletResponse
            response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser(request, response);
            //实例化Service 并调用业务类处理
            apidatas = getOrgChartWrapper(user).asyncCompanyData(ParamUtil.request2Map(request), user);
        } catch (Exception e) {
            //异常处理
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        //数据转换
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 异步组织数据
     */
    @GET
    @Path("/synchronousData")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult synchronousData(@Context HttpServletRequest request, @Context HttpServletResponse
            response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> params = ParamUtil.request2Map(request);
            return ReturnResult.successed(getOrgChartWrapper(user).synchronousData(params));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

}
