package com.engine.salary.web;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.entity.auth.dto.*;
import com.engine.salary.entity.auth.param.*;
import com.engine.salary.entity.auth.vo.Permission;
import com.engine.salary.util.ResponseResult;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.wrapper.AuthWrapper;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.slf4j.Slf4j;
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.List;

/**
 * 权限
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
@Slf4j
public class AuthController {

    private AuthWrapper getAuthWrapper(User user) {
        return ServiceUtil.getService(AuthWrapper.class, user);
    }

    @POST
    @Path("/role/list")
    @Produces(MediaType.APPLICATION_JSON)
    public String roleList(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody AuthRoleListQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<AuthRoleListQueryParam, PageInfo<AuthRoleDTO>>(user).run(getAuthWrapper(user)::roleList, param);
    }

    @GET
    @Path("/role/getRole")
    @Produces(MediaType.APPLICATION_JSON)
    public String getRole(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody @QueryParam(value = "id") Long id) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<Long, AuthRoleDTO>(user).run(getAuthWrapper(user)::getRole, id);
    }

    @POST
    @Path("/role/save")
    @Produces(MediaType.APPLICATION_JSON)
    public String saveRole(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody AuthRoleSaveParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<AuthRoleSaveParam, Long>(user).run(getAuthWrapper(user)::saveRole, param);
    }

    @POST
    @Path("/role/delete")
    @Produces(MediaType.APPLICATION_JSON)
    public String deleteRole(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody List<Long> ids) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<List<Long>, Long>(user).run(getAuthWrapper(user)::deleteRole, ids);
    }

    @POST
    @Path("/member/list")
    @Produces(MediaType.APPLICATION_JSON)
    public String memberList(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody AuthMemberQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<AuthMemberQueryParam, PageInfo<AuthMemberDTO>>(user).run(getAuthWrapper(user)::memberList, param);
    }

    @POST
    @Path("/member/detail")
    @Produces(MediaType.APPLICATION_JSON)
    public String memberDetail(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody AuthMemberQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<AuthMemberQueryParam, PageInfo<AuthRoleEmpDTO>>(user).run(getAuthWrapper(user)::memberDetail, param);
    }

    @POST
    @Path("/member/save")
    @Produces(MediaType.APPLICATION_JSON)
    public String saveMember(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody List<AuthMemberSaveParam> param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<List<AuthMemberSaveParam>, Long>(user).run(getAuthWrapper(user)::saveMember, param);
    }

    @POST
    @Path("/member/delete")
    @Produces(MediaType.APPLICATION_JSON)
    public String deleteMember(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody List<Long> ids) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<List<Long>, Long>(user).run(getAuthWrapper(user)::deleteMember, ids);
    }

    @POST
    @Path("/member/sync")
    @Produces(MediaType.APPLICATION_JSON)
    public String syncMember(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody AuthSyncParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<AuthSyncParam, Long>(user).run(getAuthWrapper(user)::syncMember, param);
    }


    @GET
    @Path("/opt/tree")
    @Produces(MediaType.APPLICATION_JSON)
    public String optTree(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam(value = "roleId") Long roleId) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<Long, AuthOptDTO>(user).run(getAuthWrapper(user)::optTree, roleId);
    }

    @POST
    @Path("/opt/save")
    @Produces(MediaType.APPLICATION_JSON)
    public String saveOpt(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody AuthOptSaveParam opt) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<AuthOptSaveParam, AuthOptDTO>(user).run(getAuthWrapper(user)::saveOpt, opt);
    }

    @POST
    @Path("/data/list")
    @Produces(MediaType.APPLICATION_JSON)
    public String dataList(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody AuthDataQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<AuthDataQueryParam, PageInfo<AuthDataDTO>>(user).run(getAuthWrapper(user)::dataList, param);
    }

    @POST
    @Path("/data/detail")
    @Produces(MediaType.APPLICATION_JSON)
    public String dataDetail(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody AuthDataQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<AuthDataQueryParam,PageInfo<AuthRoleDataDTO>>(user).run(getAuthWrapper(user)::dataDetail, param);
    }

    @POST
    @Path("/data/save")
    @Produces(MediaType.APPLICATION_JSON)
    public String saveData(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody List<AuthDataSaveParam> params) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<List<AuthDataSaveParam>, Long>(user).run(getAuthWrapper(user)::saveData, params);
    }

    @POST
    @Path("/data/delete")
    @Produces(MediaType.APPLICATION_JSON)
    public String deleteData(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody List<Long> ids) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<List<Long>, Long>(user).run(getAuthWrapper(user)::deleteData, ids);
    }

    @POST
    @Path("/data/sync")
    @Produces(MediaType.APPLICATION_JSON)
    public String syncData(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody AuthSyncParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<AuthSyncParam, AuthOptDTO>(user).run(getAuthWrapper(user)::syncData, param);
    }

    @GET
    @Path("/data/auth")
    @Produces(MediaType.APPLICATION_JSON)
    public String auth(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam(value = "page") String page) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<String, Object>(user).run(getAuthWrapper(user)::auth, page);
    }


    /**
     * 权限信息
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/permission")
    @Produces(MediaType.APPLICATION_JSON)
    public String permission(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam(value = "page") String page) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<String, Permission>(user).run(getAuthWrapper(user)::permission, page);
    }


    @POST
    @Path("/permission4FsdSocialDetail")
    @Produces(MediaType.APPLICATION_JSON)
    public String permission4FsdSocialDetail(@Context HttpServletRequest request, @Context HttpServletResponse response,@RequestBody FsdSocialDetailAuthParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<FsdSocialDetailAuthParam, String>(user).run(getAuthWrapper(user)::permission4FsdSocialDetail, param);
    }


    /**
     * 查询
     *
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/tree")
    @Produces(MediaType.APPLICATION_JSON)
    public String tree(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody AuthTreeQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<AuthTreeQueryParam, AuthTreeDTO>(user).run(getAuthWrapper(user)::tree,param);
    }


    /**
     * 同步
     */
    @POST
    @Path("/sync")
    @Produces(MediaType.APPLICATION_JSON)
    public String sync(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<AuthSyncParam, String>(user).run(getAuthWrapper(user)::sync);
    }

}
