<?php
/**
 * +----------------------------------------------------------------------
 * 评论
 * +----------------------------------------------------------------------
 * 官网：https://www.sw-x.cn
 * +----------------------------------------------------------------------
 * 作者：小黄牛 <1731223728@qq.com>
 * +----------------------------------------------------------------------
 * 开源协议：http://www.apache.org/licenses/LICENSE-2.0
 * +----------------------------------------------------------------------
*/

namespace app\controller\admin\index;
use app\controller\SuperClass\Admin;

class Comment extends Admin
{

    /**
     * @AopBefore(class="app\aop\AdminLogin", function="before")
     * @RequestMapping(route="/admin/index/comment_submit", method="POST", title="提交评论")
    */
    public function run() {
        $Db = new \x\Db();
        $param = \x\Request::post();

        $res = $Db->name('comment')->insert([
            'admin_id' => $this->admin['id'],
            'task_id' => $param['task_id'],
            'content' => $param['content'],
            'create_time' => time(),
        ]);
        $Db->return();
        if (!$res) return $this->returnJson('01', '发表失败，数据库写入异常');

        return $this->returnJson('00', '发表成功！');
    }

    /**
     * @AopBefore(class="app\aop\AdminLogin", function="before")
     * @RequestMapping(route="/admin/index/comment_list", method="POST", title="评论查询")
    */
    public function comment_list() {
        $Db = new \x\Db();
        $param = \x\Request::post();

        $list = $Db->name('comment')->alias('A')
                ->join('auth_admin B', 'A.admin_id=B.id')
                ->where('A.task_id', $param['task_id'])
                ->order('A.id asc')
                ->field('A.*, B.color, B.nick, B.identity, B.logo')
                ->select();
        $Db->return();
        foreach ($list as $key => $value) {
            $list[$key]['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
        }
        return $this->returnJson('00', '查询成功！', $list);
    }
}

