<?php
/**
 * Created by PhpStorm.
 * Author: NODELOG
 * DateTime: 2017/3/8 16:32
 * Description:
 */

namespace api\common\models;


use api\modules\v1\models\Circle;
use api\modules\v1\models\Clock;
use api\modules\v1\models\ClockLike;
use api\modules\v1\models\Comment;
use api\modules\v1\models\UserCar;
use common\models\Article;
use common\models\Footprint;
use common\models\Friend;
use common\models\SearchRecord;
use common\models\Suggest;
use yii\helpers\Url;

class User extends \common\modules\user\models\User
{
    /**
     * 默认属性
     * @return array|false
     * @author nodelog
     */
    public function fields()
    {
        return [
            'nickname',
            'tel',
            'avatar' => function ($model) {
                return Url::to($model->getAvatar(), true);
            },
            'profile',
            'count',
        ];
    }

    /**
     * 扩展属性
     * 请求时接口url加参数，如：?expand=profile
     * @return array|false
     * @author nodelog
     */
    public function extraFields()
    {
        return [
            'profile',
            'count',
        ];
    }

    /**
     * 数据统计
     * @author nodelog
     */
    public function getCount()
    {
        $data = [];

        $where = ['user_id' => \Yii::$app->user->id];

        $searchCount = SearchRecord::find()->where($where)->andWhere(['del' => 0])->sum('count');
        if (empty($searchCount)) {
            $searchCount = 0;
        }

        $pagePathList = \Yii::$app->config->get('page_path_list');

        $data[] = [
            'title' => '搜索',
            'count' => $searchCount,
            'link' => $pagePathList['search_page'],
            'link_type' => 1
        ];
        $data[] = [
            'title' => '足迹',
            'count' => Footprint::find()->my()->count(),
            'link' => $pagePathList['footprint_page'],
            'link_type' => 1
        ];
        $data[] = [
            'title' => '企业反馈',
            'count' => Article::find()->my()->count(),
            'link' => $pagePathList['company_page'],
            'link_type' => 1
        ];
        $data[] = [
            'title' => '意见反馈',
            'count' => Suggest::find()->where($where)->count(),
            'link' => $pagePathList['feedback_page'],
            'link_type' => 1
        ];

        return $data;
    }


    /**
     * 获取用户信息
     * @return \yii\db\ActiveQuery
     * @author nodelog
     */
    public function getProfile()
    {
        return $this->hasOne(Profile::className(), ['user_id' => 'id']);
    }

}