package com.engine.salary.web;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.util.ResponseResult;
import com.engine.salary.util.SalaryEnumUtil;
import com.engine.salary.wrapper.SalaryCommonWrapper;
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.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.Map;

@Slf4j
public class SalaryCommonController {

    private SalaryCommonWrapper getSalaryCommonWrapper(User user) {
        return ServiceUtil.getService(SalaryCommonWrapper.class, user);
    }

    @GET
    @Path("/enum/list")
    @Produces(MediaType.APPLICATION_JSON)
    public String getSearchCondition(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam(value = "enumClass") String enumClass) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<String, List<Map<String, Object>>>(user).run(SalaryEnumUtil::enumSelectList, enumClass);
    }


    @GET
    @Path("/cache/info")
    @Produces(MediaType.APPLICATION_JSON)
    public String getCacheInfo(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam(value = "key") String key) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<String, String>(user).run(getSalaryCommonWrapper(user)::getCacheInfo, key);
    }

    @GET
    @Path("/cache/remove")
    @Produces(MediaType.APPLICATION_JSON)
    public String removeCache(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam(value = "key") String key) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<String, String>(user).run(getSalaryCommonWrapper(user)::removeCache, key);
    }


}
