MessageModel.class.php 6.91 KB
<?php
namespace Common\Model;

class MessageModel extends AbstractModel
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 添加留言内容
     * User: <362431947@qq.com>
     * @param array $data
     * @return bool|mixed
     * Date: 2018-10-10 Time: 22:32
     */
    public function addOne($data = [])
    {
        if(! is_array($data)|| ! $data){
            return false;
        }

        $data['created'] = $this->microtime();

        $result = $this->_m->insert($data);

        return $result;
    }

    /**
     * 留言信息列表
     * @author <362431947@qq.com>
     * @date   2018-10-10
     * @param  $page   int 页码
     * @return array
     */
    public function message_list($conds = [],$page = 0)
    {
        if ($page < 1 || !$page) {
            $page = self::PAGING_DEFAULT_PAGE;
        }

        $params = [];
        $wheres = [];

        $conds['a.status'] = $this->get_st_create();


        if (!$this->_parse_where($wheres, $params, $conds)) {
            return false;
        }

        $page_size = self::PAGING_DEFAULT_LIMIT;

        $page_start = ($page - 1) * $page_size;

        $option = [$page_start,$page_size];



        $m_person = new PersonModel();
        $table = $m_person->get_tname();


        if (in_array('title',$conds)) {
            $where = " title LIKE '?' ";
        }
        else{
            $fields = array_keys($conds);

            if ($fields != 'status') {
                $field = $fields[0];
            }

            $where = "  $field = ? ";

        }

        $sql = "SELECT a.message_id,a.title,a.message,IFNULL(b.name,'') AS `from`,IFNULL(c.name,'') AS `to`,a.checked
                FROM __TABLE__ a 
                LEFT JOIN {$table} b ON a.uid = b.person_id 
                LEFT JOIN {$table} c ON a.receiver_uid = c.person_id 
                WHERE $where";

        $list =  $this->_m->fetch_array($sql, $params,$option);

        $messageList['total'] = count($list);
        $messageList['page'] = $page;
        $messageList['limit'] = $page_size;
        $messageList['list'] = $list;

        return $messageList;
    }

    /**
     * 后台留言删除
     * User: <362431947@qq.com>
     * @param int $message_id
     * @return bool
     * Date: 2018-10-10 Time: 22:24
     */
    public function del($message_id = 0)
    {
        $this->_m->startTrans();


        $attachment_id = $this->getAttachmentIdByMsgId($message_id);

        // 删除相关附件图片
        $attachment_del = true;
        if ($attachment_id) {
            $m_attachment = new AttachmentModel();
            $attachment_del = $m_attachment->del($attachment_id);
        }



        // 删除留言
        $message_del =  $this->_m->delete($message_id);


        // 删除留言评论

        $m_comment = new CommentModel();
        $exists = $m_comment->getCommentByMsgId($message_id = 0);

        $comment_del = true;
        if ($exists) {
            $comment_del = $m_comment->del($message_id);
        }

        if ($message_del && $comment_del && $attachment_del) {
            $this->_m->commit();
            return true;
        }
        else{
            $this->_m->rollback();
            return false;
        }
    }

    /**
     * 根据搜索条件获取留言信息
     * User: <362431947@qq.com>
     * @param array $conds 搜索条件
     * @return array|bool
     * Date: 2018-10-10 Time: 22:24
     */
    public function search($conds = [])
    {
        $params = [];
        $wheres = [];

        if (!$this->_parse_where($wheres, $params, $conds)) {
            return false;
        }

        $m_person = new PersonModel();
        $table = $m_person->get_tname();

        if (in_array('title',$conds)) {
            $where = " title LIKE ? ";
        }
        else{
            $fields = array_keys($conds);
            $field = $fields[0];

            $where = " $field = ? ";
        }

        if ($where) {
            $sql = "SELECT a.* FROM __TABLE__ a 
                JOIN {$table} b ON a.uid = b.person_id 
                WHERE $where";
        }
        else {
            return [];
        }


        return $this->_m->fetch_array($sql,$params);
    }

    /**
     * 留言详情
     * User: <362431947@qq.com>
     * @param int $message_id
     * @return array|bool
     * Date: 2018-10-11 Time: 11:47
     */
    public function message_detail($message_id = 0)
    {
        $params = [];
        $wheres = [];

        $conds = ['message_id' =>$message_id];

        if (!$this->_parse_where($wheres, $params, $conds)) {
            return false;
        }

        $m_person = new PersonModel();
        $table_person = $m_person->get_tname();


        $sql = "SELECT a.message_id,a.title,a.message,b.name as `from`,IFNULL(c.name,'') as `to` 
            FROM __TABLE__ a
            LEFT JOIN {$table_person} b ON a.uid = b.person_id
            LEFT JOIN {$table_person} c ON a.receiver_uid = c.person_id
            WHERE `message_id` = ? ";

        $message =  $this->_m->fetch_row($sql,$params);


        $m_attachment = new AttachmentModel();
        $table_attachment = $m_attachment->get_tname();


        $attachment_id_str = $this->getAttachmentIdByMsgId($message_id);

        $attachment = [];
        if ($attachment_id_str) {
            $sql = "SELECT `path` FROM {$table_attachment} 
                WHERE attachment_id  IN ($attachment_id_str)";

            $attachment = $this->_m->fetch_array($sql,$params);
        }


        $message['attachment'] = $attachment;

        // 获取评论信息
        $m_comment = new CommentModel();
        $message['comment'] = $m_comment->getCommentByMsgId($message_id);

        return $message;
    }

    /**
     * getAttachmentIdByMsgId
     * User: <362431947@qq.com>
     * @param int $message_id
     * @return bool
     * Date: 2018-10-10 Time: 22:18
     */
    public function getAttachmentIdByMsgId($message_id = 0)
    {
        $params = [];
        $wheres = [];
        $conds = [
            'message_id' => $message_id,
        ];
        if (!$this->_parse_where($wheres, $params, $conds)) {
            return false;
        }

        $sql = "SELECT attachment
                FROM __TABLE__ WHERE `message_id` = ? ";
        $attachment =  $this->_m->fetch_row($sql,$params);

       return isset($attachment['attachment']) ? $attachment['attachment'] : '';
    }

    /**
     * 留言审核
     * User: <362431947@qq.com>
     * @param int $message_id
     * @return bool
     * Date: 2018-10-11 Time: 21:32
     */
    public function check($message_id = 0,$checked = 1)
    {
//        // 未审核状态
//        $conds = [
//            'message_id' => $message_id,
//        ];

        $exists = $this->_m->field('message_id')->find($message_id);

        if ($exists) {

            $data = [
                'checked' =>$checked,
            ];

            return $this->update($message_id,$data,true);
        }
        else{
            return false;
        }

    }
}