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

import com.engine.core.impl.Service;
import com.engine.htsc.payment.service.PaymentQueryDetailService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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;

public class PaymentQueryDetailServiceImpl extends Service implements PaymentQueryDetailService {

  private Log log = LogFactory.getLog(this.getClass());

  @Override
  public List<Map<String, Object>> getDetailInfo(Map<String, Object> params) {
    int requestId = Util.getIntValue((String) params.get("requestId"));
    int bmbm = Util.getIntValue((String) params.get("bmbm"));
    String type = Util.null2String(params.get("type"));
    if (StringUtils.isBlank(type)) {
      log.info("input param type can not be empty or null!");
      return null;
    }
    String slptly = null;
    if (type.contains("-")) {
      String[] split = type.split("-");
      slptly = split[0].toUpperCase();
      type = split[1];
    }
    RecordSet rs = new RecordSet();
    String tablename = "formtable_main_";
    String tableSql = "select formid from workflow_base where id = " +
        "(select workflowid from workflow_requestbase where requestid = " + requestId + ")";
    rs.execute(tableSql);
    if (rs.next()) {
      int formid = Math.abs(Util.getIntValue(rs.getString("formid")));
      tablename = tablename + formid;
    }
    //查询主表id
    int mainId = -1;
    rs.executeQuery(String.format("select id from %s where requestid = ?", tablename), requestId);
    while (rs.next()) {
      mainId = Util.getIntValue(rs.getString("id"));
    }
    if (mainId == -1) {
      log.info("get maintable id failed!");
      return null;
    }
    if ("plane".equals(type)) {
      log.info("query plane order detail");
      return getPlaneDetail(bmbm, tablename, mainId, slptly);
    }
    if ("train".equals(type)) {
      log.info("query train order detail");
      return getTrainDetail(bmbm, tablename, mainId, slptly);
    }
    if ("car".equals(type)) {
      log.info("query car order detail");
      return getCarDetail(bmbm, tablename, mainId, slptly);
    }
    if ("hotel".equals(type)) {
      log.info("query hotel order detail");
      return getHotelDetail(bmbm, tablename, mainId, slptly);
    }
    if ("allowance".equals(type)) {
      log.info("query allowance detail");
      return getAllowanceDetail(bmbm, tablename, mainId);
    }
    return null;
  }

  private String getDepartmentName(String deptCode) {
    String deptName = "";
    RecordSet rs = new RecordSet();
    rs.executeQuery("select departmentname from hrmdepartment where departmentcode= ?", deptCode);
    while (rs.next()) {
      deptName = Util.null2String(rs.getString("departmentname"));
    }
    return deptName;
  }

  private List<Map<String, Object>> getPlaneDetail(int bmbm, String tablename, int mainid,
      String slptly) {
    List<Map<String, Object>> result = new ArrayList<>();
    RecordSet rs = new RecordSet();
    tablename = tablename + "_dt15";
    if (slptly == null) {
      rs.executeQuery(String.format("select * from %s where mainid = ? and bmbm = ?", tablename),
          mainid, bmbm);
    } else {
      rs.executeQuery(
          String.format("select * from %s where mainid = ? and bmbm = ? and slptly = ?", tablename),
          mainid, bmbm, slptly);
    }
    while (rs.next()) {
      Map<String, Object> map = new HashMap<>();
      map.put("fycdbm", getDepartmentName(rs.getString("fycdbm")));
      map.put("yggh", rs.getString("yggh"));
      map.put("cjr", rs.getString("cjr"));
      map.put("ccsqbh", rs.getString("ccsqbh"));
      map.put("cjjj", rs.getString("cjjj"));
      map.put("ry", rs.getString("ry"));
      map.put("pmje", rs.getString("pmje"));
      map.put("tpf", rs.getString("tpf"));
      map.put("gqf", rs.getString("gqf"));
      map.put("fwf", rs.getString("fwf"));
      map.put("kdkse", rs.getString("kdkse"));
      map.put("bkdkse", rs.getString("bkdkse"));
      map.put("spf", rs.getString("spf"));
      map.put("bxf", rs.getString("bxf"));
      map.put("gqcj", rs.getString("gqcj"));
      map.put("hj", rs.getString("hj"));
      map.put("cfsj", rs.getString("cfsj"));
      map.put("cfcs", rs.getString("cfcs"));
      map.put("cfjc", rs.getString("cfjc"));
      map.put("ddsj", rs.getString("ddsj"));
      map.put("ddcs", rs.getString("ddcs"));
      map.put("ddjc", rs.getString("ddjc"));
      map.put("hbh", rs.getString("hbh"));
      map.put("fxsc", rs.getString("fxsc"));
      map.put("ddh", rs.getString("ddh"));
      map.put("ddlx", rs.getString("ddlx"));
      map.put("zkl", rs.getString("zkl"));
      map.put("djhbjg", rs.getString("djhbjg"));
      map.put("wxdjhbyy", rs.getString("wxdjhbyy"));
      map.put("bmbm", rs.getString("bmbm"));
      map.put("ygbm", rs.getString("ygbm"));
      map.put("slptly", rs.getString("slptly"));
      result.add(map);
    }
    return result;
  }

