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

import com.engine.jucailinkq.attendance.workflow.service.BusinessTripsApplyService;
import com.engine.jucailinkq.attendance.workflow.service.impl.BusinessTripsApplyServiceImpl;
import com.engine.jucailinkq.common.util.ApiReturnTools;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
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.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.List;
import java.util.Map;

/**
 * @Author: sy
 * @Description: 出差申请
 * @Date: 2024/3/15
 **/
@Slf4j
public class BusinessTripsApplyApi {
    private BusinessTripsApplyService getBusinessTripsApplyService(User user) {
        return ServiceUtil.getService(BusinessTripsApplyServiceImpl.class,user);
    }
    /**
     * 出差申请列表
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getBusinessTripsApplyList")
    @Produces(MediaType.APPLICATION_JSON)
    public String getBusinessTripsApplyList(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String,Object> param = ParamUtil.request2Map(request);
            return ApiReturnTools.success(getBusinessTripsApplyService(user).getBusinessTripsApplyList(param));
        }catch (Exception e){
            log.error("execute fail,catch error: [{}]",e);
            return ApiReturnTools.error("500","getBusinessTripsApplyList error");
        }
    }

    /**
     * 生成出差明细表数据
     * @return
     */
    @GET
    @Path("/generateBusinessTripsList")
    @Produces({"text/plain"})
    public String generateBusinessTripsList(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            Map<String,Object> param = ParamUtil.request2Map(request);
            User user = HrmUserVarify.getUser(request, response);
            String mainTableDataStr = Util.null2String(param.get("mainTableData"));
            String detailTableDataStr = Util.null2String(param.get("detailTableData"));
            Gson gson = new Gson();
            Map<String,Object> mainTableData = gson.fromJson(mainTableDataStr, (Type) Map.class);
            List<Map<String, String>> detailTableData = gson.fromJson(detailTableDataStr, (Type) List.class);

            param.put("mainTableData",mainTableData);
            param.put("detailTableData",detailTableData);
            return new ResponseResult<Map<String, Object>, Map<String, Object>>(user).run(getBusinessTripsApplyService(user) :: generateBusinessTripsList,param);
        }catch (Exception e){
            log.error("generateBusinessTripsList status fail,catch error: [{}]",e);
            return ApiReturnTools.error("200","查询失败");
        }
    }

}
