package com.engine.organization.web;

import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.organization.enums.ModuleTypeEnum;
import com.engine.organization.util.response.ReturnResult;
import com.engine.organization.wrapper.VersionManageWrapper;
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/12/8
 * @Version V1.0
 **/
public class VersionManageController {

    private VersionManageWrapper getVersionManageWrapper(User user){
        return ServiceUtil.getService(VersionManageWrapper.class, user);
    }

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

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