| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- /**
- * 会员相关队列
- */
- namespace jobs;
- use app\common\model\UserFansModel;
- use app\common\model\UserModel;
- use think\Exception;
- use think\facade\Db;
- class UserJobs extends BaseJob
- {
- protected $model = null;
- public function __construct (UserModel $model)
- {
- $this->model = $model;
- }
- /**
- * 绑定用户关系和添加用户静态数据
- * @param string $invite_code
- * @param int $uid
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function userRelation (string $invite_code, int $uid)
- {
- $data['px_pid'] = $data['pid'] = $data['is_px'] = 0;
- if (($up_user = $this->model->where('code', $invite_code)->field('id')->lock(true)->find()) !== false && !empty($up_user)) { // 直推码
- $data['pid'] = $up_user['id']; // pid
- $relation = $this->model->where('id', $up_user['id'])->value('path');
- $data['path'] = $relation . (empty($relation)?'':',') . $up_user['id'];
- } elseif (($up_user = $this->model->where('px_code', $invite_code)->field('id')->lock(true)->find()) !== false && !empty($up_user)) { // 排线码
- $last_user = $this->model->where('id', $up_user['id'])->field('uid,px_pid,path')->order('id desc')->find(); // 排线最后一位
- if (!empty($last_user)) { // 如果有则在该用户下继续排线
- $data['pid'] = $last_user['id']; // 上级id
- $data['path'] = $last_user['path'] . ',' . $last_user['id'];
- } else { // 没有就开启新的排线
- $relation = $this->model->where('id', $up_user['id'])->value('path');
- $data['pid'] = $up_user['id']; // pid
- $data['path'] = $relation . ',' . $up_user['id'];
- }
- $data['px_pid'] = $up_user['id']; // 排线pid
- $data['is_px'] = 1; // 标记排线
- }
- $data['path'] = trim_string($data['path']);
- $data['nickname'] = '会员_' . $uid;
- if (!isset($data['path']) || empty($data['path']))
- throw new Exception('注册失败');
- $this->model->where('id', $uid)->save($data);
- $data['path'] .= ',' . $uid;
- $data['path'] = trim_string($data['path']);
- $this->model->where('id', 'in', $data['path'])->save(['total_number' => ['inc', 1]]);
- return !0;
- }
- // invite_code:注册的邀请码 uid:注册用户的 uid
- public function userRelationRegister (string $invite_code, int $uid)
- {
- $model = new UserModel();
- $modelfans = new UserFansModel();
- Db::startTrans();
- try {
- $curid = $uid;
- while (1){
- $cur_user = $model->where(['id' => $curid])->find();
- if ($cur_user['pid'] == 0){
- break;
- }
- $curid = $cur_user['pid'];
- if (!$modelfans->where(['uid'=>$cur_user['pid'], 'fans_uid'=>$uid])->find()){
- $model->where(['id'=>$cur_user['pid']])->save(['total_number' => ['inc', 1]]);
- $modelfans->insert(['uid'=>$cur_user['pid'], 'fans_uid'=>$uid, 'create_time'=>sr_getcurtime(time())]);
- }
- }
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- sr_testDb($e->getMessage());
- Db::name('failed_jobs')->save(['connection'=>'redis', 'queue'=>'aa', 'payload'=>'aa', 'failed_at'=>sr_getcurtime(time()), 'attempts'=>2, 'exception'=>$e->getMessage()]);
- }
- return true;
- }
- // invite_code:注册的邀请码 uid:注册用户的 uid
- public function userRelationAuth (string $invite_code, int $uid)
- {
- $model = new UserModel();
- Db::startTrans();
- try {
- $curid = $uid;
- while (1){
- $cur_user = $model->where(['id' => $curid])->find();
- if ($cur_user['pid'] == 0){
- break;
- }
- $model->where(['id'=>$cur_user['pid']])->save(['total_number_real' => ['inc', 1]]);
- $curid = $cur_user['pid'];
- }
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- Db::name('failed_jobs')->save(['connection'=>'redis', 'queue'=>'aa', 'payload'=>'aa', 'failed_at'=>sr_getcurtime(time()), 'attempts'=>2, 'exception'=>$e->getMessage()]);
- }
- return !0;
- }
- // $uid 当前产生
- /**
- * 邀新奖励(分30天发放)
- * @param int $uid
- * @param int $from_uid
- * @return bool
- */
- public function inviteRewards (int $uid, int $from_uid)
- {
- $user = $this->model->where('id', $uid)->field('level,vip,vip_type')->findOrEmpty();
- if (empty($user))
- return !0;
- if (!$user['level'] && !$user['vip'])
- return !0;
- if ($user['vip']) {
- $invite = get_vip_config($user['vip_type'])['invite'] ?? 0;
- } else {
- $invite = get_star_config($user['level'])['invite'] ?? 0;
- }
- if (!$invite)
- return !0;
- $insert['uid'] = $uid;
- $insert['type'] = 1; // 邀新奖励
- $insert['total_amount'] = $invite;
- $insert['total_days'] = 30;
- $insert['surplus_days'] = $insert['total_days'];
- $insert['daily_amount'] = $invite / $insert['total_days'];
- $insert['from_uid'] = $from_uid;
- Db::name('daily_reward')->insert();
- return !0;
- }
- }
|