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;
  }
}
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);
//   }
//
// })();