123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- /**
- * Created by PhpStorm.
- * User: songdb
- * Date: 2017/12/26
- * Time: 下午5:26
- */
- namespace App\Console\Commands\BookGiftStats;
- use App\Modules\BookGifts\Services\BookGiftsStatsByBookService;
- use Log;
- use DB;
- use Illuminate\Console\Command;
- class BookGiftStatsByBook extends Command
- {
- /**
- * 执行命令 php artisan generate_order_day_stat
- *
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'BookGiftDailyStatsByBook';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '送礼记录数据按书统计';
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- print_r("======送礼记录数据按书统计 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
- Log::info("======送礼记录数据按书统计 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
- $start = date('Y-m-d 00:00:00',strtotime('-1 day'));
- $end = date('Y-m-d 23:59:59',strtotime('-1 day'));
- $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
- FROM book_gifts_send
- LEFT JOIN books ON books.id=book_gifts_send.bid
- WHERE book_gifts_send.created_at BETWEEN '{$start}' AND '{$end}'
- GROUP BY bid ");
- foreach ($res as $item){
- try{
- $data_to_add = array(
- 'date'=>date('Y-m-d'),
- 'bid'=>$item->bid,
- 'book_name'=>$item->name,
- 'gift_send_times'=>$item->send_times,
- 'gift_send_user_num'=>$item->send_user_num,
- 'gift_send_cost'=>$item->cost_sum,
- );
- BookGiftsStatsByBookService::addBookStats($data_to_add);
- }catch (\Exception $e){
- \Log::error('add Book Gifts Stats Failed:'.$e->getMessage());
- }
- }
- Log::info("======送礼记录数据按书统计 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
- print_r("======送礼记录数据按书统计 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
- }
- }
|