WholeBookOrderData.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2017/11/20
  6. * Time: 下午5:26
  7. */
  8. namespace App\Console\Commands;
  9. use App\Modules\Book\Models\Book;
  10. use App\Modules\Subscribe\Services\BookOrderService;
  11. use App\Modules\User\Services\ReadRecordService;
  12. use DB;
  13. use Illuminate\Console\Command;
  14. use Log;
  15. class WholeBookOrderData extends Command
  16. {
  17. /**
  18. * 执行命令 检查图书封面
  19. *
  20. * The name and signature of the console command.
  21. *
  22. * @var string
  23. */
  24. protected $signature = 'wholeBookOrderData';
  25. /**
  26. * The console command description.
  27. *
  28. * @var string
  29. */
  30. protected $description = '整本书订购的统计';
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. Log::info('wholeBookOrderData start command');
  39. $this->loadWholeBookOrderInfo();
  40. Log::info('wholeBookOrderData end command');
  41. }
  42. function loadWholeBookOrderInfo()
  43. {
  44. $bookArray = [];
  45. $lastCidArray = [];
  46. $pageSize = 10000;
  47. $totalCount = BookOrderService::getCount();
  48. $totalPageCount = Ceil($totalCount / $pageSize); ////总页数
  49. for ($pageIndex = 0; $pageIndex < $totalPageCount; $pageIndex++) {
  50. $startIndex = $pageIndex * $pageSize;
  51. $orderInfo = BookOrderService::getOrderInfos($startIndex, $startIndex + $pageSize);
  52. foreach ($orderInfo as $orderItem) {
  53. $bid = $orderItem->bid;
  54. $readRecod = ReadRecordService::getRecordByUidBid($orderItem->uid, $bid);
  55. if ($readRecod) {
  56. $read_cid = explode($readRecod, '_')[0];
  57. if (isset($lastCidArray[$bid])) {
  58. $last_cid = $lastCidArray[$bid];
  59. } else {
  60. $cidInfo = Book::where('id', $bid)->select('last_cid')->first();
  61. $last_cid = $cidInfo->last_cid;
  62. $lastCidArray[$bid] = $last_cid;
  63. }
  64. if ($read_cid == $last_cid) {
  65. $bookArray[$bid] = isset($bookArray[$bid]) ? ($bookArray[$bid] + 1) : 1;
  66. }
  67. }
  68. }
  69. }
  70. foreach ($bookArray as $bid => $count) {
  71. DB::table('temp_book_order_stats')->where('bid', $bid)->update(['read_end_sub_user_num' => $count]);
  72. }
  73. }
  74. }