123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2022/12/14
- * Time: 14:37
- */
- namespace App\Console\Repair;
- use App\Models\CustomerDetails;
- use App\Models\DjOrder;
- use App\Models\Tag;
- use App\Models\TagGroup;
- use App\RedisModel;
- use App\Service\CustomerTagService;
- use App\Service\TagService;
- use Illuminate\Console\Command;
- class CustomerPlayletTagRepair extends Command
- {
- protected $signature = 'CustomerPlayletTagRepair {corpid?}';
- protected $description = '企微客户主标签清洗';
- public function __construct()
- {
- parent::__construct();
- }
- public function handle()
- {
- $this->info(date('H:i') . ' 开始处理');
- // 获取企微下的所有剧集标签
- $tagGroup = TagGroup::query()->where('enable', 1)
- ->where('group_name', '剧集标签')->get();
- if($tagGroup->isEmpty()){
- $this->error(date('H:i') . ' 查询标签组ID失败');
- return false;
- }
- foreach ($tagGroup as $item) {
- // 查询标签组对应的剧集标签
- $tagList = Tag::query()->where('corpid', $item->corpid)->where('enable', 1)
- ->where('group_id', $item->group_id)->where('customer_num', 0)->get();
- if($tagList->isEmpty()){
- $this->error(date('H:i') . ' 查询标签列表失败');
- continue;
- }
- $tagIdList = array_column($tagList->toArray(), 'tag_id');
- foreach($tagIdList as $tagId) {
- TagService::delTag($item->corpid, [$tagId],[]);
- }
- }
- }
- public function handle2()
- {
- try{
- $corpid = $this->argument('corpid');
- $this->info(date('H:i') . ' 开始处理');
- // 获取企微下的所有剧集标签
- $tagGroup = TagGroup::query()->where('corpid', $corpid)->where('enable', 1)
- ->where('group_name', '剧集标签')->first();
- if(empty($tagGroup)){
- $this->error(date('H:i') . ' 查询标签组ID失败');
- return false;
- }
- // 查询标签组对应的剧集标签
- $tagList = Tag::query()->where('corpid', $corpid)->where('enable', 1)
- ->where('group_id', $tagGroup->group_id)->where('customer_num', 0)->get();
- if($tagList->isEmpty()){
- $this->error(date('H:i') . ' 查询标签列表失败');
- return false;
- }
- $tagIdList = array_column($tagList->toArray(), 'tag_id');
- foreach($tagIdList as $tagId) {
- TagService::delTagNew($corpid, $tagId);
- }
- return true;
- $platformArr = config('platform.base');
- $startId = 0;
- // 遍历处理客户数据
- while(true){
- $customerList = CustomerDetails::suffix($corpid)->where('corpid', $corpid)->where('pay_num', '>', 1)
- ->where('id', '>', $startId)->select(['id', 'user_id', 'external_userid', 'tag_list'])
- ->where('loss_status', 1)
- ->orderBy('id', 'asc')->limit(1000)->get();
- if($customerList->isEmpty()){
- $this->info(date('H:i') . ' 处理完成');
- return false;
- }
- $this->info(date('H:i') . ' 本次查询数据'.$customerList->count().'条');
- foreach($customerList as $customerDetail) {
- $startId = $customerDetail->id;
- // 查询该客户首单剧集以及平台
- $orderInfo = DjOrder::query()->select(['playlet_name', 'order_source'])->where('system_corpid', $corpid)
- ->where('external_userid', $customerDetail->external_userid)->where('pay_status', 1)
- ->where('playlet_name', '>', '')->orderBy('id', 'asc')
- ->first();
- if(empty($orderInfo)){
- $this->info(date('H:i') . '查询客户支付订单失败');
- continue;
- }
- $platform = $platformArr[$orderInfo->order_source];
- $playletName = $orderInfo->playlet_name . '-' . $platform;
- // 找到现在所有的剧集标签
- $customerTag = empty($customerDetail->tag_list) ? [] : explode(',', $customerDetail->tag_list);
- if(count($customerTag) == 1) {// 本身就只有一个剧集标签,直接跳过
- continue;
- }
- $tagIdList = [];
- foreach($customerTag as $tagMd5) {
- $tag = $tagList->where('tag_md5', $tagMd5)->first();
- if(!empty($tag)){
- $tagIdList[] = $tag->tag_id;
- }
- }
- // 创建新标签
- $tagId = CustomerTagService::createPlayletTag($corpid, $playletName, null);
- // 将数据放入标签处理队列中
- if($tagId) {
- $redisData = [
- 'corpid' => $corpid,
- 'external_userid' => $customerDetail->external_userid,
- 'user_id' => $customerDetail->user_id,
- 'tag_id_list' => [$tagId],
- // 'remove_tag_id_list'=> $tagIdList,
- ];
- if(!empty($tagIdList)) $redisData['remove_tag_id_list'] = $tagIdList;
- $res = RedisModel::lPush(CustomerTagService::CUSTOMER_TAG_UNIONID, json_encode($redisData));
- if(!$res) {
- $this->error(date('H:i') . ' 入队列失败,数据ID:'.$startId);
- return false;
- }
- } else {
- $this->error(date('H:i') . ' 生成剧集标签'.$playletName.'失败,数据ID:'.$startId);
- return false;
- }
- }
- }
- } catch (\Exception $exception) {
- $this->error($exception->getLine().': '.$exception->getMessage());
- }
- }
- }
|