const system = require("../../../system");
const ServiceBase = require("../../sve.base");
const settings = require("../../../../config/settings");
const axios = require("axios");

class ChannelmerchantService extends ServiceBase {
  constructor() {
    super("channel", ServiceBase.getDaoName(ChannelmerchantService));
    this.originDao = system.getObject("db.channel.originDao");
    this.channelDao = system.getObject("db.channel.channelDao");
    this.platformchannelDao = system.getObject("db.channel.platformchannelDao");
  }

  async info(params) {
    let channel = await this.dao.getById(params.id);
    await this.setChannelInfo([channel]);
    return system.getResultSuccess(channel);
  }


  async save(params) {
    let mcht;
    if(params.id) {
      mcht = await this.dao.findById(params.id);
    } else {
      mcht = {};
    }

    let origin = await this.originDao.findById(params.origin_id);
    if(!origin) {
      return system.getResult(null, "商户来源不存在");
    }

    mcht.origin_merchant_name = this.trim(params.origin_merchant_name);
    mcht.merchant_name = this.trim(params.merchant_name);
    mcht.contact_man = this.trim(params.contact_man);
    mcht.contact_mobile = this.trim(params.contact_mobile);
    mcht.contact_email = this.trim(params.contact_email);
    mcht.contact_addr = this.trim(params.contact_addr);

    if(mcht.id) {
      await mcht.save();
    } else {
      mcht.channel_id = Number(params.channel_id);
      mcht.origin_id = Number(params.origin_id);
      if(origin.is_api && !params.origin_merchant_id) {
        return system.getResult(null, `来源${origin.name}下, 请选择商户`);
      }
      mcht.origin_merchant_id = params.origin_merchant_id;
      mcht = await this.dao.create(mcht);
    }
    return system.getResultSuccess(mcht);
  }


  async merchantSaves(params) {
    let mcht;
    if(params.id) {
      mcht = await this.dao.findById(params.id);
    } else {
      mcht = {};
    }

    let origin = await this.originDao.findById(params.origin_id);
    if(!origin) {
      return system.getResult(null, "商户来源不存在");
    }

    mcht.origin_merchant_name = this.trim(params.origin_merchant_name);
    mcht.merchant_name = this.trim(params.merchant_name);
    mcht.contact_man = this.trim(params.contact_man);
    mcht.contact_mobile = this.trim(params.contact_mobile);
    mcht.contact_email = this.trim(params.contact_email);
    mcht.contact_addr = this.trim(params.contact_addr);

    if(mcht.id) {
      await mcht.save();
    } else {
      mcht.channel_id = Number(params.channel_id);
      mcht.origin_id = 10000;
      mcht.origin_merchant_id = params.origin_merchant_id;
      mcht = await this.dao.create(mcht);
    }
    return system.getResultSuccess(mcht);
  }

  async page(params) {
    let currentPage = Number(params.currentPage || 1);
    let pageSize = Number(params.pageSize || 10);

    let where = {};
    if(params.channel_id) {
      where.channel_id = Number(params.channel_id);
    }

    if(params.origin_id) {
      where.origin_id = Number(params.origin_id);
    }

    if (params.merchant_name) {
      where.merchant_name = {
        [this.db.Op.like]: "%" + params.merchant_name + "%"
      };
    }

    if(params.types == 1){
        where.origin_merchant_id = "";
        where.origin_merchant_name = "";
    }

    let orderby = [
      ["id", 'desc']
    ];

    let page = await this.getPageList(currentPage, pageSize, where, orderby, null);
    if (page && page.rows) {
      for (var row of page.rows) {
        this.handleDate(row, ["created_at"], null, -8);
      }

      if(!params.types && params.types == 0 ){
        await this.setpage(page)
      }
      await this.setChannelInfo(page.rows);

    }
    return system.getResultSuccess(page);
  }


  async setpage(page){
    var rows = page.rows;
    if (!rows || rows.length == 0) {
      return;
    }
    var row = [];
    for (var i=0;i<rows.length; i++){
      if(rows[i].origin_merchant_id != "" ){
        row.push(rows[i]);
      }
    }
    page.rows = row;
    page.count = row.length;
  }

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

    var ids = [];
    for (var row of rows) {
      ids.push(row.channel_id);
    }

    var map = await this.channelDao.findMapByIds(ids);
    for (var row of rows) {
      let obj = map[row.channel_id] || {};
      row.channel_name = obj.name || "";
      row.channel_idcode = obj.idcode || "";
    }
  }

  async getPushMerchantIds(params) {
    let rs = [];
    let allChannelIds = await this.platformchannelDao.allChannelIds() || [];

    if(allChannelIds.length == 0) {
      return rs;
    }

    return await this.dao.findIdsByChannelIds(allChannelIds) || [];
  }
}

module.exports = ChannelmerchantService;
