const system = require("../../../system");
const Dao = require("../../dao.base");

class SaasOrderDao extends Dao {
  constructor() {
    super(Dao.getModelName(SaasOrderDao));
  }

  async countByCondition(params) {
    var sql = [];
    sql.push("SELECT");
    sql.push("count(1) as num");
    sql.push("FROM");
    sql.push("saas_order t1");
    sql.push("INNER JOIN `saas_order_bminfo` t2 ON t1.`id` = t2.`id`");
    sql.push("WHERE t1.deleted_at IS NULL");

    this.setCondition(sql, params);

    var list = await this.customQuery(sql.join(" "), params);
    if (!list || list.length == 0) {
      return 0;
    }
    return list[0].num;
  }

  async listByCondition(params) {
    params.startRow = Number(params.startRow || 0);
    params.pageSize = Number(params.pageSize || 10);

    var sql = [];
    sql.push("SELECT");
    sql.push("t1.*");
    sql.push("FROM");
    sql.push("saas_order t1");
    sql.push("INNER JOIN `saas_order_bminfo` t2 ON t1.`id` = t2.`id`");
    sql.push("WHERE t1.deleted_at IS NULL");

    this.setCondition(sql, params);

    sql.push("ORDER BY t1.id DESC");
    sql.push("LIMIT :startRow, :pageSize");
    return await this.customQuery(sql.join(" "), params);
  }

  setCondition(sql, params) {
    if (!params || !sql) {
      return;
    }
    if(params.attribute && params.value){
      sql.push(`AND t1.${params.attribute} = :value`);
    }

    if (params.saas_id) {
      sql.push("AND t1.saas_id = :saas_id");
    }

    if (params.merchant_id) {
      sql.push("AND t1.merchant_id = :merchant_id");
    }

    if (params.pay_status) {
      sql.push("AND t1.pay_status = :pay_status");
    }

    if (params.audit_status) {
      sql.push("AND t1.audit_status = :audit_status");
    }

    if (params.handle_status) {
      sql.push("AND t1.handle_status = :handle_status");
    }

    if (params.handle_statuses) {
      sql.push("AND t1.handle_status IN (:handle_statuses) ");
    }

    if (params.saas_merchant_id) {
      sql.push("AND t1.merchant_id = :saas_merchant_id");
    }

    if (params.createBegin) {
      sql.push("AND t1.created_at >= :createBegin");
    }

    if (params.createEnd) {
      sql.push("AND t1.created_at <= :createEnd");
    }
    if (params.merchant_app_user_id) {
      sql.push("AND t1.merchant_app_user_id = :merchant_app_user_id");
    }

    if (params.legal_idno) {
      sql.push("AND t2.legal_idno = :legal_idno");
    }
    if (params.saas_invoice_id) {
      sql.push("AND t1.saas_invoice_id = :saas_invoice_id");
    }
    if (params.unInvoice) {
      sql.push("AND t1.saas_invoice_id = ''");
    }
  }

  async mapByIds(ids, attrs) {
    let result = {};

    if (!ids || ids.length == 0) {
      return result;
    }
    let sql = [];
    sql.push("SELECT");
    sql.push(attrs || "*");
    sql.push("FROM");
    sql.push(this.model.tableName);
    sql.push("WHERE id IN (:ids)");

    var list = await this.customQuery(sql.join(" "), {
      ids: ids
    });
    if (!list || list.length == 0) {
      return result;
    }
    for (var item of list) {
      result[item.id] = item;
    }
    return result;
  }

  async byIds(params) {
    let attrs = params.attrs || "*";
    let ids = params.ids;
    if (!ids || ids.length == 0) {
      return [];
    }

    let sql = [];
    sql.push("SELECT");
    sql.push(attrs);
    sql.push("FROM");
    sql.push(this.model.tableName);
    sql.push("WHERE id IN (:ids) ");
    if (params.handle_status && params.handle_status.length > 0) {
      sql.push("AND handle_status IN (:handle_status)");
    }
    if (params.saas_merchant_id) {
      sql.push("AND merchant_id IN (:saas_merchant_id)");
    }
    if (params.unInvoice) {
      sql.push("AND saas_invoice_id = ''");
    }
    return await this.customQuery(sql.join(" "), params);
  }
  async updateInvoice(params, t) {
    let sql = "UPDATE saas_order SET saas_invoice_id = :saas_invoice_id WHERE id IN (:ids) ";
    await this.customUpdate(sql, params, t);
  }

}

module.exports = SaasOrderDao;