123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- <?php
- /**
- * Created by: PhpStorm
- * User: lytian
- * Date: 2020/3/19
- * Time: 21:26
- */
- namespace app\admin\command;
- use app\admin\service\LogService;
- use app\common\library\Redis;
- use think\Config;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\Db;
- use think\Env;
- use think\Request;
- class FlushActivityTipCache extends Command
- {
- protected function configure()
- {
- $this->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;
- }
- }
|