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

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

  async login(params) {
    var rs = await this.callms("common", "deliverLogin", params);
    return rs;
  }

  async all(params) {
    var rs = await this.callms("common", "deliverAll", {});
    await this.doPercent(rs.data);
    return rs;
  }

  async page(params) {
    var rs = await this.callms("common", "deliverPage", params);
    await this.doPercent(rs.data.rows);
    return rs;
  }

  async info(params) {
    var rs = await this.callms("common", "deliverInfo", params);
    await this.doPercent([rs.data]);
    return rs;
  }

  async mayByIds(ids) {
    var rs = await this.callms("common", "deliverUserMap", {ids: ids});
    await this.doPercent([rs.data]);
    return rs;
  }

  async save(params) {
    params.invoiceDivide = system.y2f(params.invoiceDivide);
    params.businessmenDivide = system.y2f(params.businessmenDivide);
    var rs = await this.callms("common", "deliverSave", params);
    await this.doPercent([rs.data]);
    return rs;
  }

  async del(params) {
    return await this.callms("common", "deliverDelete", params);
  }

  async deliverUserPage(params) {
    return await this.callms("common", "deliverUserPage", params);
  }
  async deliverUserById(params) {
    return await this.callms("common", "deliverUserById", params);
  }
  async deliverUserSave(params) {
    let rs = await this.callms("common", "deliverUserSave", params);
    if(rs.status === 0) {
      this.synSave(rs.data.id, params.password);
    }
    return rs;
  } 

  async synSave(id, password) {
    let rs = await this.callms("common", "deliverUserInfo", {id: id});
    if(!rs || !rs.data || !rs.data.user || !rs.data.deliver) {
      return;
    }

    let user = rs.data.user;
    let deliver = rs.data.deliver;
    // 是否已经推送过
    // if(user.nt_user_id) {
    //   return;
    // }

    // 判断是否有财务宝和税务报
    let cwb = Number(user.auth[1] || 0);
    let swb = Number(user.auth[2] || 0);
    if(!cwb && !swb) {
      return;
    }

    let data = {
      companyNo: deliver.id,
      companyName: deliver.name,
      path: user.org_path,
      staffNo: user.id,
      staffName: user.real_name,
      loginId: user.ucname,
      pwd: password || "",
    };

    if(user.isAdmin) {
      data.classType = "leader";
      data.className = "主管";
    } else {
      data.classType = "member";
      data.className = " 普通员工";
    }
    let url = settings.ntapi().synUserDetails;
    let res = await this.callApi(url, data, "创建交付商用户");
    if(res && res.data && res.data.datas) {
      let _d = res.data.datas;
      await this.callms("common", "deliverSynUpdate", {user_id: id, nt_user_id: _d.id, nt_company_id: _d.companyId});
    }
  }

  async allOrg(params) {
    return await this.callms("common", "allOrg", params);
  }
  async orgTree(params) {
    return await this.callms("common", "orgTree", params);
  }
  async orgById(params) {
    return await this.callms("common", "orgById", params);
  }
  async orgSave(params) {
    return await this.callms("common", "orgSave", params);
  }

  async doPercent(rows) {
    if(!rows) {
      return;
    }
    for(var row of rows) {
      if(!row) {
        continue;
      }
      row.invoiceDivide = system.f2y(row.invoiceDivide);
      row.businessmenDivide = system.f2y(row.businessmenDivide);
    }
  }
}
module.exports = DeliverService;