MailListV2.php 14.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
<?php

namespace Controller\fob_ai;



use Controller\Base;
use Lib\Mail\MailFun;
use Lib\Verify;
use Model\folderSql;
use function Swoole\Coroutine\Http\request;

/**
 * 黑格 fob 那边专用 业务
 * 为定制逻辑
 * @author:dc
 * @time 2024/7/18 11:40
 * Class MailList
 * @package Controller\fob_ai
 */
class MailListV2 extends Base {

    private $cache = 600;

    /**
     * 当前邮箱下指定的文件夹
     * @param string $folder
     * @return array
     * @throws \Lib\Err
     * @author:dc
     * @time 2024/7/19 11:37
     */
    private function getFolderId(string $folder,$emails=[]){
        // 查询 文件夹
        static $folderList;
        if(!$folderList){
            $folderList = db()->all(folderSql::all($emails?:$this->getEmails('id')));
        }
        $folder_id = [];
        // 文件夹id
        if($folderList){
            foreach ($folderList as $item){
                if(folderAlias($item['folder']) == $folder){
                    $folder_id[] = $item['id'];
                }
            }
        }
        if(!$folder_id){
            app()->e('folder_not_fount');
        }

        return $folder_id;
    }


    /**
     * 处理like条件
     * @param $str
     * @return string
     * @author:dc
     * @time 2024/9/4 11:14
     */
    private function getLikeStr($str){
        if(str_starts_with($str, '^')){
            return addslashes(mb_substr($str,1,99)).'%%';
        }
        return '%%'.addslashes($str).'%%';
    }

    /**
     * 邮件列表
     * 接收参数
     *  page 分页
     * limit 每页数量
     * mail_id 指定邮件id
     * attachment 是否附件
     * emails 邮箱
     * folder 文件夹
     * from_not_in 不在这些发件箱的邮件
     * @throws \Lib\Err
     * @author:dc
     * @time 2024/7/18 11:40
     */
    public function lists(){

        // 分页 页数
        $page   =   app()->request('page',1,'intval');
        $page   =   $page ? $page : 1;

        $limit   =   app()->request('limit',20,'intval');
        $limit   =   $limit ? $limit : 1;

        // 指定id
        $ids = app()->request('mail_id');
        $ids = is_array($ids) ? $ids : [$ids];
        foreach ($ids as $i=>$d){
            if(!is_numeric($d)){
                unset($ids[$i]);
            }
        }

        $where = [];
        // 目录
        $folder = app()->request('folder','收件箱');
        // 只允许查询这里文件夹
        if(!in_array($folder,['收件箱','发件箱','垃圾箱','星标邮件','预热收件箱','预热发件箱','自动回复收件箱','Starred'])){
            app()->e('folder_not_fount');
        }
        $where['is_hots'] = 0;

        if($folder=='星标邮件'||$folder=='Starred'){
            $folder = '收件箱';
            $where['flagged'] = 1; // 星标
            unset($where['is_hots']);
        }elseif ($folder=='预热收件箱'){
            $folder = '收件箱';
            $where['is_hots'] = 1;
        }elseif ($folder=='预热发件箱'){
            $folder = '发件箱';
            $where['is_hots'] = 1;
        } elseif ($folder=='自动回复收件箱'){
            $is_auto_inbox = 1;
            $folder = '收件箱';
            $where['is_hots'] = 0;
        }

        $seen =   app()->request('seen',-1,'intval');
        // 已读/未读
        if(paramHas('seen')){
            if(in_array($seen,[0,1])){
                $where['seen'] = $seen;
            }
        }

        // 星标就不加文件夹赛选
        if(empty($where['flagged'])){
//            $folder_id = $this->getFolderId($folder);

            //目录
//            $where['folder_id'] = $folder_id;
            $where['_'] = trim(trim($this->assignSql($folder)),'and');
        }else{
//            $where['lists|email_id'] = $this->getEmails('id');
            $where['_'] = trim(trim($this->assignSql2()),'and');
        }

        if($ids) $where['id'] = $ids;

        if(app()->request('attachment',0,'bool_Val')){
            // 附件
            $where['is_file'] = 1; //附件
        }

        // 软删
        $where['deleted'] = 0;

//        $where['from.notin'] = $this->from_not_in();
//        $where['_'] = "`from` not like 'postmaster@%%'";
//        if(!$where['from.notin']) unset($where['from.notin']);

        $filed = '`id`,`uid`,`subject`,`from`,`from_name`,`flagged`,`seen`,`udate`,`folder_id`,`is_file`,`description`,`lists`.`email_id`,`to_name`';

        $sql = "select %s from `lists` where ".dbWhere($where);

        /************ 2024-09-02 *************/
        if($folder=='收件箱' && empty($where['flagged'])) {

            $sql = "select %s from `lists` left join `lists_auto` on `lists`.`id`= `lists_auto`.`list_id` where ".dbWhere($where);
            if(!empty($is_auto_inbox)){
                $sql.= ' and `lists_auto`.`list_id` IS NOT NULL';
            }else{
                $sql.= ' and `lists_auto`.`list_id` IS NULL';

                // 模糊标题搜索
                $subject = app()->request('subject');
                if ($subject) {
                    $subject = is_array($subject) ? $subject : [$subject];
                    foreach ($subject as $k => $sub) {
                        $subject[$k] = '`subject` ' . (empty($is_auto_inbox) ? ' not ' : '') . 'like "' . $this->getLikeStr($sub) . '"';
                    }
                }
                // 不在此邮箱中的
                $from_not_in_like = app()->request('from_not_in_like');
                if ($from_not_in_like) {
                    $from_not_in_like = is_array($from_not_in_like) ? $from_not_in_like : [$from_not_in_like];
                    foreach ($from_not_in_like as $k => $sub) {
                        $from_not_in_like[$k] = '`from` ' . (empty($is_auto_inbox) ? ' not ' : '') . 'like "' . $this->getLikeStr($sub) . '"';
                    }
                }

                if ($subject || $from_not_in_like) {
                    $sql .= ' and (';
                    if ($subject) $sql .= implode(empty($is_auto_inbox) ? ' and ' : ' or ', $subject);
                    if ($from_not_in_like) $sql .= ' and (' . implode(empty($is_auto_inbox) ? ' and ' : ' or ', $from_not_in_like) . ')';
                    $sql .= ')';
                }

            }

        }

        /************ 2024-09-02 *************/

        // 查询列表数据
        $lists = db()->all(sprintf($sql,$filed).(empty($where['flagged'])?'':' group by `udate`')." order by `udate` desc limit {$limit} offset ".(($page-1)*$limit));

        // map
        $lists = array_map(function ($v){
            $v['uuid'] = md5($v['email_id'].'-'.$v['folder_id'].'-'.$v['uid']);
            if(!empty($v['description'])){
                $v['description'] = @html_entity_decode($v['description'], ENT_COMPAT, 'UTF-8');
            }
            $v['to_name'] = @json_decode($v['to_name'],true);
            $v['to_name'] = $v['to_name']?:[];
            if($v['to_name']){
                if(!empty($v['to_name'][0]['email'])){
                    $v['to'] = $v['to_name'][0]['email'];
                }
                $v['to_name'] = MailFun::mb_coding($v['to_name'][0]['name']??'');
            }
            if(is_array($v['to_name'])){
                $v['to_name'] = '';
            }
            return $v;
        },$lists?:[]);

        // 总数
        if(empty($where['flagged'])){
            $total  = db()->cache($this->cache)->count(sprintf($sql,"count(*)"));
        }else{
            $total  = db()->cache($this->cache)->count("select count(*) from (".sprintf($sql,"`id`").' group by `udate`) as t');
        }


        app()->_json(listsPage($lists,$total,$page,$limit));

    }



