ShareUser.php 6.0 KB
<?php

namespace App\Console\Commands\AyrShare;
use App\Helper\AyrShare as AyrShareHelper;
use App\Models\Ai\AiVideo;
use App\Models\AyrShare\AyrRelease as AyrReleaseModel;
use App\Models\Project\AiVideoTask;
use App\Services\ProjectServer;
use Carbon\Carbon;
use App\Models\AyrShare\AyrShare as AyrShareModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class ShareUser extends Command
{
    public $error = 0;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'share_user';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '用户一周内无记录清除Ayr_share';
    /**
     * @name   :(定时执行)handle
     * @author :lyh
     * @method :post
     * @time   :2023/5/12 14:48
     */
    public function handle()
    {
        $this->output('start');
        $this->user_operator_record();
        return true;
    }

    /**
     * @name   : 检测用户是否无操作记录
     * @author :lyh
     * @method :post
     * @time   :2023/5/12 14:55
     */
    protected function user_operator_record(){
        //获取所有ayr_share用户
        $ayr_share_model = new AyrShareModel();
        $ayr_release = new AyrReleaseModel();
        $ayr_share_list = $ayr_share_model->list(['profile_key'=>['!=','']]);
        foreach ($ayr_share_list as $v){
            $this->output('执行数据的邮箱--'.$v['title']);
            $time = Carbon::now()->modify('-1 days')->toDateString();
            //创建时间小于7天前的当前时间
            if($v['created_at'] > $time){
                $this->output('创建时间小于7天跳过--'.$v['title']);
                continue;
            }
            //查询当前用户是否有未推送的博文
            $release_info = $this->release_info($ayr_release,$v);
            //有推文时,直接跳出循环
            if($release_info !== false){
                $this->output('有未推送的推文直接跳过--'.$v['title']);
                continue;
            }
            //查询7天是否发送博文
            $release_info = $this->release_seven_info($ayr_release,$v);
            //有发送博文,则跳出循环
            if($release_info  !== false){
                $this->output('7天内有推文跳过--'.$v['title']);
                continue;
            }
            $aiVideoInfo = $this->aiVideoInfo($v['project_id'] ?? 0);
            if($aiVideoInfo  !== false){
                $this->output('7天内有ai视频推送跳过--'.$v['title']);
                continue;
            }
            //删除用户第三方配置
            if(!empty($v['profile_key'])){
                $res = $this->del_profiles($v);
                if($res === false){
                    continue;
                }
            }
            //更新数据库
            $this->save_ayr_share($ayr_share_model,$v);
        }
        return $this->error;
    }

    /**
     * @name   :(删除第三方配置)del_profiles
     * @author :lyh
     * @method :post
     * @time   :2023/6/14 16:10
     */
    public function del_profiles($v){
        $ayr_share_helper = new AyrShareHelper();
        $data_profiles = [
            'title'=>$v['title'],
            'profileKey'=>$v['profile_key']
        ];
        $res = $ayr_share_helper->deleted_profiles($data_profiles);
        if($res['status'] == 'fail'){
            $this->output('第三方删除失败'.json_encode($data_profiles,true));
            return false;
        }
        return true;
    }

    /**
     * @name   :(更新数据库)save_ayr_share
     * @author :lyh
     * @method :post
     * @time   :2023/6/14 16:14
     */
    public function save_ayr_share(&$ayr_share_model,$v){

        //更新数据库
        $data = [
            'title'=>'',
            'bind_platforms'=>'',
            'profile_key'=>'',
            'ref_id'=>'',
        ];
        $res = $ayr_share_model->edit($data,['id'=>$v['id']]);
        if($res == false){
            echo '更新数据库失败';
            return true;
        }
        return true;
    }

    /**
     * @name   :(查询是否有定时发送报文)info
     * @author :lyh
     * @method :post
     * @time   :2023/6/14 16:17
     */
    public function release_info(&$ayr_release,$v){
        //查询当前用户是否有未推送的博文
        $release_info = $ayr_release->read(['schedule_date'=>['>',date('Y-m-d H:i:s',time())],'share_id'=>$v['id']]);
        return $release_info;
    }

    /**
     * @param $ayr_release
     * @name   :7天内无发送记录release_seven_info
     * @author :lyh
     * @method :post
     * @time   :2023/6/14 16:28
     */
    public function release_seven_info(&$ayr_release,$v){
        //查看用户是否在一周内有发送博客
        $start_at = Carbon::now()->modify('-7 days')->toDateString();
        $end_at = Carbon::now()->toDateString();
        $release_info = $ayr_release->read(['created_at'=>['between',[$start_at,$end_at]],'share_id'=>$v['id']]);
        return $release_info;
    }

    /**
     * @remark :7天内是否推送了ai视频
     * @name   :aiVidoe
     * @author :lyh
     * @method :post
     * @time   :2025/9/22 17:13
     */
    public function aiVideoInfo($project_id)
    {
        if($project_id == 0){
            return false;
        }
        $start_at = Carbon::now()->modify('-7 days')->toDateString();
        $end_at = Carbon::now()->toDateString();
        $aiVideoModel = new AiVideoTask();
        $videoInfo = $aiVideoModel->read(['project_id'=>$project_id,'next_auto_date'=>null,'created_at'=>['between',[$start_at,$end_at]]]);
        return $videoInfo;
    }

    /**
     * 输入日志
     * @param $message
     * @return bool
     */
    public function output($message)
    {
        $message = date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
        echo $message;
        file_put_contents(storage_path('logs/share_user/') . date('Ymd') . '.log', $message, FILE_APPEND);
        return true;
    }
}