BookTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Modules\Book\Services\BookSubscribleChapterService;
  4. use App\Modules\Subscribe\Services\ChapterOrderService;
  5. use App\Modules\User\Services\ReadRecordService;
  6. use App\Modules\YunQi\Models\BookUser;
  7. use App\Modules\YunQi\Models\YqUserBidRelation;
  8. use App\Modules\YunQi\Models\YqZsyTest;
  9. use App\Modules\YunQi\Services\YqZsyTestService;
  10. use GuzzleHttp\Client;
  11. use Illuminate\Console\Command;
  12. use Log;
  13. use DB;
  14. use Redis;
  15. use App\Modules\Book\Models\Book;
  16. use App\Modules\Book\Models\Chapter;
  17. use App\Modules\SendOrder\Models\SendOrder;
  18. use App\Modules\SendOrder\Services\SendOrderService;
  19. use App\Modules\Subscribe\Models\Order;
  20. use App\Jobs\SendStatisticsList;
  21. use App\Modules\OfficialAccount\Models\ForceSubscribeUsers;
  22. use App\Modules\User\Services\UserSignService;
  23. use App\Modules\Book\Models\BookUvStat;
  24. use App\Modules\Subscribe\Models\ChapterOrder;
  25. class BookTest extends Command
  26. {
  27. /**
  28. * The name and signature of the console command.
  29. *
  30. * @var string
  31. */
  32. protected $signature = 'book:test
  33. {--bid= : the id of a book}';
  34. /**
  35. * The console command description.
  36. *
  37. * @var string
  38. */
  39. protected $client;
  40. protected $description = 'Command description';
  41. /**
  42. * Create a new command instance.
  43. *
  44. * @return void
  45. */
  46. public function __construct()
  47. {
  48. parent::__construct();
  49. $this->client = new Client(['timeout' => 8.0, 'allow_redirects' => true]);
  50. }
  51. /**
  52. * Execute the console command.
  53. *
  54. * @return mixed
  55. */
  56. public function handle()
  57. {
  58. //$this->data1();
  59. //$this->testTest();
  60. //$this->activity();
  61. //$this->testBookSmartPushStats();
  62. //$this->sendOrderStats105();
  63. //$this->you2();
  64. $this->transfromUserOrder();
  65. }
  66. public function activity()
  67. {
  68. $res = \App\Modules\Activity\Services\ActivityService::ActivityStatsI(16);
  69. $str = "日期,site id,活动页面访问uv,按钮uv,生成订单数,成功支付订单数,首充用户数,点击阅读页面文字链uv,阅读页面文字链订单数,阅读页面文字链成功订单数,阅读页面文字链首充用户数,签到回调文字链uv,签到回调文字链生成订单数,签到回调文字链成功成功订单数,签到回调文字链首充用户数\r\n";
  70. $str = mb_convert_encoding($str, 'gbk');
  71. $file_name = date('Y-m-d') . '.csv';
  72. file_put_contents($file_name, $str);
  73. $temp = '';
  74. foreach ($res as $val) {
  75. $temp .= "{$val['day']},{$val['siteid']},{$val['uv']},{$val['button_uv']},{$val['order_num']},{$val['success_order_num']},{$val['first_charge_num']},{$val['reader_uv']},{$val['reader_order']},{$val['reader_success_order']},{$val['reader_first_charge']},{$val['signcallback_uv']},{$val['signcallback_order']},{$val['signcallback_success_order']},{$val['signcallback_first_charge']}\r\n";
  76. }
  77. file_put_contents($file_name, $temp, FILE_APPEND);
  78. }
  79. public function data1(){
  80. //1,图书uv
  81. $keys = Redis::SMEMBERS('book_before_five_uvs');
  82. if(!$keys){
  83. return 0;
  84. }
  85. $data = [];
  86. $book_name_arr = [];
  87. $i = 0;
  88. foreach ($keys as $k){
  89. $k_array = explode('_',$k);
  90. $uv = Redis::scard('book_before_five_uv_'.$k);
  91. if(isset($book_name_arr[$k_array[1]]) && !empty($book_name_arr[$k_array[1]])){
  92. $book_name = $book_name_arr[$k_array[1]];
  93. }else{
  94. $book_name_info = DB::table('book_configs')->where('bid',$k_array[1])->select('book_name')->first();
  95. if($book_name_info){
  96. $book_name_arr[$k_array[1]] = $book_name_info->book_name;
  97. $book_name = $book_name_info->book_name;
  98. }else{
  99. $book_name = '未知';
  100. }
  101. }
  102. $data[] = [
  103. 'bid'=>$k_array[1],
  104. 'day'=>$k_array[0],
  105. 'sequence'=>$k_array[2],
  106. 'book_name'=>$book_name,
  107. 'uv'=>$uv,
  108. 'created_at'=>date('Y-m-d H:i:s'),
  109. 'updated_at'=>date('Y-m-d H:i:s')
  110. ];
  111. $i++;
  112. if($i && $i%200 == 0){
  113. DB::table('book_five_chapter_uv')->insert($data);
  114. $data = [];
  115. }
  116. }
  117. DB::table('book_five_chapter_uv')->insert($data);
  118. }
  119. public function data2(){
  120. //强关
  121. $keys = Redis::SMEMBERS('subscribe_page_uv_send_order_ids');
  122. $data = [];
  123. $i = 0;
  124. foreach ($keys as $k){
  125. $subscribe_uv = Redis::scard('subscribe_page_uv'.$k);
  126. $data[] = [
  127. 'send_order_id'=>$k,
  128. 'subscribe_uv'=>$subscribe_uv,
  129. 'pay_uv'=>0,
  130. 'created_at'=>date('Y-m-d H:i:s'),
  131. 'updated_at'=>date('Y-m-d H:i:s')
  132. ];
  133. $i++;
  134. if($i && $i%200 == 0){
  135. DB::table('subscribe_pay_page_uv')->insert($data);
  136. $data = [];
  137. }
  138. }
  139. $keys = null;
  140. $k = null;
  141. DB::table('subscribe_pay_page_uv')->insert($data);
  142. $data = [];
  143. //支付
  144. $keys = Redis::SMEMBERS('pay_page_uv_send_order_ids');
  145. foreach ($keys as $v){
  146. $pay_uv = Redis::scard('pay_page_uv'.$v);
  147. $o = DB::table('subscribe_pay_page_uv')->where('send_order_id',$v)->first();
  148. if($o){
  149. DB::table('subscribe_pay_page_uv')->where('send_order_id',$v)->update([
  150. 'pay_uv'=>$pay_uv
  151. ]);
  152. }else{
  153. DB::table('subscribe_pay_page_uv')->insert([
  154. 'send_order_id'=>$v,
  155. 'subscribe_uv'=>0,
  156. 'pay_uv'=>$pay_uv,
  157. 'created_at'=>date('Y-m-d H:i:s'),
  158. 'updated_at'=>date('Y-m-d H:i:s')
  159. ]);
  160. }
  161. }
  162. }
  163. public function testTest(){
  164. $res = \App\Modules\Statistic\Services\WapVisitStatService::smartPushTestBookStats(6);
  165. echo \Hashids::encode(6).PHP_EOL;
  166. print_r($res);
  167. }
  168. private function testBookSmartPushStats(){
  169. $sql1 = 'TRUNCATE test_book_smart_push_stats';
  170. DB::update($sql1);
  171. $result = DB::table('book_configs')->whereIn('test_status',[1,2])->select('bid','vip_seq')->get();
  172. /*foreach ($result as $v){
  173. t($v->bid,$v->vip_seq);
  174. }*/
  175. function t($bid,$vip_seq){
  176. $data = [];
  177. $key1 = 'smartPushTestBookPayPageUv:bid:%s';
  178. $pay_page_uv = (int)(Redis::scard(sprintf($key1,$bid)));
  179. $key2 = 'smartPushTestBookPaidUv:bid:%s';
  180. $paid_num = (int)(Redis::scard(sprintf($key2,$bid)));
  181. $amount = Redis::Hget('smartPushTestBookPaidAmount',$bid);
  182. if(!$amount)
  183. $amount = 0;
  184. for ($i = 1;$i<=60;$i++){
  185. $key3 = 'smartPushTestBookChapterUv:bid:%s:seq:%s';
  186. $sequence_uv = (int)(Redis::scard(sprintf($key3,$bid,$i)));
  187. if(!$sequence_uv){
  188. continue;
  189. }
  190. $data[] = [
  191. 'bid'=>$bid,
  192. 'sequence'=>$i,
  193. 'sequence_uv'=>$sequence_uv,
  194. 'vip_sequence'=>$vip_seq,
  195. 'pay_page_uv'=>$pay_page_uv,
  196. 'paid_user_num'=>$paid_num,
  197. 'amount'=>$amount,
  198. 'created_at'=>date('Y-m-d H:i:s'),
  199. 'updated_at'=>date('Y-m-d H:i:s')
  200. ];
  201. }
  202. if($data){
  203. DB::table('test_book_smart_push_stats')->insert($data);
  204. }
  205. }
  206. foreach ($result as $v){
  207. t($v->bid,$v->vip_seq);
  208. }
  209. }
  210. private function sendOrderStats105(){
  211. $sql1 = 'TRUNCATE send_order_stats_105';
  212. DB::update($sql1);
  213. $send_order_ids = Redis::hgetall('sendOrderIdToBid');
  214. if(!$send_order_ids){
  215. return ;
  216. }
  217. function one(int $sendOrderId,int $time){
  218. $info = SendOrderService::getById($sendOrderId);
  219. if(!$info || !$info->book_id){
  220. return ;
  221. }
  222. $subscribe_chaper_seq = 0;
  223. if($info && $info->subscribe_chapter_seq){
  224. $subscribe_chaper_seq = $info->subscribe_chapter_seq;
  225. }
  226. if(!$subscribe_chaper_seq){
  227. $subscribe = BookSubscribleChapterService::getSubcribleChapter($info->book_id,$info->distribution_channel_id);
  228. if ($subscribe) {
  229. $subscribe_chaper_seq = (isset($subscribe->subscribe_chapter_id) && $subscribe->subscribe_chapter_id > 0) ? $subscribe->subscribe_chapter_id : 0;
  230. }
  231. }
  232. $key_1 = 'accurateSendOrderSubscribePageUv:bid:%s:sendorderid:'.$sendOrderId;
  233. //强关页面uv
  234. $subscribe_uv = (int)(Redis::scard(sprintf($key_1,$info->book_id)));
  235. $key_2 = 'accurateSendOrderPayPageUv:bid:%s:send_order_id:'.$sendOrderId;
  236. //付费页面UV
  237. $pay_page_uv = (int)(Redis::scard(sprintf($key_2,$info->book_id)));
  238. //强关数
  239. $subscribe_num = DB::table('force_subscribe_users')
  240. ->where('send_order_id',$sendOrderId)
  241. ->where('bid',$info->book_id)
  242. ->where('created_at','>=',date('Y-m-d H:i:s',$time))
  243. ->count();
  244. $paid_user_num = DB::table('orders')
  245. ->where('send_order_id',$sendOrderId)
  246. ->where('from_bid',$info->book_id)
  247. ->where('status','PAID')
  248. ->where('created_at','>=',date('Y-m-d H:i:s',$time))
  249. ->count();
  250. $book_info = DB::table('book_configs')->where('bid',$info->book_id)->select('book_name','force_subscribe_chapter_seq','vip_seq')->first();
  251. $send_order_chapter_sequence_info = DB::table('chapters')->where('id',$info->chapter_id)->select('sequence')->first();
  252. $send_order_chapter_sequence = 0;
  253. if($send_order_chapter_sequence_info && $send_order_chapter_sequence_info->sequence){
  254. $send_order_chapter_sequence = $send_order_chapter_sequence_info->sequence;
  255. }
  256. if(!$subscribe_chaper_seq){
  257. $subscribe_chaper_seq = $book_info->force_subscribe_chapter_seq;
  258. }
  259. $vip_seq = 0;
  260. $vip_seq_info = Chapter::where('bid',$info->book_id)->where('is_vip',1)->orderBy('sequence')->select('sequence')->first();
  261. if($vip_seq_info){
  262. $vip_seq = $vip_seq_info->sequence;
  263. }
  264. $data = [];
  265. for ($i = 1;$i<=30;$i++){
  266. $key_3 = 'accurateSendOrderChapterUv:bid:%s:seq:%s:sendorderid:'.$sendOrderId;
  267. $chapter_seq_uv = (int)(Redis::scard(sprintf($key_3,$info->book_id,$i)));
  268. if(!$chapter_seq_uv){
  269. continue;
  270. }
  271. $data[] = [
  272. 'send_order_id'=>$sendOrderId,
  273. 'send_order_chapter_seq'=>$send_order_chapter_sequence,
  274. 'bid'=>$info->book_id,
  275. 'book_name'=>$book_info->book_name,
  276. 'chaper_sequence'=>$i,
  277. 'chapter_uv'=>$chapter_seq_uv,
  278. 'subscribe_chaper_seq'=>$subscribe_chaper_seq,
  279. 'subscribe_page_uv'=>$subscribe_uv,
  280. 'subscribe_user_num'=>$subscribe_num,
  281. 'vip_chapter_sequence'=>$vip_seq,
  282. 'pay_page_uv'=>$pay_page_uv,
  283. 'paid_user_num'=>$paid_user_num,
  284. 'created_at'=>date('Y-m-d H:i:s'),
  285. 'updated_at'=>date('Y-m-d H:i:s')
  286. ];
  287. }
  288. if($data){
  289. DB::table('send_order_stats_105')->insert($data);
  290. }
  291. }
  292. foreach ($send_order_ids as $send_order_id=> $v){
  293. $array = explode('-',$v);
  294. if(count($array) == 2){
  295. one($send_order_id,$array[1]);
  296. }
  297. }
  298. }
  299. private function you2(){
  300. $sql1 = 'TRUNCATE yq_stats2';
  301. DB::update($sql1);
  302. $info = BookUser::where('type','ENABLE')->select('bid','uid','type','updated_at')
  303. ->orderBy('updated_at')->limit(1500)->get();
  304. $info2 = BookUser::where('type','GROUP_1')
  305. ->select('bid','uid','type','updated_at')->orderBy('updated_at')->limit(1500)->get();
  306. $this->you2Type($info,'GROUP_2');
  307. $this->you2Type($info2,'GROUP_1');
  308. }
  309. private function you2Type($info,$type){
  310. $result = [];
  311. $chapter_model = new ChapterOrder();
  312. foreach ($info as $item){
  313. $to_bids = YqUserBidRelation::where('uid',$item->uid)->where('to_bid','>',0)->select('to_bid','created_at')->get();
  314. $from_bids = YqUserBidRelation::where('uid',$item->uid)->where('from_bid','>',0)->select('from_bid','created_at')->get();
  315. $chapter_model->setCurrentTable($item->uid);
  316. if($to_bids){
  317. foreach ($to_bids as $v){
  318. $to_bid = $v->to_bid;
  319. $time = $v->created_at->format('Y-m-d H:i:s');
  320. if(!$to_bid) continue;
  321. $fee = $chapter_model->where('uid',$item->uid)
  322. ->where('bid',$to_bid)
  323. ->where('created_at','>=',$time)
  324. ->sum('fee');
  325. $amount = Order::where('uid',$item->uid)
  326. ->where('from_bid',$to_bid)
  327. ->where('created_at','>=',$time)
  328. ->where('status','PAID')
  329. ->sum('price');
  330. $result[] = [
  331. 'bid'=>$to_bid,
  332. 'uid'=>$item->uid,
  333. 'fee'=>$fee,
  334. 'amount'=>$amount,
  335. 'start_time'=>$time,
  336. 'type'=>'to',
  337. 'group'=>$type,
  338. 'created_at'=>date('Y-m-d H:i:s'),
  339. 'updated_at'=>date('Y-m-d H:i:s')
  340. ];
  341. }
  342. if($result){
  343. DB::table('yq_stats2')->insert($result);
  344. $result = [];
  345. }
  346. }
  347. $to_bid = null;
  348. if($from_bids){
  349. foreach ($from_bids as $from_bid){
  350. $bid = $from_bid->from_bid;
  351. $test = YqZsyTestService::getByBid($bid);
  352. if($test) continue;
  353. $time = $from_bid->created_at->format('Y-m-d H:i:s');
  354. $fee = $chapter_model->where('uid',$item->uid)
  355. ->where('bid',$bid)
  356. ->where('created_at','>=',$time)
  357. ->sum('fee');
  358. $amount = Order::where('uid',$item->uid)
  359. ->where('from_bid',$bid)
  360. ->where('created_at','>=',$time)
  361. ->where('status','PAID')
  362. ->sum('price');
  363. $result[] = [
  364. 'bid'=>$bid,
  365. 'uid'=>$item->uid,
  366. 'fee'=>$fee,
  367. 'amount'=>$amount,
  368. 'start_time'=>$time,
  369. 'type'=>'from',
  370. 'group'=>$type,
  371. 'created_at'=>date('Y-m-d H:i:s'),
  372. 'updated_at'=>date('Y-m-d H:i:s')
  373. ];
  374. }
  375. if($result){
  376. DB::table('yq_stats2')->insert($result);
  377. $result = [];
  378. }
  379. }
  380. }
  381. }
  382. private function transfromUserOrder(){
  383. $user = DB::table('users')
  384. ->where('distribution_channel_id',4815)
  385. ->where('created_at','>=','2019-01-31 15:00:00')
  386. ->select('openid','id')
  387. ->get();
  388. $chapter_model = new ChapterOrder();
  389. foreach ($user as $v){
  390. Log::info('now user is :');
  391. Log::info('openid is: '.$v->openid.',old uid is :'.$v->id);
  392. $froce_user = DB::table('force_subscribe_users')
  393. ->where('openid',$v->openid)
  394. ->where('distribution_channel_id',384)
  395. ->select('uid')
  396. ->first();
  397. if(!$froce_user){
  398. continue;
  399. }
  400. Log::info('$froce_user user is :');
  401. Log::info('$froce_user is: '.$froce_user->uid);
  402. if($froce_user && $froce_user->uid != $v->id){
  403. $chapter_model->setCurrentTable($froce_user->uid);
  404. $data = [];
  405. //章节订购
  406. $chapter_order_info = $chapter_model->where('uid',$froce_user->uid)->get();
  407. Log::info('$chapter_order_info user is :');
  408. Log::info($chapter_order_info);
  409. if($chapter_order_info){
  410. foreach ($chapter_order_info as $chapter_order_item){
  411. $data = [
  412. 'distribution_channel_id'=>4815,
  413. 'bid'=>$chapter_order_item->bid,
  414. 'cid'=>$chapter_order_item->cid,
  415. 'chapter_name'=>$chapter_order_item->chapter_name,
  416. 'book_name'=>$chapter_order_item->book_name,
  417. 'uid'=>$v->id,
  418. 'send_order_id'=>0,
  419. 'fee'=>0,
  420. 'created_at'=>$chapter_order_item->created_at,
  421. 'updated_at'=>date('Y-m-d H:i:s'),
  422. 'charge_balance'=>0,
  423. 'reward_balance'=>0
  424. ];
  425. $chapter_model->setCurrentTable($v->id);
  426. $old = $chapter_model->where('uid',$v->id)
  427. ->where('bid',$chapter_order_item->bid)
  428. ->where('cid',$chapter_order_item->cid)
  429. ->select('id')->first();
  430. if(!$old){
  431. $chapter_model->insert($data);
  432. }
  433. }
  434. }
  435. /*if($data){
  436. Log::info('$chapter_order_info user $data is :');
  437. Log::info($data);
  438. $chapter_model->setCurrentTable($v->id);
  439. $chapter_model->insert($data);
  440. }*/
  441. //单本订购
  442. if($froce_user && $froce_user->uid != $v->id){
  443. $book_order_data = [];
  444. $book_orders = DB::table('book_orders')->where('uid',$froce_user->uid)->get();
  445. Log::info('$book_orders user is :');
  446. if($book_orders){
  447. foreach ($book_orders as $book_order_item){
  448. $book_order_data[] = [
  449. 'uid'=>$v->id,
  450. 'distribution_channel_id'=>4815,
  451. 'bid'=>$book_order_item->bid,
  452. 'book_name'=>$book_order_item->book_name,
  453. 'u'=>$book_order_item->u,
  454. 'send_order_id'=>0,
  455. 'charge_balance'=>0,
  456. 'reward_balance'=>0,
  457. 'fee'=>0,
  458. 'created_at'=>$book_order_item->created_at,
  459. 'updated_at'=>date('Y-m-d H:i:s'),
  460. ];
  461. }
  462. }
  463. if($book_order_data){
  464. Log::info('$book_order_data user is :');
  465. Log::info($book_order_data);
  466. foreach ($book_order_data as $book_o ){
  467. $old = DB::table('book_orders')
  468. ->where('uid',$book_o['uid'])
  469. ->where('bid',$book_o['bid'])
  470. ->select('id')->first();
  471. if(!$old){
  472. DB::table('book_orders')->insert($book_order_data);
  473. }
  474. }
  475. }
  476. }
  477. //阅读记录
  478. $read_bids = Redis::hgetall('book_read:' . $froce_user->uid);
  479. Log::info('$read_bids is :');
  480. Log::info($read_bids);
  481. if ($read_bids){
  482. foreach ($read_bids as $key=>$field){
  483. Redis::hset('book_read:' . $v->id,$key,$field);
  484. }
  485. }
  486. //用户信息
  487. $old_user = DB::table('users')->where('id', $froce_user->uid)->first();
  488. if($old_user){
  489. DB::table('users')->where('id',$v->id)->update([
  490. 'balance'=>$old_user->balance,
  491. 'charge_balance'=>$old_user->charge_balance,
  492. 'reward_balance'=>$old_user->reward_balance,
  493. ]);
  494. }
  495. //年费
  496. $year = DB::table('year_orders')->where('uid',$froce_user->uid)->orderBy('id','desc')->first();
  497. if($year){
  498. $old_year = DB::table('year_orders')->where('uid',$v->id)->orderBy('id','desc')->first();
  499. if(!$old_year){
  500. DB::table('year_orders')->insert([[
  501. 'uid'=>$v->id,
  502. 'begin_time'=>$year->begin_time,
  503. 'end_time'=>$year->end_time,
  504. 'distribution_channel_id'=>4815,
  505. 'created_at'=>$year->created_at,
  506. 'updated_at'=>date('Y-m-d H:i:s'),
  507. 'fee'=>0,
  508. ]]);
  509. }
  510. }
  511. }
  512. }
  513. }
  514. }