package com.engine.htsc.payment.web;

import com.alibaba.fastjson.JSONArray;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.payment.pojo.HTPaymentCostDetail;
import com.engine.htsc.payment.service.PaymentExcelTemplateService;
import com.engine.htsc.payment.service.impl.PaymentExcelTemplateServiceImpl;
import com.engine.htsc.trip.util.AjaxResult;
import weaver.general.BaseBean;
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 javax.ws.rs.core.Response;
import java.util.List;
import java.util.Map;

public class PaymentExcelTemplateAction {

    private PaymentExcelTemplateService getService(User user) {
        return ServiceUtil.getService(PaymentExcelTemplateServiceImpl.class, user);
    }


    @GET
    @Path("/getDetailByExcel")
    @Produces({MediaType.APPLICATION_JSON})
    public AjaxResult<List<HTPaymentCostDetail>> getDetailByExcel(@Context HttpServletRequest request,
                                                                  @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            List<HTPaymentCostDetail> resultStr = this.getService(user).getDetailByExcel(ParamUtil.request2Map(request));
            return AjaxResult.ok(resultStr);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 联合通用付款导入Excel
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/getUnionDetailByExcel")
    @Produces({MediaType.APPLICATION_JSON})
    public Response getLhDetailByExcel(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> map = ParamUtil.request2Map(request);
        try {
            User user = HrmUserVarify.getUser(request, response);
            int imageId = Integer.parseInt((String) map.get("imageId"));
            String ngrid = (String) map.get("ngrid");
            JSONArray jsonArray = this.getService(user).readByImageId(imageId,ngrid);
            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();
        }

    }
}
