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

  async getList(params) {
    let attrs = params.attrs || "*"
    var sql = [];
    sql.push("SELECT ");
    sql.push(attrs);
    sql.push("FROM d_deliver WHERE  1 = 1 ");
    if (params.isEnabled == 1) {
      sql.push("AND isEnabled = 1");
    }
    if (params.saas_id) {
      sql.push("AND saas_id = :saas_id");
    }
    return await this.customQuery(sql.join(" "),params) || [];
  }

  async delById(id, t) {
    var sql = "DELETE FROM d_deliver WHERE id = :id";
    return await this.customUpdate(sql, {
      id: id
    }, t);
  }

  async findListByIds(ids) {
    if (!ids || ids.length == 0) {
      return [];
    }
    var sql = [];
    sql.push("SELECT");
    sql.push("id, name, businessmenDivide, invoiceDivide, isEnabled, created_at ");
    sql.push("FROM d_deliver");
    sql.push("WHERE id IN (:ids)");
    return await this.customQuery(sql.join(" "), {
      ids: ids
    }) || [];
  }

  async findMapByIds(ids) {
    var result = {};
    if (!ids || ids.length == 0) {
      return result;
    }
    var list = await this.findListByIds(ids);
    if (!list || list.length == 0) {
      return result;
    }
    for (var item of list) {
      result[item.id] = item;
    }
    return result;
  }

  async countChannelDeliver(params) {
    let sql = [];
    sql.push("SELECT");
    sql.push("COUNT(DISTINCT t1.`id`) AS num");
    sql.push("FROM `d_deliver_channel` t2");
    sql.push("INNER JOIN `d_deliver` t1 ON t2.`deliver_id` = t1.`id`");
    sql.push("WHERE 1 = 1");

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

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

    let sql = [];
    sql.push("SELECT");
    sql.push("t1.*, t2.`channel_id`");
    sql.push("FROM `d_deliver_channel` t2");
    sql.push("INNER JOIN `d_deliver` t1 ON t2.`deliver_id` = t1.`id`");
    sql.push("WHERE 1 = 1");
    this.setChannelDeliverCondition(sql, params);
    sql.push("GROUP BY t1.id");
    sql.push("ORDER BY t1.created_at DESC");
    if (params.pageSize > 0) {
      sql.push("LIMIT :startRow, :pageSize");
    }
    return await this.customQuery(sql.join(" "), params);
  }

  setChannelDeliverCondition(sql, params) {
    if (!params || !sql) {
      return;
    }
    if (params.saasId) {
      sql.push("AND t1.saas_id = :saasId");
    }
    if (params.id) {
      sql.push("AND t1.id = :id");
    }
    if (params.deliver_id) {
      sql.push("AND t1.id = :deliver_id");
    }
    if (params.name) {
      params.nameLike = `%${params.name}%`;
      sql.push("AND t1.name LIKE :nameLike");
    }
    if (params.channel_id) {
      sql.push("AND t2.channel_id = :channel_id");
    }
  }
}
module.exports = DeliverDao;
// var u=new UserDao();
// var roledao=system.getObject("db.roleDao");
// (async ()=>{
//   var users=await u.model.findAll({where:{app_id:1}});
//   var role=await roledao.model.findOne({where:{code:"guest"}});
//   console.log(role);
//   for(var i=0;i<users.length;i++){
//     await users[i].setRoles([role]);
//     console.log(i);
//   }
//
// })();