BookGiftStatsByGift.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 DB;
  10. use App\Modules\BookGifts\Services\BookGiftsStatsByGiftService;
  11. use Log;
  12. use Illuminate\Console\Command;
  13. class BookGiftStatsByGift 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 = 'BookGiftDailyStatsByGift';
  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. \Log::info('start:'.$start);
  41. \Log::info('end:'.$end);
  42. $res = DB::select("SELECT gift_id,book_gifts.name_desc,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
  43. FROM book_gifts_send
  44. LEFT JOIN book_gifts ON book_gifts.id=book_gifts_send.gift_id
  45. WHERE book_gifts_send.created_at BETWEEN '{$start}' AND '{$end}'
  46. GROUP BY gift_id");
  47. foreach ($res as $item){
  48. try{
  49. $data_to_add = array(
  50. 'date'=>date('Y-m-d'),
  51. 'gift_id'=>$item->gift_id,
  52. 'gift_name'=>$item->name_desc,
  53. 'gift_send_times'=>$item->send_times,
  54. 'gift_send_user_num'=>$item->send_user_num,
  55. 'gift_send_cost'=>$item->cost_sum,
  56. );
  57. BookGiftsStatsByGiftService::addGiftStats($data_to_add);
  58. }catch (\Exception $e){
  59. \Log::error('add Book Gifts Stats Failed2:'.$e->getMessage());
  60. }
  61. }
  62. Log::info("======送礼记录数据礼物统计 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
  63. print_r("======送礼记录数据按礼物统计 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
  64. }
  65. }