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.hrmresource.po.ResourceItemClassPO;
import com.engine.organization.entity.hrmresource.po.ResourceItemPO;
import com.engine.organization.util.response.ReturnResult;
import com.engine.organization.wrapper.ResourceBasicInfoWrapper;
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 weaver_cl
 * @Description:
 * @Date 2022/8/23
 * @Version V1.0
 **/
public class ResourceBasicInfoController {

    private ResourceBasicInfoWrapper getResourceBasicInfoWrapper(User user) {
        return ServiceUtil.getService(ResourceBasicInfoWrapper.class, user);
    }

    @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(getResourceBasicInfoWrapper(user).getHasRight());
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }


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


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

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


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


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

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

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

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

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

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

    @POST
    @Path("/deleteResourceItemClass")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult deleteResourceItemClass(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody Integer fid) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getResourceBasicInfoWrapper(user).deleteResourceItemClass(fid));
        } 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(getResourceBasicInfoWrapper(user).getSearchCondition());
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

}
