package com.engine.salary.web;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.entity.salaryformula.dto.ExpressFormulaDTO;
import com.engine.salary.entity.salaryformula.param.SalaryFormulaDetailQueryParam;
import com.engine.salary.entity.salaryformula.param.SalaryFormulaFieldQueryParam;
import com.engine.salary.entity.salaryformula.param.SalaryFormulaMockParam;
import com.engine.salary.entity.salaryformula.param.SalaryFormulaSaveParam;
import com.engine.salary.entity.salaryformula.po.FormulaPO;
import com.engine.salary.entity.salaryformula.po.FormulaVar;
import com.engine.salary.formlua.constant.FormluaConstant;
import com.engine.salary.util.ResponseResult;
import com.engine.salary.wrapper.SalaryFormulaWrapper;
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.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class SalaryFormulaController {


    private SalaryFormulaWrapper getSalaryFormulaWrapper(User user) {
        return ServiceUtil.getService(SalaryFormulaWrapper.class, user);
    }

    //变量项
    @POST
    @Path("/search/group")
    @Produces(MediaType.APPLICATION_JSON)
    public String fieldGroupList(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody Map<String, Object> param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<Map<String, Object>, List<Map<String, Object>>>(user).run(getSalaryFormulaWrapper(user)::fieldGroupList, param);
    }

    //变量字段列表
    @POST
    @Path("/search/field")
    @Produces(MediaType.APPLICATION_JSON)
    public String fieldList(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryFormulaFieldQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<SalaryFormulaFieldQueryParam, List<FormulaVar>>(user).run(getSalaryFormulaWrapper(user)::fieldList, param);
    }

    //获取公式详情
    @POST
    @Path("/detail")
    @Produces(MediaType.APPLICATION_JSON)
    public String detail(@Context HttpServletRequest request, @Context HttpServletResponse response, SalaryFormulaDetailQueryParam queryParam) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<SalaryFormulaDetailQueryParam, ExpressFormulaDTO>(user).run(getSalaryFormulaWrapper(user)::detail, queryParam);
    }

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

    //数据源列表
    @GET
    @Path("/datasource/list")
    @Produces(MediaType.APPLICATION_JSON)
    public String datasource(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<String, ArrayList>(user).run(getSalaryFormulaWrapper(user)::datasourceList);
    }

    /**
     * 测试公式
     *
     * @param request
     * @param response
     * @param param
     * @return
     */
    @POST
    @Path("/mock")
    @Produces(MediaType.APPLICATION_JSON)
    public String mock(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryFormulaMockParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<SalaryFormulaMockParam, Object>(user).run(getSalaryFormulaWrapper(user)::mock, param);
    }


    @GET
    @Path("/des")
    @Produces(MediaType.APPLICATION_JSON)
    public String des(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        return FormluaConstant.DES;
    }

}
