| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- /**
- * 描述.
- *
- * @author nj
- * @date 2021-07-10 16:47
- */
- namespace Modules\InnerApi\Services;
- use App\Constants\RedisKeysConstant;
- use App\Events\InnerUserNewIncEvent;
- use App\Jobs\UserHyperLogLogJob;
- use App\Models\Mongo\UsersModel;
- use App\Repositories\AgentRepository;
- use App\Repositories\AppDeductionRuleRepository;
- use App\Repositories\ConfigRepoistory;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Redis;
- use Zhuzhichao\IpLocationZh\Ip;
- class UserService
- {
- /**
- * 缓存时间
- * @var int
- */
- private $cacheTime = 300;
- /**
- * 增量写入新用户.
- * @param array $data
- * @return mixed
- */
- public function inc(array $data)
- {
- $eventParams = [];
- foreach ($data as $item) {
- $item['created_at'] = Carbon::parse($item['created_at']);
- $item['invite_uid'] = !empty($item['invite_uid']) ? $item['invite_uid'] : '';
- if ($item['invite_uid'] == $item['user_uid']) {
- // 不能等于自己.
- $item['invite_uid'] = '';
- }
- $ipInfo = Ip::find($item['ip']);
- $item['ip_address'] = $ipInfo[0] . ' ' . $ipInfo[1] . ' ' . $ipInfo[2];
- $logObj = UsersModel::create($item);
- if (!empty($logObj->_id)) {
- $eventParams[] = [
- 'log_id' => $logObj->_id,
- ];
- }
- }
- // 新用户注册时,也算一次登录.
- self::login($data);
- // 触发事件.
- event(new InnerUserNewIncEvent($eventParams));
- return true;
- }
- /**
- * 登录
- * @param array $data
- * @return bool
- */
- public function login(array $data)
- {
- // 丢job构造函数时,必须精简数据.
- $jobData = [];
- if (!empty($data)) {
- foreach ($data as $item) {
- $jobData[] = [
- 'agent_uid' => $item['agent_uid'],
- 'channel_uid' => $item['channel_uid'],
- 'created_at' => $item['created_at'],
- 'device_type' => $item['device_type'],
- 'mom_app_uid' => $item['mom_app_uid'],
- 'user_uid' => $item['user_uid'],
- ];
- }
- }
- if (!empty($jobData)) {
- dispatch(new UserHyperLogLogJob($jobData));
- }
- return true;
- }
- /**
- * 随机扣量标记. cpa扣量
- * @param array $item
- * @return array
- */
- public function oneRandDeduction(array $item)
- {
- $item['is_deduction'] = 0;
- //获取代理扣量配置
- $deductionConfig = app(ConfigRepoistory::class)->pickByKeys(['preferential_user', 'cpa_deduction_switch'], 'sys');
- if ($deductionConfig['cpa_deduction_switch']['svalue'] == 0) {
- return $item;
- }
- $iTodayRegUser = UsersModel::where('channel_uid', $item['channel_uid'])->count();
- //每日前几单不扣
- if ($iTodayRegUser < $deductionConfig['preferential_user']['svalue']) {
- return $item;
- }
- $agentInfo = app(AgentRepository::class)->findBy('uid', $item['agent_uid'], ['deduction_switch', 'cpa_deduction_rule']);
- if ($agentInfo->deduction_switch == 2 && !empty($agentInfo->cpa_deduction_rule)) {
- $item['is_deduction'] = $this->isDeduction($item, $agentInfo->cpa_deduction_rule);
- } else if ($agentInfo->deduction_switch == 0 || $agentInfo->deduction_switch == 3) {
- //获取扣量规则
- $rulesInfo = app(AppDeductionRuleRepository::class)->findByWhere(['app_uid' => $item['mom_app_uid'], 'deduction_type' => 'cpa'], ['deduction_pro']);
- if (empty($rulesInfo)) {
- return $item;
- }
- if ($agentInfo->deduction_switch == 3) {
- $method = 3;
- } else {
- $method = 1;
- }
- //超过每日设定的默认单就开始扣量
- $item['is_deduction'] = $this->isDeduction($item, $rulesInfo->deduction_pro, $method);
- } else {
- $item['is_deduction'] = 0;
- }
- return $item;
- }
- /**
- * 扣量
- *
- * @param $item
- * @param $deductionPro
- * @param $method
- * @return int|mixed
- */
- private function isDeduction($item, $deductionPro, $method = '')
- {
- $isDeduction = 0;
- //设置扣量参数
- $ratio = $deductionPro;
- $ratio = explode('-',$ratio);
- if ($ratio[1] == 0) {
- return $isDeduction;
- }
- $hashKey = RedisKeysConstant::DEDUCTION_RULES_HASH_CPA . $item['mom_app_uid'] . $item['channel_uid'] . $method;
- //扣量标记
- $areaKey = $item['channel_uid'];
- $deductionMark = Redis::hget($hashKey, $areaKey);
- $mark = [0,0];
- if ($deductionMark) {
- $mark = explode('-', $deductionMark);
- if ($mark[0] == $ratio[0] && $mark[1] == $ratio[1]) {
- Redis::hDel($hashKey, $areaKey);
- $mark = [0,0];
- }
- //如果扣量达到,但是一轮还没结束则直接返回
- if ($mark[1] == $ratio[1]) {
- // $mark[0] + 1 参与订单总数加1
- Redis::hset($hashKey, $areaKey, $mark[0] + 1 . '-' . $mark[1]);
- return $isDeduction;
- }
- }
- //必扣计算
- if (($ratio[0] - $mark[0]) <= ($ratio[1] - $mark[1])) {
- $isDeduction = 1;
- } else {
- $isDeduction = mt_rand(0, 1);
- }
- if ($isDeduction == 1) {
- $mark[1] += 1;
- }
- $mark[0] += 1;
- Redis::hset($hashKey, $areaKey, $mark[0] . '-' . $mark[1]);
- return $isDeduction;
- }
- }
|