123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- namespace App\Console\Commands;
- use App\Log;
- use App\Models\CorpMapping;
- use App\Models\DjOrder;
- use App\Models\TencentAdConf;
- use App\Models\VpAccount;
- use App\RedisModel;
- use App\Service\HttpService;
- use App\Service\YouZiService;
- use Illuminate\Console\Command;
- class YouZiOrderList extends Command
- {
- protected $signature = 'YouZiOrderList {year?} {startMonth?} {endMonth?}';
- protected $description = '柚子分销端订单数据抓取';
- protected $year;
- protected $startMonth;
- protected $endMonth;
- protected $page = 1;
- protected $perPage = 200;
- protected $accountLabel;
- protected $platformId;
- public function handle()
- {
- \DB::connection()->disableQueryLog();
- $this->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;
- $this->startMonth = $this->argument('startMonth') ? $this->argument('startMonth') : null;
- $this->endMonth = $this->argument('endMonth') ? $this->argument('endMonth') : null;
- 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 ($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 {
- $start = !empty($this->startMonth) ? $this->startMonth : 1;
- $end = !empty($this->endMonth) ? $this->endMonth : 12;
- for ($month = $start; $month <= $end; $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)
- {
- $platformType = 'BcJV6mb504Kt2lML';
- 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);
- }
- if(isset($responseData['code']) && $responseData['code'] == -20001) {
- Log::logError('当日获取数据条数超出上限', [$this->page] , 'YouZiOrderList');
- }
- # 是否存在合法数据
- $totalPage = isset($responseData['data']['totalPage']) ? $responseData['data']['totalPage'] : 0;
- $data = isset($responseData['data']['items']) ? $responseData['data']['items'] : [];
- # 检出数据
- foreach ($data as $datum) {
- // 根据企业id映射关系表查询自己的企业id
- $corpList = CorpMapping::getCorpList($platformType);
- $systemCorpid = isset($corpList[$datum['mpAppId']]['corpid']) ? $corpList[$datum['mpAppId']]['corpid'] : '';
- $datum['system_corpid'] = $systemCorpid;
- if(!empty($datum['bindUserActionSetId'])) {
- // 根据数据源id获取绑定的公众号id
- $appList = TencentAdConf::getAppList();
- $bindAppId = isset($appList[$datum['bindUserActionSetId']]['app_id']) ? $appList[$datum['bindUserActionSetId']]['app_id'] : '';
- $datum['bind_app_id'] = $bindAppId;
- } else {
- // 避免出现企微号但是未绑定数据源id的情况
- if(empty($orderPay['system_corpid'])) {
- $datum['bind_app_id'] = $datum['mpAppId'];
- }
- }
- $datum['enable'] = 1;
- $isDeleted = isset($datum['isDeleted']) ? $datum['isDeleted'] : 0;
- if($isDeleted) $datum['enable'] = -1;
- $orderId = isset($datum['orderId']) ? $datum['orderId'] : '';
- if(empty($orderId)) {
- Log::logError('订单Id获取失败', $datum, 'YouZiOrderList');
- continue;
- }
- DjOrder::youziHistoryOrderSave($datum, $platformType);
- }
- $this->page++;
- $this->info('本次获取数据条数'.$totalPage);
- } while($this->page <= $totalPage);
- }catch(\Exception $e) {
- Log::logError('柚子分销平台订单数据同步失败: '.$e->getMessage(), [], 'YouZiOrderList');
- return false;
- }
- return true;
- }
- }
|