UserCollectUpdate.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2019/9/16
  6. * Time: 下午4:58
  7. */
  8. namespace app\admin\command;
  9. use app\admin\service\LogService;
  10. use app\main\constants\UserConstants;
  11. use app\main\service\UserCollectUpdateService;
  12. use think\console\Command;
  13. use think\console\Input;
  14. use think\console\input\Option;
  15. use think\console\Output;
  16. use think\Request;
  17. class UserCollectUpdate extends BaseCommand
  18. {
  19. public function configure()
  20. {
  21. $this->setName('UserCollectUpdate')
  22. ->addOption('current_time', 'c', Option::VALUE_OPTIONAL, '时间节点:2019-08-11', null)
  23. ->addOption('type', 't', Option::VALUE_OPTIONAL, '执行类型', 0)
  24. ->setDescription('用户按月和汇总统计');
  25. }
  26. public function execute(Input $input, Output $output)
  27. {
  28. Request::instance()->module('admin');
  29. //初始化
  30. if ($input->getOption('type')) {
  31. $end_time = time() - 86400;
  32. $current_time = strtotime($input->getOption('current_time'));
  33. while ($current_time < $end_time) {
  34. $this->start($current_time);
  35. $current_time += 86400;
  36. }
  37. } else {
  38. $current_time = strtotime($input->getOption('current_time'));
  39. if (!$current_time) {
  40. //默认取昨天
  41. $current_time = time() - 86400;
  42. }
  43. $this->start($current_time);
  44. }
  45. }
  46. public function start($current_time)
  47. {
  48. $msg = 'processing:'.date('Y-m-d H:i:s', $current_time) . "\n";
  49. echo $msg;
  50. LogService::debug($msg);
  51. UserCollectUpdateService::instance()->setCurrentTime($current_time);
  52. $types = [
  53. UserConstants::USER_COLLECT_TYPE_MONTH,
  54. UserConstants::USER_COLLECT_TYPE_TOTAL,
  55. ];
  56. $admin_list = UserCollectUpdateService::instance()->getAdminIdsToUpdate()->data;
  57. foreach ($types as $type) {
  58. if ($admin_list) {
  59. array_push($admin_list, 0);
  60. foreach (array_chunk($admin_list, 100) as $admin_ids) {
  61. try {
  62. $list = UserCollectUpdateService::instance()->getUserCollectData($type, $admin_ids)->data;
  63. UserCollectUpdateService::instance()->insertOrUpdateCollect($list);
  64. } catch (\Exception $e) {
  65. LogService::exception($e);
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }