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

import cn.hutool.core.lang.Assert;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htsc.financialadjustment.excel.ExcelDataListener;
import com.engine.htsc.financialadjustment.pojo.FinancialAjExcelData;
import com.engine.htsc.financialadjustment.service.FinService;
import com.engine.htsc.financialadjustment.util.BizUtil;
import org.apache.commons.lang.StringUtils;
import weaver.file.ImageFileManager;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.interfaces.htsc.cus.DepartmentUtils;
import weaver.interfaces.htsc.cus.HTUtil;
import weaver.workflow.logging.Logger;
import weaver.workflow.logging.LoggerFactory;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class FinServiceImpl extends Service implements FinService {

  private final Logger logger = LoggerFactory.getLogger();

  @Override
  public JSONArray readByImageId(int imageId, String zbOrLh) throws Exception {
    List<FinancialAjExcelData> source = read2List(imageId);
    logger.info(String.format("parse image id: %s, and type: %s", imageId, zbOrLh));
    JSONArray jsa = new JSONArray();
    for (FinancialAjExcelData d : source) {
      JSONObject jso = new JSONObject();
      jso.put("entryCredit", d.getCredit());
      jso.put("expenseUid", BizUtil.getUidByWc(d.getExpensePerson()));
      jso.put("expenseDeptId", BizUtil.getDidByCode(d.getExpenseDept()));
      jso.put("budgetCode", BizUtil.getBudgetObj(d.getBudgetCode(), zbOrLh));
      jso.put("amount", d.getAmount());
      jso.put("overBudget", d.getOverBudget());
      jso.put("accountCode", BizUtil.getAccountObj(d.getAccountCode(), zbOrLh));
      jso.put("debtSide", d.getDebtSide());
      jso.put("projectId", StringUtils.isNotBlank(d.getProjectCode()) ? BizUtil.getProjectBrowserByCode(d.getExpensePerson(), d.getProjectCode()) : "");
      jso.put("projectDeptId", StringUtils.isNotBlank(d.getProjectDept()) ? BizUtil.getDidByCode(d.getProjectDept()) : "");
      jsa.add(jso);
    }
    return jsa;
  }

  @Override
  public JSONObject getDeptBrowserByProjectId(String userCode, String projectId) throws Exception {
    HTUtil htUtil = new HTUtil();
    JSONObject res = new JSONObject();
    Assert.notBlank(projectId, "公司项目id不能为空");
    Map<String, Object> info = htUtil.getProjectInfoById(userCode, projectId);
    if (info.containsKey("id")) {
      String deptCode = Util.null2String(info.get("deptCode"));
      res.put("value", DepartmentUtils.getDeptId(deptCode));
      res.put("name", Util.null2String(info.get("deptName")));
      res.put("shortName", Util.null2String(info.get("name")));
      res.put("code",Util.null2String(info.get("code")));
    } else {
      res.put("value", "");
      res.put("name", "");
    }
    htUtil.writeLog(String.format("userCode:%s; project id:%s; json content:%s", userCode,
            projectId, res));
    return res;
  }


  private List<FinancialAjExcelData> read2List(int imageFileId) throws Exception {
    InputStream fis = ImageFileManager.getInputStreamById(imageFileId);
    ExcelDataListener<FinancialAjExcelData> listener = new ExcelDataListener<>();
    // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
    EasyExcel.read(fis, FinancialAjExcelData.class, listener).sheet().doRead();
    return listener.getList();
  }
}
