小说推广数据系统

ZDOrderList.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Log;
  4. use App\Models\Account;
  5. use App\Models\Order;
  6. use App\Services\HttpService;
  7. use App\Services\ZDService;
  8. use Illuminate\Console\Command;
  9. class ZDOrderList extends Command
  10. {
  11. protected $signature = 'ZDOrderList {year?}';
  12. protected $description = '获取掌读充值订单数据';
  13. protected $year;
  14. protected $page;
  15. protected $channel_id;
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. }
  20. public function handle()
  21. {
  22. \DB::connection()->disableQueryLog();
  23. $this->year = $this->argument('year') ? $this->argument('year') : null;
  24. $this->getOrderList();
  25. }
  26. public function getOrderList()
  27. {
  28. # 获取渠道列表
  29. $channelList = Account::getChannelId(ZDService::PLATFORM_ID);
  30. if (date('Y-m-d H:i:s') <= date('Y-m-d') . ' 00:00:10') {
  31. $date = date('Y-m-d', strtotime('-1 days'));
  32. } else {
  33. $date = date('Y-m-d');
  34. }
  35. foreach ($channelList as $item) {
  36. if (is_null($this->year)) {
  37. $this->order($date, $item);
  38. } else {
  39. for ($month = 1; $month <= 12; $month++) {
  40. $days = getDaysByMonth($this->year, $month);
  41. foreach ($days as $day) {
  42. if ($day > date("Y-m-d"))
  43. continue 2;
  44. $this->order($day, $item);
  45. }
  46. }
  47. }
  48. }
  49. return true;
  50. }
  51. public function order($date, $channelId)
  52. {
  53. try {
  54. $this->info($date);
  55. $this->info($channelId);
  56. $sTime = $date . ' 00:00:00';
  57. $eTime = (($date . ' 23:59:59') > date('Y-m-d H:i:s')) ? date('Y-m-d H:i:s') : $date . ' 23:59:59';
  58. $this->page = 1;
  59. do {
  60. $this->info('当前页码数:'.$this->page);
  61. $timestamps = time();
  62. $params = [
  63. 'vipid' => ZDService::VIP_ID,
  64. 'timestamp' => $timestamps,
  65. 'channelid' => $channelId,
  66. 'starttime' => strtotime($sTime),
  67. 'endtime' => strtotime($eTime),
  68. 'page' => $this->page
  69. ];
  70. $params['sign'] = ZDService::sign($timestamps);
  71. $requestUri = ZDService::API_BASE_URL . ZDService::ORDER_LIST . '?' . http_build_query($params);
  72. # 获取列表
  73. $response = HttpService::httpGet($requestUri);
  74. if($response === false)
  75. Log::logError('掌读充值订单数据获取失败' . $requestUri, $params, 'ZDOrderList');
  76. $responseData = json_decode($response, true);
  77. $pageCount = isset($responseData['data']['pageCount']) ? $responseData['data']['pageCount'] : 0;
  78. $this->info('共有数据'.$pageCount.'页');
  79. $total = 0;
  80. if($pageCount > 0) {
  81. $data = isset($responseData['data']['list']) ? $responseData['data']['list'] : [];
  82. $platformId = ZDService::PLATFORM_ID;
  83. # 获取渠道信息
  84. $channelList = Account::select(['channel_id', 'app_id'])
  85. ->where('enable', 1)
  86. ->where('platform_id', $platformId)
  87. ->get();
  88. $appIdInfo = $channelList->where('channel_id', $channelId)->first();
  89. $appId = isset($appIdInfo->app_id) ? $appIdInfo->app_id : null;
  90. $total = count($data);
  91. foreach ($data as $item) {
  92. $insertData = [
  93. 'platform_id' => $platformId,
  94. 'channel_id' => $channelId,
  95. 'app_id' => $appId,
  96. 'order_id' => $item['orderno'],
  97. 'border_id' => $item['id'],
  98. 'member_openid' => $item['openid'],
  99. 'subscribed_at' => null,
  100. 'user_created_at' => date("Y-m-d H:i:s", $item['regtime']),
  101. 'product' => null,
  102. 'price' => $item['amount'] * 100,
  103. 'status' => $item['status'],
  104. 'agent_uid' => null,
  105. 'from_novel' => null,
  106. 'referral_link_id' => null,
  107. 'from_novel_id' => $item['book_entry'],
  108. 'platform_created_at' => date('Y-m-d H:i:s', $item['ctime']),
  109. 'paid_at' => !empty($item['ctime']) ? date('Y-m-d H:i:s', $item['ctime']) : null,
  110. ];
  111. # 创建或更新
  112. Order::updateOrCreate(
  113. ['platform_id'=>$platformId, 'order_id'=>$insertData['order_id'], 'channel_id'=>$channelId], $insertData
  114. );
  115. }
  116. $this->page++;
  117. } else {
  118. if(isset($responseData['err']) && $responseData['err'] != 0) {
  119. Log::logError('掌读订单信息获取失败', [
  120. 'response' => $responseData,
  121. 'params' => $params
  122. ], 'ZDOrderList');
  123. }
  124. }
  125. $this->info('本次共同步账号'.$total.'个');
  126. } while($pageCount >= $this->page && $pageCount);
  127. } catch (\Exception $e) {
  128. Log::logError('掌中云订单数据同步失败: '.$e->getMessage(), [], 'ZDOrderList');
  129. }
  130. return true;
  131. }
  132. }