企微短剧业务系统

CustomerPlayletTagRepair.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2022/12/14
  6. * Time: 14:37
  7. */
  8. namespace App\Console\Repair;
  9. use App\Models\CustomerDetails;
  10. use App\Models\DjOrder;
  11. use App\Models\Tag;
  12. use App\Models\TagGroup;
  13. use App\RedisModel;
  14. use App\Service\CustomerTagService;
  15. use App\Service\TagService;
  16. use Illuminate\Console\Command;
  17. class CustomerPlayletTagRepair extends Command
  18. {
  19. protected $signature = 'CustomerPlayletTagRepair {corpid?}';
  20. protected $description = '企微客户主标签清洗';
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. }
  25. public function handle()
  26. {
  27. $this->info(date('H:i') . ' 开始处理');
  28. // 获取企微下的所有剧集标签
  29. $tagGroup = TagGroup::query()->where('enable', 1)
  30. ->where('group_name', '剧集标签')->get();
  31. if($tagGroup->isEmpty()){
  32. $this->error(date('H:i') . ' 查询标签组ID失败');
  33. return false;
  34. }
  35. foreach ($tagGroup as $item) {
  36. // 查询标签组对应的剧集标签
  37. $tagList = Tag::query()->where('corpid', $item->corpid)->where('enable', 1)
  38. ->where('group_id', $item->group_id)->where('customer_num', 0)->get();
  39. if($tagList->isEmpty()){
  40. $this->error(date('H:i') . ' 查询标签列表失败');
  41. continue;
  42. }
  43. $tagIdList = array_column($tagList->toArray(), 'tag_id');
  44. foreach($tagIdList as $tagId) {
  45. TagService::delTag($item->corpid, [$tagId],[]);
  46. }
  47. }
  48. }
  49. public function handle2()
  50. {
  51. try{
  52. $corpid = $this->argument('corpid');
  53. $this->info(date('H:i') . ' 开始处理');
  54. // 获取企微下的所有剧集标签
  55. $tagGroup = TagGroup::query()->where('corpid', $corpid)->where('enable', 1)
  56. ->where('group_name', '剧集标签')->first();
  57. if(empty($tagGroup)){
  58. $this->error(date('H:i') . ' 查询标签组ID失败');
  59. return false;
  60. }
  61. // 查询标签组对应的剧集标签
  62. $tagList = Tag::query()->where('corpid', $corpid)->where('enable', 1)
  63. ->where('group_id', $tagGroup->group_id)->where('customer_num', 0)->get();
  64. if($tagList->isEmpty()){
  65. $this->error(date('H:i') . ' 查询标签列表失败');
  66. return false;
  67. }
  68. $tagIdList = array_column($tagList->toArray(), 'tag_id');
  69. foreach($tagIdList as $tagId) {
  70. TagService::delTagNew($corpid, $tagId);
  71. }
  72. return true;
  73. $platformArr = config('platform.base');
  74. $startId = 0;
  75. // 遍历处理客户数据
  76. while(true){
  77. $customerList = CustomerDetails::suffix($corpid)->where('corpid', $corpid)->where('pay_num', '>', 1)
  78. ->where('id', '>', $startId)->select(['id', 'user_id', 'external_userid', 'tag_list'])
  79. ->where('loss_status', 1)
  80. ->orderBy('id', 'asc')->limit(1000)->get();
  81. if($customerList->isEmpty()){
  82. $this->info(date('H:i') . ' 处理完成');
  83. return false;
  84. }
  85. $this->info(date('H:i') . ' 本次查询数据'.$customerList->count().'条');
  86. foreach($customerList as $customerDetail) {
  87. $startId = $customerDetail->id;
  88. // 查询该客户首单剧集以及平台
  89. $orderInfo = DjOrder::query()->select(['playlet_name', 'order_source'])->where('system_corpid', $corpid)
  90. ->where('external_userid', $customerDetail->external_userid)->where('pay_status', 1)
  91. ->where('playlet_name', '>', '')->orderBy('id', 'asc')
  92. ->first();
  93. if(empty($orderInfo)){
  94. $this->info(date('H:i') . '查询客户支付订单失败');
  95. continue;
  96. }
  97. $platform = $platformArr[$orderInfo->order_source];
  98. $playletName = $orderInfo->playlet_name . '-' . $platform;
  99. // 找到现在所有的剧集标签
  100. $customerTag = empty($customerDetail->tag_list) ? [] : explode(',', $customerDetail->tag_list);
  101. if(count($customerTag) == 1) {// 本身就只有一个剧集标签,直接跳过
  102. continue;
  103. }
  104. $tagIdList = [];
  105. foreach($customerTag as $tagMd5) {
  106. $tag = $tagList->where('tag_md5', $tagMd5)->first();
  107. if(!empty($tag)){
  108. $tagIdList[] = $tag->tag_id;
  109. }
  110. }
  111. // 创建新标签
  112. $tagId = CustomerTagService::createPlayletTag($corpid, $playletName, null);
  113. // 将数据放入标签处理队列中
  114. if($tagId) {
  115. $redisData = [
  116. 'corpid' => $corpid,
  117. 'external_userid' => $customerDetail->external_userid,
  118. 'user_id' => $customerDetail->user_id,
  119. 'tag_id_list' => [$tagId],
  120. // 'remove_tag_id_list'=> $tagIdList,
  121. ];
  122. if(!empty($tagIdList)) $redisData['remove_tag_id_list'] = $tagIdList;
  123. $res = RedisModel::lPush(CustomerTagService::CUSTOMER_TAG_UNIONID, json_encode($redisData));
  124. if(!$res) {
  125. $this->error(date('H:i') . ' 入队列失败,数据ID:'.$startId);
  126. return false;
  127. }
  128. } else {
  129. $this->error(date('H:i') . ' 生成剧集标签'.$playletName.'失败,数据ID:'.$startId);
  130. return false;
  131. }
  132. }
  133. }
  134. } catch (\Exception $exception) {
  135. $this->error($exception->getLine().': '.$exception->getMessage());
  136. }
  137. }
  138. }