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.department.param.*;
import com.engine.organization.entity.searchtree.SearchTreeParams;
import com.engine.organization.util.response.ReturnResult;
import com.engine.organization.wrapper.DepartmentWrapper;
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 weaver_cl
 * @description:
 * @Date 2022/5/20
 * @Version V1.0
 **/
public class DepartmentController {

    private DepartmentWrapper getDepartmentWrapper(User user) {
        return ServiceUtil.getService(DepartmentWrapper.class, user);
    }


    @POST
    @Path("/getDeptListByPid")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getDeptListByPid(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody QuerySingleDeptListParam querySingleDeptListParam) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return getDepartmentWrapper(user).getDeptListByPid(querySingleDeptListParam);
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

    @GET
    @Path("/getJobListByPid")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getJobListByPid(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            String parentDept = (String) map.get("parentDept");
            return ReturnResult.successed(getDepartmentWrapper(user).getJobListByPid(QuerySingleDeptListParam.builder().parentDept(Integer.parseInt(parentDept)).build()));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

    /**
     * 左侧树接口
     *
     * @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 getDepartmentWrapper(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 DeptSearchParam params) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getDepartmentWrapper(user).listPage(params));
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

    /**
     * 保存公司/分部基础信息
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/saveBaseForm")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult saveBaseComp(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> params = ParamUtil.request2Map(request);
            return ReturnResult.successed(getDepartmentWrapper(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 DeptSearchParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getDepartmentWrapper(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(getDepartmentWrapper(user).updateForm(map));
        } 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(getDepartmentWrapper(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(getDepartmentWrapper(user).getSearchCondition(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(getDepartmentWrapper(user).getHasRight());
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

    /**
     * 获取基础表单
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getDeptBaseForm")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getCompBaseForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            return ReturnResult.successed(getDepartmentWrapper(user).getDeptBaseForm(map));
        } 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> params = ParamUtil.request2Map(request);
            return ReturnResult.successed(getDepartmentWrapper(user).getSaveForm(params));
        } 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 ReturnResult.successed(getDepartmentWrapper(user).getCopyForm());
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

    @POST
    @Path("/copyDepartment")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult copyDepartment(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DeptCopyParam copyParam) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getDepartmentWrapper(user).copyDepartment(copyParam));
        } 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);
            return ReturnResult.successed(getDepartmentWrapper(user).getMergeForm());
        } catch (Exception e) {
            return ReturnResult.exceptionHandle(e);
        }
    }

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

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

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

}
