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.postion.po.PostPO;
import com.engine.organization.util.response.ReturnResult;
import com.engine.organization.wrapper.PostWrapper;
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;

/**
 * @description:
 * @author:dxfeng
 * @createTime: 2022/05/13
 * @version: 1.0
 */
public class PostController {
    public PostWrapper getPostWrapper(User user) {
        return ServiceUtil.getService(PostWrapper.class, user);
    }


    /**
     * 添加职务分类
     *
     * @param request
     * @param response
     * @param postPO
     * @return
     */
    @POST
    @Path("/savePost")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult savePost(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody PostPO postPO) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getPostWrapper(user).savePost(postPO));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

    /**
     * 更新职务分类，修改所有字段
     *
     * @param request
     * @param response
     * @param postPO
     * @return
     */
    @POST
    @Path("/updatePost")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult updatePost(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody PostPO postPO) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getPostWrapper(user).updatePost(postPO));
        } 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(getPostWrapper(user).deleteByIds(param.getIds()));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }


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

    /**
     * 获取左侧树
     * @param request
     * @param response
     * @return
     */
    @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(getPostWrapper(user).getTreeData());
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

}
