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.company.param.CompSearchParam;
import com.engine.organization.entity.department.param.DepartmentMoveParam;
import com.engine.organization.util.response.ReturnResult;
import com.engine.organization.wrapper.CompWrapper;
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/16
 * @version: 1.0
 */
public class CompController {
    public CompWrapper getCompWrapper(User user) {
        return ServiceUtil.getService(CompWrapper.class, user);
    }

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


    /**
     * 保存公司/分部基础信息
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/saveBaseComp")
    @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(getCompWrapper(user).saveBaseComp(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 CompSearchParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getCompWrapper(user).updateForbiddenTagById(param));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

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

    /**
     * 获取基础表单
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getCompBaseForm")
    @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(getCompWrapper(user).getCompBaseForm(map));
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }


    /**
     * 新增保存表单
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getCompSaveForm")
    @Produces(MediaType.APPLICATION_JSON)
    public ReturnResult getCompSaveForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            return ReturnResult.successed(getCompWrapper(user).getCompSaveForm());
        } 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(getCompWrapper(user).getMoveForm());
        } catch (Exception e) {
           return ReturnResult.exceptionHandle(e);
        }
    }

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


}
