123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- namespace App\Console\Commands;
- use App\Log;
- use App\Models\VpAccount;
- use App\Models\VpOrder;
- use App\RedisModel;
- use App\Services\HttpService;
- use App\Services\YouZiService;
- use Illuminate\Console\Command;
- class YouZiOrderList extends Command
- {
- protected $signature = 'YouZiOrderList {accountLabel?} {year?}';
- protected $description = '柚子分销端订单数据抓取';
- protected $year;
- protected $page = 1;
- protected $perPage = 200;
- protected $accountLabel;
- protected $platformId;
- public function handle()
- {
- \DB::connection()->disableQueryLog();
- $this->accountLabel = $this->argument('accountLabel') ? $this->argument('accountLabel') : 0;
- $this->info('本次查询的账号对应键值为:' . $this->accountLabel);
- $this->getAccountList();
- }
- public function getAccountList()
- {
- # 获取账号列表
- $fields = ['ma_app_id', 'app_id', 'service_type'];
- if($this->accountLabel == 1) {
- $this->platformId = 3;
- } else {
- $this->platformId = 1;
- }
- $accountList = VpAccount::getAccountList($this->platformId, $fields, []);
- $this->year = $this->argument('year') ? $this->argument('year') : null;
- if(date('Y-m-d H:i:s') <= date('Y-m-d') . ' 00:00:30') {
- $date = date('Y-m-d', strtotime('-1 days'));
- } else {
- $date = date('Y-m-d');
- }
- foreach ($accountList as $item) {
- $maAppId = isset($item->ma_app_id) ? $item->ma_app_id : null;
- $appId = isset($item->app_id) ? $item->app_id : null;
- $this->info('本次处理账号appId【'.$appId.'】');
- if(empty($maAppId) || empty($appId)) {
- Log::logError('账号数据存在异常', $item, 'YouZiOrderList');
- continue;
- }
- $serviceType = isset($item->service_type) ? $item->service_type : 0;
- $this->info('该账号ServiceType【'.$serviceType.'】');
- if(!in_array($serviceType, YouZiService::ACCOUNT_SERVICE_TYPE)) {
- Log::logError('ServiceType非法', $item, 'YouZiOrderList');
- continue;
- }
- if(is_null($this->year)) {
- $month = null;
- $this->getOrderList($appId, $maAppId, $serviceType, $month, $date);
- } else {
- for ($month = 1; $month <= 12; $month++) {
- $this->getOrderList($appId, $maAppId, $serviceType, $month, null);
- }
- }
- }
- }
- public function getOrderList($appId, $maAppId, $serviceType, $month, $date)
- {
- if(is_null($month)) {
- $this->getOrder($appId, $maAppId, $serviceType, $date);
- } else {
- $days = getDaysByMonth($this->year, $month);
- foreach ($days as $day) {
- if($day > date("Y-m-d"))
- return false;
- $this->getOrder($appId, $maAppId, $serviceType, $day);
- }
- }
- return true;
- }
- public function getOrder($appId, $maAppId, $serviceType, $date, $retry=0)
- {
- try{
- $requestUri = YouZiService::BASE_URI . YouZiService::ORDER_LIST_URI;
- $sTime = strtotime($date. ' 00:00:00') . '000';
- $eTime = strtotime($date.' 23:59:59') . '999';
- $this->page = 1;
- do {
- $this->info('日期:'.$date);
- $this->info('公众号:'.$appId);
- $this->info('当前页码数:'.$this->page);
- $this->info('每页显示条数:'.$this->perPage);
- $param = [
- 'dbSorted' => array([
- 'fieldName' => 'createdTs',
- 'order' => 'ASC'
- ]),
- 'page' => $this->page,
- 'limit' => $this->perPage,
- 'beginCreatedTs' => $sTime,
- 'endCreatedTs' => $eTime
- ];
- # 获取令牌并拼装Header
- $accessToken = YouZiService::getAccessToken($this->accountLabel);
- if(empty($accessToken) && $retry < 5) {
- # 清除已缓存的AccessToken
- RedisModel::del(YouZiService::ACCESS_TOKEN_RDS_KEY . '_' .$this->accountLabel);
- $retry++;
- $this->getOrder($appId, $maAppId, $serviceType, $date, $retry);
- }
- $headers = [
- 'accesstoken: ' . $accessToken,
- 'maappid: ' . $maAppId,
- 'mpappid: ' . $appId
- ];
- # 获取列表
- $response = HttpService::httpPost($requestUri, json_encode($param), true, $headers);
- $responseData = json_decode($response, True);
- if(isset($responseData['code']) && $responseData['code'] == -10001 && $retry < 5) {
- # 清除已缓存的AccessToken
- RedisModel::del(YouZiService::ACCESS_TOKEN_RDS_KEY . '_' .$this->accountLabel);
- $retry++;
- $this->getOrder($appId, $maAppId, $serviceType, $date, $retry);
- }
- # 是否存在合法数据
- $totalPage = isset($responseData['data']['totalPage']) ? $responseData['data']['totalPage'] : 0;
- $data = isset($responseData['data']['items']) ? $responseData['data']['items'] : [];
- $platformId = $this->platformId;
- # 检出数据
- foreach ($data as $datum) {
- # 数据源ID
- $bindUserActionSetId = isset($datum['bindUserActionSetId']) ? $datum['bindUserActionSetId'] : null;
- # 公众账号AppId获取
- if($serviceType == 2) { // 公众账号
- $orderAppId = isset($datum['mpAppId']) ? $datum['mpAppId'] : '';
- if($appId != $orderAppId) {
- Log::logError('订单关联账号与筛选条件不一致【'.$appId.'】', $datum, 'YouZiOrderList');
- }
- } else { // 企微号
- $orderAppId = isset($datum['bindAdMpAppId']) ? $datum['bindAdMpAppId'] : null;
- }
- $orderId = isset($datum['orderId']) ? $datum['orderId'] : '';
- if($orderId=='620360d485d9af6b3dc7bc78') {
- Log::logError('发现异常数据', $datum, 'YouZiOrder0210');
- Log::logError('账号AppId【'.$appId.'】', [], 'YouZiOrderAppId0210');
- }
- if(empty($orderAppId)) {
- // Log::logError('公众号AppId获取失败', $datum, 'YouZiOrderList');
- continue;
- }
- $enable = 1;
- $isDeleted = isset($datum['isDeleted']) ? $datum['isDeleted'] : 0;
- if($isDeleted) $enable = -1;
- if(empty($orderId)) {
- Log::logError('订单Id获取失败', $datum, 'YouZiOrderList');
- continue;
- }
- $insertData = [
- 'created_id' => isset($datum['createdId']) ? $datum['createdId'] : null,
- 'platform_created_at' => isset($datum['createdTs']) ? date('Y-m-d H:i:s', round($datum['createdTs'] / 1000)) : null,
- 'updated_id' => isset($datum['updatedId']) ? $datum['updatedId'] : null,
- 'platform_updated_at' => isset($datum['updatedTs']) ? date('Y-m-d H:i:s', round($datum['updatedTs'] / 1000)) : null,
- 'user_id' => isset($datum['userId']) ? $datum['userId'] : null,
- 'openid' => isset($datum['mpUserId']) ? $datum['mpUserId'] : null,
- 'nickname' => isset($datum['mpUserNickName']) ? $datum['mpUserNickName'] : null,
- 'ma_app_id' => isset($datum['maAppId']) ? $datum['maAppId'] : null,
- 'app_id' => $orderAppId,
- 'enable' => $enable,
- 'pay_status' => isset($datum['payStatus']) ? $datum['payStatus'] : null,
- 'order_type' => isset($datum['orderType']) ? $datum['orderType'] : null,
- 'pay_money' => isset($datum['payMoney']) ? $datum['payMoney'] : null,
- 'pay_activity_id' => isset($datum['payActivityId']) ? $datum['payActivityId'] : null,
- 'pay_setup_id' => isset($datum['paySetupId']) ? $datum['paySetupId'] : null,
- 'member_setup_id' => isset($datum['memberSetupId']) ? $datum['memberSetupId'] : null,
- 'coin_a_num' => isset($datum['coinANum']) ? $datum['coinANum'] : null,
- 'coin_b_num' => isset($datum['coinBNum']) ? $datum['coinBNum'] : null,
- 'first_add_coin_b_num' => isset($datum['firstAddCoinBNum']) ? $datum['firstAddCoinBNum'] : null,
- 'member_add_coin_b_num' => isset($datum['memberAddCoinBNum']) ? $datum['memberAddCoinBNum'] : null,
- 'source_type' => isset($datum['sourceType']) ? $datum['sourceType'] : null,
- 'video_id' => isset($datum['videoId']) ? $datum['videoId'] : null,
- 'playlet_id' => isset($datum['playletId']) ? $datum['playletId'] : null,
- 'playlet_name' => isset($datum['playletName']) ? $datum['playletName'] : null,
- 'cp_admin_id' => isset($datum['cpAdminId']) ? $datum['cpAdminId'] : null,
- 'user_register_time' => isset($datum['userRegisterTime']) ? date('Y-m-d H:i:s', round($datum['userRegisterTime'] / 1000)) : null,
- 'mp_user_register_time' => isset($datum['mpUserRegisterTime']) ? date('Y-m-d H:i:s', round($datum['mpUserRegisterTime'] / 1000)) : null,
- 'pay_times' => isset($datum['payTimes']) ? $datum['payTimes'] : null,
- 'mp_pay_times' => isset($datum['mpPayTimes']) ? $datum['mpPayTimes'] : null,
- 'user_action_return_set_type' => isset($datum['userActionReturnSetType']) ? $datum['userActionReturnSetType'] : null,
- 'ad_report_status' => isset($datum['adReportStatus']) ? $datum['adReportStatus'] : null,
- 'ad_report_time' => isset($datum['adReportTime']) ? date('Y-m-d H:i:s', round($datum['adReportTime'] / 1000)) : null,
- 'ad_report_action_type' => isset($datum['adReportActionType']) ? $datum['adReportActionType'] : null,
- 'ad_report_error_message' => isset($datum['adReportErrorMessage']) ? $datum['adReportErrorMessage'] : null,
- 'is_full_report' => isset($datum['isFullReport']) ? $datum['isFullReport'] : null,
- 'is_full_report_success' => isset($datum['isFullReportSuccess']) ? $datum['isFullReportSuccess'] : null,
- 'bind_ad_mp_app_id' => isset($datum['bindAdMpAppId']) ? $datum['bindAdMpAppId'] : null,
- 'bind_user_action_set_id' => $bindUserActionSetId,
- ];
- VpOrder::updateOrCreate(
- ['platform_id'=>$platformId, 'order_id' => $orderId], $insertData
- );
- }
- $this->page++;
- $this->info('本次获取数据条数'.$totalPage);
- } while($this->page <= $totalPage);
- }catch(\Exception $e) {
- Log::logError('柚子分销平台订单数据同步失败: '.$e->getMessage(), [], 'YouZiOrderList');
- return false;
- }
- return true;
- }
- }
|