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.CardAccessWrapper;
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 weaver_cl
 * @Description:
 * @Date 2022/11/1
 * @Version V1.0
 **/

public class CardAccessController {

    public CardAccessWrapper getCardAccessWrapper(User user) {
        return ServiceUtil.getService(CardAccessWrapper.class, user);
    }


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

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

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

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

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