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.scheme.param.GradeSearchParam;
import com.engine.organization.util.response.ReturnResult;
import com.engine.organization.wrapper.GradeWrapper;
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;

/**
 * @description:
 * @author:dxfeng
 * @createTime: 2022/05/11
 * @version: 1.0
 */
public class GradeController {


    public GradeWrapper getGradeWrapper(User user) {
        return ServiceUtil.getService(GradeWrapper.class, user);
    }

    /**
     * 获取list列表
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getGradeTable")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult listGrade(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            return ReturnResult.successed(getGradeWrapper(user).listPage(map));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

    /**
     * 添加职级
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/saveGrade")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult saveGrade(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody GradeSearchParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getGradeWrapper(user).saveGrade(param));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

    /**
     * 更新职级，修改所有字段
     *
     * @param request
     * @param response
     * @param param
     * @return
     */
    @POST
    @Path("/updateGrade")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult updateGrade(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody GradeSearchParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getGradeWrapper(user).updateGrade(param));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

    /**
     * 更新禁用标记
     *
     * @param request
     * @param response
     * @param param
     * @return
     */
    @POST
    @Path("/updateForbiddenTagById")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult updateForbiddenTagById(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody GradeSearchParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getGradeWrapper(user).updateForbiddenTagById(param));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }


    /**
     * 根据ID批量删除数据
     *
     * @param request
     * @param response
     * @param param
     * @return
     */
    @POST
    @Path("/deleteByIds")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult deleteByIds(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DeleteParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getGradeWrapper(user).deleteByIds(param.getIds()));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

    /**
     * 高级搜索条件
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getSearchCondition")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getSearchCondition(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            return ReturnResult.successed(getGradeWrapper(user).getSearchCondition(map));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }


    /**
     * 新增、编辑表单
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getGradeForm")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getGradeForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            //String s = Util.null2String(map.get(ParamConstant.PARAM_IP));
            return ReturnResult.successed(getGradeWrapper(user).getGradeForm(map));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

    /**
     * 列表页顶部按钮
     *
     * @param request
     * @param response
     * @return
     */
    @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(getGradeWrapper(user).getHasRight());
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

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

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

}
