coupon511.js 3.41 KB
const _ = require('lodash');
const aes = require('../../../utils/aes');
const mySqlCli = global.yoho.utils.mysqlCli;
const UfoApi = global.yoho.UfoAPI;
const TABLE_PRIZE = 'act_prize_user_log';
const TABLE_USER_DETAIL = 'act_user_detail_info';
const logger = global.yoho.logger;
const mysqlCli = global.yoho.utils.mysqlCli;

const coupons = ['57008016-f16a-4814-854b-2ed8c8cab3b5', '418b5d9b-6574-4d62-8457-d74da6ec94e2'];

class Coupon511 extends global.yoho.BaseModel {
  constructor(ctx) {
    super(ctx);
    this.redis = global.yoho.redis;
    this.client = this.redis.client;
  }
  _getCoupon(uid, inx) {
    const token = coupons[inx - 1];

    return this.get({
      url: 'coupon',
      data: {
        method: 'ufo.coupons.send',
        uid: uid,
        coupon_tokens: token
      },
      api: UfoApi
    });
  }
  concatPhone(uid) {
    let phone = '';
    let appStrLen = 11 - uid.toString().length;

    for (let i = 0; i < appStrLen; i++) {
        phone += '0';
    }
    phone += uid.toString();
    return phone;
  }
  _saveUserInfo(uid) {
    let actId = 9999, name = '000', birthday = 0, gender = '', email = 'u@yoho.cn', province = '', city = '';
    let phone = this.concatPhone(uid);
    
    let strFind = `SELECT COUNT(*) AS user_count 
                    FROM ${TABLE_USER_DETAIL} 
                    WHERE act_id=:actId 
                    AND user_phone=:phone`;

    let strInsert = `INSERT INTO ${TABLE_USER_DETAIL} 
                    (act_id, user_name, user_birthday, user_gender, 
                    user_phone, user_email, user_province, user_city) 
                    VALUES (:actId, :name, :birthday, :gender, :phone, :email, :province, :city)`;

    return mySqlCli.query(strFind, {
        actId,
        phone
    }).then(ret => {
        if (ret && ret[0].user_count <= 0) {
            return mySqlCli.insert(strInsert, {
                actId,
                name,
                birthday,
                gender,
                phone,
                email,
                province,
                city
            }).then(ret2 => {
                return Promise.resolve({
                    code: 200,
                    message: '用户信息添加成功',
                    data: ret2
                });
            });
        } else {
            return Promise.resolve({
                code: 203,
                message: '已存在的用户信息'
            });
        }
    });
  }

  async getStatus(uid) {
    const hasInx = await this.client.getAsync(`coupon511_${uid}`);

    if (hasInx) {
      return {
        code: 200,
        data: hasInx
      };
    }
    return {
      code: 204,
    };
  }
  async takeCoupon(obj, inx) {
    const hasInx = await this.client.getAsync(`coupon511_${obj.uid}`);

    if (hasInx) {
      return {
        code: 200,
        message: '已领取过 不可重复领取'
      };
    }
    let uid = {
      toString: () => {
        return _.parseInt(obj.uid);
      },
      sessionKey: obj.sessionKey,
      appVersion: obj.appVersion,
      appSessionType: obj.sessionType
    };

    let result;

    if (inx < 3) {
      result = await this._getCoupon(uid, inx);
    } else {
      this._saveUserInfo(obj.uid);
      result = {
        code: 200,
        message: '领取成功'
      };
    }
    if (result.code === 200 || result.code === 401) {
      this.client.set(`coupon511_${obj.uid}`, inx.toString());
    }
    return result;
  }
}

module.exports = Coupon511;