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.ImportCommonWrapper;
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.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:dxfeng
 * @createTime: 2022/06/29
 * @version: 1.0
 */
public class ImportCommonController {

    private ImportCommonWrapper getImportCommonWrapper(User user) {
        return ServiceUtil.getService(ImportCommonWrapper.class, user);
    }

    @GET
    @Path("/getImportFields")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getImportFields(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {

            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            String importType = (String) map.get("importType");
            return ReturnResult.successed(getImportCommonWrapper(user).getImportFields(importType));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

    @GET
    @Path("/getImportForm")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getImportForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {

            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            String importType = (String) map.get("importType");
            String columns = (String) map.get("columns");
            return ReturnResult.successed(getImportCommonWrapper(user).getImportForm(importType, columns));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

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


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

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

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

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