123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- namespace App\Console\Commands;
- use App\Log;
- use App\Models\Account;
- use App\Models\Order;
- use App\Services\HttpService;
- use App\Services\ZDService;
- use Illuminate\Console\Command;
- class ZDOrderList extends Command
- {
- protected $signature = 'ZDOrderList {year?}';
- protected $description = '获取掌读充值订单数据';
- protected $year;
- protected $page;
- protected $channel_id;
- public function __construct()
- {
- parent::__construct();
- }
- public function handle()
- {
- \DB::connection()->disableQueryLog();
- $this->year = $this->argument('year') ? $this->argument('year') : null;
- $this->getOrderList();
- }
- public function getOrderList()
- {
- # 获取渠道列表
- $channelList = Account::getChannelId(ZDService::PLATFORM_ID);
- if (date('Y-m-d H:i:s') <= date('Y-m-d') . ' 00:00:10') {
- $date = date('Y-m-d', strtotime('-1 days'));
- } else {
- $date = date('Y-m-d');
- }
- foreach ($channelList as $item) {
- if (is_null($this->year)) {
- $this->order($date, $item);
- } else {
- for ($month = 1; $month <= 12; $month++) {
- $days = getDaysByMonth($this->year, $month);
- foreach ($days as $day) {
- if ($day > date("Y-m-d"))
- continue 2;
- $this->order($day, $item);
- }
- }
- }
- }
- return true;
- }
- public function order($date, $channelId)
- {
- try {
- $this->info($date);
- $this->info($channelId);
- $sTime = $date . ' 00:00:00';
- $eTime = (($date . ' 23:59:59') > date('Y-m-d H:i:s')) ? date('Y-m-d H:i:s') : $date . ' 23:59:59';
- $this->page = 1;
- do {
- $this->info('当前页码数:'.$this->page);
- $timestamps = time();
- $params = [
- 'vipid' => ZDService::VIP_ID,
- 'timestamp' => $timestamps,
- 'channelid' => $channelId,
- 'starttime' => strtotime($sTime),
- 'endtime' => strtotime($eTime),
- 'page' => $this->page
- ];
- $params['sign'] = ZDService::sign($timestamps);
- $requestUri = ZDService::API_BASE_URL . ZDService::ORDER_LIST . '?' . http_build_query($params);
- # 获取列表
- $response = HttpService::httpGet($requestUri);
- if($response === false)
- Log::logError('掌读充值订单数据获取失败' . $requestUri, $params, 'ZDOrderList');
- $responseData = json_decode($response, true);
- $pageCount = isset($responseData['data']['pageCount']) ? $responseData['data']['pageCount'] : 0;
- $this->info('共有数据'.$pageCount.'页');
- $total = 0;
- if($pageCount > 0) {
- $data = isset($responseData['data']['list']) ? $responseData['data']['list'] : [];
- $platformId = ZDService::PLATFORM_ID;
- # 获取渠道信息
- $channelList = Account::select(['channel_id', 'app_id'])
- ->where('enable', 1)
- ->where('platform_id', $platformId)
- ->get();
- $appIdInfo = $channelList->where('channel_id', $channelId)->first();
- $appId = isset($appIdInfo->app_id) ? $appIdInfo->app_id : null;
- $total = count($data);
- foreach ($data as $item) {
- $insertData = [
- 'platform_id' => $platformId,
- 'channel_id' => $channelId,
- 'app_id' => $appId,
- 'order_id' => $item['orderno'],
- 'border_id' => $item['id'],
- 'member_openid' => $item['openid'],
- 'subscribed_at' => null,
- 'user_created_at' => date("Y-m-d H:i:s", $item['regtime']),
- 'product' => null,
- 'price' => $item['amount'] * 100,
- 'status' => $item['status'],
- 'agent_uid' => null,
- 'from_novel' => null,
- 'referral_link_id' => null,
- 'from_novel_id' => $item['book_entry'],
- 'platform_created_at' => date('Y-m-d H:i:s', $item['ctime']),
- 'paid_at' => !empty($item['ctime']) ? date('Y-m-d H:i:s', $item['ctime']) : null,
- ];
- # 创建或更新
- Order::updateOrCreate(
- ['platform_id'=>$platformId, 'order_id'=>$insertData['order_id'], 'channel_id'=>$channelId], $insertData
- );
- }
- $this->page++;
- } else {
- if(isset($responseData['err']) && $responseData['err'] != 0) {
- Log::logError('掌读订单信息获取失败', [
- 'response' => $responseData,
- 'params' => $params
- ], 'ZDOrderList');
- }
- }
- $this->info('本次共同步账号'.$total.'个');
- } while($pageCount >= $this->page && $pageCount);
- } catch (\Exception $e) {
- Log::logError('掌中云订单数据同步失败: '.$e->getMessage(), [], 'ZDOrderList');
- }
- return true;
- }
- }
|