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


  async updDefaultByMerchantId(merchantId, isDefault, t) {
    var sql = [];
    sql.push("UPDATE");
    sql.push("m_merchant_address");
    sql.push("SET");
    sql.push("isDefault = :isDefault ");
    sql.push("WHERE merchant_id = :merchantId");
    return await this.customUpdate(sql.join(" "), {
      merchantId: merchantId,
      isDefault: isDefault
    }, t);
  }


  async updDefaultById(id, isDefault, t) {
    var sql = [];
    sql.push("UPDATE");
    sql.push("m_merchant_address");
    sql.push("SET");
    sql.push("isDefault = :isDefault ");
    sql.push("WHERE id = :id");
    return await this.customUpdate(sql.join(" "), {
      id: id,
      isDefault: isDefault
    }, t);
  }

  async defaultByMerchantId(merchantId, t) {
    var sql = "SELECT * FROM m_merchant_address WHERE merchant_id = :merchantId AND isDefault = 1";
    var list = await this.customQuery(sql, {
      merchantId: merchantId
    }, t) || [null];

    return list[0];
  }

  async listByMerchantId(merchantId, t) {
    var sql = "SELECT * FROM m_merchant_address WHERE merchant_id = :merchantId";
    return await this.customQuery(sql, {
      merchantId: merchantId
    }, t) || [];
  }

  async getDefaultByMerchantId(merchantId, t) {
    var sql = "SELECT * FROM m_merchant_address WHERE merchant_id = :merchantId AND isDefault = 1";
    var list = await this.customQuery(sql, {
      merchantId: merchantId
    }, t) || [];
    return list && list.length > 0 ? list[0] : null;
  }
}
module.exports = MerchantaddressDao;
// 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);
//   }
//
// })();