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

use common\helpers\Date;
use common\models\Message;
use common\models\MessageText;

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

    /**
     * 列表
     * @return mixed
     * @throws \yii\web\HttpException
     */
    public function actionList(){
        $condition = ' AND MT.IS_DEL=0 AND M.USER_ID=:USER_ID';
        $params = [':USER_ID'=>\Yii::$app->user->id];
        $data = Message::lists($condition, $params, [
            'select' => 'M.ID,MT.TITLE,MT.CREATED_AT,M.IS_READ',
            'from' => Message::tableName().' AS M',
            'join' => [
                ['LEFT JOIN', MessageText::tableName().' AS MT', 'M.TEXT_ID=MT.ID'],
            ],
            'orderBy' => 'MT.CREATED_AT DESC',
            'useSlaves' => true,
        ]);
        return static::notice($data);
    }

    /**
     * 获取站内信详细
     * @return mixed
     * @throws \yii\web\HttpException
     */
    public function actionDetail(){
        $id = \Yii::$app->request->get('id');
        $data = null;
        if($id){
            $oneMessage = Message::findOne($id);
            if($oneMessage){
                $data = MessageText::find()->where('ID=:ID AND IS_DEL=0', [':ID'=>$oneMessage['TEXT_ID']])->select('ID,TITLE,CONTENT,CREATED_AT')->asArray()->one();
                $data['CONTENT_DATA'] = is_resource($data['CONTENT']) && $data['CONTENT']  ? base64_decode(stream_get_contents($data['CONTENT'])) : '';
                //$data['ATTACHMENT'] = json_decode(stripslashes($data['ATTACHMENT']));
                $data['ATTACHMENT'] = json_decode('');
                $oneMessage->IS_READ = 1;
                $oneMessage->READ_AT = Date::nowTime();
                $oneMessage->save();
            }
        }
        if($data){
            return static::notice($data);
        } else {
            return static::notice('站内信不存在', 400);
        }
    }

    /**
     * 抓取站内信
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\web\HttpException
     */
    public function actionPull(){
        Message::pullMsgByUser(\Yii::$app->user->id);
        return static::notice('抓取完成');
    }

    /**
     * 获取未读数量
     * @return mixed
     * @throws \yii\web\HttpException
     */
    public function actionUnreadNum(){
        return static::notice(Message::unreadNum(\Yii::$app->user->id));
    }

    /**
     * 获取前五条未读信息
     * @return mixed
     * @throws \yii\web\HttpException
     */
    public function actionUnreadText(){
        // 获取前5条为读站内信
        $allData = Message::find()->select('M.ID,MT.TITLE,MT.CREATED_AT')->from(Message::tableName().' AS M')->join('LEFT JOIN', MessageText::tableName().' AS MT', 'M.TEXT_ID=MT.ID')->where('M.USER_ID=:USER_ID AND M.IS_READ=0', [':USER_ID'=>\Yii::$app->user->id])->orderBy('M.CREATED_AT DESC')->limit(5)->asArray()->all();
        return static::notice($allData);
    }
}