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

import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htsc.payment.util.BizUtils;
import com.engine.htsc.payment.util.PayUtil;
import com.engine.htsc.trip.pojo.UnionStroke;
import com.engine.htsc.trip.pojo.UnionTraveler;
import com.engine.htsc.trip.service.TripExcelOutService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.usermodel.Workbook;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.cus.HTUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TripExcelOutServiceImpl extends Service implements TripExcelOutService {

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

  @Override
  public Workbook getExportData(String startTime, String endTime) throws Exception {

    //sheet1
    String sheetOneSql = "\n" +
            "select t1.LCBH,t2.REQUESTNAME,t1.NGR,t1.NGRQ,\n" +
            "       t1.cclxzxx,\n" +
            "       t1.TXXMJC,t1.TXXMBH,t1.xmjc,\n" +
            "       t1.KHBM,t1.KHMC,\n" +
            "       t1.CCQK,\n" +
            "       t1.XDFHSJ,\n" +
            "\n" +
            "       t3.CCRGH,\n" +
            "       t3.XM,\n" +
            "       t3.BM,t3.BMFZR,\n" +
            "       t3.FYCDBM,t3.FYCDBMFZR,\n" +
            "       t3.GZD,t3.ZJ,t3.BXD\n" +
            "\n" +
            "from FORMTABLE_MAIN_58 t1\n" +
            "left join WORKFLOW_REQUESTBASE t2\n" +
            "on t1.REQUESTID=t2.REQUESTID\n" +
            "left join FORMTABLE_MAIN_58_DT2 t3\n" +
            "on t1.ID=t3.MAINID\n" +
            "where t1.LCBH is not null and t1.CCQK is not null and t1.CCLXZXX is not null\n" +
            "and to_date(t1.NGRQ,'yyyy-MM-dd HH24:MI:SS')\n" +
            "between to_date('" + startTime + "','yyyy-MM-dd') and to_date('" + endTime + "','yyyy-MM-dd')";
    List<UnionTraveler> traveler = getTravelerData(sheetOneSql);
//    sheet2
    String sheetTSql = "select t1.LCBH,t2.REQUESTNAME,t1.NGR,t1.NGRQ,\n" +
            "       t1.cclxzxx,\n" +
            "       t1.TXXMJC,t1.TXXMBH,t1.xmjc,\n" +
            "       t1.KHBM,t1.KHMC,\n" +
            "       t1.CCQK,\n" +
            "       t1.XDFHSJ,\n" +
            "\n" +
            "       t3.SFBC,t3.CFDGN,t3.MDDGN,t3.CFDHW,t3.MDDHW,t3.CFSJ,t3.FHSJ\n" +
            "\n" +
            "from FORMTABLE_MAIN_58 t1\n" +
            "left join WORKFLOW_REQUESTBASE t2\n" +
            "on t1.REQUESTID=t2.REQUESTID\n" +
            "left join FORMTABLE_MAIN_58_DT3 t3\n" +
            "on t1.ID=t3.MAINID\n" +
            "where t1.LCBH is not null and t1.CCQK is not null and t1.CCLXZXX is not null and\n" +
            "to_date(t1.NGRQ,'yyyy-MM-dd HH24:MI:SS')\n" +
            "between to_date('" + startTime + "','yyyy-MM-dd') and to_date('" + endTime + "','yyyy-MM-dd')";
    List<UnionStroke> stroke = getStrokeData(sheetTSql);
    return null;
  }

  @Override
  public List<UnionTraveler> getTravelerData(String sql) throws Exception {
    log.info("获取出差人相关信息:" + sql);
    RecordSet rs = new RecordSet();
    ResourceComInfo rc = new ResourceComInfo();
    DepartmentComInfo dc = new DepartmentComInfo();
    rs.executeQuery(sql);

    List<UnionTraveler> list = new ArrayList<>();

    while (rs.next()) {
      String uid = Util.null2String(rs.getString("REQUESTNAME"));
      String xmjc = Util.null2String(rs.getString("xmjc"));
      String TXXMBH = Util.null2String(rs.getString("TXXMBH"));
      UnionTraveler u = new UnionTraveler();

      u.setLcbh(Util.null2String(rs.getString("LCBH")));
      u.setRequestName(Util.null2String(rs.getString("REQUESTNAME")));
      u.setNgr(rc.getLastname(uid));
      u.setNgrq(Util.null2String(rs.getString("NGRQ")));
      u.setCclxzxx(Util.null2String(rs.getString("cclxzxx")));
      u.setTxxmjc(xmjc);
      if ("".equals(TXXMBH)) {
        u.setTxmxbh(convertXmId2Name(xmjc));
      } else {
        u.setTxmxbh(TXXMBH);
      }
      u.setKhmc(Util.null2String(rs.getString("KHBM")));
      u.setKhbm(Util.null2String(rs.getString("KHMC")));
      u.setCcqk(Util.null2String(rs.getString("CCQK")));
      u.setXdfhsj(Util.null2String(rs.getString("XDFHSJ")));

      String ccruid = Util.null2String(rs.getString("XM"));
      String bmid = Util.null2String(rs.getString("BM"));
      String BMFZRuid = Util.null2String(rs.getString("BMFZR"));
      String fycdbmid = Util.null2String(rs.getString("FYCDBM"));
      String fycdbmfzruid = Util.null2String(rs.getString("FYCDBMFZR"));

      u.setCcrgh(rc.getWorkcode(ccruid));
      u.setXm(rc.getLastname(ccruid));
      u.setBm(dc.getDepartmentmark(bmid));

      u.setBmfzr(rc.getLastname(BMFZRuid));
      u.setFycdbm(dc.getDepartmentmark(fycdbmid));
      u.setBmfzr(rc.getLastname(fycdbmfzruid));
      u.setGzd(Util.null2String(rs.getString("GZD")));
      u.setZj(Util.null2String(rs.getString("ZJ")));
      u.setBxd(Util.null2String(rs.getString("BXD")));

      list.add(u);

    }

    return list;
  }

  @Override
  public List<UnionStroke> getStrokeData(String sql) throws Exception {
    log.info("获取出差行程相关信息:" + sql);
    RecordSet rs = new RecordSet();
    ResourceComInfo rc = new ResourceComInfo();
    DepartmentComInfo dc = new DepartmentComInfo();
    rs.executeQuery(sql);

    List<UnionStroke> list = new ArrayList<>();

    while (rs.next()) {
      String uid = Util.null2String(rs.getString("REQUESTNAME"));
      String xmjc = Util.null2String(rs.getString("xmjc"));
      String TXXMBH = Util.null2String(rs.getString("TXXMBH"));
      UnionStroke u = new UnionStroke();

      u.setLcbh(Util.null2String(rs.getString("LCBH")));
      u.setRequestName(Util.null2String(rs.getString("REQUESTNAME")));
      u.setNgr(rc.getLastname(uid));
      u.setNgrq(Util.null2String(rs.getString("NGRQ")));
      u.setCclxzxx(Util.null2String(rs.getString("cclxzxx")));
      u.setTxxmjc(xmjc);
      if ("".equals(TXXMBH)) {
        u.setTxmxbh(convertXmId2Name(xmjc));
      } else {
        u.setTxmxbh(TXXMBH);
      }
      u.setLcbh(Util.null2String(rs.getString("LCBH")));
      u.setRequestName(Util.null2String(rs.getString("REQUESTNAME")));
      u.setNgr(rc.getLastname(uid));
      u.setNgrq(Util.null2String(rs.getString("NGRQ")));
      u.setCclxzxx(Util.null2String(rs.getString("cclxzxx")));
      u.setTxxmjc(Util.null2String(rs.getString("TXXMJC")));
      u.setTxmxbh(Util.null2String(rs.getString("TXXMBH")));
      u.setKhmc(Util.null2String(rs.getString("KHBM")));
      u.setKhbm(Util.null2String(rs.getString("KHMC")));
      u.setCcqk(Util.null2String(rs.getString("CCQK")));
      u.setXdfhsj(Util.null2String(rs.getString("XDFHSJ")));

      u.setSfbx(Util.null2String(rs.getString("SFBC")));
      u.setCfdgn(PayUtil.cityCodeToName(Util.null2String(rs.getString("CFDGN"))));
      u.setMddgn(PayUtil.cityCodeToName(Util.null2String(rs.getString("MDDGN"))));
      u.setCfdhw(Util.null2String(rs.getString("CFDHW")));
      u.setMddhw(Util.null2String(rs.getString("MDDHW")));
      u.setCfsj(Util.null2String(rs.getString("CFSJ")));
      u.setFhsj(Util.null2String(rs.getString("FHSJ")));

      list.add(u);
    }

    return list;
  }


  public String convertXmId2Name(String xmId) {
    Map<String, Object> project = new HashMap<>();
    project.put("projectId", xmId);
    String res = new HTUtil().getProjectById(project);
    if (StringUtils.isBlank(res)) {
      return "";
    }
    JSONObject jsonObject = JSONObject.parseObject(res);
    return Util.null2String(jsonObject.get("shortName"));
  }
}
