const system = require("../../../system");
const ServiceBase = require("../../sve.base")
const settings = require("../../../../config/settings")
class SaasMainService extends ServiceBase {
  constructor() {
    super("main", ServiceBase.getDaoName(SaasMainService));
  }
  // -----------------------以此间隔，上面为API，下面为service---------------------------------

  async dics(params) {
    let saas_id = Number(this.trim(params.saas_id) || 0);
    if (!saas_id) {
      return system.getResultSuccess([]);
    }
    let list = await this.dao.bySaasId(Number(params.saas_id), "id, name");
    return system.getResultSuccess(list);
  }

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

    let main = await this.dao.getById(params.id);
    if (!main) {
      return getResult(null, "签约主体不存在");
    }

    this.handleDate(main, ["created_at"], null, -8);
    return system.getResultSuccess(main);
  }

  async save(params) {
    let id = Number(params.id || 0);
    let saas_id = Number(params.saas_id || 0);
    let name = this.trim(params.name);
    let bank_account = this.trim(params.bank_account);
    let bank_name = this.trim(params.bank_name);
    let bank_no = this.trim(params.bank_no);
    let credit_code = this.trim(params.credit_code);
    let addr = this.trim(params.addr);
    let mobile = this.trim(params.mobile);
    let main;
    if (id) {
      main = await this.dao.findById(id);
      main.name = name;
      main.bank_account = bank_account;
      main.bank_name = bank_name;
      main.bank_no = bank_no;
      main.credit_code = credit_code;
      main.addr = addr;
      main.mobile = mobile;
      await main.save();
    } else {
      main = {
        name: name,
        bank_account: bank_account,
        bank_name: bank_name,
        bank_no: bank_no,
        is_enabled: true,
        saas_id: saas_id,
        mobile :mobile,
        addr : addr,
        credit_code :credit_code,
      };
      main = await this.dao.model.create(main);
    }
    return system.getResultSuccess(main);
  }

  async pageByCondition(params) {
    var currentPage = Number(params.currentPage || 0);
    var pageSize = Number(params.pageSize || 10);

    var where = {
      saas_id : params.saas_id,
    };

    if (params.name) {
      where.name = {
        [this.db.Op.like]: "%" + params.name + "%"
      };
    }

    // this.addWhereTime(where, 'created_at', params.createdBegin, params.createdEnd, true);

    var orderby = [
      ["id", 'desc']
    ];
    var 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);
      }
    }
    return system.getResultSuccess(page);
  }

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