package com.engine.htsc.financialadjustment.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.financialadjustment.service.FinService;
import com.engine.htsc.financialadjustment.service.impl.FinServiceImpl;
import weaver.general.BaseBean;

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 javax.ws.rs.core.Response;
import java.util.Map;

public class FinAjAction {

  private FinService getFinService() {
    return ServiceUtil.getService(FinServiceImpl.class);
  }

  @GET
  @Path("/getProjectDeptBrowser")
  @Produces({MediaType.APPLICATION_JSON})
  public Response getProjectDeptBrowser(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    Map<String, Object> map = ParamUtil.request2Map(request);
    try {
      String projectId = (String) map.get("projectId");
      String userCode = (String) map.get("userCode");
      JSONObject json = getFinService().getDeptBrowserByProjectId(userCode, projectId);
      return Response.ok(json)
              .build();
    } catch (Exception e) {
      new BaseBean().writeLog(e.getMessage());
      e.printStackTrace();
      return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
    }

  }

  @POST
  @Path("/extractExcel")
  @Produces({MediaType.APPLICATION_JSON})
  public Response extractExcel(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    Map<String, Object> map = ParamUtil.request2Map(request);
    try {
      int imageId = Integer.parseInt((String) map.get("imageId"));
      String type = (String) map.get("type");
      JSONArray jsonArray = getFinService().readByImageId(imageId, type);
      return Response.ok(jsonArray)
              .build();
    } catch (Exception e) {
      new BaseBean().writeLog(e.getMessage());
      e.printStackTrace();
      return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
    }

  }

}
