package com.engine.organization.web;

import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.organization.entity.extend.param.ExtendFieldSearchParam;
import com.engine.organization.entity.extend.param.ExtendInfoChangeParam;
import com.engine.organization.entity.extend.param.ExtendTitleSaveParam;
import com.engine.organization.entity.fieldset.param.FieldTypeTreeParam;
import com.engine.organization.enums.ModuleTypeEnum;
import com.engine.organization.util.response.ReturnResult;
import com.engine.organization.wrapper.FieldDefinedWrapper;
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.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.Map;

/**
 * @Author weaver_cl
 * @description:
 * @Date 2022/6/13
 * @Version V1.0
 **/
public class FieldDefinedController {

    private FieldDefinedWrapper getFieldDefinedWrapper(User user) {
        return ServiceUtil.getService(FieldDefinedWrapper.class, user);
    }

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

    @POST
    @Path("/{moduleTypeEnum}/saveTree")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult saveTree(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                 @PathParam("moduleTypeEnum") ModuleTypeEnum moduleTypeEnum, @RequestBody FieldTypeTreeParam fieldTypeTreeParam) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return getFieldDefinedWrapper(user).saveTree(moduleTypeEnum, fieldTypeTreeParam);
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

    @POST
    @Path("/{moduleTypeEnum}/changeTree")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult changeTree(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                   @PathParam("moduleTypeEnum") ModuleTypeEnum moduleTypeEnum, @RequestBody FieldTypeTreeParam fieldTypeTreeParam) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return getFieldDefinedWrapper(user).changeTree(moduleTypeEnum, fieldTypeTreeParam);
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

    @POST
    @Path("/{moduleTypeEnum}/deleteTree")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult deleteTree(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                   @PathParam("moduleTypeEnum") ModuleTypeEnum moduleTypeEnum, @RequestBody FieldTypeTreeParam fieldTypeTreeParam) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return getFieldDefinedWrapper(user).deleteTree(fieldTypeTreeParam.getId());
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }


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

    /**
     * 分组维护
     *
     * @param request
     * @param response
     * @param param
     * @return
     */
    @POST
    @Path("/{moduleTypeEnum}/saveGroup")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult saveGroup(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("moduleTypeEnum") ModuleTypeEnum moduleTypeEnum, @RequestBody ExtendTitleSaveParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getFieldDefinedWrapper(user).saveGroup(param));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

    /**
     * 新建分组
     *
     * @param request
     * @param response
     * @param param
     * @return
     */
    @POST
    @Path("/{moduleTypeEnum}/saveTitle")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult saveTitle(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("moduleTypeEnum") ModuleTypeEnum moduleTypeEnum, @RequestBody ExtendTitleSaveParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getFieldDefinedWrapper(user).saveTitle(param));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

    @POST
    @Path("/{moduleTypeEnum}/deleteTitle")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult deleteTitle(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("moduleTypeEnum") ModuleTypeEnum moduleTypeEnum, @RequestBody ExtendTitleSaveParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getFieldDefinedWrapper(user).deleteTitle(param));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

    @POST
    @Path("/{moduleTypeEnum}/saveFields")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult saveFields(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("moduleTypeEnum") ModuleTypeEnum moduleTypeEnum, @RequestBody ExtendTitleSaveParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getFieldDefinedWrapper(user).saveFields(param.getData()));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

    @GET
    @Path("/{moduleTypeEnum}/getFieldDefinedInfo")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getFieldDefinedInfo(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("moduleTypeEnum") ModuleTypeEnum moduleTypeEnum) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            Long groupType = Long.parseLong((String) map.get("groupType"));
            Long groupId = Long.parseLong((String) map.get("groupId"));
            return ReturnResult.successed(getFieldDefinedWrapper(user).getFieldDefinedInfo(ExtendFieldSearchParam.builder().groupType(groupType).groupId(groupId).build()));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

    @POST
    @Path("/{moduleTypeEnum}/del")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult deleteFieldDefined(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("moduleTypeEnum") ModuleTypeEnum moduleTypeEnum, @RequestBody ExtendFieldSearchParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            getFieldDefinedWrapper(user).deleteFieldDefined(param);
            return ReturnResult.successed();
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

    @POST
    @Path("/{moduleTypeEnum}/changeGroup")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult changeGroup(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("moduleTypeEnum") ModuleTypeEnum moduleTypeEnum, @RequestBody ExtendInfoChangeParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            getFieldDefinedWrapper(user).changeGroup(param);
            return ReturnResult.successed();
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }
}
