|
@@ -0,0 +1,152 @@
|
|
|
|
+<?php
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+namespace App\Modules\AdPosition\Services;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+use App\Libs\Singleton;
|
|
|
|
+use App\Modules\AdPosition\Models\AdPosition;
|
|
|
|
+use App\Modules\AdPosition\Models\AdPositionDispatch;
|
|
|
|
+use App\Modules\Trade\Services\OrderService;
|
|
|
|
+use App\Modules\User\Models\QappUser;
|
|
|
|
+
|
|
|
|
+class AdPositionService
|
|
|
|
+{
|
|
|
|
+ use Singleton;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @param $uid
|
|
|
|
+ * @param $accountId
|
|
|
|
+ * @return array
|
|
|
|
+ */
|
|
|
|
+ public function getAdPositions($uid, $accountId): array
|
|
|
|
+ {
|
|
|
|
+ // 获取广告位
|
|
|
|
+ $positions = AdPosition::getAllValidAdPositions();
|
|
|
|
+ if (empty($positions)) {
|
|
|
|
+ return [];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 获取广告位分配信息
|
|
|
|
+ $positionIds = array_column($positions, 'id');
|
|
|
|
+ $dispatches = AdPositionDispatch::getValidPositionDispatches($positionIds, $accountId);
|
|
|
|
+ if (empty($dispatches)) {
|
|
|
|
+ return [];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 针对分配数据筛选出优先级最高的分配任务
|
|
|
|
+ $result = [];
|
|
|
|
+ $dispatches = collect($dispatches)->groupBy('ad_position_id')->all();
|
|
|
|
+ foreach ($positions as $position) {
|
|
|
|
+ // 相关变量获取
|
|
|
|
+ $positionId = getProp($position, 'id');
|
|
|
|
+ $ident = getProp($position, 'ident');
|
|
|
|
+ $filterContentJson = getProp($position, 'filter_condition');
|
|
|
|
+
|
|
|
|
+ // 获取优先级最高的任务
|
|
|
|
+ $validDispatches = getProp($dispatches, $positionId, []);
|
|
|
|
+ $validDispatch = $validDispatches ? $validDispatches[0] : [];
|
|
|
|
+ if (empty($validDispatch)) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 过滤条件判断
|
|
|
|
+ $matchFilter = $this->filterDispatch($uid, $filterContentJson);
|
|
|
|
+ if (!$matchFilter) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 添加到数据中
|
|
|
|
+ $result[$ident][] = $this->buildPositionData($validDispatch);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return $result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 过滤条件筛选
|
|
|
|
+ * @param $uid
|
|
|
|
+ * @param $filterContentJson
|
|
|
|
+ * @return bool
|
|
|
|
+ */
|
|
|
|
+ private function filterDispatch($uid, $filterContentJson): bool
|
|
|
|
+ {
|
|
|
|
+ // 过滤条件
|
|
|
|
+ $filterContent = json_decode($filterContentJson, true);
|
|
|
|
+ if (empty($uid) || empty($filterContentJson) || empty($filterContent)) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 判断注册时间
|
|
|
|
+ $registerFilter = false;
|
|
|
|
+ $registerAtArr = getProp($filterContent, 'registerAt');
|
|
|
|
+ if ($registerAtArr) {
|
|
|
|
+ // 获取用户信息
|
|
|
|
+ $user = QappUser::getUserByUid($uid);
|
|
|
|
+ $registerAt = getProp($user, 'created_at');
|
|
|
|
+ $registerUnix = strtotime($registerAt);
|
|
|
|
+
|
|
|
|
+ // 循环条件,满足一条即可
|
|
|
|
+ foreach ($registerAtArr as $register) {
|
|
|
|
+ $unit = getProp($register, 'unit');
|
|
|
|
+ $range = getProp($register, 'range');
|
|
|
|
+ sort($range);
|
|
|
|
+ [$min, $max] = $range;
|
|
|
|
+ $startUnix = strtotime('-' . $max . ' ' . $unit);
|
|
|
|
+ $endUnix = strtotime('-' . $min . ' ' . $unit);
|
|
|
|
+ if ($registerUnix >= $startUnix && $registerUnix <= $endUnix) {
|
|
|
|
+ $registerFilter = true;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 判断付费情况
|
|
|
|
+ $paidFilter = false;
|
|
|
|
+ if (isset($filterContent['isPaid'])) {
|
|
|
|
+ // 获取用户付费情况
|
|
|
|
+ $order = OrderService::getUserLastestOrder($uid);
|
|
|
|
+
|
|
|
|
+ // 有付费
|
|
|
|
+ if ($order && $filterContent['isPaid']) {
|
|
|
|
+ $paidFilter = true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 未付费
|
|
|
|
+ if (!$order && !$filterContent['isPaid']) {
|
|
|
|
+ $paidFilter = true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 两者如有一个不满足条件则不展示
|
|
|
|
+ if (!$registerFilter || !$paidFilter) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 返回可显示数据
|
|
|
|
+ * @param $param
|
|
|
|
+ * @return array
|
|
|
|
+ */
|
|
|
|
+ private function buildPositionData($param): array
|
|
|
|
+ {
|
|
|
|
+ if (empty($param)) {
|
|
|
|
+ return [];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 链接做特殊处理
|
|
|
|
+ $url = getProp($param, 'link_url');
|
|
|
|
+ $urlArr = parse_url($url);
|
|
|
|
+ parse_str(getProp($urlArr, 'query'), $queryArr);
|
|
|
|
+
|
|
|
|
+ return [
|
|
|
|
+ 'img' => getProp($param, 'image_url'),
|
|
|
|
+ 'url' => getProp($urlArr, 'path'),
|
|
|
|
+ 'title' => getProp($param, 'title'),
|
|
|
|
+ 'params' => $queryArr ?: (object)[]
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+}
|