作者 李美松

优化程序

@@ -12,6 +12,8 @@ use Illuminate\Http\File; @@ -12,6 +12,8 @@ use Illuminate\Http\File;
12 12
13 class ProjectFilePDF extends Command 13 class ProjectFilePDF extends Command
14 { 14 {
  15 + use CmdSignal;
  16 +
15 /** 17 /**
16 * The name and signature of the console command. 18 * The name and signature of the console command.
17 * 19 *
@@ -26,12 +28,17 @@ class ProjectFilePDF extends Command @@ -26,12 +28,17 @@ class ProjectFilePDF extends Command
26 */ 28 */
27 protected $description = '网站项目数据,生成PDF文件'; 29 protected $description = '网站项目数据,生成PDF文件';
28 30
29 - protected $AiccWechat; 31 + protected ProjectAssociation $AiccWechat;
30 32
31 - protected $DataFile; 33 + protected DataFile $DataFile;
32 34
33 protected $time; 35 protected $time;
34 36
  37 + // 最大支持5个进程
  38 + public int $maxRunNumber = 50;
  39 +
  40 + public FileController $fileController;
  41 +
35 /** 42 /**
36 * Create a new command instance. 43 * Create a new command instance.
37 * 44 *
@@ -39,73 +46,57 @@ class ProjectFilePDF extends Command @@ -39,73 +46,57 @@ class ProjectFilePDF extends Command
39 */ 46 */
40 public function __construct() 47 public function __construct()
41 { 48 {
42 - $this->AiccWechat = new ProjectAssociation();  
43 - $this->DataFile = new DataFile();  
44 - $this->time = date("Y-m"); 49 + $this->AiccWechat = new ProjectAssociation();
  50 + $this->DataFile = new DataFile();
  51 + $this->time = date("Y-m");
  52 + $this->fileController = new FileController();
45 parent::__construct(); 53 parent::__construct();
46 } 54 }
47 55
48 - /**  
49 - * Execute the console command.  
50 - *  
51 - * @return int  
52 - */  
53 - public function handle() 56 + public function start(): int
54 { 57 {
55 - $data = $this->get_data();  
56 - # 详细数据  
57 - $items = $data['items'];  
58 - # 总分页  
59 - $totalPage = $data['totalPage'];  
60 - $this->dataPush($items);  
61 - if ($totalPage > 1) {  
62 - for ($page = 2; $page <= $totalPage; $page++) {  
63 - $da = $this->get_data();  
64 - $this->dataPush($da['items']);  
65 - }  
66 - }  
67 - $this->info('生成pdf完成');  
68 - return 0;  
69 - }  
70 -  
71 - /**  
72 - * 数据生成并保存  
73 - * @param array $items  
74 - * @return void  
75 - */  
76 - public function dataPush(array $items)  
77 - {  
78 - foreach ($items as $item) {  
79 - $project_id = $item->project_id;  
80 - $user_id = $item->user_id;  
81 - $friend_id = $item->friend_id;  
82 - // todo 根据项目查询数据  
83 - $project_data = [];  
84 - $html = $this->html($project_data);  
85 - $filename = hash('md5', $this->time . '-' . $project_id . '-' . $friend_id . '-' . $user_id);  
86 - $file_path = $this->savePDF($html, $filename);  
87 - var_dump($file_path);  
88 - $this->DataFile->saveData(compact('project_id', 'user_id', 'friend_id', 'file_path') + ['time' => $this->time]); 58 + $status = 1; # 1 - 正常, 0 - 禁用
  59 + $is_pdf = 0; # 0 - 未生成 1 - 已生成 2 - 其它问题
  60 + $lists = $this->DataFile::query()->where('status', $status)
  61 + ->where('project_id', '!=', 0)
  62 + ->where('friend_id', '!=', 0)
  63 + ->where('user_id', '!=', 0)
  64 + ->where('is_pdf', $is_pdf)->where('created_at', 'like', $this->time . '%')->first();
  65 + if (is_null($lists)) {
  66 + $this->debug_echo('没有任务,等待中');
  67 + sleep(60);
  68 + return 0;
89 } 69 }
90 - }  
91 -  
92 -  
93 - public function get_data($page = 1, $perPage = 20)  
94 - {  
95 - $data = $this->AiccWechat->allData($page, $perPage);  
96 - # 总条数  
97 - $total = $data['total'];  
98 - if (empty($total)) {  
99 - $this->error('暂无绑定AICC微信数据'); 70 + $key = $this->signature . '-' . $lists->id;
  71 + $count = redis_get($key);
  72 + $item = $lists->getOriginal();
  73 + $project_id = $item->project_id;
  74 + $user_id = $item->user_id;
  75 + $friend_id = $item->friend_id;
  76 + // todo 根据项目查询数据
  77 + $project_data = [];
  78 + $html = $this->html($project_data);
  79 + $filename = hash('md5', $this->time . '-' . $project_id . '-' . $friend_id . '-' . $user_id);
  80 + $file_path = $this->savePDF($html, $filename);
  81 + if (empty($file_path)) {
  82 + $this->debug_echo('pdf生成失败!');
100 return 0; 83 return 0;
101 } 84 }
102 - # 详细数据  
103 - $items = $data['items'];  
104 - # 总分页  
105 - $totalPage = $data['totalPage'];  
106 - # 当前页  
107 - $currentPage = $data['currentPage'];  
108 - return compact('total', 'items', 'totalPage', 'currentPage'); 85 + $isRes = $this->DataFile->saveData(compact('project_id', 'user_id', 'friend_id', 'file_path') + ['time' => $this->time]);
  86 + if (!$isRes) {
  87 + if ($count == 2) {
  88 + $lists->is_pdf = 2;
  89 + $lists->save();
  90 + $this->debug_echo('项目文件数据保存失败! - 其他原因 - ' . $key);
  91 + } else {
  92 + redis_add($key, $count + 1);
  93 + $this->debug_echo('项目文件数据保存失败! - ' . $key);
  94 + }
  95 + }
  96 + $lists->is_pdf = 1;
  97 + $lists->save();
  98 + $this->debug_echo('项目文件数据保存成功!');
  99 + return 0;
109 } 100 }
110 101
111 public function savePDF($html, $filename) 102 public function savePDF($html, $filename)
@@ -138,16 +129,13 @@ class ProjectFilePDF extends Command @@ -138,16 +129,13 @@ class ProjectFilePDF extends Command
138 // 获取PDF内容 129 // 获取PDF内容
139 $pdfContent = $dompdf->output(); 130 $pdfContent = $dompdf->output();
140 131
141 - $fileController = new FileController();  
142 -  
143 // 将PDF内容保存到文件 132 // 将PDF内容保存到文件
144 @file_put_contents($savePath, $pdfContent); 133 @file_put_contents($savePath, $pdfContent);
145 134
146 // 创建一个文件实例 135 // 创建一个文件实例
147 $file = new File($savePath); 136 $file = new File($savePath);
148 -var_dump($file->getFilename());  
149 -exit();  
150 - return $fileController->single($file); 137 +
  138 + return $this->fileController->single($file);
151 } 139 }
152 140
153 /** 141 /**
@@ -24,10 +24,12 @@ class WebsiteData extends Command @@ -24,10 +24,12 @@ class WebsiteData extends Command
24 protected $description = '向AICC推送数据'; 24 protected $description = '向AICC推送数据';
25 25
26 // 最大支持5个进程 26 // 最大支持5个进程
27 - public $maxRunNumber = 50; 27 + public int $maxRunNumber = 50;
28 28
29 protected $time; 29 protected $time;
30 30
  31 + protected $url;
  32 +
31 /** 33 /**
32 * Create a new command instance. 34 * Create a new command instance.
33 * 35 *
@@ -35,13 +37,17 @@ class WebsiteData extends Command @@ -35,13 +37,17 @@ class WebsiteData extends Command
35 */ 37 */
36 public function __construct() 38 public function __construct()
37 { 39 {
38 - $this->time = date('y-d'); 40 + $this->time = date('Y-m');
  41 + $this->url = env('AICC_URL');
39 parent::__construct(); 42 parent::__construct();
40 } 43 }
41 44
42 45
43 public function start(): int 46 public function start(): int
44 { 47 {
  48 + # 0 - 未推送
  49 + # 1 - 已推送
  50 + # 2 - 其他问题
45 $status = 0; 51 $status = 0;
46 $lists = DataFile::query()->where('status', $status) 52 $lists = DataFile::query()->where('status', $status)
47 ->where('created_at', 'like', $this->time . '%')->first(); 53 ->where('created_at', 'like', $this->time . '%')->first();
@@ -50,44 +56,23 @@ class WebsiteData extends Command @@ -50,44 +56,23 @@ class WebsiteData extends Command
50 sleep(60); 56 sleep(60);
51 return 0; 57 return 0;
52 } 58 }
53 - var_dump($lists);  
54 - exit();  
55 - $data = $lists['items'];  
56 -  
57 - $url = env('AICC_URL');  
58 - $msg = http_post($url, json_encode(compact('data')));  
59 -  
60 - }  
61 -  
62 - /**  
63 - * Execute the console command.  
64 - *  
65 - * @return int  
66 - */  
67 -// public function handle()  
68 -// {  
69 -// $DataFile = new DataFile();  
70 -// $data = $DataFile->allData();  
71 -// # 详细数据  
72 -// $items = $data['items'];  
73 -// # 总分页  
74 -// $totalPage = $data['totalPage'];  
75 -// $this->post_data($items);  
76 -// if ($totalPage > 1) {  
77 -// for ($page = 2; $page <= $totalPage; $page++) {  
78 -// $da = $DataFile->allData($page);  
79 -// $this->post_data($da['items']);  
80 -// }  
81 -// }  
82 -// $this->info('项目文件数据推送完成!');  
83 -// return 0;  
84 -// }  
85 -  
86 - public function post_data($data)  
87 - {  
88 - $url = env('AICC_URL');  
89 - $msg = http_post($url, json_encode(compact('data')));  
90 - print_r($msg); 59 + $key = $this->signature . '-' . $lists->id;
  60 + $count = redis_get($key);
  61 + $isRes = http_post($this->url, json_encode(['data' => $lists->getOriginal()]));
  62 + if (!$isRes) {
  63 + if ($count == 2) {
  64 + $lists->is_pdf = 2;
  65 + $lists->save();
  66 + $this->debug_echo('项目文件数据推送失败! - 其他原因 - ' . $key);
  67 + } else {
  68 + redis_add($key, $count + 1);
  69 + $this->debug_echo('项目文件数据推送失败! - ' . $key);
  70 + }
  71 + }
  72 + $lists->status = 1;
  73 + $lists->save();
  74 + $this->debug_echo('项目文件数据保存成功!');
  75 + return 0;
91 } 76 }
92 77
93 } 78 }
@@ -9,6 +9,7 @@ use GuzzleHttp\Client; @@ -9,6 +9,7 @@ use GuzzleHttp\Client;
9 use GuzzleHttp\Exception\GuzzleException; 9 use GuzzleHttp\Exception\GuzzleException;
10 use Illuminate\Support\Carbon; 10 use Illuminate\Support\Carbon;
11 use App\Models\File\File; 11 use App\Models\File\File;
  12 +use Illuminate\Support\Facades\Redis;
