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

class BusinessmenService extends ServiceBase {
  constructor() {
    super();
  }

  async allPage(params) {
    let rs = await this.callms("order", "businessmenPage", params);
    if (rs.status != 0 || !rs.data || !rs.data.rows) {
      return rs;
    }
    this.transField(rs.data.rows);
    return rs;
  }

  /**
   * @params 个体户签约
   */
  async signing(params){
    try {
      return await this.callms("order", "signing", params);
    } catch (error) {
      console.log(error);
      return system.getResult(error);
    }
  }

  /**
   * @params 建仗
   */
  async createAccount(params){
    try {
      // 查个体户
      let businessmen = await this.callms("order", "queryObusinessmen", params);
      businessmen = businessmen.data;
      // 查个体户的交付商
      var deliver = await this.callms("common", "deliverInfo", {id: params.deliver_id});
      deliver = deliver.data;

      var deliver = await this.callms("common", "deliverInfo", {id: params.deliver_id});
      deliver = deliver.data;
      let data = {
        customerName: businessmen.name,
        initPeriod: Number(moment().format("YYYYMM")),
        // initPeriod:  moment().format("YYYYMM"),
        accountStandard: 1,
        valueAddedTax: 2,
        bankStatus: businessmen.is_bank ? 1 : 0,
        accountant:"管理员",
        province: params.province,
        companyId: deliver.nt_company_id,
      }
      let url = settings.ntapi().createCustomer;
      let res = await this.callApi(url, data, "建账");
      console.log(res);
      if(!res || !res.data) {
        return system.getResult(null, "建账失败");
      }
      if(res.data) {
        await this.callms("order", "createAccount", {
          id: params.id,
          province: params.province,
          customer_id: res.data,
        });
      }
      return system.getResultSuccess();
    } catch (error) {
      console.log(error);
      return system.getResult(error);
    }
  }

  transField(rows) {
    if (!rows) {
      return;
    }

    for (var row of rows) {
      row.costRate = system.f2y(row.costRate);
      row.taxRate = system.f2y(row.taxRate);
      row.serviceRate = system.f2y(row.serviceRate);
      row.serviceBeginTime = system.f2y(row.serviceRate);
      row.serviceRate = system.f2y(row.serviceRate);
      this.handleDate(row, ["service_begin_time", "service_end_time", "tax_reg_day", "reg_date", "sign_time"], "YYYY-MM-DD", -8);
      this.parseJsonField(row, ["common_tax_ladder", "common_other_ladder", "special_tax_ladder", "special_other_ladder"]);
    }
  }

  parseJsonField(row, fields) {
    if (!row || !fields || fields.length == 0) {
      return;
    }
    for (var f of fields) {
      if (!f) {
        continue;
      }
      if(!row[f]) {
        row[f] = [];
        continue;
      }
      try {
        row[f] = JSON.parse(row[f] || "[]");
      } catch (error) {
        console.log(error);
      }
    }
  }
}

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