<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/2/24
 * Time: 下午12:48
 */
namespace backendApi\modules\v1\controllers;

use backendApi\modules\v1\models\Admin;
use common\models\forms\MessageForm;
use common\models\Message;
use common\models\MessageText;
use common\models\UserInfo;
use Yii;

class MessageController extends BaseController
{
    public $modelClass = Message::class;

    /**
     * 列表
     * @return mixed
     * @throws \yii\web\HttpException
     */
    public function actionList(){
        //$filter = $this->filterCondition(MessageText::tableName());
        $condition = ' AND M.IS_DEL=0 ';
        $params = '';
        $data = MessageText::lists($condition, $params, [
            'select' => 'M.ID,M.TITLE,M.CONTENT,M.TO_UID,M.CREATED_AT,UI.USER_NAME,A.ADMIN_NAME',
            'from' => MessageText::tableName().' AS M',
            'join' => [
                ['LEFT JOIN', UserInfo::tableName().' AS UI', 'M.TO_UID=UI.USER_ID'],
                ['LEFT JOIN', Admin::tableName().' AS A', 'M.ADMIN_ID=A.ID'],
            ],
            'orderBy' => 'M.CREATED_AT DESC',
        ]);
        return static::notice($data);
    }

    /**
     * 添加站内信
     * @return mixed
     * @throws \yii\web\HttpException
     */
    public function actionAdd(){
        if(Yii::$app->request->isPost) {
            return parent::edit(MessageForm::class, '站内信发送成功');
        }
    }

    /**
     * 删除
     * @return mixed
     * @throws \yii\db\Exception
     * @throws \yii\web\HttpException
     */
    public function actionDelete(){
        $result = static::delete(MessageText::class);
        return $result;
    }

}