ReferralSlave.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: lytian
  5. * Date: 2019/4/22
  6. * Time: 21:47
  7. */
  8. namespace app\common\model;
  9. use app\common\library\Redis;
  10. use app\main\constants\BookConstants;
  11. use think\Model;
  12. class ReferralSlave extends Model
  13. {
  14. // 表名
  15. protected $table = 'referral_slave';
  16. // 自动写入时间戳字段
  17. protected $autoWriteTimestamp = 'int';
  18. // 定义时间戳字段名
  19. protected $createTime = 'createtime';
  20. protected $updateTime = 'updatetime';
  21. // 追加属性
  22. protected $append = [];
  23. /**
  24. * 随机获取vip分流链接的id
  25. * @param $library_id
  26. * @return int
  27. */
  28. public function getReferralId($library_id)
  29. {
  30. $redisKey = BookConstants::VIP_REFERRAL_KEY.$library_id;
  31. $referralId = Redis::instance()->sRandMember($redisKey);
  32. if (!$referralId){
  33. $ids = model("ReferralSlave")->where('library_id', 'eq', $library_id)
  34. ->field('referral_id')
  35. ->select();
  36. if (empty($ids)){
  37. $ids = [];
  38. $ids['referral_id'] = 0;
  39. }
  40. $ids = array_column($ids,'referral_id');
  41. $referralId = array_rand($ids);
  42. Redis::instance()->sAddArray($redisKey,$ids);
  43. Redis::instance()->expire($redisKey,3600);
  44. }
  45. return (int)$referralId;
  46. }
  47. }