const system = require("../../../system");
const Dao = require("../../dao.base");
class MerchantpushDao extends Dao {
  constructor() {
    super(Dao.getModelName(MerchantpushDao));
  }
  /**
   * 获取推送地址
   * @param {*} merchant_id 商户id
   */
  async getItemByMerchantId(merchant_id) {
    return this.model.findOne({
      where: {
        merchant_id: merchant_id
      },
      raw: true
    });
  }

  async pushInfo(merchantId, field, params) {
    var pushConfig = this.dao.findOne({
      merchant_id: merchantId
    }) || {};
    var url = pushConfig[field];
    if(!url) {
      return;
    }

    var rs = await this.restClient.execPost(params, url);
    var success = false;
    if (rs.stdout) {
      let rsData = JSON.parse(rs.stdout);
      if (rsData.code == "0" || rsData.code == "0000") {
        success = true;
      }
    }
    await this.merchantpushlogDao.create({
      merchant_id: merchantId,
      api: url,
      params: params,
      rs: rs,
      success: success
    });
  }
}
module.exports = MerchantpushDao;
// 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);
//   }
//
// })();