NewUserExport.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace App\Jobs\OpenWork\External;
  3. use App\Facades\Servers\Logs\Log;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Servers\DB\DbService;
  10. use App\Models\OpenWork\DownloadTask as TaskModel;
  11. use App\Models\OpenWork\External\User as Model;
  12. use App\Models\OpenWork\External\FollowTag as ExternalFollowTag;
  13. use App\Models\OpenWork\External\TagGroup;
  14. use App\Jobs\OpenWork\External\UserExportDataJobs;
  15. use Illuminate\Support\Facades\Cache;
  16. /**
  17. * 导出客户数据-下载
  18. * @author 刘相欣
  19. * @version 2.0
  20. * @date 2025-09-09
  21. */
  22. class NewUserExport implements ShouldQueue
  23. {
  24. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  25. protected $taskData;
  26. protected $Records;
  27. /**
  28. * Create a new job instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct(array $taskData)
  33. {
  34. $this->taskData = $taskData;
  35. }
  36. public function getCorpId()
  37. {
  38. return $this->taskData['corpid'] ?? null;
  39. }
  40. public function handle()
  41. {
  42. // 数据穿透
  43. $companyId = $this->taskData['company_id'];
  44. // 切换数据库
  45. (new DbService())->getConnectionNameByCompanyId($companyId);
  46. // 调用
  47. try {
  48. $this->toxls();
  49. } catch (\Exception $e) {
  50. $fileId = $this->taskData['file_id'];
  51. $corpid = $this->taskData['corpid'];
  52. $companyId = $this->taskData['company_id'];
  53. //查询下载任务
  54. (new TaskModel)->query()->where(['file_id' => $fileId, 'corpid' => $corpid])->update(['status' => 2, 'update_time' => time()]);
  55. Log::error('new_user_export_jobs', $e->getMessage());
  56. }
  57. // 返回结果
  58. return 0;
  59. }
  60. public function toxls()
  61. {
  62. $fileId = $this->taskData['file_id'];
  63. $corpid = $this->taskData['corpid'];
  64. $companyId = $this->taskData['company_id'];
  65. $map = $this->taskData['map'];
  66. $tag_ids = $this->taskData['tag_ids'];
  67. $tag_and_ids = $this->taskData['tag_and_ids'];
  68. $fileName = $this->taskData['file_name'];
  69. $corp_name = $this->taskData['corp_name'];
  70. $chunkSize = 1000;
  71. //查询下载任务
  72. $TaskModel = (new TaskModel);
  73. // 查询数据
  74. $taskId = $TaskModel->query()->where(['file_id' => $fileId, 'corpid' => $corpid])->value('id');
  75. // // // 如果没有对应的数据
  76. if (!$taskId) return 0;
  77. // 超时时间设置为 30分钟
  78. set_time_limit(1800);
  79. // 临时调整内存
  80. ini_set('memory_limit', '512M');
  81. // 实例化
  82. $Model = new Model();
  83. // 构建基础查询
  84. $query = $Model->query()
  85. ->join('openwork_external_follow', 'openwork_external_follow.external_userid', '=', 'openwork_external.external_userid')
  86. ->join('custom', 'openwork_external.custom_uid', '=', 'custom.uid', 'LEFT')
  87. ->where($map);
  88. if ($corp_name) {
  89. $query->where(function ($query) use ($corp_name) {
  90. $query->where('openwork_external_follow.remark_corp_name', 'like', "%$corp_name%")
  91. ->orWhere('openwork_external.corp_name', 'like', "%$corp_name%");
  92. });
  93. }
  94. // 如果标签存在子查询
  95. // 写法
  96. if (!empty($tag_and_ids) && !empty($tag_ids)) {
  97. // 且查询和或查询都要
  98. $subQuery = (new ExternalFollowTag)->query()->whereIn('tag_id', $tag_ids)->select('external_userid')->distinct('external_userid');
  99. // 添加子查询
  100. $query->whereIn('openwork_external.external_userid', function ($query1) use ($subQuery) {
  101. $query1->select('external_userid')->fromSub($subQuery, 'sub1');
  102. });
  103. $subQuery = (new ExternalFollowTag)->query()->where('corpid', $corpid)->whereIn('tag_id', $tag_and_ids)->groupBy('external_userid')->havingRaw('COUNT(DISTINCT tag_id) = ?', [count($tag_and_ids)])->select('external_userid');
  104. // $tag_ids
  105. $query->whereIn('openwork_external.external_userid', function ($query1) use ($subQuery) {
  106. $query1->select('external_userid')->fromSub($subQuery, 'sub1');
  107. });
  108. } else if (!empty($tag_ids)) { //或查询
  109. // 子查询
  110. $subQuery = (new ExternalFollowTag)->query()->whereIn('tag_id', $tag_ids)->select('external_userid')->distinct('external_userid');
  111. // 添加子查询
  112. $query->whereIn('openwork_external.external_userid', function ($query1) use ($subQuery) {
  113. $query1->select('external_userid')->fromSub($subQuery, 'sub1');
  114. });
  115. } else if (!empty($tag_and_ids)) { //且查询
  116. // 子查询
  117. $subQuery = (new ExternalFollowTag)->query()->where('corpid', $corpid)->whereIn('tag_id', $tag_and_ids)->groupBy('external_userid')->havingRaw('COUNT(DISTINCT tag_id) = ?', [count($tag_and_ids)])->select('external_userid');
  118. // $tag_ids
  119. $query->whereIn('openwork_external.external_userid', function ($query1) use ($subQuery) {
  120. $query1->select('external_userid')->fromSub($subQuery, 'sub1');
  121. });
  122. }
  123. // 获取总数
  124. $query = $query->orderByDesc('openwork_external_follow.createtime')->select([
  125. 'openwork_external_follow.external_userid',
  126. 'openwork_external.name',
  127. 'openwork_external_follow.remark',
  128. 'openwork_external_follow.description',
  129. 'openwork_external_follow.status',
  130. 'openwork_external.corp_name',
  131. 'openwork_external_follow.remark_corp_name',
  132. 'openwork_external_follow.follow_userid',
  133. 'openwork_external_follow.createtime',
  134. 'openwork_external_follow.remark_mobiles',
  135. 'custom.phone',
  136. 'openwork_external.custom_uid',
  137. ]);
  138. // 获取标签分组
  139. $tagGroup = (new TagGroup)->query()->pluck('group_name', 'group_id')->toArray();
  140. // 获取客户ID
  141. $totle = $query->count();
  142. $totle_page = intval(ceil($totle / $chunkSize));
  143. $now_page = 1;
  144. // 循环数据
  145. $query->chunk($chunkSize, function ($chunk) use (&$tagGroup, $corpid, $companyId, $fileName, $totle, $taskId, $totle_page, &$now_page) {
  146. // 转格式
  147. $chunk = $chunk->toArray();
  148. $message_data = [
  149. 'corpid' => $corpid,
  150. 'totle' => $totle,
  151. 'file_name' => $fileName,
  152. 'tagGroup' => $tagGroup,
  153. 'companyId' => $companyId,
  154. 'chunk' => $chunk,
  155. 'taskId' => $taskId,
  156. 'totle_page' => $totle_page,
  157. 'now_page' => $now_page,
  158. ];
  159. UserExportDataJobs::dispatch($message_data);
  160. // UserExportDataJobs::dispatchSync($message_data);
  161. $now_page++;
  162. });
  163. return true;
  164. }
  165. }