listsSql.php 4.6 KB
<?php

namespace Model;

/**
 * 邮件列表
 * @author:dc
 * @time 2023/2/17 14:15
 * Class lists
 * @package Model
 */
class listsSql {

    /**
     * 表
     * @var string
     */
    public static $table = 'lists';


    /**
     * 查询列表
     * @param string $where
     * @param int $p
     * @param int $size
     * @return string
     * @author:dc
     * @time 2023/3/16 18:11
     */
    public static function lists(string $where, int $p, int $size){

        $filed = '`id`,`uid`,`subject`,`from`,`from_name`,`to`,`date`,`size`,`recent`,`flagged`,`answered`,`deleted`,`seen`,`draft`,`udate`,`folder_id`,`is_file`,`cc`,`bcc`,`description`,`email_id`';

        return "select {$filed} from `".static::$table."` where ".$where." order by `udate` desc limit {$size} offset ".(($p-1)*$size);

    }

    /**
     * 统计列表
     * @param string $where
     * @return string
     * @author:dc
     * @time 2023/3/16 18:10
     */
    public static function listCount(string $where){

        return "select count(*) from `".static::$table."` where ".$where;
    }


    /**
     * 获取已有的uid
     * @param int $email_id
     * @param int $folder_id
     * @param array $uids
     * @return string
     * @author:dc
     * @time 2023/4/23 16:54
     */
    public static function getUids(int $email_id, int $folder_id, array $uids){
        return "select `uid` from `".static::$table."` where ".dbWhere(['email_id'=>$email_id,'folder_id'=>$folder_id,'uid'=>$uids]);
    }



    /**
     * 根据id查询
     * @param string $where
     * @return string
     * @author:dc
     * @time 2023/3/17 16:24
     */
    public static function first(string $where,$filed='*'):string {

        return "select {$filed} from `".self::$table."` where ".$where.' limit 1';
    }

    /**
     * 查询所有
     * @param $where
     * @param string $filed
     * @return string
     * @author:dc
     * @time 2024/1/22 11:15
     */
    public static function all($where,$filed='*'){
        return "select {$filed} from `".self::$table."` where ".$where;
    }


    /**
     * 存草稿
     * @param array $data
     * @param array $email
     * @param int $draftid
     * @return int
     * @author:dc
     * @time 2023/4/11 9:44
     */
    public static function saveDraft(array $data, array $email,int $draftid = 0):int {
        $draftData = [
            'uid'   =>  -rand(0,99999999),
            'subject'   =>  $data['subject'],
            'from'   =>  $data['email'],
            'from_name'   =>  $data['nickname'],
            'date'   =>  time(),
            'udate'   =>  time(),
            'draft'   =>  1,
            'seen'   =>  1,
            'folder_id'   =>  db()->value(folderSql::first(['folder'=>'草稿箱','email_id'=>$email['id']],'`id`')),
            'email_id'    =>  $email['id'],
            'is_file'  =>  $data['attachment'] ? 1 : 0, //是否附件
            'description'  =>  mb_substr(strip_tags($data['body']),0,150), //是否附件
            'references'    =>  [
                'receipt'   =>  $data['receipt'],
                'priority'  =>  $data['priority'],
                'massSuit'  =>  $data['massSuit']?1:0,
                'jobName'  =>  $data['jobName']??'',
            ]
        ];
        $draftData['to'] = $data['tos'][0]['email'];
        $draftData['to_name'] = $data['tos'];
        $draftData['cc'] = $data['cc'];
        $draftData['bcc'] = $data['bcc'];

//        $draftData['uuid'] = md5($draftData['email_id'].$draftData['folder_id'].rand(1111111,9999999999));

        if($draftid){
            // 修改
            if(!db()->update(listsSql::$table,$draftData,dbWhere(
                [
                    'id'    =>  $draftid,
                    'email_id'  =>  $draftData['email_id']
                ]
            ))){
                return 0;
            }
        }else{
            $draftid = db()->insert(listsSql::$table,$draftData);
        }

        if($draftid){
            $draftBody = [
                [
                    'type'  =>  'text/html',
                    'charset'   =>  'utf-8',
                    'body'  =>  base64_encode($data['body'])
                ]
            ];

            foreach ($data['attachment'] as $da){
                $da['name'] = base64_encode($da['name']);
                $da['filename'] = base64_encode($da['filename']);
                $draftBody[] = $da;
            }

            bodySql::insertOrUpdate(db(),[
                'lists_id'    =>  $draftid,
                'text_html'  =>  $draftBody
            ]);

            return $draftid;

        }else{
            return 0;
        }
    }


}