1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- /**
- * Created by PhpStorm.
- * User: lytian
- * Date: 2019/4/22
- * Time: 21:47
- */
- namespace app\common\model;
- use app\common\library\Redis;
- use app\main\constants\BookConstants;
- use think\Model;
- class ReferralSlave extends Model
- {
- // 表名
- protected $table = 'referral_slave';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- // 追加属性
- protected $append = [];
- /**
- * 随机获取vip分流链接的id
- * @param $library_id
- * @return int
- */
- public function getReferralId($library_id)
- {
- $redisKey = BookConstants::VIP_REFERRAL_KEY.$library_id;
- $referralId = Redis::instance()->sRandMember($redisKey);
- if (!$referralId){
- $ids = model("ReferralSlave")->where('library_id', 'eq', $library_id)
- ->field('referral_id')
- ->select();
- if (empty($ids)){
- $ids = [];
- $ids['referral_id'] = 0;
- }
- $ids = array_column($ids,'referral_id');
- $referralId = array_rand($ids);
- Redis::instance()->sAddArray($redisKey,$ids);
- Redis::instance()->expire($redisKey,3600);
- }
- return (int)$referralId;
- }
- }
|