const system = require("../../../system");
const ServiceBase = require("../../svems.base")
const settings = require("../../../../config/settings")
class MerchantService extends ServiceBase {
  constructor() {
    super();
    this.SETTINGS_KEY = "merchant_settings_";
    this.INFO_KEY = "merchant_info_";
    this.channelSve = system.getObject("service.saas.channelSve");
    this.mainSve = system.getObject("service.saas.mainSve");
    this.redisClient = system.getObject("util.redisClient");
  }

  async mapByIds(params) {
    var rs = await this.callms("merchant", "mchtMapByIds", params || {});
    return rs;
  }

  async dics(params) {
    var rs = await this.callms("merchant", "mchtDics", params);
    return rs;
  }

  async info(params) {
    var rs = await this.callms("merchant", "mchtInfo", params);
    if(rs.data && rs.data.sign) {
      this.transSignFields([rs.data.sign]);
      await this.setChannel([rs.data]);
      await this.setMain(rs.data.rows);
    }
    return rs;
  }

  async save(params) {
    var rs = await this.callms("merchant", "mchtSave", params);
    return rs;
  }

  async page(params) {
    var rs = await this.callms("merchant", "mchtPage", params);
    if(rs.data && rs.data.rows) {
      this.transSignFields(rs.data.rows);
      await this.setChannel(rs.data.rows);
      await this.setMain(rs.data.rows);
    }
    return rs;
  }

  async sign(params) {
    var rs = await this.callms("merchant", "mchtSign", params);
    return rs;
  }

  async signInfo(params) {
    var rs = await this.callms("merchant", "mchtSignInfo", params);
    if(rs.data) {
      this.transSignFields([rs.data]);
      await this.setMain([rs.data]);
    }
    return rs;
  }

  async getMerchantWithCache(params) {
    let key = this.INFO_KEY + params.id;
    let merchant = await this.redisClient.get(key);
    if(!merchant || !merchant.id || params.forceUpdate) {
      let info = await this.info(params) || {};
      merchant = info.data || {};
      await this.redisClient.setWithEx(key, JSON.stringify(merchant), 60 * 5);
    } else {
      merchant = JSON.parse(merchant);
    }
    return merchant;
  }

  async getFeeTypeWithCache(params) {
    let merchant = await this.getMerchantWithCache({id: params.saas_merchant_id}) || {};
    let sign = merchant.sign || {};
    return sign.main_id_trade ? "10" : "20";
  }

  async getSettings(merchant_id) {
    if(!merchant_id) {
      return;
    }
    let key = this.SETTINGS_KEY + merchant_id;
    let settings = await this.redisClient.get(key);
    if(!settings) {
      let rs = await this.callms("merchant", "mchtSettings", {id: merchant_id}) || {};
      settings = rs.data;
      if(settings && settings.appid && settings.secret) {
        await this.redisClient.setWithEx(key, JSON.stringify(settings), 60 * 5);
      }
    } else {
      settings = JSON.parse(settings);
    }
    return settings;
  }


  async title(params) {
    let rs = await this.callms("merchant", "mchtTitles", params);
    let list = rs.data || [];
    let u = {};
    if(list.length > 0) {
      u = list[0];
    }
    return system.getResultSuccess(u);
  }

  async addr(params) {
    let rs = await this.callms("merchant", "mchtAddrs", params);
    let list = rs.data || [];
    let u = {};
    if(list.length > 0) {
      u = list[0];
    }
    return system.getResultSuccess(u);
  }

  async saveAddr(params) {
    let rs = await this.callms("merchant", "mchtAddrSave", params);
    return rs;
  }

  async setChannel(rows) {
    if(!rows) {
      return;
    }

    let channelIds = [];
    for(let row of rows) {
      if(row.channel_id) {
        channelIds.push(row.channel_id);
      }
    }
    if(channelIds.length == 0) {
      return;
    }
    let map = await this.channelSve.mapByIds({ids: channelIds}) || {};
    map = map.data || {};
    for(let row of rows) {
      row.channel = map[row.channel_id] || {};
    }
  }

  async setMain(rows) {
    if(!rows) {
      return;
    }

    let mainIds = [];
    for(let row of rows) {
      if(row.main_id) {
        mainIds.push(row.main_id);
      }
      if(row.main_id_trade) {
        mainIds.push(row.main_id_trade);
      }
      if(row.main_id_invoice) {
        mainIds.push(row.main_id_invoice);
      }
    }
    if(mainIds.length == 0) {
      return;
    }
    let map = await this.mainSve.mapByIds({ids: mainIds}) || {};
    map = map.data || {};
    for(let row of rows) {
      row.main = map[row.main_id] || {};
      row.main_trade = map[row.main_id_trade] || {};
      row.main_invoice = map[row.main_id_invoice] || {};
    }
  }

  transSignFields(rows) {
    if (!rows) {
      return;
    }
    for (var item of rows) {
      if (!item) {
        continue;
      }
      item.bm_reg_price = system.f2y(item.bm_reg_price);
      item.invoice_service_rate = system.f2y(item.invoice_service_rate);
      item.trans_service_rate = system.f2y(item.trans_service_rate);
    }
  }

  async setMerchant(rows) {
    if (!rows || rows.length == 0) {
      return;
    }

    let ids = [];
    for(let row of rows) {
      if(row.saas_merchant_id) {
        ids.push(row.saas_merchant_id);
      }
    }

    let map = await this.mapByIds({ids: ids, attrs: "id, name, short_name"}) || {};
    map = map.data || {};
    for(let row of rows) {
      row.merchant = map[row.saas_merchant_id] || {};
    }
  }

  async addConsumeLog(params) {
    let rs = await this.callms("merchant", "addConsumeLog", params);
    return rs;
  }
}
module.exports = MerchantService;
