const system=require("../../../system");
const ServiceBase=require("../../sve.base")
const settings=require("../../../../config/settings")
class MerchantaccountService extends ServiceBase{
  constructor(){
     super("merchant", ServiceBase.getDaoName(MerchantaccountService));

     this.merchantDao = system.getObject("db.merchant.merchantDao");
  }

  async apiPage(params) {
    try {
      return await this.page(params);
    } catch (error) {
      console.log(error);
      return system.getResult(null, "接口异常");
    }
  }

  async apiInfo(params) {
    try {
      return await this.info(params);
    } catch (error) {
      console.log(error);
      return system.getResult(null, "接口异常");
    }
  }

  async apiInfoByMerchantId(params) {
    try {
      return await this.infoByMerchantId(params);
    } catch (error) {
      console.log(error);
      return system.getResult(null, "接口异常");
    }
  }

  // -----------------------以此间隔，上面为API，下面为service---------------------------------

  async info(params) {
    var obj = await this.dao.getById(params.id);
    if (obj) {
      this.handleDate(obj, ["created_at", "updated_at"], null, -8);
      await this.setMerchantName([obj], "id, name,");
    }
    return system.getResultSuccess(obj);
  }

  async infoByMerchantId(params) {
    var obj = await this.dao.getByMerchantId(params.merchantId || params.merchant_id);
    if (obj) {
      this.handleDate(obj, ["created_at", "updated_at"], null, -8);
      await this.setMerchantName([obj], "id, name");
    }
    return system.getResultSuccess(obj);
  }

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

    var where = {};

    var merchantIds ;
    if (params.merchantName) {
      merchantIds = await this.merchantDao.idsLikeName(params.merchantName);
      if (!merchantIds || merchantIds.length == 0) {
        return {count : 0, rows: []};
      }
    }

    if(params.merchantId) {
      var mid = Number(params.merchantId);
      if(merchantIds) {
        if(merchantIds.indexOf(mid) == -1) {
          return {count : 0, rows: []};
        } else {
          merchantIds = [mid];
        }
      } else {
        merchantIds = [mid];
      }
    }

    if(merchantIds && merchantIds.length > 0) {
      where.merchant_id = {
        [this.db.Op.in]: merchantIds
      };
    }

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

    var attributes = ["id", "merchant_id", "banlance", "created_at"];

    var page = await this.getPageList(currentPage, pageSize, where, orderby, attributes);
    if (page && page.rows) {
      for (var row of page.rows) {
        this.handleDate(row, ["created_at"], null, -8);
      }
      await this.setMerchantName(page.rows, "id, name")
    }
    return system.getResultSuccess(page);
  }

  async setMerchantName(rows, attrs) {
    var map = await this.getMerchantMap(rows, attrs);
    for (var row of rows) {
      row.merchantName = (map[row.merchant_id] || {}).name || "";
    }
  }

  async setMerchant(rows, attrs) {
    var map = await this.getMerchantMap(rows, attrs);
    for(var row of rows) {
      row.merchant = map[row.merchant_id] || {};
    }
  }

  async getMerchantMap(rows, attrs) {
    if(!rows || rows.length == 0) {
      return {};
    }
    var ids = [];
    for(var row of rows) {
      ids.push(row.merchant_id);
    }
    return await this.merchantDao.findMapByIds(ids, attrs);
  }
}
module.exports=MerchantaccountService;
// var task=new UserService();
// task.getUserStatisticGroupByApp().then(function(result){
//   console.log((result));
// }).catch(function(e){
//   console.log(e);
// });