    private function assignSql($folder){
        // 被分配的
        $assign = app()->request('assign');
        $sql = '';
        $fids = [];
        if(!empty($assign['email_id'])){
            // 此处请求中的
            $email = array_values(array_intersect($assign['email_id'],$this->getEmails('id')));
            if($email){
                $fids = $this->getFolderId($folder,$email);
                // 有目录id和from
                if($fids && !empty($assign['from'])){
                    $sql = dbWhere(['folder_id'=>$fids, $folder=='发件箱'?'to':'from'=>$assign['from']]);
                }
            }

            // 返回 不在assign中的目录
            $afids = array_values(array_diff($this->getFolderId($folder),$fids));

            if($sql && $afids){
                return '(('.dbWhere(['folder_id'=>$afids]).') or ('.$sql.')) and ';
            }else if($afids){
                return dbWhere(['folder_id'=>$afids]).' and ';
            }else if($sql&&!$afids){
                return '('.$sql.') and ';
            }

            return '`folder_id` = 0 and ';

        }

        return dbWhere(['folder_id'=>$this->getFolderId($folder)]).' and ';

    }
    private function assignSql2(){
        // 被分配的
        $assign = app()->request('assign');
        $sql = '';
        if(!empty($assign['email_id'])){
            // 分配的联系人
            $email = array_values(array_intersect($assign['email_id'],$this->getEmails('id')));
            if($email&&!empty($assign['from'])){
                $sql = dbWhere(['email_id'=>$email]).' and ('.dbWhere(['from'=>$assign['from']]).' or '.dbWhere(['to'=>$assign['from']]).')';
            }

            // 分配的邮箱
            $email2 = array_diff($this->getEmails('id'),$assign['email_id']);
            if($email2){
                if($sql){
                    return '('.dbWhere(['email_id'=>$email2]).' or ('.$sql.')) and ';
                }
                return dbWhere(['email_id'=>$email2]);
            }

            if($sql){
                return '('.$sql.') and ';
            }


            return '`email_id` = 0 and ';
        }

        // 默认
        return dbWhere(['email_id'=>$this->getEmails('id')]).' and ';

    }
    private function assignSql3($folder){
        // 被分配的
        $assign = app()->request('assign');
        $sql = '';
        $fids = [];
        if(!empty($assign['email_id'])){
            // 此处请求中的
            $email = array_diff($this->getEmails('id'),$assign['email_id']);
            if($email){
                $fids = $this->getFolderId($folder,$email);
                // 有目录id和from
                if($fids){
                    return dbWhere(['folder_id'=>$fids]);
                }
            }


            return '`folder_id` = 0 and ';

        }

        return dbWhere(['folder_id'=>$this->getFolderId($folder)]).' and ';

    }

