package com.engine.attendance.workflow.web;

import com.engine.attendance.workflow.service.AskForLeaveService;
import com.engine.attendance.workflow.service.impl.AskForLeaveServiceImpl;
import com.engine.common.util.*;
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;

/**
 * 请假流程
 */
@Slf4j
public class AskForLeaveAction {

    private AskForLeaveService getAskForLeaveService(User user) {
        return ServiceUtil.getService(AskForLeaveServiceImpl.class,user);
    }

    /**
     * 请假流程获得实际请假项目
     * @return
     */
    @GET
    @Path("/getUesdHolidayItem")
    @Produces({"text/plain"})
    public String generateVocation(@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(getAskForLeaveService(user) :: getUesdHolidayItem,param);
        }catch (Exception e){
            log.error("sync fund status fail,catch error: [{}]",e);
            return ApiReturnTools.error("200","查询失败");
        }
    }

    /**
     * 获得请假记录列表数据用于销假
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getAskForLeaveRecordList")
    @Produces(MediaType.APPLICATION_JSON)
    public String getAskForLeaveRecordList(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String,Object> param = ParamUtil.request2Map(request);
            return new ResponseResult<Map<String, Object>, Map<String, Object>>(user).run(getAskForLeaveService(user) :: getAskForLeaveRecordList,param);
        }catch (Exception e){
            log.error("execute fail,catch error: [{}]",e);
            return ApiReturnTools.error("500","getBusinessTripsApplyList error");
        }
    }

    /**
     * 获得请假项目时间范围内的休息和节假日
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getRestDayInterval")
    @Produces(MediaType.APPLICATION_JSON)
    public String getRestDayInterval(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String,Object> param = ParamUtil.request2Map(request);
            return new ResponseResult<Map<String, Object>, Map<String, Object>>(user).run(getAskForLeaveService(user) :: getRestDayInterval,param);
        }catch (Exception e){
            log.error("execute fail,catch error: [{}]",e);
            return ApiReturnTools.error("500","getBusinessTripsApplyList error");
        }
    }
}
