const system = require("../../../system");
const ServiceBase = require("../../sve.base")
const settings = require("../../../../config/settings")
class UserService extends ServiceBase {
  constructor() {
    super("user", ServiceBase.getDaoName(UserService));
    this.user
  }


  /**
   * 条件查询
   * @param {*} params 
   */
  async apiFindUserBySaasId(params){
    try {
      return await this.findUserBySaasId(params);
    } catch (error) {
      return system.getResult(-1,`系统错误 错误信息 ${error}`);
    }
  }




/**************************************************************** */

  /**
   * 条件查询
   * @param {*} params 
   */
  async findUserBySaasId(params){
    try { 
      let res = await this.dao.findUserBySaasId(params);
      return system.getResult(res);
    } catch (error) {
      return system.getResult(-1,`系统错误 错误信息 ${error}`);
    }
  }

  async add(obj) {
    var roles = obj.roles || [];
    var org = obj.org;
    var saas_id = Number(obj.saas_id || 0);
    var ucname = this.trim(obj.ucname);
    var password = this.trim(obj.password);
    var uctype = this.trim(obj.uctype);
    var mobile = this.trim(obj.mobile);
    var realName = this.trim(obj.realName);

    if(!saas_id) {
      // TODO 写到这
    }

    var user = {
      ucid: ucid,
      ucname: ucname,
      password: password,
      uctype: uctype,
      org_id: org.id,
      isMain: obj.isMain || 0,
      orgpath: "",
      password: "",
      isEnabled: 1,
    }

    var info = {
      mobile: mobile,
      realName: realName,

    }

    var self = this;
    await self.db.transaction(async function (t) {
      // 创建商户
      user = await self.dao.create(user, t);

      // 商户id赋值
      title.merchant_id = merchant.id;
      address.merchant_id = merchant.id;

      // 创建账户
      var account = await self.merchantaccountDao.create({
        merchant_id: merchant.id,
        channel_id: "1",
        banlance: 0
      }, t);

      // 创建抬头
      title = await self.merchanttitleDao.create(title, t);

      // 创建收件地址
      address = await self.merchantaddressDao.create(address, t);

      merchant.accountList = [account];
      merchant.titleList = [title];
      merchant.addressList = [merchant.addressList];
      return merchant;
    });
    
  }
}
module.exports = UserService;
