'use strict';
const ManagerController = require('../manager.js');
/**
 * [exports 用户控制器]
 * @type {[type]}
 */
module.exports = class UsersController extends ManagerController {
  /**
   * [useModel 使用模型]
   * @return {[type]} [description]
   */
  get useModel() {
    const that = this;
    return that.app.model.Users;
  }

  /**
   * [selectValidate 查询用户列表]
   * @return {[type]} [description]
   */
  get selectValidate() {
    const that = this;
    return {
      page: that.ctx.rules.default(1)
        .number(),
      limit: that.ctx.rules.default(20)
        .number(),
      uname: that.ctx.rules.default('')
        .required(),
    };
  }

  /**
   * [updateValidate 用户充值验证器]
   * @return {[type]} [description]
   */
  get updateValidate() {
    const that = this;
    return {
      user_id: that.ctx.rules.name('用户ID')
        .required()
        .notEmpty()
        .number(),
      money: that.ctx.rules.name('充值金额')
        .required()
        .notEmpty()
        .number(),
      admin_id: that.ctx.rules.default(that.service.manager.ActionAdminUserId())
        .number(),
    };
  }

  /**
   * [moneyLogsValidate description]
   * @return {[type]} [description]
   */
  get moneyLogsValidate() {
    const that = this;
    return {
      user_id: that.ctx.rules.name('用户ID')
        .required()
        .notEmpty()
        .number(),
      page: that.ctx.rules.default(1)
        .number(),
      limit: that.ctx.rules.default(20)
        .number(),
    };
  }

  /**
   * [selectOptions description]
   * @param  {Object} data [description]
   * @return {[type]}      [description]
   */
  async selectOptions(data = {}) {
    const that = this;
    const seq = that.app.Sequelize;
    const options = {
      offset: (data.page - 1) * data.limit, limit: data.limit, where: {},
      attributes: { exclude: [ 'password' ] }, order: [[ 'user_id', 'desc' ]],
    };
    if (data.uname) {
      options.where = {
        [seq.Op.or]: [
          { nickname: { [seq.Op.regexp]: data.uname } },
          { account_name: { [seq.Op.regexp]: data.uname } },
        ],
      };
    }
    return options;
  }

  /**
   * [create 给用户充值]
   * @return {[type]} [description]
   */
  async update() {
    const that = this;
    let transaction;
    try {
      const data = await that.ctx.validate(that.updateValidate, await that.ctx.anyParse());
      // console.log(data);// { money: 168, user_id: 26, admin_id: 19 }
      transaction = await that.app.model.transaction();
      const result = await that.service.shop.userMoneyAdd(data.user_id, data.money, transaction, '管理员给用户充值', data.admin_id);
      await transaction.commit();
      return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
    } catch (err) {
      if (transaction) transaction.rollback();
      return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
    }
  }

  /**
   * [moneyLogs 获取用户资金明细记录]
   * @return {[type]} [description]
   */
  async moneyLogs() {
    const that = this;
    try {
      const data = await that.ctx.validate(that.moneyLogsValidate, await that.ctx.getParse());
      const seq = that.app.Sequelize;
      const options = {
        offset: (data.page - 1) * data.limit, limit: data.limit, where: {
          user_id: data.user_id,
        }, include: [
          { model: that.app.model.AdminUser, as: 'admin_user', attributes: [], required: false },
        ], attributes: {
          include: [[ seq.col('admin_user.username'), 'username' ]],
        },
      };
      const selectBean = await that.app.comoBean.instance(data, options);
      const result = await that.service.base.select(selectBean, that.app.model.UsersMoneyLogs, '查询资金明细失败,请重试', true, true);
      return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
    } catch (err) {
      return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
    }
  }

};