12 13
13 define('HTTP_OPENAI_URL', 'http://openai.waimaoq.com/'); 14 define('HTTP_OPENAI_URL', 'http://openai.waimaoq.com/');
14 /** 15 /**
@@ -616,3 +617,29 @@ function getRouteMap($source,$source_id){ @@ -616,3 +617,29 @@ function getRouteMap($source,$source_id){
616 } 617 }
617 return $route; 618 return $route;
618 } 619 }
  620 +
  621 +function redis_get($key){
  622 + return Redis::connection()->client()->get($key);
  623 +}
  624 +function redis_del(...$key){
  625 + return Redis::connection()->client()->del(...$key);
  626 +}
  627 +
  628 +function redis_set($key,$val,$ttl=3600){
  629 + return Redis::connection()->client()->set($key,$val,$ttl);
  630 +}
  631 +
  632 +/**
  633 + * 添加缓存,存在则失败
  634 + * @param $key
  635 + * @param $val
  636 + * @param int $ttl
  637 + * @return mixed
  638 + * @author:dc
  639 + * @time 2023/10/25 9:48
  640 + */
  641 +function redis_add($key,$val,$ttl=3600){
  642 + return Redis::connection()->client()->eval(
  643 + "return redis.call('exists',KEYS[1])<1 and redis.call('setex',KEYS[1],ARGV[2],ARGV[1])", [$key, $val, $ttl], 1
  644 + );
  645 +}