OrdersCollectUpdate.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2019/9/12
  6. * Time: 下午1:44
  7. */
  8. namespace app\admin\command;
  9. use app\admin\service\LogService;
  10. use app\main\constants\OrderContents;
  11. use app\main\service\OrdersCollectUpdateService;
  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 OrdersCollectUpdate extends BaseCommand
  18. {
  19. protected function configure()
  20. {
  21. $this->setName('OrdersCollectUpdate')
  22. ->addOption('current_time', 'c', Option::VALUE_OPTIONAL, '时间节点:2019-08-11', null)
  23. ->addOption('type', 't', Option::VALUE_OPTIONAL, '执行类型', 0)
  24. ->setDescription('订单按月和汇总统计');
  25. }
  26. protected 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. if (!$current_time) {
  34. LogService::error('初始化未提供开始参数');
  35. echo '初始化未提供开始参数';
  36. exit;
  37. }
  38. while ($current_time < $end_time) {
  39. $this->start($current_time);
  40. $current_time += 86400;
  41. }
  42. } else {
  43. $current_time = strtotime($input->getOption('current_time'));
  44. if (!$current_time) {
  45. //默认取昨天
  46. $current_time = time() - 86400;
  47. }
  48. $this->start($current_time);
  49. }
  50. }
  51. /**
  52. * 执行
  53. * @param $end_time
  54. */
  55. public function start($end_time)
  56. {
  57. $msg = 'processing:' . date('Y-m-d H:i:s', $end_time) . "\n";
  58. echo $msg;
  59. LogService::debug($msg);
  60. OrdersCollectUpdateService::instance()->setCurrentTime($end_time);
  61. $flags = [
  62. OrderContents::ORDER_COLLECT_FLAG_SELF,
  63. OrderContents::ORDER_COLLECT_FLAG_CHILD,
  64. OrderContents::ORDER_COLLECT_FLAG_ALL,
  65. ];
  66. $types = [
  67. OrderContents::ORDER_COLLECT_TYPE_MONTH,
  68. OrderContents::ORDER_COLLECT_TYPE_TOTAL,
  69. ];
  70. foreach ($flags as $flag) {
  71. $admin_list = OrdersCollectUpdateService::instance()->getAdminIdsToUpdate($flag)->data;
  72. foreach ($types as $type) {
  73. if ($admin_list) {
  74. array_push($admin_list, 0);
  75. foreach (array_chunk($admin_list, 100) as $admin_ids) {
  76. try {
  77. $list = OrdersCollectUpdateService::instance()->getOrderCollectData($type, $flag, $admin_ids)->data;
  78. OrdersCollectUpdateService::instance()->insertOrUpdateCollect($list);
  79. } catch (\Exception $e) {
  80. LogService::exception($e);
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }