package com.engine.htsc.fundbusinesspay.service.impl;

import com.engine.core.impl.Service;
import com.engine.htsc.fundbusinesspay.service.FundBusinessService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.HashMap;
import java.util.Map;

public class FundBusinessServiceImpl extends Service implements FundBusinessService {

  private final Log log = LogFactory.getLog(FundBusinessServiceImpl.class);

  @Override
  public String getKjBrowserValue(String budgetCode) throws Exception {
    PropBean pp = new PropBean();
    RecordSet rs = new RecordSet();
    String accountTreeId = pp.getPropName("KBP_KJ_PRE");
    if (StringUtils.isBlank(accountTreeId)) {
      throw new Exception("资金业务付款会计科目前缀未维护");
    }
    String sql = "SELECT t1.HJKMBM,t1.HJKMMC\n" +
            "FROM uf_zjywhjkm t1\n" +
            "LEFT OUTER JOIN uf_zjywhjkm_DT1 t2 on t1.id=t2.mainid\n" +
            "WHERE (t2.yskm='" + budgetCode + "')";
    log.info("资金业务付款查询sql:" + budgetCode);
    rs.executeQuery(sql);
    String value = "";
    String name = "";
    if (rs.next()) {
      value = Util.null2String(rs.getString("HJKMBM"));
      name = Util.null2String(rs.getString("HJKMMC"));
    }
    if (StringUtils.isEmpty(value)) {
      throw new Exception("资金业务付款未查询到对应会计科目:" + budgetCode);
    } else {
      return accountTreeId + "_" + value + "@" + name;
    }
  }

  @Override
  public Map<String, String> getPaymentDateRange(String startDate) throws Exception {

    if (StringUtils.isBlank(startDate)) {
      throw new Exception("起始日期格式错误");
    }

    String sql = "select JYR from (\n" +
            "    select JYR\n" +
            "    from UF_JYRB order by JYR)\n" +
            "where jyr > '" + startDate + "' and rownum < 5";

    RecordSet rs = new RecordSet();
    rs.executeQuery(sql);
    String endDate = "";
    while (rs.next()) {
      endDate = rs.getString("JYR");
    }

    Map<String, String> resp = new HashMap<>();
    resp.put("start", startDate);
    resp.put("end", endDate);

    return resp;
  }

}