    /**
     * 统计收件箱的数量
     * @author:dc
     * @time 2024/7/19 10:15
     */
    public function count(){
        $emails = $this->getEmails('id');

        // show_count_filed
        $show_count_filed = app()->requestArr('show_count_filed',['inbox', 'send', 'unseen', 'flagged', 'junk', 'hot_inbox', 'hot_send']);

        $sql = "select count(*) from `lists` where ";

        $where['deleted']   =   0; //未删状态

        // 发件箱
        if(in_array('hot_send',$show_count_filed)){
//            $where['folder_id'] = $this->getFolderId('发件箱');
            // 预热发件箱
            $where['is_hots'] = 1;
            $fCount = db()->cache($this->cache)->count($sql.$this->assignSql3('发件箱').dbWhere($where));
        }

        // 预热收件箱
        if(in_array('hot_inbox',$show_count_filed)) {
//            $where['folder_id'] = $this->getFolderId('收件箱');
            $where['is_hots'] = 1;
            $sCount = db()->cache($this->cache)->count($sql .$this->assignSql3('收件箱'). dbWhere($where));
        }
        unset($where['folder_id']);

        if(in_array('send',$show_count_filed)) {
//            $where['folder_id'] = $this->getFolderId('发件箱');
            $where['is_hots'] = 0;
            $faCount = db()->cache($this->cache)->count($sql . $this->assignSql('发件箱').dbWhere($where));
        }
// 垃圾箱
        if(in_array('junk',$show_count_filed)) {
//            $where['folder_id'] = $this->getFolderId('垃圾箱');
            $where['is_hots'] = 0;
            $lajiCount = db()->cache($this->cache)->count($sql . $this->assignSql('垃圾箱').dbWhere($where));
        }

        // 收件箱
//        $where['folder_id'] = $this->getFolderId('收件箱');
        $where['is_hots'] = 0;
        $sql = "select count(*) from `lists` left join `lists_auto` on `lists`.`id`= `lists_auto`.`list_id` where `lists_auto`.`list_id` IS NULL and ".$this->assignSql('收件箱');

        // 模糊标题搜索
        $subject = app()->request('subject');
        if($subject){
            $subject = is_array($subject) ? $subject : [$subject];
            foreach ($subject as $k=>$sub){
                $subject[$k] = '`subject` not like "'.$this->getLikeStr($sub).'"';
            }
        }
        // 不在此邮箱中的
        $from_not_in_like = app()->request('from_not_in_like');
        if($from_not_in_like){
            $from_not_in_like = is_array($from_not_in_like) ? $from_not_in_like : [$from_not_in_like];
            foreach ($from_not_in_like as $k=>$sub){
                $from_not_in_like[$k] = '`from`  not like "'.$this->getLikeStr($sub).'"';
            }
        }

        if($subject || $from_not_in_like){
            $sql .= ' (';
            if($subject) $sql.= implode(' and ',$subject);
            if($from_not_in_like) $sql.= ' and ('.implode(' and ',$from_not_in_like).')';
            $sql .= ') and ';
        }


        $shouCount = db()->cache($this->cache)->count($sql . dbWhere($where));

        // 未读
        if(in_array('unseen',$show_count_filed)) {
            $where['seen'] = 0;
            $where['is_hots'] = 0;
            $seenCount = db()->cache($this->cache)->count($sql . dbWhere($where));
            unset($where['seen']);
        }




        // 星标
        if(in_array('flagged',$show_count_filed)) {
            $where['flagged'] = 1;
            unset($where['folder_id']);
//            $where['email_id'] = $emails;
            unset($where['is_hots']);
            $flaggedCount = db()->cache($this->cache)->count("SELECT COUNT(*) FROM (select `id` from `lists` where " . $this->assignSql2().dbWhere($where).' group by `udate`) as c');
        }

        $data = [];
        if(isset($shouCount)) $data['inbox'] = $shouCount;
        if(isset($faCount)) $data['send'] = $faCount;
        if(isset($seenCount)) $data['unseen'] = $seenCount;
        if(isset($flaggedCount)) $data['flagged'] = $flaggedCount;
        if(isset($lajiCount)) $data['junk'] = $lajiCount;
        if(isset($sCount)) $data['hot_inbox'] = $sCount;
        if(isset($fCount)) $data['hot_send'] = $fCount;

        app()->_json($data);

    }



}