const system = require("../../../system");
const ServiceBase = require("../../sve.base")
const settings = require("../../../../config/settings")
class TcompanytaxService extends ServiceBase {
  constructor() {
    super("tax", ServiceBase.getDaoName(TcompanytaxService));

    this.tcompanyDao = system.getObject("db.tax.tcompanyDao");
  }

  async pageByCondition(condition) {
    condition.currentPage = Number(condition.currentPage || 1);
    condition.pageSize = Number(condition.pageSize || 10);
    condition.startRow = Number((condition.currentPage - 1) * condition.pageSize);

    let total = await this.dao.countByCondition(condition);
    if (total == 0) {
      return {count: 0, rows: []};
    }

    let list = await this.dao.pageByCondition(condition);
    if (list) {
      for (var item of list) {
        this.handleDate(item, ['updated_at', 'created_at'], 'YYYY-MM-DD HH:mm:ss');
        this.handleDate(item, ['month_date'], 'YYYY-MM');
      }
      await this.setCompanyName(list);
    }
    return {count: total, rows: list};
  }

  async setCompanyName(rows) {
    if (!rows || rows.length == 0) {
      return;
    }

    var copmanyIds = [];
    for (var row of rows) {
      copmanyIds.push(row.companyId);
    }

    var map = await this.tcompanyDao.findMapByIds(copmanyIds);
    for (var row of rows) {
      let company = map[row.companyId] || {};
      row.companyName = company.name || "";
      row.mchtId = company.mchtId;
    }
  }

}
module.exports = TcompanytaxService;
