| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace common\models\elasticsearch\user;
- use common\models\base\EsBaseActiveRecord;
- use common\models\user\User;
- use yii\behaviors\TimestampBehavior;
- use yii\elasticsearch\ActiveRecord;
- /**
- * Class LoginLog
- * @package addons\RfExample\common\models
- */
- class LoginLog extends EsBaseActiveRecord
- {
- public static $currentIndex;
- /**
- * 数据库名称
- * @return string
- */
- public static function index()
- {
- return 'es_log';
- }
- /**
- * 表名
- *
- * @return string
- */
- public static function type()
- {
- return 'user_login_log';
- }
- public function attributes()
- {
- $mapConfig = self::mapConfig();
- return array_keys($mapConfig['properties']);
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id','nickname','user_id','ip','platform'], 'required'],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'mall_id' => '商城id',
- 'user_id' => '用户id',
- 'nickname' => '昵称',
- 'platform' => '登录渠道',
- 'ip' => '登录ip',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- ];
- }
- /**
- * mapping配置(表字段说明)
- *
- * 如果需要在mapping中添加其他的字段,那么添加后在运行一次updateMapping()
- * 另外需要注意的是:elasticSearch的mapping是不能删除的,建了就是建了,如果要删除,您只能删除index(相当于mysql的db)
- * 然后重建mapping,因此,您最好写一个脚本,执行es的所有model的mapping。
- *
- * @return array
- */
- public static function mapConfig()
- {
- return [
- 'properties' => [
- // 不想进行分词等操作,想当成一个和数据库类似的搜索 设置为not_analyzed
- // index 默认可不设置
- 'mall_id' => ['type' => 'keyword'],
- 'user_id' => ['type' => 'keyword'],
- 'nickname' => ['type' => 'keyword'],
- 'platform' => ['type' => 'keyword'],
- 'ip' => ['type' => 'keyword'],
- 'created_at' => ['type' => 'long'],
- 'updated_at' => ['type' => 'long'],
- ],
- ];
- }
- /**
- * @return array
- */
- public static function mapping()
- {
- return [
- static::type() => self::mapConfig(),
- ];
- }
- /**
- * 更新字段
- *
- * @throws \yii\base\InvalidConfigException
- */
- public static function updateMapping()
- {
- $db = self::getDb();
- $command = $db->createCommand();
- if (!$command->indexExists(self::index())) {
- $command->createIndex(self::index());
- }
- $command->setMapping(self::index(), self::type(), self::mapping(),['include_type_name'=>'true']);
- }
- /**
- * @return array
- */
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class,
- 'attributes' => [
- ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
- ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
- ],
- ],
- ];
- }
- /**
- * 设置数据
- * @param $params
- */
- public static function setData($params){
- $add_data = ['LoginLog'=>$params];
- $model = new self();
- $model->load($add_data);
- $model->save();
- }
- /**
- * 获取登录渠道
- * @param $user_id
- * @return string
- */
- public static function getAllPlatform($user_id){
- $query = self::find();
- $count = $query->where(['user_id'=>$user_id])->count();
- $page_size = 100;
- $page = ceil($count/$page_size);
- $platform_arr = [];
- for ($i=0;$i<=$page;$i++){
- $list = $query->offset($i*$page_size)->limit($page_size)->asArray()->all();
- if(!empty($list)){
- foreach ($list as $v){
- if(!empty($v['_source']['platform'])){
- if(isset(User::TYPE_NAME_LIST[$v['_source']['platform']])){
- if(!in_array(User::TYPE_NAME_LIST[$v['_source']['platform']],$platform_arr)){
- $platform_arr[]=User::TYPE_NAME_LIST[$v['_source']['platform']];
- }
- }
- }
- }
- }
- }
- return join('/',$platform_arr);
- }
- /**
- * 获取登录日志列表
- * @param $params
- * @return array
- */
- public static function getLoginLogList($params){
- return self::listPage($params,true);
- }
- }
|