const system = require("../../../system");
const ServiceBase = require("../../sve.base")
const settings = require("../../../../config/settings")
class MerchantaddressService extends ServiceBase {
  constructor() {
    super("merchant", ServiceBase.getDaoName(MerchantaddressService));
    this.merchantDao = system.getObject("db.merchant.merchantDao");
  }

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

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

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

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

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

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

  async saveAddress(params) {
    var id = params.id;
    var merchantId = params.merchantId;
    var address;
    if (id) {
      address = await this.findById(id);
    } else {
      address = {
        merchant_id: merchantId
      };
    }
    address.mailAddr = this.trim(params.mailAddr);
    address.mailTo = this.trim(params.mailTo);
    address.mailMobile = this.trim(params.mailMobile);

    if (id) {
      address = await address.save();
    } else {
      address = await this.create(address);
    }

    if (params.isDefault) {
      await this.dao.updDefaultByMerchantId(address.merchant_id, false);
      await this.dao.updDefaultById(address.id, true);
    }

    return system.getResultSuccess("");
  }


  async getDefault(params) {
    var merchantId = params.merchantId || params.merchant_id;
    var obj = await this.dao.getDefaultByMerchantId(merchantId);
    return system.getResultSuccess(obj);
  }
  async setDefault(id) {
    var address = await this.dao.findById(id);
    if (!address) {
      return system.getResult(null, "地址不存在");
    }

    await this.dao.updDefaultByMerchantId(address.merchant_id, false);
    await this.dao.updDefaultById(address.id, true);
    return system.getResultSuccess();
  }

  async addressPage(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 = [
      ["isDefault", 'desc'],
      ["id", 'desc']
    ];

    var attributes = ["id", "merchant_id", "mailAddr", "mailTo", "mailMobile", "isDefault", "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);
    }
    return system.getResultSuccess(page);
  }

  async addressInfo(params) {
    var addr = await this.dao.getById(params.id);
    if(addr) {
      this.handleDate(addr, ["created_at", "updated_at"], null, -8);
    }
    return addr;
  }

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