UpdateProgress.php 2.8 KB
<?php
/**
 * @remark :
 * @name   :UpdateProgress.php
 * @author :lyh
 * @method :post
 * @time   :2023/11/1 9:22
 */

namespace App\Console\Commands;

use App\Models\Mail\Mail;
use App\Models\Project\Project;
use App\Models\User\User;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Models\Com\UpdateProgress as UpdateProgressModel;

class UpdateProgress extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'update_progress';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '守护进程--更新界面';
    /**
     * @name   :(定时执行)handle
     * @author :lyh
     * @method :post
     * @time   :2023/5/12 14:48
     */
    public function handle()
    {
        //获取所有项目
        $projectModel = new Project();
        $list = $projectModel->list(['type'=>['in',[1,2,3,4]]]);
        try {
            foreach ($list as $v) {
                echo date('Y-m-d H:i:s') . ' start: ' . $v['id'] . PHP_EOL;
                ProjectServer::useProject($v['id']);
                $this->getUpdateProgress($v['id']);
                DB::disconnect('custom_mysql');
            }
        }catch (\Exception $e){
            echo date('Y-m-d H:i:s') . ' error:  ->' . $e->getMessage() . PHP_EOL;
        }
        echo date('Y-m-d H:i:s') . ' end: ' . PHP_EOL;
    }

    /**
     * @remark :查看是否有为更新的记录
     * @name   :updateProgress
     * @author :lyh
     * @method :post
     * @time   :2023/11/1 10:47
     */
    public function getUpdateProgress($project_id){
        $info = DB::connection('custom_mysql')->table('gl_update_progress')->whereRaw('total_num > current_num')->first();
        if(!empty($info)){
            $info = (array)$info;
            //超时时间
            $time =  date("Y-m-d H:i:s",strtotime($info['created_at']) + $info['total_num'] * 30);
            if($time > date("Y-m-d H:i:s")){
                DB::connection('custom_mysql')->table('gl_update_progress')->where('id',$info['id'])->update(['current_num'=>$info['total_num']]);
                //获取当前项目的用户
                $user = new User();
                $info = $user->read(['project_id'=>$project_id,'role'=>0]);
                //发送站内信,请重新更新
                $data["title"] = "页面更新通知";
                $data["user_list"] = $info['id'];
                $data["content"] = "部分页面更新超时,请重新更新,或联系管理员";
                $mail = new Mail();
                $mail->add($data);
            }
        }
        echo date('Y-m-d H:i:s') . ' end: ' . $project_id . PHP_EOL;
        return true;
    }
}