企微短剧业务系统

YouZiOrderList.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Log;
  4. use App\Models\CorpMapping;
  5. use App\Models\DjOrder;
  6. use App\Models\TencentAdConf;
  7. use App\Models\VpAccount;
  8. use App\RedisModel;
  9. use App\Service\HttpService;
  10. use App\Service\YouZiService;
  11. use Illuminate\Console\Command;
  12. class YouZiOrderList extends Command
  13. {
  14. protected $signature = 'YouZiOrderList {year?} {startMonth?} {endMonth?}';
  15. protected $description = '柚子分销端订单数据抓取';
  16. protected $year;
  17. protected $startMonth;
  18. protected $endMonth;
  19. protected $page = 1;
  20. protected $perPage = 200;
  21. protected $accountLabel;
  22. protected $platformId;
  23. public function handle()
  24. {
  25. \DB::connection()->disableQueryLog();
  26. $this->accountLabel = 0;
  27. $this->info('本次查询的账号对应键值为:' . $this->accountLabel);
  28. $this->getAccountList();
  29. }
  30. public function getAccountList()
  31. {
  32. # 获取账号列表
  33. $fields = ['ma_app_id', 'app_id', 'service_type'];
  34. if($this->accountLabel == 1) {
  35. $this->platformId = 3;
  36. } else {
  37. $this->platformId = 1;
  38. }
  39. $accountList = VpAccount::getAccountList($this->platformId, $fields, []);
  40. $this->year = $this->argument('year') ? $this->argument('year') : null;
  41. $this->startMonth = $this->argument('startMonth') ? $this->argument('startMonth') : null;
  42. $this->endMonth = $this->argument('endMonth') ? $this->argument('endMonth') : null;
  43. if(date('Y-m-d H:i:s') <= date('Y-m-d') . ' 00:00:10') {
  44. $date = date('Y-m-d', strtotime('-1 days'));
  45. } else {
  46. $date = date('Y-m-d');
  47. }
  48. foreach ($accountList as $item) {
  49. $maAppId = isset($item->ma_app_id) ? $item->ma_app_id : null;
  50. $appId = isset($item->app_id) ? $item->app_id : null;
  51. $this->info('本次处理账号appId【'.$appId.'】');
  52. if(empty($maAppId) || empty($appId)) {
  53. Log::logError('账号数据存在异常', $item, 'YouZiOrderList');
  54. continue;
  55. }
  56. $serviceType = isset($item->service_type) ? $item->service_type : 0;
  57. $this->info('该账号ServiceType【'.$serviceType.'】');
  58. if(!in_array($serviceType, YouZiService::ACCOUNT_SERVICE_TYPE)) {
  59. Log::logError('ServiceType非法', $item, 'YouZiOrderList');
  60. continue;
  61. }
  62. if(is_null($this->year)) {
  63. $month = null;
  64. $this->getOrderList($appId, $maAppId, $serviceType, $month, $date);
  65. } else {
  66. $start = !empty($this->startMonth) ? $this->startMonth : 1;
  67. $end = !empty($this->endMonth) ? $this->endMonth : 12;
  68. for ($month = $start; $month <= $end; $month++) {
  69. $this->getOrderList($appId, $maAppId, $serviceType, $month, null);
  70. }
  71. }
  72. }
  73. }
  74. public function getOrderList($appId, $maAppId, $serviceType, $month, $date)
  75. {
  76. if(is_null($month)) {
  77. $this->getOrder($appId, $maAppId, $serviceType, $date);
  78. } else {
  79. $days = getDaysByMonth($this->year, $month);
  80. foreach ($days as $day) {
  81. if($day > date("Y-m-d"))
  82. return false;
  83. $this->getOrder($appId, $maAppId, $serviceType, $day);
  84. }
  85. }
  86. return true;
  87. }
  88. public function getOrder($appId, $maAppId, $serviceType, $date, $retry=0)
  89. {
  90. $platformType = 'BcJV6mb504Kt2lML';
  91. try{
  92. $requestUri = YouZiService::BASE_URI . YouZiService::ORDER_LIST_URI;
  93. $sTime = strtotime($date. ' 00:00:00') . '000';
  94. $eTime = strtotime($date.' 23:59:59') . '999';
  95. $this->page = 1;
  96. do {
  97. $this->info('日期:'.$date);
  98. $this->info('公众号:'.$appId);
  99. $this->info('当前页码数:'.$this->page);
  100. $this->info('每页显示条数:'.$this->perPage);
  101. $param = [
  102. 'dbSorted' => array([
  103. 'fieldName' => 'createdTs',
  104. 'order' => 'ASC'
  105. ]),
  106. 'page' => $this->page,
  107. 'limit' => $this->perPage,
  108. 'beginCreatedTs' => $sTime,
  109. 'endCreatedTs' => $eTime
  110. ];
  111. # 获取令牌并拼装Header
  112. $accessToken = YouZiService::getAccessToken($this->accountLabel);
  113. if(empty($accessToken) && $retry < 5) {
  114. # 清除已缓存的AccessToken
  115. RedisModel::del(YouZiService::ACCESS_TOKEN_RDS_KEY . '_' .$this->accountLabel);
  116. $retry++;
  117. $this->getOrder($appId, $maAppId, $serviceType, $date, $retry);
  118. }
  119. $headers = [
  120. 'accesstoken: ' . $accessToken,
  121. 'maappid: ' . $maAppId,
  122. 'mpappid: ' . $appId
  123. ];
  124. # 获取列表
  125. $response = HttpService::httpPost($requestUri, json_encode($param), true, $headers);
  126. $responseData = json_decode($response, True);
  127. if(isset($responseData['code']) && $responseData['code'] == -10001 && $retry < 5) {
  128. # 清除已缓存的AccessToken
  129. RedisModel::del(YouZiService::ACCESS_TOKEN_RDS_KEY . '_' .$this->accountLabel);
  130. $retry++;
  131. $this->getOrder($appId, $maAppId, $serviceType, $date, $retry);
  132. }
  133. if(isset($responseData['code']) && $responseData['code'] == -20001) {
  134. Log::logError('当日获取数据条数超出上限', [$this->page] , 'YouZiOrderList');
  135. }
  136. # 是否存在合法数据
  137. $totalPage = isset($responseData['data']['totalPage']) ? $responseData['data']['totalPage'] : 0;
  138. $data = isset($responseData['data']['items']) ? $responseData['data']['items'] : [];
  139. # 检出数据
  140. foreach ($data as $datum) {
  141. // 根据企业id映射关系表查询自己的企业id
  142. $corpList = CorpMapping::getCorpList($platformType);
  143. $systemCorpid = isset($corpList[$datum['mpAppId']]['corpid']) ? $corpList[$datum['mpAppId']]['corpid'] : '';
  144. $datum['system_corpid'] = $systemCorpid;
  145. if(!empty($datum['bindUserActionSetId'])) {
  146. // 根据数据源id获取绑定的公众号id
  147. $appList = TencentAdConf::getAppList();
  148. $bindAppId = isset($appList[$datum['bindUserActionSetId']]['app_id']) ? $appList[$datum['bindUserActionSetId']]['app_id'] : '';
  149. $datum['bind_app_id'] = $bindAppId;
  150. } else {
  151. // 避免出现企微号但是未绑定数据源id的情况
  152. if(empty($orderPay['system_corpid'])) {
  153. $datum['bind_app_id'] = $datum['mpAppId'];
  154. }
  155. }
  156. $datum['enable'] = 1;
  157. $isDeleted = isset($datum['isDeleted']) ? $datum['isDeleted'] : 0;
  158. if($isDeleted) $datum['enable'] = -1;
  159. $orderId = isset($datum['orderId']) ? $datum['orderId'] : '';
  160. if(empty($orderId)) {
  161. Log::logError('订单Id获取失败', $datum, 'YouZiOrderList');
  162. continue;
  163. }
  164. DjOrder::youziHistoryOrderSave($datum, $platformType);
  165. }
  166. $this->page++;
  167. $this->info('本次获取数据条数'.$totalPage);
  168. } while($this->page <= $totalPage);
  169. }catch(\Exception $e) {
  170. Log::logError('柚子分销平台订单数据同步失败: '.$e->getMessage(), [], 'YouZiOrderList');
  171. return false;
  172. }
  173. return true;
  174. }
  175. }