package com.engine.htsc.payment.web;

import com.alibaba.fastjson.JSON;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.payment.pojo.SyncVo;
import com.engine.htsc.payment.service.PaymentCommonService;
import com.engine.htsc.payment.service.impl.PaymentCommonServiceImpl;
import com.engine.htsc.trip.util.AjaxResult;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.File;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

public class PaymentCommonAction extends BaseBean {

  private PaymentCommonService getServices() {
    return ServiceUtil.getService(PaymentCommonServiceImpl.class);
  }

  @GET
  @Path("/download/attachments/zip")
  public Response attachmentsZip(@Context HttpServletRequest request, @Context HttpServletResponse response) {
    try {
      writeLog("/download/attachments/zip");
      User user = HrmUserVarify.getUser(request, response);
      File zipFile = getServices().buildAttachmentsZipFile();
      String mt = new MimetypesFileTypeMap().getContentType(zipFile);
      return Response.ok(zipFile, mt)
              .header("Content-disposition", "attachment;filename=" + URLEncoder.encode(zipFile.getName(), "UTF-8"))
              .header("Cache-Control", "no-cache").build();
    } catch (Exception e) {
      return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
    }
  }

  @GET
  @Path("/sync/contract")
  public Response updateContractInfo(@Context HttpServletRequest request) {
    try {
      Map<String, Object> params = ParamUtil.request2Map(request);
      String fromDB = (String) params.getOrDefault("fromDB", "");
      StringBuilder rids = new StringBuilder((String) params.getOrDefault("rids", ""));
      String syncIt = (String) params.getOrDefault("syncIt", "");
      if("true".equals(fromDB)){
        RecordSet rs = new RecordSet();
        rids = new StringBuilder();
        rs.executeQuery("select distinct reqid from uf_reSync_Payment t ");
        while (rs.next()){
          rids.insert(0, Util.null2String(rs.getString("reqid")) + ",");
        }
      }
      String resp = getServices().syncContractInfo(syncIt, rids.toString());
      return Response.ok(resp).build();
    } catch (Exception e) {
      return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
    }
  }

  @POST
  @Path("/sync/fundStatus/all")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<List<Map<String, String>>> syncFundAllStatus(Map<String, String> params) {
    writeLog("sync fund status starting... params: " + JSON.toJSONString(params));
    try {
      List<Map<String, String>> responseMsg = this.getServices()
          .syncFundAllStatus(Util.null2String(params.get("requestMarks"), ""));
      return AjaxResult.ok(responseMsg);
    } catch (Exception e) {
      writeLog("sync fund status fail, catch error: ", e);
      return AjaxResult.error(e.getMessage());
    }
  }
}
