<?php
namespace app\service\controller;

/**
 * 个人快捷语类
 */
class Words extends Common
{


    /**
     * 获取用户快捷语
     *
     * @access public
     * @return array JsonString
     */
    public function userWords()
    {
        // 验证token.
        $tokenStatus = $this->verifyToken();
        $code        = -2;
        $msg         = '错误';
        if ($tokenStatus === false) {
            $msg = 'token错误';
            return json(['code' => $code, 'data' => [], 'msg' => $msg]);
        }

        try {
            // 获取用户信息.
            $getUserInfo = $this->getUserInfo();
            // 获取字段.
            $wordsField = [
                'id',
                'content',
                'title',
            ];
            // 条件.
            $userWordsWhere['user_id'] = $getUserInfo->id;
            // 查询用户私有快捷语.
            $userWords = model('words')->selectWords($wordsField, $userWordsWhere);

            return json(['code' => 1, 'data' => $userWords, 'msg' => '成功']);
        } catch (\Exception $e) {
            return json(['code' => $code, 'data' => [], 'msg' => $msg]);
        }//end try

    }//end userWords()


    /**
     * 快捷语修改
     *
     * @access public
     * @return array JsonString
     */
    public function updateWords()
    {
        // 验证token.
        $tokenStatus = $this->verifyToken();
        $code        = -2;
        $msg         = '错误';
        if ($tokenStatus === false) {
            $msg = 'token错误';
            return json(['code' => $code, 'data' => [], 'msg' => $msg]);
        }

        try {
            // 获取用户信息.
            $getUserInfo    = $this->getUserInfo();
            $userWordsWhere = [
                'user_id' => $getUserInfo->id,
                'id'      => input('post.id'),
            ];
            $content        = input('post.content');
            $status         = input('post.status');
            $title          = input('post.title');
            $userWordsData['update_time'] = date('Y-m-d H:i:s');
            // 更新内容.
            if (strlen($content)) {
                $userWordsData['content'] = input('post.content');
            }

            if (strlen($status)) {
                $userWordsData['status'] = input('post.status');
            }

            if (strlen($title)) {
                $userWordsData['title'] = input('post.title');
            }

            $hasWhere['title']   = input('post.title');
            $hasWhere['user_id'] = $getUserInfo->id;
            $hasWhere['id']      = [
                '<>',
                input('post.id'),
            ];
            // 查询是否存在.
            $has = model('Words')->findHas($hasWhere);
            if (empty($has) === false) {
                return json(['code' => -1, 'data' => [], 'msg' => '该快捷语已经存在']);
            }

            // 更新.
            if (isset($userWordsData) === true) {
                model('Words')->updateWords($userWordsWhere, $userWordsData);
            }

            // 参数返回.
            return json(['code' => 1, 'data' => [], 'msg' => '成功']);
        } catch (\Exception $e) {
            return json(['code' => $code, 'data' => [], 'msg' => $msg]);
        }//end try

    }//end updateWords()


    /**
     * 删除快捷语
     *
     * @access public
     * @return array JsonString
     */
    public function deleteWords()
    {
        // 验证token.
        $tokenStatus = $this->verifyToken();
        $code        = -2;
        $msg         = '错误';
        if ($tokenStatus === false) {
            $msg = 'token错误';
            return json(['code' => $code, 'data' => [], 'msg' => $msg]);
        }

        try {
            // 获取用户信息.
            $getUserInfo               = $this->getUserInfo();
            $userWordsWhere['user_id'] = $getUserInfo->id;
            foreach (input('post.') as $v) {
                $userWordsWhere['id'] = $v;
                model('Words')->deleteWords($userWordsWhere);
            }

            // 参数返回.
            return json(['code' => 1, 'data' => [], 'msg' => '成功']);
        } catch (\Exception $e) {
            return json(['code' => $code, 'data' => [], 'msg' => $msg]);
        }//end try

    }//end deleteWords()


    /**
     * 新增快捷语
     *
     * @access public
     * @return array JsonString
     */
    public function addWords()
    {
        // 验证token.
        $tokenStatus = $this->verifyToken();
        $code        = -2;
        $msg         = '错误';
        if ($tokenStatus === false) {
            $msg = 'token错误';
            return json(['code' => $code, 'data' => [], 'msg' => $msg]);
        }

        try {
            // 获取用户信息.
            $getUserInfo         = $this->getUserInfo();
            $hasWhere['title']   = input('post.title');
            $hasWhere['user_id'] = $getUserInfo->id;
            // 查询是否存在.
            $has = model('Words')->findHas($hasWhere);
            if (empty($has) === false) {
                return json(['code' => -1, 'data' => [], 'msg' => '该快捷语已经存在']);
            }

            $userWordsData = [
                'user_id'     => $getUserInfo->id,
                'title'       => input('post.title'),
                'content'     => input('post.content'),
                'status'      => input('post.status'),
                'update_time' => date('Y-m-d H:i:s'),
            ];
            $addResult     = model('Words')->addWords($userWordsData);

            // 参数返回.
            if (empty($addResult) === false) {
                return json(['code' => 1, 'data' => ['id' => $addResult], 'msg' => '成功']);
            } else {
                return json(['code' => 1, 'data' => [], 'msg' => 1]);
            }
        } catch (\Exception $e) {
            return json(['code' => $code, 'data' => [], 'msg' => $msg]);
        }//end try

    }//end addWords()


}
