const system = require("../../../system");
const Dao = require("../../dao.base");
class SaasOrderDeliverInfoDao extends Dao {
  constructor() {
    super(Dao.getModelName(SaasOrderDeliverInfoDao));
  }

  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_deliver_info` 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);

    let attrs = "t2.*";
    if (params.attrs && params.attrs.length > 0) {
      attrs = "t2." + params.attrs.join(", t2.");
    }

    var sql = [];
    sql.push("SELECT");
    sql.push(attrs);
    sql.push("FROM");
    sql.push("saas_order t1");
    sql.push("INNER JOIN `saas_order_deliver_info` 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.isBusinessmenCreated) {
      sql.push("AND t2.sve_businessmen_customer_id <> ''");
    }
    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.saas_merchant_id) {
      sql.push("AND t1.merchant_id = :saas_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.createBegin) {
      sql.push("AND t2.created_at >= :createBegin");
    }

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

    if (params.sve_businessmen_name) {
      params.sve_businessmen_name_like = `%${params.sve_businessmen_name}%`;
      sql.push("AND t2.sve_businessmen_name LIKE :sve_businessmen_name_like");
    }
    if (params.sve_businessmen_credit_code) {
      sql.push("AND t2.sve_businessmen_credit_code = :sve_businessmen_credit_code");
    }
  }

  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 mapByCreditCodes(creditCodes, attrs) {
    let result = {};
    if (!creditCodes || creditCodes.length == 0) {
      return result;
    }
    let sql = [];
    sql.push("SELECT");
    sql.push(attrs || "*");
    sql.push("FROM");
    sql.push(this.model.tableName);
    sql.push("WHERE sve_businessmen_credit_code IN (:creditCodes)");
    let list = await this.customQuery(sql.join(" "), {creditCodes: creditCodes}) || [];
    if (list.length == 0) {
      return result;
    }
    for (var item of list) {
      result[item.sve_businessmen_credit_code] = item;
    }
    return result;
  }

}
module.exports = SaasOrderDeliverInfoDao;