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

  async nameList() {
    var sql = "SELECT id, name FROM xgg_merchant";
    return await this.customQuery(sql);
  }
/**
 * 获取商户信息
 * @param {*} app_id appid
 */
  async getItemByAppId(app_id) {
    return this.model.findOne({
      where: {
        id: app_id
      },
      attributes: ["id",
        "name",//商户名称                                          
        "taxType",//纳税人类型 0一般纳税人 1小规模纳税人  
        "contactName",//联系人姓名                                       
        "contactMobile",//联系人手机                                       
        "contactEmail",//联系人邮箱                                       
        "taxNo",//纳税人识别号                                    
        "invoiceAddr",//开票地址                                          
        "invoiceMobile",//开票电话                                          
        "accountName",//开户名称                                          
        "accountPublic",//对公账户                                          
        "mailAddr",//邮寄地址                                          
        "mailTo",//收件人                                             
        "mailMobile",//收件电话                                          
        "ownerId",//客户经理id                                        
        "ownerPath",//客户经理组织机构路径                        
        "status",//订单状态 0待审核 1待签约 2签约完成      
        "ucname",//登录账号                                          
        "ucid",//登录账号用户id                                  
        "appSecret"],
      raw: true
    });
  }

  async idsLikeName(name) {
    var result = [];
    if(!name || !name.trim()) {
      return result;
    }

    var sql = "SELECT id FROM xgg_merchant WHERE name LIKE :name ";
    var list = await this.customQuery(sql, {name : "%" + name + "%"});
    
    if(list) {
      for(var item of list) {
        result.push(item.id);
      }
    }
    return result;
  }

  async findMapByIds(ids, attrs) {
    var result = {};
    if(!ids || ids.length == 0) {
      return result;
    }

    attrs = attrs || "*";
    var sql = "SELECT " + attrs + " FROM xgg_merchant where id IN (:ids) ";
    var list = await this.customQuery(sql, {ids:ids});
    if(!list || list.length == 0) {
      return result;
    }

    for(var item of list) {
      result[item.id] = item;
    }

    return result;
  }
}
module.exports = MerchantDao;
    // 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);
    //   }
    //
    // })();