  private List<Map<String, Object>> getTrainDetail(int bmbm, String tablename, int mainid,
      String slptly) {
    List<Map<String, Object>> result = new ArrayList<>();
    RecordSet rs = new RecordSet();
    tablename = tablename + "_dt16";
    if (slptly == null) {
      rs.executeQuery(String.format("select * from %s where mainid = ? and bmbm = ?", tablename),
          mainid, bmbm);
    } else {
      rs.executeQuery(
          String.format("select * from %s where mainid = ? and bmbm = ? and slptly = ?", tablename),
          mainid, bmbm, slptly);
    }
    while (rs.next()) {
      Map<String, Object> map = new HashMap<>();
      map.put("fycdbm", getDepartmentName(rs.getString("fycdbm")));
      map.put("yggh", rs.getString("yggh"));
      map.put("ccr", rs.getString("ccr"));
      map.put("ccsqbh", rs.getString("ccsqbh"));
      map.put("pmje", rs.getString("pmje"));
      map.put("fwf", rs.getString("fwf"));
      map.put("hhzfwfje", rs.getString("hhzfwfje"));
      map.put("tpfy", rs.getString("tpfy"));
      map.put("hj", rs.getString("hj"));
      map.put("cfsj", rs.getString("cfsj"));
      map.put("cfcs", rs.getString("cfcs"));
      map.put("cfcz", rs.getString("cfcz"));
      map.put("ddsj", rs.getString("ddsj"));
      map.put("ddcs", rs.getString("ddcs"));
      map.put("ddcz", rs.getString("ddcz"));
      map.put("cc", rs.getString("cc"));
      map.put("xb", rs.getString("xb"));
      map.put("ddh", rs.getString("ddh"));
      map.put("ddlx", rs.getString("ddlx"));
      map.put("bmbm", rs.getString("bmbm"));
      map.put("ygbm", rs.getString("ygbm"));
      map.put("slptly", rs.getString("slptly"));
      result.add(map);
    }
    return result;
  }

