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.SchemeSearchParam;
import com.engine.organization.util.response.ReturnResult;
import com.engine.organization.wrapper.SchemeWrapper;
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.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
 * @description:
 * @Date 2022/5/9
 * @Version V1.0
 **/
public class SchemeController {

    public SchemeWrapper getSchemeWrapper(User user) {
        return ServiceUtil.getService(SchemeWrapper.class, user);
    }


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

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

    /**
     * 修改等级方案，修改所有字段
     *
     * @param request
     * @param response
     * @param param
     * @return
     */
    @POST
    @Path("/updateScheme")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult updateScheme(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SchemeSearchParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getSchemeWrapper(user).updateScheme(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 SchemeSearchParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getSchemeWrapper(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(getSchemeWrapper(user).deleteByIds(param.getIds()));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

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


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

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

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


}
