const system = require("../../../system");
const ServiceBase = require("../../svems.base");
const settings = require("../../../../config/settings");

class InvoiceService extends ServiceBase {
  constructor() {
    super();
    this.userSve = system.getObject("service.uc.userSve");
  }
  async processDics(params) {
    var rs = await this.callms("invoice", "processDics", params);
    return rs;
  }

  async invoiceApply(params) {
    var rs = await this.callms("invoice", "invoiceApply", params);
    return rs;
  }

  async invoicePage(params) {
    params.currentPage = Number(params.currentPage || 1);
    params.pageSize = Number(params.pageSize || 10);

    var rs = await this.callms("invoice", "invoicePage", params);
    if (rs.data && rs.data.rows) {
      for (let item of rs.data.rows) {
        item.invoice_amount = system.f2y(item.invoice_amount);
      }
    }
    return rs;
  }


  async deliverInvoicePage(params) {
    params.currentPage = Number(params.currentPage || 1);
    params.pageSize = Number(params.pageSize || 10);
    var rs = await this.callms("invoice", "deliverInvoicePage", params);
    if (rs.data && rs.data.rows) {
      for (let item of rs.data.rows) {
        item.invoice_amount = system.f2y(item.invoice_amount);
      }
    }
    return rs;
  }

  async handleStatus(params) {
    var rs = await this.callms("invoice", "handleStatus", params);
    return rs;
  }

  async invoice(params) {
    var rs = await this.callms("invoice", "invoice", params);
    await this.setUcUser([rs.data]);
    return rs;
  }

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

    let ids = [];
    for (let row of rows) {
      if (row.bd_id) {
        ids.push(row.bd_id);
      }
      if (row.assign_user_id) {
        ids.push(row.assign_user_id);
      }
    }

    let userRs = await this.userSve.mapByIds({ ids: ids });
    let userMap = userRs.data || {};

    for (let row of rows) {
      let user = userMap[row.bd_id] || {};
      row.bd_name = user.realName || "";

      let assignUser = userMap[row.assign_user_id] || {};
      row.assign_user_name = assignUser.realName || "";
    }
  }
  
}
module.exports = InvoiceService;