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.JclOrgWrapper;
import weaver.general.Util;
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.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.Map;

/**
 * @author:dxfeng
 * @createTime: 2022/08/02
 * @version: 1.0
 */
public class JclOrgController {

    public JclOrgWrapper getJclOrgWrapper(User user) {
        return ServiceUtil.getService(JclOrgWrapper.class, user);
    }

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

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

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

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