package com.engine.organization.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.organization.entity.hrmresource.param.HrmRelationSaveParam;
import com.engine.organization.entity.hrmresource.param.HrmResourceSearchParam;
import com.engine.organization.entity.searchtree.SearchTreeParams;
import com.engine.organization.util.response.ReturnResult;
import com.engine.organization.wrapper.HrmResourceWrapper;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
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.POST;
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.Map;

/**
 * @author:dxfeng
 * @createTime: 2022/06/21
 * @version: 1.0
 */
public class HrmResourceController {
    public HrmResourceWrapper getHrmResourceWrapper(User user) {
        return ServiceUtil.getService(HrmResourceWrapper.class, user);
    }

    /**
     * 左侧树接口
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getSearchTree")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getSearchTree(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> map = ParamUtil.request2Map(request);
        SearchTreeParams params = JSONObject.toJavaObject((JSON) JSONObject.toJSON(map), SearchTreeParams.class);
        return getHrmResourceWrapper(user).getSearchTree(params);

    }

    /**
     * 获取list列表
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/listPage")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult listPage(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            HrmResourceSearchParam param = JSONObject.toJavaObject((JSON) JSONObject.toJSON(map), HrmResourceSearchParam.class);
            return ReturnResult.successed(getHrmResourceWrapper(user).listPage(param));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

    /**
     * 获取新增表单
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getSaveForm")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getSaveForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getHrmResourceWrapper(user).getSaveForm());
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

    /**
     * 新增保存
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/saveBaseForm")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult saveBaseForm(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody Map<String, Object> params) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getHrmResourceWrapper(user).saveBaseForm(params));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

    /**
     * 获取基础表单
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getBaseForm")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getBaseForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            return ReturnResult.successed(getHrmResourceWrapper(user).getBaseForm(map));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

    /**
     * 更新表单
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/updateForm")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult updateForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            return ReturnResult.successed(getHrmResourceWrapper(user).updateForm(map));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

    /**
     * 高级搜索条件
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getSearchCondition")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getSearchCondition(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            return ReturnResult.successed(getHrmResourceWrapper(user).getSearchCondition(map));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

    /**
     * 列表页顶部按钮
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getHasRight")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getHasRight(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getHrmResourceWrapper(user).getHasRight());
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

    @GET
    @Path("/getTabForm")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getTabForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            return ReturnResult.successed(getHrmResourceWrapper(user).getTabForm(map));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

    @POST
    @Path("/saveTabForm")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult saveTabForm(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody HrmRelationSaveParam params) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getHrmResourceWrapper(user).saveTabForm(params));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

    @POST
    @Path("/updateTabForm")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult updateTabForm(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody HrmRelationSaveParam params) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getHrmResourceWrapper(user).updateTabForm(params));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }
}
