<?php

namespace common\models;

use Yii;
use yii\db\Expression;

/**
 * This is the model class for table "log_user_operation".
 *
 * @property integer $id
 * @property string $title
 * @property string $memo
 * @property integer $uid
 * @property integer $user_type
 * @property string $user_name
 * @property integer $resources_id
 * @property integer $resources_type
 * @property string $resources_name
 * @property string $phpsessid
 * @property string $user_agent
 * @property string $create_time
 */
class LogUserOperation extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'log_user_operation';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'memo', 'user_name'], 'required'],
            [['memo'], 'string'],
            [['uid', 'user_type', 'resources_id', 'resources_type'], 'integer'],
            [['create_time'], 'safe'],
            [['title'], 'string', 'max' => 120],
            [['user_name', 'resources_name', 'phpsessid', 'user_agent','last_login'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'memo' => 'Memo',
            'uid' => 'Uid',
            'user_type' => 'User Type',
            'user_name' => 'User Name',
            'resources_id' => 'Resources ID',
            'resources_type' => 'Resources Type',
            'resources_name' => 'Resources Name',
            'phpsessid' => 'Phpsessid',
            'user_agent' => 'User Agent',
            'create_time' => 'Crate Time',
            'last_login' => 'last_login'
        ];
    }

    public function getList($param, $type)
    {
        //where条件
        $where = ['and'];
        if ($param['user_name'] != '') {
            $where[] = ['=', 'user_name', $param['user_name']];
        }
        if ($param['title'] != '') {
            $where[] = ['like', 'title', $param['title']];
        }
        if ($param['resource_id'] != '') {
            $where[] = ['=', 'resources_id', $param['resource_id']];
        }
        if ($param['create_time_start'] != '') {
            $where[] = ['>=', 'create_time', $param['create_time_start']];
        }
        if ($param['create_time_end'] != '') {
            $where[] = ['<', 'create_time', date('Y-m-d', strtotime("{$param['create_time_end']} +1 day"))];
        }
        if ($type == 1) {//获取count
            $select = ["count(1) cnt"];
            $result = self::find()->select($select)
                ->where($where)
                ->asArray()
                ->one();
            return $result['cnt'];
        } else {//获取列表
            //查询字段
            $select = [
                'id',
                'title',
                'memo' ,
                'uid' ,
                'user_type',
                'user_name',
                'resources_id'=>new Expression("if(resources_id=0,' - ',resources_id)"),
                'resources_type',
                'resources_name',
                'phpsessid',
                'user_agent',
                'create_time',
                'last_login',
                new Expression("CASE user_type
                    WHEN 1 THEN '管理员'
                    WHEN 2 THEN '分销商'
                    WHEN 3 THEN '客户'
                    WHEN 4 THEN 'CS管理员'
                    ELSE '-'
                    END
                    as user_type_name
                "),
                new Expression("CASE resources_type
                    WHEN 1 THEN '产品'
                    WHEN 2 THEN '订单'
                    ELSE '-'
                    END
                    as resources_type_name
                ")
            ];
            $offset = ($param['current_page'] - 1) * $param['page_size'];
            $result = self::find()->select($select)
                ->where($where)
                ->orderBy('create_time DESC')
                ->offset($offset)
                ->limit($param['page_size'])
                ->asArray()
                ->all();
            return $result;
        }
    }
}
