1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2019/9/16
- * Time: 下午4:58
- */
- namespace app\admin\command;
- use app\admin\service\LogService;
- use app\main\constants\UserConstants;
- use app\main\service\UserCollectUpdateService;
- use think\console\Command;
- use think\console\Input;
- use think\console\input\Option;
- use think\console\Output;
- use think\Request;
- class UserCollectUpdate extends BaseCommand
- {
- public function configure()
- {
- $this->setName('UserCollectUpdate')
- ->addOption('current_time', 'c', Option::VALUE_OPTIONAL, '时间节点:2019-08-11', null)
- ->addOption('type', 't', Option::VALUE_OPTIONAL, '执行类型', 0)
- ->setDescription('用户按月和汇总统计');
- }
- public function execute(Input $input, Output $output)
- {
- Request::instance()->module('admin');
- //初始化
- if ($input->getOption('type')) {
- $end_time = time() - 86400;
- $current_time = strtotime($input->getOption('current_time'));
- while ($current_time < $end_time) {
- $this->start($current_time);
- $current_time += 86400;
- }
- } else {
- $current_time = strtotime($input->getOption('current_time'));
- if (!$current_time) {
- //默认取昨天
- $current_time = time() - 86400;
- }
- $this->start($current_time);
- }
- }
- public function start($current_time)
- {
- $msg = 'processing:'.date('Y-m-d H:i:s', $current_time) . "\n";
- echo $msg;
- LogService::debug($msg);
- UserCollectUpdateService::instance()->setCurrentTime($current_time);
- $types = [
- UserConstants::USER_COLLECT_TYPE_MONTH,
- UserConstants::USER_COLLECT_TYPE_TOTAL,
- ];
- $admin_list = UserCollectUpdateService::instance()->getAdminIdsToUpdate()->data;
- foreach ($types as $type) {
- if ($admin_list) {
- array_push($admin_list, 0);
- foreach (array_chunk($admin_list, 100) as $admin_ids) {
- try {
- $list = UserCollectUpdateService::instance()->getUserCollectData($type, $admin_ids)->data;
- UserCollectUpdateService::instance()->insertOrUpdateCollect($list);
- } catch (\Exception $e) {
- LogService::exception($e);
- }
- }
- }
- }
- }
- }
|