package com.engine.jucailinkq.attendance.workflow.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.jucailinkq.attendance.workflow.service.AllowanceService;
import com.engine.jucailinkq.attendance.workflow.service.impl.AllowanceServiceImpl;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.jucailinkq.common.util.ApiReturnTools;
import com.engine.jucailinkq.common.util.ResponseResult;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
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.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: sy
 * @Description: 出勤津贴
 * @Date: 2024/4/9
 **/
@Slf4j
public class AllowanceApi {

    private AllowanceService getAllowanceService(User user) {
        return ServiceUtil.getService(AllowanceServiceImpl.class,user);
    }

    /**
     * 生成出勤津贴数据
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/addAllowanceRecords")
    @Produces(MediaType.TEXT_PLAIN)
    public String addAllowanceRecords(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        User user = HrmUserVarify.getUser(request, response);
        apidatas = getAllowanceService(user).addAllowanceRecords(ParamUtil.request2Map(request));
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 获得津贴记录列表数据用于出勤津贴确认流程
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getAllowanceRecordList")
    @Produces(MediaType.APPLICATION_JSON)
    public String getAllowanceRecordList(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String,Object> param = ParamUtil.request2Map(request);
            String mainTableDataStr = Util.null2String(param.get("mainTableData"));
            Gson gson = new Gson();
            Map<String,Object> mainTableData = gson.fromJson(mainTableDataStr, (Type) Map.class);
            param.put("mainTableData",mainTableData);
            return new ResponseResult<Map<String, Object>, Map<String, Object>>(user).run(getAllowanceService(user) :: getAllowanceRecordList,param);
        }catch (Exception e){
            log.error("execute fail,catch error: [{}]",e);
            return ApiReturnTools.error("500","getAllowanceRecordList error");
        }
    }
}
