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

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

     this.invoiceDao = system.getObject("db.invoice.invoiceDao");
  }


  async pageByCondition(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
      };
    }

    if(params.invoiceApplyNo) {
      var invoice = await this.invoiceDao.findOne({applyNo: params.invoiceApplyNo});
      if(!invoice) {
        return {count : 0, rows: []};
      }
      where.invoice_id = invoice.id;
    }

    if(params.type) {
      where.type = params.type;
    }

    if (params.busiType) {
      where.busiType = params.busiType;
    }

    // 创建时间
    this.addWhereTime(where, 'created_at', params.createdBegin, params.createdEnd, true);

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

    var attributes = ["id", "merchant_id", "invoice_id", "amtType", "amt", "frozen_amt", "available_amt", 
      "busiType", "complete", "created_at"];

    var page = await this.getPageList(currentPage, pageSize, where, orderby, attributes);
    if (page && page.rows) {
      var amtTypeMap = await this.dao.getAmtTypes();
      var busiTypeMap = await this.dao.getBusiTypes();

      var merchantMap = await this.getMerchantMap(page.rows, "id, name");
      var invoiceMap = await this.getInvoiceMap(page.rows, "id, applyNo");

      for (var row of page.rows) {
        row.amtTypeName = amtTypeMap[row.amtType];
        row.busiTypeName = busiTypeMap[row.busiType];

        row.merchantName = (merchantMap[row.merchant_id] || {}).name || "";
        row.invoiceApplyNo = (invoiceMap[row.invoice_id] || {}).applyNo || "";

        this.handleDate(row, ["created_at"], null, -8);
      }

      system.f2y4list(page.rows, ["amt", "frozen_amt", "available_amt"]);
    }
    return page;
  }

  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);
  }

  async getInvoiceMap(rows, attrs) {
    if(!rows || rows.length == 0) {
      return {};
    }

    var ids = [];
    for(var row of rows) {
      ids.push(row.invoice_id);
    }
    return await this.invoiceDao.findMapByIds(ids, attrs);
  }
}
module.exports=MerchanttradeprocessService;
// var task=new UserService();
// task.getUserStatisticGroupByApp().then(function(result){
//   console.log((result));
// }).catch(function(e){
//   console.log(e);
// });
