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

    this.saasorderbminfoDao = system.getObject("db.saas.saasorderbminfoDao");
  }
  // -----------------------以此间隔，上面为API，下面为service---------------------------------

  async info(params) {
    if (!params.id) {
      return system.getResult(null, "id不存在");
    }

    let order = await this.dao.getById(params.id);
    if (!order) {
      return system.getResult(null, "订单不存在");
    }

    order.bminfo = await this.saasorderbminfoDao.getById(params.id);
    this.handleDate(order, ["created_at"], null, -8);
    return system.getResultSuccess(order);
  }

  async microAdd(params) {
    let saas_id = this.trim(params.saas_id);
    let merchant_id = this.trim(params.merchant_id);
    let product_id = this.trim(params.product_id);
    let merchant_app_user_id = this.trim(params.merchant_app_user_id);
    let price = Number(params.price || 0);
    let bminfo = params.bminfo;

    let order = {
      saas_id: saas_id,
      merchant_id: merchant_id,
      product_id: product_id,
      merchant_app_user_id: merchant_app_user_id,
      price: price,
      pay_status: 10,
      audit_status: 10,
    }

    let self = this;
    order = await this.db.transaction(async t => {
      //创建orderdeliver记录
      order = await self.dao.create(order, t);

      //更新oorder订单记录
      bminfo.id = order.id;
      await self.saasorderbminfoDao.create(bminfo, t);
      return order;
    });
    return system.getResultSuccess(order);
  }

  async pageByCondition(params) {
    let page = {
      count: 0,
      rows: []
    }
    params.currentPage = Number(params.currentPage || 1);
    params.pageSize = Number(params.pageSize || 10);

    params.startRow = (params.currentPage - 1) * params.pageSize;

    page.total = await this.dao.countByCondition(params);
    if(page.total == 0)  {
      return system.getResultSuccess(page);
    }

    page.rows = await this.dao.listByCondition(params);
    if (page.rows) {
      for (var row of page.rows) {
        this.handleDate(row, ["created_at"], null, -8);
      }

      await this.setBminfo(page.rows);
      await this.setOrderStatus(page.rows);
    }
    return system.getResultSuccess(page);
  }

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

    // 订单状态总结   1010 待付款, 2010 待审核, 2030 审核驳回, 3010 业务办理中, 3020待交付, 3030 已交付
    for (let row of rows) {
      let status = "";
      // 支付状态 10待支付 20已支付
      if(row.pay_status === "10") {
        row.status = "1010";
        row.status_name = "待付款";
        continue;
      }

      // 10待审核 20审核通过 30审核驳回
      if(row.audit_status != "20") {
        row.status = row.audit_status == "10" ? "2010": "2030";;
        row.status_name = row.audit_status == "10" ? "待审核": "审核驳回";
      }

      // 审核状态
      if(row.handle_status == "10") {
        row.status = "3010";
        row.status_name = "业务办理中";
      } else if (row.handle_status == "20") {
        row.status = "3010";
        row.status_name = "待交付";
      } else if (row.handle_status == "30") {
        row.status = "3020";
        row.status_name = "已交付";
      }
    }

  }

  async mapByIds(params) {
    if(!params.ids || params.ids.length == 0) {
      return system.getResultSuccess({});
    }
    let rs = await this.dao.mapByIds(params.ids);
    return system.getResultSuccess(rs);
  }

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

    let ids = [];
    for(let row of rows) {
      ids.push(row.id);
    }

    let map = await this.saasorderbminfoDao.mapByIds(ids) || {};
    for(let row of rows) {
      row.bminfo = map[row.id] || {};
    }
  }
}
module.exports = SaasOrderService;
