setName('FlushActivityTipCache') ->setDescription('刷入弹窗缓存'); } protected function execute(Input $input, Output $output) { //弹窗位置 $popRange = [ '0:0' => '最近阅读', '0:1' => '最近阅读', '0:2' => '最近阅读', '0:3' => '最近阅读', '0:4' => '最近阅读', '1:0' => '男生', '1:1' => '男生', '1:2' => '男生', '1:3' => '男生', '1:4' => '男生', '2:0' => '女生', '2:1' => '女生', '2:2' => '女生', '2:3' => '女生', '2:4' => '女生', '3:0' => '个人中心', '3:1' => '个人中心', '3:2' => '个人中心', '3:3' => '个人中心', '3:4' => '个人中心', '4:0' => '分类', '4:1' => '分类', '4:2' => '分类', '4:3' => '分类', '4:4' => '分类', '5:0' => '免费', '5:1' => '免费', '5:2' => '免费', '5:3' => '免费', '5:4' => '免费', '6:0' => '完本', '6:1' => '完本', '6:2' => '完本', '6:3' => '完本', '6:4' => '完本', ]; Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定一下 $mainSlaveDbConfig = $this->getMainSlaveDbConfig(); //查询所有渠道商和代理商 $sql = "SELECT id FROM admin as a LEFT JOIN auth_group_access as b on a.id = b.uid where b.group_id in (3, 4)"; $adminRows = Db::connect($mainSlaveDbConfig)->query($sql); $adminIds0 = $adminIds1 = $adminIds2 = array_column($adminRows, 'id'); $time = time(); $site = model("Config")->getConfigSiteArr(); $activity_config = $site['activity_config']; $give_activity_config = $site['activity_give_config']; //自定义活动 $do = true; while ($do) { //100个渠道一起跑 $adminIdsArr = array_splice($adminIds0, 0, 100); $adminIdsStr = implode(',', $adminIdsArr); //拉取100个渠道的进行中的弹窗开启的自定义活动 $sql = "SELECT id, admin_id, starttime, endtime, config_id, pop_range,pop_ispay FROM activity WHERE admin_id in ({$adminIdsStr}) AND type = 1 AND status = '1' AND pop_status = '1' AND starttime <= {$time} AND endtime > {$time} ORDER BY createtime DESC"; $activityRows = Db::connect($mainSlaveDbConfig)->query($sql); $list = []; if ($activityRows) { //查询资源等 foreach ($activityRows as $activityRow) { //自定义活动 $configId = $activityRow['config_id']; $config = $activity_config['config'][$configId] ?? null; //配置有效&资源不为空 if ($config && intval($config['status']) && (isset($config['resource_id']) && $config['resource_id']) ) { $resourceSql = "SELECT a.id, b.show_type, a.popimage FROM resource as a INNER JOIN goods as b ON a.goods_id = b.id WHERE a.id = {$config['resource_id']}"; $resource = Db::connect($mainSlaveDbConfig)->query($resourceSql); if ($resource) { $resource = $resource[0]; $item = [ 'aid' => $activityRow['id'], 'limited' => $activityRow['limited'] ?? 0, 'id' => $resource['id'], 'popimage' => $resource['popimage'], 'pop_range' => $activityRow['pop_range'], 'pop_ispay' => $activityRow['pop_ispay'], 'show_type' => $resource['show_type'], 'starttime' => $activityRow['starttime'], 'endtime' => $activityRow['endtime'], ]; $list[$activityRow['admin_id']][] = $item; } else { continue; } } else { continue; } } } if ($list) { foreach ($adminIdsArr as $adminId) { //匹配位置和用户类别 $redisKey = "S-P-T-C:N:" . $adminId; $fields = []; foreach ($popRange as $k => $v) { $position = explode(':', $k); //该位置是否有活动 if (isset($list[$adminId])) { foreach ($list[$adminId] as $activity) { $activityPopRange = explode(',', $activity['pop_range']); if (in_array($position[0], $activityPopRange)) { if ($activity['show_type'] == 0 && !isset($fields[$k])) { $fields[$k] = json_encode($activity, JSON_UNESCAPED_UNICODE); break; } else { if ($position[1] == $activity['show_type'] && !isset($fields[$k])) { $fields[$k] = json_encode($activity, JSON_UNESCAPED_UNICODE); break; } } } } } } //有活动才写入 if ($fields) { LogService::info("自定义活动:admin_id: {$adminId} key:{$redisKey} val:" . json_encode($fields)); Redis::instance()->del($redisKey); Redis::instance()->hMSet($redisKey, $fields); Redis::instance()->expire($redisKey, 180); } } } if (empty($adminIds0)) { $do = false; } } //赠币活动 $go = true; while ($go) { //100个渠道一起跑 $adminIdsArr = array_splice($adminIds1, 0, 100); $adminIdsStr = implode(',', $adminIdsArr); $sql = "SELECT id, admin_id, starttime, endtime, config_id, pop_range, pop_ispay FROM activity WHERE admin_id in ({$adminIdsStr}) AND type = 3 AND status = '1' AND pop_status = '1' AND starttime <= {$time} AND endtime > {$time} ORDER BY createtime DESC"; $activityRows = Db::connect($mainSlaveDbConfig)->query($sql); $list = []; if ($activityRows) { //查询资源等 foreach ($activityRows as $activityRow) { //自定义活动 $configId = $activityRow['config_id']; $config = $give_activity_config['config'][$configId] ?? null; //配置有效&资源不为空 if ($config && intval($config['status']) && (isset($config['resource_id']) && $config['resource_id']) ) { $resourceSql = "SELECT id, show_type, popimage FROM resource WHERE id = {$config['resource_id']}"; $resource = Db::connect($mainSlaveDbConfig)->query($resourceSql); if ($resource) { $resource = $resource[0]; $item = [ 'aid' => $activityRow['id'], 'limited' => $activityRow['limited'] ?? 0, 'id' => $resource['id'], 'popimage' => $resource['popimage'], 'pop_range' => $activityRow['pop_range'], 'pop_ispay' => $activityRow['pop_ispay'], 'show_type' => $resource['show_type'], 'starttime' => $activityRow['starttime'], 'endtime' => $activityRow['endtime'], ]; $list[$activityRow['admin_id']][] = $item; } else { continue; } } else { continue; } } } if ($list) { foreach ($adminIdsArr as $adminId) { //匹配位置和用户类别 $redisKey = "S-P-T-G:N:" . $adminId; $fields = []; foreach ($popRange as $k => $v) { $position = explode(':', $k); //该位置是否有活动 if (isset($list[$adminId])) { foreach ($list[$adminId] as $activity) { $activityPopRange = explode(',', $activity['pop_range']); if (in_array($position[0], $activityPopRange)) { if ($activity['show_type'] == 0 && !isset($fields[$k])) { $fields[$k] = json_encode($activity, JSON_UNESCAPED_UNICODE); break; } else { if ($position[1] == $activity['show_type'] && !isset($fields[$k])) { $fields[$k] = json_encode($activity, JSON_UNESCAPED_UNICODE); break; } } } } } } //有活动才写入 if ($fields) { LogService::info("赠币活动:admin_id: {$adminId} key:{$redisKey} val:" . json_encode($fields)); Redis::instance()->del($redisKey); Redis::instance()->hMSet($redisKey, $fields); Redis::instance()->expire($redisKey, 180); } } } if (empty($adminIds1)) { $go = false; } } //平台活动 $sql = "SELECT a.id as aid, b.popimage, b.id as id, a.pop_range, a.starttime, a.endtime, c.show_type FROM activity as a INNER JOIN resource as b on a.id = b.activity_id INNER JOIN goods as c ON b.goods_id = c.id WHERE a.type = 0 AND a.status = '1' AND a.pop_status = '1' AND a.starttime <= {$time} AND a.endtime > {$time} ORDER BY a.createtime DESC"; $activityRows = Db::connect($mainSlaveDbConfig)->query($sql); if ($activityRows) { $redisKey = "S-P-T-P:N:"; $fields = []; foreach ($popRange as $k => $v) { $position = explode(':', $k); //该位置是否有活动 foreach ($activityRows as $activity) { $activityPopRange = explode(',', $activity['pop_range']); if (in_array($position[0], $activityPopRange)) { if ($activity['show_type'] == 0 && !isset($fields[$k])) { $fields[$k] = json_encode($activity, JSON_UNESCAPED_UNICODE); LogService::info("平台活动:key:{$redisKey} position:{$k} val:" . json_encode($activity)); break; } else { if ($position[1] == $activity['show_type'] && !isset($fields[$k])) { LogService::info("平台活动:key:{$redisKey} val:" . json_encode($activity)); $fields[$k] = json_encode($activity, JSON_UNESCAPED_UNICODE); break; } } } } } //有活动才写入 if ($fields) { Redis::instance()->del($redisKey); Redis::instance()->hMSet($redisKey, $fields); Redis::instance()->expire($redisKey, 180); } } } /** * 获取主库的从库配置 从库不存在返回主库 * @return array */ private function getMainSlaveDbConfig() { $hostArr = explode(',', Env::get('database.admin_hostname')); $portArr = explode(',', Env::get('database.admin_hostport', '3306,3306')); //默认主库 $mainDbConfig = array_merge(Config::get("database"), ['hostname' => $hostArr[0], 'hostport' => $portArr[0]]); if (count($hostArr) >= 2) { //从库 $mainDbConfig = array_merge(Config::get("database"), ['hostname' => $hostArr[1], 'hostport' => $portArr[1]]); } return $mainDbConfig; } }