package com.engine.htsc.payment.web;

import com.engine.common.util.ServiceUtil;
import com.engine.htsc.payment.pojo.*;
import com.engine.htsc.payment.service.PaymentService;
import com.engine.htsc.payment.service.impl.PaymentServiceImpl;
import com.engine.htsc.payment.util.PayUtil;
import com.engine.htsc.trip.util.AjaxResult;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 总部付款restful接口
 */
public class HeadGeneralPaymentAction {
  public HeadGeneralPaymentAction() {
  }

  private PaymentService getServices() {
    return ServiceUtil.getService(PaymentServiceImpl.class);
  }

  /**
   * 提交时校验银行账户(多行)
   *
   * @param dtdata 明细11数据
   * @return true or false
   */
  @POST
  @Path("/queryHistoryPayRecordWithSubmit")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<String> queryHistoryPayRecordWithSubmit(Map<String, List<String>> dtdata) {
    try {
      String resp = this.getServices().queryHistoryPayRecordWithSubmit(dtdata);
      return AjaxResult.ok(resp);
    } catch (Exception e) {
      e.printStackTrace();
      return AjaxResult.error(e.getMessage());
    }
  }

  /**
   * 获取汇率值
   *
   * @param request 请求
   * @return 汇率对应关系
   */
  @POST
  @Path("/queryExchangeRate")
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<Map<String, String>> queryExchangeRate(@Context HttpServletRequest request) {
    try {
      Map<String, String> resp = this.getServices().queryExchangeRate();
      return AjaxResult.ok(resp);
    } catch (Exception e) {
      e.printStackTrace();
      return AjaxResult.error(e.getMessage());
    }
  }

  /**
   * 查询合同已付金额
   *
   * @param contractCode 合同编码
   * @param processCode  合同流程编码
   * @return json
   */
  @GET
  @Path("/queryContractPaidMoney")
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<String> queryContractPaid(
          @QueryParam("contractCode") String contractCode,
          @QueryParam("processCode") String processCode
  ) {
    try {
      String reJson = this.getServices().queryContractPaidMoney(contractCode, processCode);
      return AjaxResult.ok(reJson);
    } catch (Exception e) {
      e.printStackTrace();
      return AjaxResult.error(e.getMessage());
    }
  }

  @GET
  @Path("/queryVirtualDepartment")
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<Map<String, String>> queryVirtualDepartment(@QueryParam("fycdbm") String fycdbm) {
    Map<String, String> result = new HashMap<>();
    try {
      String s = this.getServices().queryVirtualDepartment(fycdbm);
      result.put("fycdbm", fycdbm);
      result.put("virtualdept", s);
      return AjaxResult.ok(result);
    } catch (Exception e) {
      e.printStackTrace();
      return AjaxResult.error(e.getMessage());
    }
  }

  /**
   * 查询合同信息
   *
   * @param contractRequestParam 请求参数dto
   * @return ContractInfoVo
   */
  @POST
  @Path("/queryContract")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public ContractInfoVo queryContract(ContractRequestParam contractRequestParam) {
    ContractInfoVo info = new ContractInfoVo();
    try {
      info = this.getServices().queryContractPagination(contractRequestParam);
      return info;
    } catch (Exception e) {
      e.printStackTrace();
      return info;
    }
  }

  /**
   * 查询it项目信息
   *
   * @param condition 请求dto
   * @return ItProjectVo
   */
  @POST
  @Path("/queryItProject")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public ItProjectVo queryItProject(ItProjectQueryCondition condition) {
    ItProjectVo itProjectVo = new ItProjectVo();
    try {
      itProjectVo = this.getServices().queryItProject(condition);
    } catch (Exception e) {
      e.printStackTrace();
      itProjectVo.setOk(false);
      itProjectVo.setMsg("error: " + e);
    }
    return itProjectVo;
  }


  /**
   * 查询部门负责人
   *
   * @param deptIds 部门ids，逗号分隔
   * @return 对应部门负责人的ids（去空，去重）
   */
  @GET
  @Path("/getDivisionHead")
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<String> getDivisionHead(@QueryParam("deptIds") String deptIds) {
    try {
      String heads = PayUtil.getDivisionHead(deptIds);
      return AjaxResult.ok(heads);
    } catch (Exception e) {
      return AjaxResult.error("error: " + e);
    }

  }

  /**
   * 付款明细预算科目带出会计明细会计科目
   *
   * @param budgetCode 预算code
   * @return aja
   */
  @GET
  @Path("/getBrowser")
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<String> getBrowserValue(@QueryParam("budgetCode") String budgetCode) {
    try {
      String browserValue = this.getServices().getAccountCode(budgetCode);
      return AjaxResult.ok(browserValue);
    } catch (Exception e) {
      return AjaxResult.error("error: " + e);
    }

  }

  /**
   * 付款明细预算科目带出会计明细会计科目(联合)
   *
   * @param budgetCode 预算code
   * @return aja
   */
  @GET
  @Path("/getBrowser4Lh")
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<String> getBrowserValue4Lh(@QueryParam("budgetCode") String budgetCode) {
    try {
      String browserValue = this.getServices().getAccountCode4Lh(budgetCode);
      return AjaxResult.ok(browserValue);
    } catch (Exception e) {
      return AjaxResult.error("error: " + e);
    }

  }

  /* 付款明细预算科目带出会计明细会计科目(香港)
   *
   * @param budgetCode 预算code
   * @return aja
   */
  @GET
  @Path("/getBrowser4HK")
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<String> getBrowserValue4HK(@QueryParam("budgetCode") String budgetCode) {
    try {
      String browserValue = this.getServices().getAccountCode4HK(budgetCode);
      return AjaxResult.ok(browserValue);
    } catch (Exception e) {
      return AjaxResult.error("error: " + e);
    }

  }



  /**
   * 发票关联后,校验该发票是否已存在于之前的流程当中
   * fphm + fpdm = 唯一标识
   *
   * @param fphm 发票号码
   * @param fpdm 发票代码
   * @return json
   */
  @GET
  @Path("/queryInvoiceOnce")
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<Map<String, String>> queryInvoiceOnce(@QueryParam("fphm") String fphm, @QueryParam("fpdm") String fpdm) {
    try {
      Map<String, String> res = this.getServices().queryInvoiceOnce(fphm, fpdm);
      return AjaxResult.ok(res);
    } catch (Exception e) {
      e.printStackTrace();
      return AjaxResult.error("发票关联后,校验该发票是否已存在接口报错: " + e);
    }
  }

  /**
   * 历史付款记录查验
   *
   * @param bankAccount
   * @param payeeDept
   * @param bankName
   * @param city
   * @return map
   * res.put("flag", "0"); //0 = 不允许提交 1 = 允许提交
   * res.put("isTrueAccount", "1"); // 1 正确的银行账户 0 不正确
   * res.put("addition", ""); 附加信息
   */
  @GET
  @Path("/queryHistoryPayRecord")
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<Map<String, String>> queryHistoryPayRecord(
          @QueryParam("yhzh") String bankAccount,
          @QueryParam("skdw") String payeeDept,
          @QueryParam("khh") String bankName,
          @QueryParam("city") String city
  ) {
    try {
      Map<String, String> res = this.getServices().queryHistoryPayRecord(bankAccount, payeeDept, bankName, city);
      return AjaxResult.ok(res);
    } catch (Exception e) {
      e.printStackTrace();
      return AjaxResult.error("发票关联后,校验该发票是否已存在接口报错: " + e);
    }
  }


}
