小说推广数据系统

SunnyTaskFinish.php 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Log;
  4. use App\Models\Account;
  5. use App\Models\FansData;
  6. use App\Models\Order;
  7. use App\Models\SunnyTask;
  8. use App\Services\SunService;
  9. use Illuminate\Console\Command;
  10. class SunnyTaskFinish extends Command
  11. {
  12. protected $signature = 'SunnyTaskFinish {type}';
  13. protected $description = '阳关平台任务处理-存储响应数据到平台';
  14. protected $type;
  15. protected $typeData;
  16. protected $platform;
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. $this->typeData = SunService::TYPE_DATA;
  21. $this->platform = SunService::PLATFORM_ID;
  22. }
  23. public function handle()
  24. {
  25. \DB::connection()->disableQueryLog();
  26. $this->type = $this->argument('type');
  27. if(!in_array($this->type, $this->typeData)) {
  28. $this->info('任务类型【'.$this->type.'】非法');
  29. return false;
  30. }
  31. $this->info(date('m-d H:i:s') . ' 开始整理');
  32. while(true){
  33. $result = $this->getTask();
  34. if($result===false) {
  35. sleep(240);
  36. }
  37. sleep(5);
  38. }
  39. }
  40. public function getTask()
  41. {
  42. $this->info($this->type);
  43. # 按照任务类型获取任务数据
  44. $taskInfo = SunnyTask::getTaskFile($this->type);
  45. if(empty($taskInfo)) return false;
  46. # 根据任务类型处理数据
  47. switch($this->type) {
  48. case SunnyTask::CHANNEL_TYPE:
  49. $result = $this->accountData($taskInfo);
  50. break;
  51. case SunnyTask::ORDER_TYPE:
  52. $result = $this->getOrderList($taskInfo);
  53. break;
  54. case SunnyTask::FANS_DATA_TYPE:
  55. $result = $this->getFansDayCollect($taskInfo);
  56. break;
  57. }
  58. return $result;
  59. }
  60. /*
  61. * 账号数据获取
  62. * */
  63. public function accountData($taskObj)
  64. {
  65. try {
  66. $this->info($taskObj->path);
  67. $handle = @fopen($taskObj->path, "r");
  68. $total = 0;
  69. if ($handle) {
  70. while (($buffer = fgets($handle, 4096)) !== false) {
  71. $total++;
  72. $item = json_decode($buffer, true);
  73. $insertData = [
  74. 'platform_id' => $this->platform,
  75. 'channel_id' => $item['channel_id'],
  76. 'site_domain' => $item['domain'],
  77. 'username' => $item['username'],
  78. 'name' => $item['wx_nickname'],
  79. 'app_id' => $item['app_id'],
  80. 'raw_id' => null,
  81. 'nickname' => $item['wx_nickname'],
  82. 'platform_created_at' => null
  83. ];
  84. # 创建或更新
  85. Account::updateOrCreate(
  86. ['platform_id'=>$this->platform, 'name'=>$item['wx_nickname']], $insertData
  87. );
  88. }
  89. if (!feof($handle)) {
  90. Log::logError('账号数据读取失败', $taskObj->toArray(), 'SunnyAccountData');
  91. }
  92. }
  93. $this->info('共有数据'.$total.'条');
  94. # 更新任务状态为已完成
  95. $result = SunnyTask::taskFinish($taskObj);
  96. if(!$result) {
  97. Log::logError('任务状态变更失败', $taskObj->toArray(), 'SunnyAccountData');
  98. }
  99. $this->info('此次获取条数:'.$total);
  100. } catch (\Exception $e) {
  101. Log::logError('阳光账号信息获取失败', [
  102. 'msg' => $e->getMessage(),
  103. 'line' => $e->getLine(),
  104. 'task' => $taskObj->toArray()
  105. ], 'SunnyAccountData');
  106. return false;
  107. }
  108. return true;
  109. }
  110. /*
  111. * 订单数据获取
  112. * */
  113. public function getOrderList($taskObj)
  114. {
  115. try {
  116. $orderType = SunService::ORDER_TYPE;
  117. $orderStatus = SunService::ORDER_STATUS;
  118. $this->info($taskObj->path);
  119. $handle = @fopen($taskObj->path, "r");
  120. $total = 0;
  121. if ($handle) {
  122. # 获取渠道信息
  123. $channelList = Account::select(['channel_id', 'app_id'])
  124. ->where('enable', 1)
  125. ->where('platform_id', $this->platform)
  126. ->get();
  127. while (($buffer = fgets($handle, 4096)) !== false) {
  128. $total++;
  129. $item = json_decode($buffer, true);
  130. $type = isset($orderType[$item['type']]) ? $orderType[$item['type']] : null;
  131. $status = isset($orderStatus[$item['state']]) ? $orderStatus[$item['state']] : null;
  132. if(is_null($type) || is_null($status)) {
  133. Log::logError('订单数据异常', $item, 'SunOrderList');
  134. }
  135. $channelId = isset($item['channel_id']) ? $item['channel_id'] : null;
  136. $appIdInfo = $channelList->where('channel_id', $channelId)->first();
  137. $appId = isset($appIdInfo->app_id) ? $appIdInfo->app_id : null;
  138. $insertData = [
  139. 'platform_id' => $this->platform,
  140. 'channel_id' => $channelId,
  141. 'app_id' => $appId,
  142. 'order_id' => $item['merchant_id'],
  143. 'border_id' => $item['transaction_id'],
  144. 'type' => $type,
  145. 'member_openid' => isset($item['openid']) ? $item['openid'] : null,
  146. 'subscribed_at' => isset($item['subscribe_time']) ? $item['subscribe_time'] : null,
  147. 'user_created_at' => isset($item['user_createtime']) ? $item['user_createtime'] : null,
  148. 'product' => null,
  149. 'price' => $item['money'] * 100,
  150. 'status' => $status,
  151. 'agent_uid' => null,
  152. 'from_novel' => $item['book_name'],
  153. 'referral_link_id' => null,
  154. 'from_novel_id' => (int)$item['book_id'],
  155. 'platform_created_at' => $item['create_time'],
  156. 'paid_at' => isset($item['finish_time']) ? $item['finish_time'] : null,
  157. ];
  158. # 创建或更新
  159. Order::updateOrCreate(
  160. ['platform_id'=>$this->platform, 'order_id'=>$insertData['order_id'], 'channel_id'=>$insertData['channel_id']], $insertData
  161. );
  162. }
  163. if (!feof($handle)) {
  164. Log::logError('订单数据读取失败', $taskObj->toArray(), 'SunOrderList');
  165. }
  166. }
  167. $this->info('共有数据'.$total.'条');
  168. # 更新任务状态为已完成
  169. $result = SunnyTask::taskFinish($taskObj);
  170. if(!$result) {
  171. Log::logError('任务状态变更失败', $taskObj->toArray(), 'SunOrderList');
  172. }
  173. } catch (\Exception $e) {
  174. Log::logError('阳光账号信息获取失败', [
  175. 'msg' => $e->getMessage(),
  176. 'line' => $e->getLine(),
  177. 'task' => $taskObj->toArray()
  178. ], 'SunOrderList');
  179. return false;
  180. }
  181. return true;
  182. }
  183. public function getFansDayCollect($taskObj)
  184. {
  185. try{
  186. $this->info($taskObj->path);
  187. $handle = @fopen($taskObj->path, "r");
  188. $total = 0;
  189. # 获取账号AppId和渠道号
  190. $accountData = Account::accountInfoByPlatform($this->platform);
  191. if ($handle) {
  192. while (($buffer = fgets($handle, 4096)) !== false) {
  193. $total++;
  194. $item = json_decode($buffer, true);
  195. $appId = isset($accountData[$item['channel_id']]['app_id']) ? $accountData[$item['channel_id']]['app_id'] : null;
  196. $date = isset($item['date']) ? $item['date'] : null;
  197. if(is_null($appId) || is_null($date)) {
  198. Log::logError('基础数据获取失败', $item, 'SunFansDayCollect');
  199. continue;
  200. }
  201. $date = date('Y-m-d', strtotime($date));
  202. $insertData = [
  203. 'app_id' => $appId,
  204. 'ref_date' => $date,
  205. 'cumulate_user' => null,
  206. 'new_user' => $item['increase'],
  207. 'cancel_user' => $item['unfollow_num'],
  208. 'net_increase' => $item['net_follow_num'],
  209. 'cancel_percent' => null,
  210. ];
  211. # 创建或更新
  212. FansData::updateOrCreate(
  213. ['app_id'=>$appId, 'ref_date'=>$date], $insertData
  214. );
  215. }
  216. if (!feof($handle)) {
  217. Log::logError('粉丝日数据读取失败', $taskObj->toArray(), 'SunFansDayCollect');
  218. }
  219. }
  220. $this->info('共有数据'.$total.'条');
  221. # 更新任务状态为已完成
  222. $result = SunnyTask::taskFinish($taskObj);
  223. if(!$result) {
  224. Log::logError('粉丝日数据任务变更失败', $taskObj->toArray(), 'SunFansDayCollect');
  225. }
  226. } catch (\Exception $e) {
  227. Log::logError('粉丝日数据获取失败', [
  228. 'msg' => $e->getMessage(),
  229. 'line' => $e->getLine(),
  230. 'task' => $taskObj->toArray()
  231. ], 'SunFansDayCollect');
  232. return false;
  233. }
  234. return true;
  235. }
  236. }