package com.engine.organization.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.organization.entity.DeleteParam;
import com.engine.organization.entity.job.param.JobCopyParam;
import com.engine.organization.entity.job.param.JobMergeParam;
import com.engine.organization.entity.job.param.JobSearchParam;
import com.engine.organization.entity.searchtree.SearchTreeParams;
import com.engine.organization.util.response.ReturnResult;
import com.engine.organization.wrapper.JobWrapper;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.apache.commons.lang3.StringUtils;
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/27
 * @version: 1.0
 */
public class JobController {
    public JobWrapper getJobWrapper(User user) {
        return ServiceUtil.getService(JobWrapper.class, user);
    }

    /**
     * 左侧树接口
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getSearchTree")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getSearchTree(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        Map<String, Object> map = ParamUtil.request2Map(request);
        SearchTreeParams params = JSONObject.toJavaObject((JSON) JSONObject.toJSON(map), SearchTreeParams.class);
        return getJobWrapper(user).getSearchTree(params);

    }

    /**
     * 获取list列表
     *
     * @param request
     * @param response
     * @param params
     * @return
     */
    @POST
    @Path("/listPage")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult listDept(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody JobSearchParam params) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getJobWrapper(user).listPage(params));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

    /**
     * 新增保存表单
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getSaveForm")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getSaveForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            JobSearchParam param = JSONObject.toJavaObject((JSON) JSONObject.toJSON(map), JobSearchParam.class);
            return ReturnResult.successed(getJobWrapper(user).getSaveForm(param));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

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

    /**
     * 保存基础信息
     *
     * @param request
     * @param response
     * @param params
     * @return
     */
    @POST
    @Path("/saveBaseForm")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult saveBaseComp(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody Map<String, Object> params) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getJobWrapper(user).saveBaseForm(params));
        } 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 JobSearchParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getJobWrapper(user).updateForbiddenTagById(param));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

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

    /**
     * 复制到指定部门
     *
     * @param request
     * @param response
     * @param param
     * @return
     */
    @POST
    @Path("/copyJobItem")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult copyJobItem(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody JobCopyParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getJobWrapper(user).copyJobItem(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(getJobWrapper(user).deleteByIds(param.getIds()));
        } 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(getJobWrapper(user).getHasRight());
        } 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);
            return ReturnResult.successed(getJobWrapper(user).getSearchCondition());
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }


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


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

    @GET
    @Path("/getMergeForm")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getMergeForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            String id = (String) map.get("id");
            if (StringUtils.isBlank(id)) {
                id = "0";
            }
            return ReturnResult.successed(getJobWrapper(user).getMergeForm(Long.parseLong(id)));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

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

}
