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

class DeliverSchemeService extends ServiceBase {
  constructor() {
    super("deliver", ServiceBase.getDaoName(DeliverSchemeService));
  }

  async dics(params) {
    let list = await this.dao.dics({deliver_id: params.deliver_id, attrs: params.attrs});
    return system.getResultSuccess(list);
  }

  async save(params) {
    let id = params.id;
    let scheme;
    if (id) {
      scheme = await this.dao.findById(id);
    } else {
      scheme = {};
    }
    scheme.name = this.trim(params.name);
    if (params.common_tax_ladder) {
      scheme.common_tax_ladder = JSON.stringify(params.common_tax_ladder || "");
    }
    if (params.common_other_ladder) {
      scheme.common_other_ladder = JSON.stringify(params.common_other_ladder || "");
    }
    if (params.special_tax_ladder) {
      scheme.special_tax_ladder = JSON.stringify(params.special_tax_ladder || "");
    }
    if(params.special_other_ladder) {
      scheme.special_other_ladder = JSON.stringify(params.special_other_ladder || "");
    }
    scheme.cost_rate = system.y2f(params.cost_rate || 0);
    scheme.tax_rate = system.y2f(params.tax_rate || 0);
    scheme.add_value_up_type = this.trim(params.add_value_up_type);
    scheme.tax_up_type = this.trim(params.tax_up_type);

    if (scheme.id) {
      scheme = await scheme.save();
    } else {
      scheme.deliver_id = this.trim(params.deliver_id);
      scheme = await this.dao.create(scheme);
    }
    return system.getResultSuccess(scheme);
  }

  async info(params) {
    let item = await this.dao.getById(params.id);
    this.handleDate(item, ["created_at"], null);
    this.transField([item]);
    return system.getResultSuccess(item);
  }

  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 (let row of page.rows) {
        this.handleDate(row, ["created_at"], null);
      }
      this.transField(page.rows);
    }
    return system.getResultSuccess(page);
  }

  transField(rows) {
    if (!rows || rows.length == 0) {
      return;
    }
    for (let row of rows) {
      if (!row) {
        continue;
      }
      try {
        row.common_tax_ladder = JSON.parse(row.common_tax_ladder ?  row.common_tax_ladder : null);
        row.common_other_ladder = JSON.parse(row.common_other_ladder ? row.common_other_ladder : null);
        row.special_tax_ladder = JSON.parse(row.special_tax_ladder ? row.special_tax_ladder : null);
        row.special_other_ladder = JSON.parse(row.special_other_ladder ? row.special_other_ladder : null);
      } catch (e) {
        console.log(e);
      }
      row.cost_rate = system.f2y(row.cost_rate);
      row.tax_rate = system.f2y(row.tax_rate);
    }
  }
}

module.exports = DeliverSchemeService;
