<?php
/**
 * 日志模型
 * Created by PhpStorm.
 * User: XiaoMing
 * Date: 2019/10/30
 * Time: 14:26
 */

namespace JinDouYun\Model\Log;

use Mall\Framework\Core\StatusCode;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\ResultWrapper;

use JinDouYun\Dao\Log\DLoginLog;

class MLoginLog
{
    private $objDLoginLog;
    private $enterpriseId;

    public function __construct($enterpriseId)
    {
        $this->enterpriseId = $enterpriseId;
        $this->objDLoginLog = new DLoginLog('log');
        //$this->objDLoginLog->setSearchIndex('login_log')->setType('login');
    }

    /**
     * 搜索条件 ES
     * @param $selectParams
     * @return array
     */
    public function setSearchWhere($selectParams = [])
    {
        $defaultDSL = [];
        if (isset($selectParams['offset'])) {
            $defaultDSL['from'] = $selectParams['offset'];
        }
        if (isset($selectParams['limit'])) {
            $defaultDSL['size'] = $selectParams['limit'];
        }
        $defaultDSL['sort'] =[
                'createTime' => [
                    'order' => 'desc',
                    'unmapped_type' => 'long',
                ]
        ];

        $dsl = [];
        $dsl['query']['bool']['must'][] = [
            'term' => ['enterpriseId' => $this->enterpriseId],
        ];

        $dsl = array_merge($defaultDSL, $dsl);
        return $dsl;
    }

    /**
     * @param array $selectParams 过滤条件
     * @return ResultWrapper
     * @throws Exception
     * @throws \Exception
     */
    public function getAllLoginLog($selectParams)
    {

        return ResultWrapper::success(['data'=>[],'total'=>0]);

        /*
        $dsl = $this->setSearchWhere($selectParams);
        $result = $this->objDLoginLog->getSearchQueryDsl($dsl);

        if (isset($result['status']) && $result['status'] == 400) {
            return ResultWrapper::success([
                'data'  => [],
                'total' => 0
            ]);
        }

        if (!isset($result['hits']) || $result['hits']['total'] == 0) {
            return ResultWrapper::success([
                'data'  => [],
                'total' => 0
            ]);
        }
        $total = $result['hits']['total'];
        $dbResult = $result['hits']['hits'];

        $list = [];
        foreach ($dbResult as $key => &$value) {
            $data = [];
            $data = $value['_source'];
            $data['id'] = $value['_id'];
            $list[] = $data;
        }

        $return = [
            'data'  => $list,
            'total' => ($total) ? intval($total) : 0,
        ];
        return ResultWrapper::success($return);*/
    }
}