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

  async updDefaultByMerchantId(merchantId, isDefault, t) {
    var sql = [];
    // UPDATE m_merchant_title SET banlance = banlance - :amt, frozen_amt = frozen_amt + :amt WHERE id = 1 AND banlance - :amt >= 0
    sql.push("UPDATE");
    sql.push("m_merchant_title");
    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_title");
    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_title WHERE merchant_id = :merchantId AND isDefault = 1";
    var list = await this.customQuery(sql, {
      merchantId: merchantId
    }, t) || [];

    return list[0];
  }

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

  async getDefaultByMerchantId(merchantId, t) {
    var sql = "SELECT * FROM m_merchant_title 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 = MerchanttitleDao;
// 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);
//   }
//
// })();