FlushActivityTipCache.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /**
  3. * Created by: PhpStorm
  4. * User: lytian
  5. * Date: 2020/3/19
  6. * Time: 21:26
  7. */
  8. namespace app\admin\command;
  9. use app\admin\service\LogService;
  10. use app\common\library\Redis;
  11. use think\Config;
  12. use think\console\Command;
  13. use think\console\Input;
  14. use think\console\Output;
  15. use think\Db;
  16. use think\Env;
  17. use think\Request;
  18. class FlushActivityTipCache extends Command
  19. {
  20. protected function configure()
  21. {
  22. $this->setName('FlushActivityTipCache')
  23. ->setDescription('刷入弹窗缓存');
  24. }
  25. protected function execute(Input $input, Output $output)
  26. {
  27. //弹窗位置
  28. $popRange = [
  29. '0:0' => '最近阅读',
  30. '0:1' => '最近阅读',
  31. '0:2' => '最近阅读',
  32. '0:3' => '最近阅读',
  33. '0:4' => '最近阅读',
  34. '1:0' => '男生',
  35. '1:1' => '男生',
  36. '1:2' => '男生',
  37. '1:3' => '男生',
  38. '1:4' => '男生',
  39. '2:0' => '女生',
  40. '2:1' => '女生',
  41. '2:2' => '女生',
  42. '2:3' => '女生',
  43. '2:4' => '女生',
  44. '3:0' => '个人中心',
  45. '3:1' => '个人中心',
  46. '3:2' => '个人中心',
  47. '3:3' => '个人中心',
  48. '3:4' => '个人中心',
  49. '4:0' => '分类',
  50. '4:1' => '分类',
  51. '4:2' => '分类',
  52. '4:3' => '分类',
  53. '4:4' => '分类',
  54. '5:0' => '免费',
  55. '5:1' => '免费',
  56. '5:2' => '免费',
  57. '5:3' => '免费',
  58. '5:4' => '免费',
  59. '6:0' => '完本',
  60. '6:1' => '完本',
  61. '6:2' => '完本',
  62. '6:3' => '完本',
  63. '6:4' => '完本',
  64. ];
  65. Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定一下
  66. $mainSlaveDbConfig = $this->getMainSlaveDbConfig();
  67. //查询所有渠道商和代理商
  68. $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)";
  69. $adminRows = Db::connect($mainSlaveDbConfig)->query($sql);
  70. $adminIds0 = $adminIds1 = $adminIds2 = array_column($adminRows, 'id');
  71. $time = time();
  72. $site = model("Config")->getConfigSiteArr();
  73. $activity_config = $site['activity_config'];
  74. $give_activity_config = $site['activity_give_config'];
  75. //自定义活动
  76. $do = true;
  77. while ($do) {
  78. //100个渠道一起跑
  79. $adminIdsArr = array_splice($adminIds0, 0, 100);
  80. $adminIdsStr = implode(',', $adminIdsArr);
  81. //拉取100个渠道的进行中的弹窗开启的自定义活动
  82. $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";
  83. $activityRows = Db::connect($mainSlaveDbConfig)->query($sql);
  84. $list = [];
  85. if ($activityRows) {
  86. //查询资源等
  87. foreach ($activityRows as $activityRow) {
  88. //自定义活动
  89. $configId = $activityRow['config_id'];
  90. $config = $activity_config['config'][$configId] ?? null;
  91. //配置有效&资源不为空
  92. if ($config &&
  93. intval($config['status']) &&
  94. (isset($config['resource_id']) && $config['resource_id'])
  95. ) {
  96. $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']}";
  97. $resource = Db::connect($mainSlaveDbConfig)->query($resourceSql);
  98. if ($resource) {
  99. $resource = $resource[0];
  100. $item = [
  101. 'aid' => $activityRow['id'],
  102. 'limited' => $activityRow['limited'] ?? 0,
  103. 'id' => $resource['id'],
  104. 'popimage' => $resource['popimage'],
  105. 'pop_range' => $activityRow['pop_range'],
  106. 'pop_ispay' => $activityRow['pop_ispay'],
  107. 'show_type' => $resource['show_type'],
  108. 'starttime' => $activityRow['starttime'],
  109. 'endtime' => $activityRow['endtime'],
  110. ];
  111. $list[$activityRow['admin_id']][] = $item;
  112. } else {
  113. continue;
  114. }
  115. } else {
  116. continue;
  117. }
  118. }
  119. }
  120. if ($list) {
  121. foreach ($adminIdsArr as $adminId) {
  122. //匹配位置和用户类别
  123. $redisKey = "S-P-T-C:N:" . $adminId;
  124. $fields = [];
  125. foreach ($popRange as $k => $v) {
  126. $position = explode(':', $k);
  127. //该位置是否有活动
  128. if (isset($list[$adminId])) {
  129. foreach ($list[$adminId] as $activity) {
  130. $activityPopRange = explode(',', $activity['pop_range']);
  131. if (in_array($position[0], $activityPopRange)) {
  132. if ($activity['show_type'] == 0 && !isset($fields[$k])) {
  133. $fields[$k] = json_encode($activity, JSON_UNESCAPED_UNICODE);
  134. break;
  135. } else {
  136. if ($position[1] == $activity['show_type'] && !isset($fields[$k])) {
  137. $fields[$k] = json_encode($activity, JSON_UNESCAPED_UNICODE);
  138. break;
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. //有活动才写入
  146. if ($fields) {
  147. LogService::info("自定义活动:admin_id: {$adminId} key:{$redisKey} val:" . json_encode($fields));
  148. Redis::instance()->del($redisKey);
  149. Redis::instance()->hMSet($redisKey, $fields);
  150. Redis::instance()->expire($redisKey, 180);
  151. }
  152. }
  153. }
  154. if (empty($adminIds0)) {
  155. $do = false;
  156. }
  157. }
  158. //赠币活动
  159. $go = true;
  160. while ($go) {
  161. //100个渠道一起跑
  162. $adminIdsArr = array_splice($adminIds1, 0, 100);
  163. $adminIdsStr = implode(',', $adminIdsArr);
  164. $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";
  165. $activityRows = Db::connect($mainSlaveDbConfig)->query($sql);
  166. $list = [];
  167. if ($activityRows) {
  168. //查询资源等
  169. foreach ($activityRows as $activityRow) {
  170. //自定义活动
  171. $configId = $activityRow['config_id'];
  172. $config = $give_activity_config['config'][$configId] ?? null;
  173. //配置有效&资源不为空
  174. if ($config &&
  175. intval($config['status']) &&
  176. (isset($config['resource_id']) && $config['resource_id'])
  177. ) {
  178. $resourceSql = "SELECT id, show_type, popimage FROM resource WHERE id = {$config['resource_id']}";
  179. $resource = Db::connect($mainSlaveDbConfig)->query($resourceSql);
  180. if ($resource) {
  181. $resource = $resource[0];
  182. $item = [
  183. 'aid' => $activityRow['id'],
  184. 'limited' => $activityRow['limited'] ?? 0,
  185. 'id' => $resource['id'],
  186. 'popimage' => $resource['popimage'],
  187. 'pop_range' => $activityRow['pop_range'],
  188. 'pop_ispay' => $activityRow['pop_ispay'],
  189. 'show_type' => $resource['show_type'],
  190. 'starttime' => $activityRow['starttime'],
  191. 'endtime' => $activityRow['endtime'],
  192. ];
  193. $list[$activityRow['admin_id']][] = $item;
  194. } else {
  195. continue;
  196. }
  197. } else {
  198. continue;
  199. }
  200. }
  201. }
  202. if ($list) {
  203. foreach ($adminIdsArr as $adminId) {
  204. //匹配位置和用户类别
  205. $redisKey = "S-P-T-G:N:" . $adminId;
  206. $fields = [];
  207. foreach ($popRange as $k => $v) {
  208. $position = explode(':', $k);
  209. //该位置是否有活动
  210. if (isset($list[$adminId])) {
  211. foreach ($list[$adminId] as $activity) {
  212. $activityPopRange = explode(',', $activity['pop_range']);
  213. if (in_array($position[0], $activityPopRange)) {
  214. if ($activity['show_type'] == 0 && !isset($fields[$k])) {
  215. $fields[$k] = json_encode($activity, JSON_UNESCAPED_UNICODE);
  216. break;
  217. } else {
  218. if ($position[1] == $activity['show_type'] && !isset($fields[$k])) {
  219. $fields[$k] = json_encode($activity, JSON_UNESCAPED_UNICODE);
  220. break;
  221. }
  222. }
  223. }
  224. }
  225. }
  226. }
  227. //有活动才写入
  228. if ($fields) {
  229. LogService::info("赠币活动:admin_id: {$adminId} key:{$redisKey} val:" . json_encode($fields));
  230. Redis::instance()->del($redisKey);
  231. Redis::instance()->hMSet($redisKey, $fields);
  232. Redis::instance()->expire($redisKey, 180);
  233. }
  234. }
  235. }
  236. if (empty($adminIds1)) {
  237. $go = false;
  238. }
  239. }
  240. //平台活动
  241. $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";
  242. $activityRows = Db::connect($mainSlaveDbConfig)->query($sql);
  243. if ($activityRows) {
  244. $redisKey = "S-P-T-P:N:";
  245. $fields = [];
  246. foreach ($popRange as $k => $v) {
  247. $position = explode(':', $k);
  248. //该位置是否有活动
  249. foreach ($activityRows as $activity) {
  250. $activityPopRange = explode(',', $activity['pop_range']);
  251. if (in_array($position[0], $activityPopRange)) {
  252. if ($activity['show_type'] == 0 && !isset($fields[$k])) {
  253. $fields[$k] = json_encode($activity, JSON_UNESCAPED_UNICODE);
  254. LogService::info("平台活动:key:{$redisKey} position:{$k} val:" . json_encode($activity));
  255. break;
  256. } else {
  257. if ($position[1] == $activity['show_type'] && !isset($fields[$k])) {
  258. LogService::info("平台活动:key:{$redisKey} val:" . json_encode($activity));
  259. $fields[$k] = json_encode($activity, JSON_UNESCAPED_UNICODE);
  260. break;
  261. }
  262. }
  263. }
  264. }
  265. }
  266. //有活动才写入
  267. if ($fields) {
  268. Redis::instance()->del($redisKey);
  269. Redis::instance()->hMSet($redisKey, $fields);
  270. Redis::instance()->expire($redisKey, 180);
  271. }
  272. }
  273. }
  274. /**
  275. * 获取主库的从库配置 从库不存在返回主库
  276. * @return array
  277. */
  278. private function getMainSlaveDbConfig()
  279. {
  280. $hostArr = explode(',', Env::get('database.admin_hostname'));
  281. $portArr = explode(',', Env::get('database.admin_hostport', '3306,3306'));
  282. //默认主库
  283. $mainDbConfig = array_merge(Config::get("database"), ['hostname' => $hostArr[0], 'hostport' => $portArr[0]]);
  284. if (count($hostArr) >= 2) {
  285. //从库
  286. $mainDbConfig = array_merge(Config::get("database"),
  287. ['hostname' => $hostArr[1], 'hostport' => $portArr[1]]);
  288. }
  289. return $mainDbConfig;
  290. }
  291. }