  private List<Map<String, Object>> getCarDetail(int bmbm, String tablename, int mainid,
      String slptly) {
    List<Map<String, Object>> result = new ArrayList<>();
    RecordSet rs = new RecordSet();
    tablename = tablename + "_dt18";
    if (slptly == null) {
      rs.executeQuery(String.format("select * from %s where mainid = ? and bmbm = ?", tablename),
          mainid, bmbm);
    } else {
      rs.executeQuery(
          String.format("select * from %s where mainid = ? and bmbm = ? and slptly = ?", tablename),
          mainid, bmbm, slptly);
    }
    while (rs.next()) {
      Map<String, Object> map = new HashMap<>();
      map.put("fycdbm", getDepartmentName(rs.getString("fycdbm")));
      map.put("yggh", rs.getString("yggh"));
      map.put("ccr", rs.getString("ccr"));
      map.put("ccsqbh", rs.getString("ccsqbh"));
      map.put("jj", rs.getString("jj"));
      map.put("fwf", rs.getString("fwf"));
      map.put("jsse", rs.getString("jsse"));
      map.put("hj", rs.getString("hj"));
      map.put("cfsj", rs.getString("cfsj"));
      map.put("ddsj", rs.getString("ddsj"));
      map.put("cfcs", rs.getString("cfcs"));
      map.put("ddcs", rs.getString("ddcs"));
      map.put("cfdz", rs.getString("cfdz"));
      map.put("dddz", rs.getString("dddz"));
      map.put("jl", rs.getString("jl"));
      map.put("sc", rs.getString("sc"));
      map.put("ddh", rs.getString("ddh"));
      map.put("ddzt", rs.getString("ddzt"));
      map.put("jcfymx", rs.getString("jcfymx"));
      map.put("zzfy", rs.getString("zzfy"));
      map.put("wyj", rs.getString("wyj"));
      map.put("kdfy", rs.getString("kdfy"));
      map.put("bmbm", rs.getString("bmbm"));
      map.put("ygbm", rs.getString("ygbm"));
      map.put("slptly", rs.getString("slptly"));
      result.add(map);
    }
    return result;
  }

  private List<Map<String, Object>> getHotelDetail(int bmbm, String tablename, int mainid,
      String slptly) {
    List<Map<String, Object>> result = new ArrayList<>();
    RecordSet rs = new RecordSet();
    tablename = tablename + "_dt17";
    if (slptly == null) {
      rs.executeQuery(String.format("select * from %s where mainid = ? and bmbm = ?", tablename),
          mainid, bmbm);
    } else {
      rs.executeQuery(
          String.format("select * from %s where mainid = ? and bmbm = ? and slptly = ?", tablename),
          mainid, bmbm, slptly);
    }
    while (rs.next()) {
      Map<String, Object> map = new HashMap<>();
      map.put("fycdbm", getDepartmentName(rs.getString("fycdbm")));
      map.put("yggh", rs.getString("yggh"));
      map.put("rzr", rs.getString("rzr"));
      map.put("ccsqbh", rs.getString("ccsqbh"));
      map.put("zszje", rs.getString("zszje"));
      map.put("dj", rs.getString("dj"));
      map.put("jys", rs.getString("jys"));
      map.put("sfkzp", rs.getString("sfkzp"));
      map.put("kzpsxf", rs.getString("kzpsxf"));
      map.put("fwf", rs.getString("fwf"));
      map.put("hj", rs.getString("hj"));
      map.put("fjm", rs.getString("fjm"));
      map.put("rzsj", rs.getString("rzsj"));
      map.put("ldsj", rs.getString("ldsj"));
      map.put("cs", rs.getString("cs"));
      map.put("jdm", rs.getString("jdm"));
      map.put("ddh", rs.getString("ddh"));
      map.put("bmbm", rs.getString("bmbm"));
      map.put("ygbm", rs.getString("ygbm"));
      map.put("slptly", rs.getString("slptly"));
      result.add(map);
    }
    return result;
  }

  private List<Map<String, Object>> getAllowanceDetail(int bmbm, String tablename, int mainid) {
    List<Map<String, Object>> result = new ArrayList<>();
    RecordSet rs = new RecordSet();
    tablename = tablename + "_dt19";
    rs.executeQuery(String.format("select * from %s where mainid = ? and bmbm = ?", tablename),
        mainid, bmbm);
    while (rs.next()) {
      Map<String, Object> map = new HashMap<>();
      map.put("cbje", rs.getString("cbje"));
      map.put("gh", rs.getString("gh"));
      map.put("xm", rs.getString("xm"));
      map.put("ccrbm", rs.getString("ccrbm"));
      map.put("lcbh", rs.getString("lcbh"));
      map.put("lcbt", rs.getString("lcbt"));
      map.put("khx", rs.getString("khx"));
      map.put("kh", rs.getString("kh"));
      map.put("bmbm", rs.getString("bmbm"));
      map.put("ygbm", rs.getString("ygbm"));
      result.add(map);
    }
    return result;
  }
}
