小说推广数据系统

YouZiOrderList.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Log;
  4. use App\Models\VpAccount;
  5. use App\Models\VpOrder;
  6. use App\RedisModel;
  7. use App\Services\HttpService;
  8. use App\Services\YouZiService;
  9. use Illuminate\Console\Command;
  10. class YouZiOrderList extends Command
  11. {
  12. protected $signature = 'YouZiOrderList {accountLabel?} {year?}';
  13. protected $description = '柚子分销端订单数据抓取';
  14. protected $year;
  15. protected $page = 1;
  16. protected $perPage = 200;
  17. protected $accountLabel;
  18. protected $platformId;
  19. public function handle()
  20. {
  21. \DB::connection()->disableQueryLog();
  22. $this->accountLabel = $this->argument('accountLabel') ? $this->argument('accountLabel') : 0;
  23. $this->info('本次查询的账号对应键值为:' . $this->accountLabel);
  24. $this->getAccountList();
  25. }
  26. public function getAccountList()
  27. {
  28. # 获取账号列表
  29. $fields = ['ma_app_id', 'app_id', 'service_type'];
  30. if($this->accountLabel == 1) {
  31. $this->platformId = 3;
  32. } else {
  33. $this->platformId = 1;
  34. }
  35. $accountList = VpAccount::getAccountList($this->platformId, $fields, []);
  36. $this->year = $this->argument('year') ? $this->argument('year') : null;
  37. if(date('Y-m-d H:i:s') <= date('Y-m-d') . ' 00:00:30') {
  38. $date = date('Y-m-d', strtotime('-1 days'));
  39. } else {
  40. $date = date('Y-m-d');
  41. }
  42. foreach ($accountList as $item) {
  43. $maAppId = isset($item->ma_app_id) ? $item->ma_app_id : null;
  44. $appId = isset($item->app_id) ? $item->app_id : null;
  45. $this->info('本次处理账号appId【'.$appId.'】');
  46. if(empty($maAppId) || empty($appId)) {
  47. Log::logError('账号数据存在异常', $item, 'YouZiOrderList');
  48. continue;
  49. }
  50. $serviceType = isset($item->service_type) ? $item->service_type : 0;
  51. $this->info('该账号ServiceType【'.$serviceType.'】');
  52. if(!in_array($serviceType, YouZiService::ACCOUNT_SERVICE_TYPE)) {
  53. Log::logError('ServiceType非法', $item, 'YouZiOrderList');
  54. continue;
  55. }
  56. if(is_null($this->year)) {
  57. $month = null;
  58. $this->getOrderList($appId, $maAppId, $serviceType, $month, $date);
  59. } else {
  60. for ($month = 1; $month <= 12; $month++) {
  61. $this->getOrderList($appId, $maAppId, $serviceType, $month, null);
  62. }
  63. }
  64. }
  65. }
  66. public function getOrderList($appId, $maAppId, $serviceType, $month, $date)
  67. {
  68. if(is_null($month)) {
  69. $this->getOrder($appId, $maAppId, $serviceType, $date);
  70. } else {
  71. $days = getDaysByMonth($this->year, $month);
  72. foreach ($days as $day) {
  73. if($day > date("Y-m-d"))
  74. return false;
  75. $this->getOrder($appId, $maAppId, $serviceType, $day);
  76. }
  77. }
  78. return true;
  79. }
  80. public function getOrder($appId, $maAppId, $serviceType, $date, $retry=0)
  81. {
  82. try{
  83. $requestUri = YouZiService::BASE_URI . YouZiService::ORDER_LIST_URI;
  84. $sTime = strtotime($date. ' 00:00:00') . '000';
  85. $eTime = strtotime($date.' 23:59:59') . '999';
  86. $this->page = 1;
  87. do {
  88. $this->info('日期:'.$date);
  89. $this->info('公众号:'.$appId);
  90. $this->info('当前页码数:'.$this->page);
  91. $this->info('每页显示条数:'.$this->perPage);
  92. $param = [
  93. 'dbSorted' => array([
  94. 'fieldName' => 'createdTs',
  95. 'order' => 'ASC'
  96. ]),
  97. 'page' => $this->page,
  98. 'limit' => $this->perPage,
  99. 'beginCreatedTs' => $sTime,
  100. 'endCreatedTs' => $eTime
  101. ];
  102. # 获取令牌并拼装Header
  103. $accessToken = YouZiService::getAccessToken($this->accountLabel);
  104. if(empty($accessToken) && $retry < 5) {
  105. # 清除已缓存的AccessToken
  106. RedisModel::del(YouZiService::ACCESS_TOKEN_RDS_KEY . '_' .$this->accountLabel);
  107. $retry++;
  108. $this->getOrder($appId, $maAppId, $serviceType, $date, $retry);
  109. }
  110. $headers = [
  111. 'accesstoken: ' . $accessToken,
  112. 'maappid: ' . $maAppId,
  113. 'mpappid: ' . $appId
  114. ];
  115. # 获取列表
  116. $response = HttpService::httpPost($requestUri, json_encode($param), true, $headers);
  117. $responseData = json_decode($response, True);
  118. if(isset($responseData['code']) && $responseData['code'] == -10001 && $retry < 5) {
  119. # 清除已缓存的AccessToken
  120. RedisModel::del(YouZiService::ACCESS_TOKEN_RDS_KEY . '_' .$this->accountLabel);
  121. $retry++;
  122. $this->getOrder($appId, $maAppId, $serviceType, $date, $retry);
  123. }
  124. # 是否存在合法数据
  125. $totalPage = isset($responseData['data']['totalPage']) ? $responseData['data']['totalPage'] : 0;
  126. $data = isset($responseData['data']['items']) ? $responseData['data']['items'] : [];
  127. $platformId = $this->platformId;
  128. # 检出数据
  129. foreach ($data as $datum) {
  130. # 数据源ID
  131. $bindUserActionSetId = isset($datum['bindUserActionSetId']) ? $datum['bindUserActionSetId'] : null;
  132. # 公众账号AppId获取
  133. if($serviceType == 2) { // 公众账号
  134. $orderAppId = isset($datum['mpAppId']) ? $datum['mpAppId'] : '';
  135. if($appId != $orderAppId) {
  136. Log::logError('订单关联账号与筛选条件不一致【'.$appId.'】', $datum, 'YouZiOrderList');
  137. }
  138. } else { // 企微号
  139. $orderAppId = isset($datum['bindAdMpAppId']) ? $datum['bindAdMpAppId'] : null;
  140. }
  141. $orderId = isset($datum['orderId']) ? $datum['orderId'] : '';
  142. if($orderId=='620360d485d9af6b3dc7bc78') {
  143. Log::logError('发现异常数据', $datum, 'YouZiOrder0210');
  144. Log::logError('账号AppId【'.$appId.'】', [], 'YouZiOrderAppId0210');
  145. }
  146. if(empty($orderAppId)) {
  147. // Log::logError('公众号AppId获取失败', $datum, 'YouZiOrderList');
  148. continue;
  149. }
  150. $enable = 1;
  151. $isDeleted = isset($datum['isDeleted']) ? $datum['isDeleted'] : 0;
  152. if($isDeleted) $enable = -1;
  153. if(empty($orderId)) {
  154. Log::logError('订单Id获取失败', $datum, 'YouZiOrderList');
  155. continue;
  156. }
  157. $insertData = [
  158. 'created_id' => isset($datum['createdId']) ? $datum['createdId'] : null,
  159. 'platform_created_at' => isset($datum['createdTs']) ? date('Y-m-d H:i:s', round($datum['createdTs'] / 1000)) : null,
  160. 'updated_id' => isset($datum['updatedId']) ? $datum['updatedId'] : null,
  161. 'platform_updated_at' => isset($datum['updatedTs']) ? date('Y-m-d H:i:s', round($datum['updatedTs'] / 1000)) : null,
  162. 'user_id' => isset($datum['userId']) ? $datum['userId'] : null,
  163. 'openid' => isset($datum['mpUserId']) ? $datum['mpUserId'] : null,
  164. 'nickname' => isset($datum['mpUserNickName']) ? $datum['mpUserNickName'] : null,
  165. 'ma_app_id' => isset($datum['maAppId']) ? $datum['maAppId'] : null,
  166. 'app_id' => $orderAppId,
  167. 'enable' => $enable,
  168. 'pay_status' => isset($datum['payStatus']) ? $datum['payStatus'] : null,
  169. 'order_type' => isset($datum['orderType']) ? $datum['orderType'] : null,
  170. 'pay_money' => isset($datum['payMoney']) ? $datum['payMoney'] : null,
  171. 'pay_activity_id' => isset($datum['payActivityId']) ? $datum['payActivityId'] : null,
  172. 'pay_setup_id' => isset($datum['paySetupId']) ? $datum['paySetupId'] : null,
  173. 'member_setup_id' => isset($datum['memberSetupId']) ? $datum['memberSetupId'] : null,
  174. 'coin_a_num' => isset($datum['coinANum']) ? $datum['coinANum'] : null,
  175. 'coin_b_num' => isset($datum['coinBNum']) ? $datum['coinBNum'] : null,
  176. 'first_add_coin_b_num' => isset($datum['firstAddCoinBNum']) ? $datum['firstAddCoinBNum'] : null,
  177. 'member_add_coin_b_num' => isset($datum['memberAddCoinBNum']) ? $datum['memberAddCoinBNum'] : null,
  178. 'source_type' => isset($datum['sourceType']) ? $datum['sourceType'] : null,
  179. 'video_id' => isset($datum['videoId']) ? $datum['videoId'] : null,
  180. 'playlet_id' => isset($datum['playletId']) ? $datum['playletId'] : null,
  181. 'playlet_name' => isset($datum['playletName']) ? $datum['playletName'] : null,
  182. 'cp_admin_id' => isset($datum['cpAdminId']) ? $datum['cpAdminId'] : null,
  183. 'user_register_time' => isset($datum['userRegisterTime']) ? date('Y-m-d H:i:s', round($datum['userRegisterTime'] / 1000)) : null,
  184. 'mp_user_register_time' => isset($datum['mpUserRegisterTime']) ? date('Y-m-d H:i:s', round($datum['mpUserRegisterTime'] / 1000)) : null,
  185. 'pay_times' => isset($datum['payTimes']) ? $datum['payTimes'] : null,
  186. 'mp_pay_times' => isset($datum['mpPayTimes']) ? $datum['mpPayTimes'] : null,
  187. 'user_action_return_set_type' => isset($datum['userActionReturnSetType']) ? $datum['userActionReturnSetType'] : null,
  188. 'ad_report_status' => isset($datum['adReportStatus']) ? $datum['adReportStatus'] : null,
  189. 'ad_report_time' => isset($datum['adReportTime']) ? date('Y-m-d H:i:s', round($datum['adReportTime'] / 1000)) : null,
  190. 'ad_report_action_type' => isset($datum['adReportActionType']) ? $datum['adReportActionType'] : null,
  191. 'ad_report_error_message' => isset($datum['adReportErrorMessage']) ? $datum['adReportErrorMessage'] : null,
  192. 'is_full_report' => isset($datum['isFullReport']) ? $datum['isFullReport'] : null,
  193. 'is_full_report_success' => isset($datum['isFullReportSuccess']) ? $datum['isFullReportSuccess'] : null,
  194. 'bind_ad_mp_app_id' => isset($datum['bindAdMpAppId']) ? $datum['bindAdMpAppId'] : null,
  195. 'bind_user_action_set_id' => $bindUserActionSetId,
  196. ];
  197. VpOrder::updateOrCreate(
  198. ['platform_id'=>$platformId, 'order_id' => $orderId], $insertData
  199. );
  200. }
  201. $this->page++;
  202. $this->info('本次获取数据条数'.$totalPage);
  203. } while($this->page <= $totalPage);
  204. }catch(\Exception $e) {
  205. Log::logError('柚子分销平台订单数据同步失败: '.$e->getMessage(), [], 'YouZiOrderList');
  206. return false;
  207. }
  208. return true;
  209. }
  210. }