package com.engine.organization.web;

import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.organization.util.response.ReturnResult;
import com.engine.organization.wrapper.GroupWrapper;
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;

/**
 * @description:
 * @author:dxfeng
 * @createTime: 2022/05/16
 * @version: 1.0
 */
public class GroupController {
    public GroupWrapper getGroupWrapper(User user) {
        return ServiceUtil.getService(GroupWrapper.class, user);
    }

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

    @POST
    @Path("/updateGroup")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult updateGroup(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody Map<String, Object> map) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getGroupWrapper(user).updateGroup(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(getGroupWrapper(user).getHasRight());
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

    @GET
    @Path("/getTabInfo")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getTabInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getGroupWrapper(user).getTabInfo());
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }
}
