BookGiftStatsByBook.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: songdb
  5. * Date: 2017/12/26
  6. * Time: 下午5:26
  7. */
  8. namespace App\Console\Commands\BookGiftStats;
  9. use App\Modules\BookGifts\Services\BookGiftsStatsByBookService;
  10. use Log;
  11. use DB;
  12. use Illuminate\Console\Command;
  13. class BookGiftStatsByBook extends Command
  14. {
  15. /**
  16. * 执行命令 php artisan generate_order_day_stat
  17. *
  18. * The name and signature of the console command.
  19. *
  20. * @var string
  21. */
  22. protected $signature = 'BookGiftDailyStatsByBook';
  23. /**
  24. * The console command description.
  25. *
  26. * @var string
  27. */
  28. protected $description = '送礼记录数据按书统计';
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. print_r("======送礼记录数据按书统计 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  37. Log::info("======送礼记录数据按书统计 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  38. $start = date('Y-m-d 00:00:00',strtotime('-1 day'));
  39. $end = date('Y-m-d 23:59:59',strtotime('-1 day'));
  40. $res = DB::select("SELECT bid,name,count(book_gifts_send.id) as send_times,COUNT(DISTINCT book_gifts_send.uid) as send_user_num,sum(book_gifts_send.cost) as cost_sum
  41. FROM book_gifts_send
  42. LEFT JOIN books ON books.id=book_gifts_send.bid
  43. WHERE book_gifts_send.created_at BETWEEN '{$start}' AND '{$end}'
  44. GROUP BY bid ");
  45. foreach ($res as $item){
  46. try{
  47. $data_to_add = array(
  48. 'date'=>date('Y-m-d'),
  49. 'bid'=>$item->bid,
  50. 'book_name'=>$item->name,
  51. 'gift_send_times'=>$item->send_times,
  52. 'gift_send_user_num'=>$item->send_user_num,
  53. 'gift_send_cost'=>$item->cost_sum,
  54. );
  55. BookGiftsStatsByBookService::addBookStats($data_to_add);
  56. }catch (\Exception $e){
  57. \Log::error('add Book Gifts Stats Failed:'.$e->getMessage());
  58. }
  59. }
  60. Log::info("======送礼记录数据按书统计 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
  61. print_r("======送礼记录数据按书统计 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
  62. }
  63. }