package com.engine.htsc.trip.cmd;

import com.alibaba.fastjson.JSON;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import java.util.HashMap;
import java.util.Map;

import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.interfaces.htsc.cus.HTUtil;

public class GetBudgetCmd extends AbstractCommonCommand<Map<String, Object>> {
  
  public GetBudgetCmd(User user, Map<String, Object> params) {
    this.user = user;
    this.params = params;
  }
  
  @Override
  public BizLogContext getLogContext() {
    return null;
  }
  
  @Override
  public Map<String, Object> execute(CommandContext commandContext) {
    Map<String, Object> apidatas = new HashMap<>();
    try {
      HTUtil ht = new HTUtil();
      writeLog("====进入GetBudgetCmd");
      writeLog("====入参@" + JSON.toJSONString(params));
      String deptid = Util.null2String(params.get("deptid"));
      String Bgitem = Util.null2String(params.get("Bgitem"));
      String BgPeriod = Util.null2String(params.get("BgPeriod"));
      DepartmentComInfo dc = new DepartmentComInfo();
      String bmCode = dc.getDepartmentCode(deptid);
      Map<String,Object> param = new HashMap<>();
      param.put("deptNum", bmCode);
      param.put("deptType", getDeptTypeCode(deptid));
      param.put("Bgitem", Bgitem);
      param.put("BgPeriod", BgPeriod.split(" ")[0]);
      writeLog("====getBudget入参@" + JSON.toJSONString(param));
      String datas = ht.getBudget(param);
      writeLog("====getBudget返回值@" + datas);
      apidatas.put("datas", datas);
    } catch (Exception e) {
      e.printStackTrace();
      return apidatas;
    }
    return apidatas;
  }

  private String getDeptTypeCode(String deptId) {
    String code = "";
    String sql = "select depttypecode from hrmdepartmentdefined where deptid=?";
    RecordSet rs = new RecordSet();
    rs.executeQuery(sql, deptId);
    while (rs.next()) {
      code = rs.getString("depttypecode");
      break;
    }
    return code;
  }
}
