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

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

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

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

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

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

  async saveSign(params) {
    var merchantId = params.merchantId;
    var sign = await this.dao.findOne({merchant_id: merchantId});
    var merchant = await this.merchantDao.findById(merchantId);
    if(!sign) {
      sign = {};
      sign.merchant_id = merchantId;
    }
    sign.nameA = params.nameA;
    sign.contractNo = params.contractNo;
    sign.beginDate = params.beginDate;
    sign.endDate = params.endDate;
    sign.serviceRate = params.serviceRate;

    if(sign.id) {
      await sign.save();
    } else {
      sign = await this.create(sign);
    }
    merchant.status = "90";
    await merchant.save();
    return system.getResultSuccess();
  }

  async getByMerchantId(merchantId, attrs) {
    var attrs = attrs || ["id", "merchant_id", "nameA", "contractNo", "beginDate", "endDate", "serviceRate", "created_at"];

    var sign = await this.dao.model.findOne({
      where: { merchant_id: merchantId }, attrs, raw: true
    });
    if(sign) {
      this.handleDate(sign, ["beginDate",  "endDate"], "YYYY-MM-DD");
      this.handleDate(sign, ["created_at"], null, -8);
      await this.setMerchantName([sign]);
    }
    return system.getResultSuccess(sign);
  }

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

    var where = {};

    if(params.merchantId > 0) {
      where.merchant_id = params.merchantId;
    }

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

    var attributes = ["id", "merchant_id", "nameA", "contractNo", "beginDate", "endDate", "serviceRate", "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, ["beginDate",  "endDate"], "YYYY-MM-DD");
        this.handleDate(row, ["created_at"], null, -8);
      }
      await this.setMerchantName(page.rows);
    }
    return system.getResultSuccess(page);
  }

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

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

  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=MerchantsignedService;
// var task=new UserService();
// task.getUserStatisticGroupByApp().then(function(result){
//   console.log((result));
// }).catch(function(e){
//   console.log(e);
// });
