package com.engine.organization.web;

import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.organization.entity.DeleteParam;
import com.engine.organization.entity.detach.param.ManagerDetachParam;
import com.engine.organization.util.response.ReturnResult;
import com.engine.organization.wrapper.ManagerDetachWrapper;
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.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.Map;

/**
 * @Author weaver_cl
 * @Description: 模块管理分权
 * @Date 2022/10/21
 * @Version V1.0
 **/
public class ManagerDetachController {

    public ManagerDetachWrapper getManagerDetachWrapper(User user) {
        return ServiceUtil.getService(ManagerDetachWrapper.class,user);
    }


    @GET
    @Path("/getTable")
    @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);
            return ReturnResult.successed(getManagerDetachWrapper(user).listPage(map));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }


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


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

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

    @POST
    @Path("/deleteByIds")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult deleteByIds(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DeleteParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getManagerDetachWrapper(user).deleteByIds(param.getIds()));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }


    @POST
    @Path("/save")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult saveDetach(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody ManagerDetachParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getManagerDetachWrapper(user).save(param));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

    @POST
    @Path("/update")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult updateDetach(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody ManagerDetachParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getManagerDetachWrapper(user).updateDetach(param));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

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