const system = require("../../../system");
const ServiceBase = require("../../sve.base")

class StOrderService extends ServiceBase {
  constructor() {
    super("trade", ServiceBase.getDaoName(StOrderService));
    this.storderitemDao = system.getObject("db.trade.storderitemDao");
    this.dictionary = system.getObject("util.dictionary");
  }

  async test(params) {
    return system.getResultSuccess("test");
  }

  // 打款列表页
  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.count = await this.dao.countByCondition(params);
    if (page.count == 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);
      }
      this.dictionary.setRowsName("ORDER", page.rows, ["order_type", "acc_type", "trade_mode", "trade_status", "check_status"]);
    }
    return system.getResultSuccess(page);
  }

  // 保存打款信息
  async saveOrder(order) {
    if (!order) {
      return system.getResult(null, "订单不存在");
    }
    let itemList = order.itemList || [];

    if (itemList.length == 0) {
      return system.getResult(null, "订单明细不存在");
    }

    let self = this;
    order = await this.db.transaction(async t => {
      order = await self.dao.create(order, t);

      for (let item of itemList) {
        item.autoIncrement = true;
        item.order_id = order.id;
        item.saas_merchant_id = order.saas_merchant_id;
        item.out_trade_no = order.out_trade_no;
        item.saas_id = order.saas_id;
        item.trade_status = "01";
      }
      await self.storderitemDao.bulkCreate(itemList, t);
      await self.storderitemDao.genTradeNo(order.id, t);
      return order;
    });

    return system.getResultSuccess(order);
  }

  async pay(params) {
    let type = this.trim(params.type);
    let rs;
    if (type === "offline") {
      rs = await this.offlinePay(params);
    } else {
      return system.getResult(null, "暂不支持在线付款");
    }
    return rs;
  }

  async offlinePay(params) {
    let id = this.trim(params.id);
    let order = await this.dao.findById(id);
    if (!order) {
      return system.getResult(null, "批次不存在");
    }
    if (order.pay_voucher) {
      // 防止重复更新
      return system.getResultSuccess();
    }
    if (!params.pay_voucher) {
      return system.getResult(null, "请上传凭证");
    }
    order.pay_voucher = this.trim(params.pay_voucher);

    let pay_bank_account = this.trim(params.pay_bank_account);
    if (pay_bank_account) {
      order.pay_bank_account = pay_bank_account;
    }
    let pay_bank_name = this.trim(params.pay_bank_name);
    if (pay_bank_name) {
      order.pay_bank_name = pay_bank_name;
    }
    let pay_bank_no = this.trim(params.pay_bank_no);
    if (pay_bank_no) {
      order.pay_bank_no = pay_bank_no;
    }
    await order.save();
    return system.getResultSuccess();
  }


  // 详情

  // 保存打款信息

  // 付款申请提交

  // 一审

  // 二审

  // 打款

  // 结果反馈-批量

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

    let where = {};
    if (params.id) {
      where.id = this.trim(params.id);
    }
    if (params.name) {
      where.name = this.trim(params.name);
    }
    let orderby = [
      ["id", 'desc']
    ];

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

}

module.exports = StOrderService;
// var task=new UserService();
// task.getUserStatisticGroupByApp().then(function(result){
//   console.log((result));
// }).catch(function(e){
//   console.log(e);
// });