作者 邓超

x

正在显示 1 个修改的文件 包含 32 行增加130 行删除
<?php
use Model\listsSql;
/**
* 只有黑格 在使用此业务
... ... @@ -15,7 +17,10 @@
*/
class HotMail {
public function __construct(){$this->start();}
public function __construct(){
$this->db = db();
$this->start();
}
/**
* shopk那边的预热邮箱
... ... @@ -24,6 +29,12 @@ class HotMail {
private $shopkHotEmail = [];
/**
* @var \Lib\Db|\Lib\DbPool
*/
private $db;
/**
* @author:dc
* @time 2024/7/18 14:04
*/
... ... @@ -41,140 +52,48 @@ class HotMail {
$this->shopkHotEmail['job@aicc-email.com'] = 1;
$db = db();
foreach ($this->shopkHotEmail as $e=>$i){
try {
if(!$db->count("select count(*) from `hot_mail` where ".dbWhere(['email'=>$e]))){
$db->throw()->insert('hot_mail',['email'=>$e],false);
if(!$this->db->count("select count(*) from `hot_mail` where ".dbWhere(['email'=>$e]))){
$this->db->throw()->insert('hot_mail',['email'=>$e],false);
}
}catch (Throwable $e){}
}
// if($this->shopkHotEmail){
// array_map([$this,'moveMail'],$fob);
// }
$maxId = $this->db->value("select `id` from `lists` order by `id` desc limit 1");
for ($i=0;$i<ceil($maxId/1000);$i++){
$this->run(rand($i*1000,($i+1)*1000));
}
echo '已执行完成'.PHP_EOL;
}
/**
* 邮件检查及移动
* @param string $email 检查的邮箱
* @author:dc
* @time 2024/7/18 14:06
*/
private function moveMail(string $email){
echo '正在处理 '.$email."\n";
// 读取邮箱在表里的id
$email_id = db()->value(sprintf("select `id` from `%s` where `email` = '%s' limit 1",\Model\emailSql::$table, $email));
if($email_id){
$this->findFolder($email_id,'收件箱','s');
$this->findFolder($email_id,'发件箱','f');
$this->findFolder($email_id,'垃圾箱','l');
}
return true;
}
private $folder = [];
/**
* 查找文件夹
* @param int $email_id 邮箱id
* @param string $folder 那个文件夹
* @author:dc
* @time 2024/7/18 15:10
*/
private function findFolder(int $email_id, string $folder,string $type){
// 查询 文件夹
$folders = db()->all(\Model\folderSql::all($email_id,'`id`,`folder`'));
if(!$folders){
return true;
}
foreach ($folders as $f){
if(folderAlias($f['folder']) == $folder){
// 最后记录的id
// $last_id = db()->value('select max(`lists_id`) from `fob_hot_mail` where `email_id` = '.$email_id.' and `folder` = "'.$type.'" limit 1');
// $last_id = $last_id?$last_id:0;
$this->findList(new Data($email_id,$f['id'],$type,0));
}
}
return true;
private function run($id){
$list = $this->db->all(\Model\listsSql::first(dbWhere(['id'=>$id,'is_hots'=>0]),'`id`,`from`,`to`,`folder_id`'));
foreach ($list as $item){
if(empty($this->folder[$item['folder_id']])){
$this->folder[$item['folder_id']] = folderAlias($this->db->value(\Model\folderSql::first($item['folder_id'],'folder')));
}
/**
* 查找 邮件
* @author:dc
* @time 2024/7/18 15:11
*/
private function findList(Data $data, int $p = 1){
$lists = db()->throw()->all(
sprintf(
"select `id`,`folder_id`,`lists`.`email_id`,`%s` from `lists` left join `fob_hot_mail` on `fob_hot_mail`.`lists_id` = `lists`.`id` where `lists`.`email_id` = %d and `folder_id` = %d and `fob_hot_mail`.`lists_id` is null order by `id` asc limit 1000 offset ".(($p-1)*1000),
$data->type=='f' ? 'to_name':'from', // 收件箱查询 from字段 发件箱 查询 to_name字段
$data->email_id,
$data->folder_id
)
);
if($lists){
echo '找到邮件 '.count($lists)."\n";
// 处理数据
foreach ($lists as $list){
$list['folder'] = $data->type;
$this->insertData($list);
}
$lists = null;
$list = null;
// 再次查找
$this->findList($data,$p + 1);
}
return true;
}
/**
* 插入数据
* @param array $data
* @author:dc
* @time 2024/7/18 15:25
*/
private function insertData(array $data){
$isInsert = false;
if($data['folder'] != 'f'){
// 收件箱 匹配 from 字段
$isInsert = isset($this->shopkHotEmail[strtolower($data['from'])]);
// 是否是发件箱
if($this->folder[$item['folder_id']] == '发件箱'){
$w = ['email' => explode(',',$item['to'])];
}else{
// 发件箱 匹配 to_name字段
$to_name = is_array($data['to_name']) ? $data['to_name'] : @json_decode($data['to_name'],1);
if($to_name){
foreach ($to_name as $item){
// 找到了就退出去
if(isset($this->shopkHotEmail[strtolower($item['email'])])){
$isInsert = true;
$data['from'] = $item['email'];
break;
$w = ['email' =>$item['from']];
}
// 是否在 预热邮箱中
if($this->db->count('select count(*) from `hot_mail` where '.dbWhere($w))){
echo date('d H:i:s').' ==》 '.$item['id']."\n";
$this->db->update(listsSql::$table,['is_hots'=>1],dbWhere(['id'=>$item['id']]));
}
}
}
// 是预热 邮箱发来的邮件
if($isInsert){
// 不存在 数据
if(db()->count("select count(*) from `fob_hot_mail` where `lists_id` = ".$data['id']) == 0){
db()->insert('fob_hot_mail',[
'lists_id' => $data['id'],
'email_id' => $data['email_id'],
'hot_form' => $data['from']??'',
'folder' => $data['folder']
],false);
}
}
return true;
}
/**
* 获取预热邮箱
... ... @@ -221,23 +140,6 @@ class HotMail {
}
class Data {
public int $email_id = 0;
public int $folder_id = 0;
public string $type = '';
public int $last_id = 0;
public function __construct(int $email_id,int $folder_id, string $type,int $last_id)
{
$this->email_id = $email_id;
$this->folder_id = $folder_id;
$this->type = $type;
$this->last_id = $last_id;
}
}
swoole_set_process_name('hot-email-run-man');
$pm = new Swoole\Process\Manager();
... ...