<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-08-31 20:43
// +----------------------------------------------------------------------

namespace app\system\controller\v1;


use app\BaseController;
use app\model\system\SystemAdmin;
use app\model\system\SystemLog;
use library\services\UtilService;
use library\utils\IpLocation;

class Log extends  BaseController
{

    public function list()
    {








        $pageSize = 50;
        $post = UtilService::getMore([
            ['page',1],
            ['data',''],
            ['action',''],
            ['username',''],
            ['ip','']
        ]);

        list($pageCount,$data) = (new SystemLog())
            ->getList($post['page'],[function($query) use($post){
                if($post['data'] == "lately7") {
                    $sTime = date('Y-m-d',strtotime('-7 day'));
                    $eTime = date('Y-m-d');
                    $query->whereBetweenTime('time',$sTime,$eTime);
                }else if($post['data'] == "lately30") {
                    $sTime = date('Y-m-d',strtotime('-30 day'));
                    $eTime = date('Y-m-d');
                    $query->whereBetweenTime('time',$sTime,$eTime);
                }else if($post['data'] == "today") {
                    $query->whereTime('time','today');
                }else if($post['data'] == "yesterday") {
                    $query->whereTime('time','yesterday');
                } else if($post['data'] == "month") {
                    $query->whereMonth('time');
                }else if($post['data'] == "year") {
                    $query->whereYear('time');
                } else {
                    $split = explode('-',$post['data']);
                    if(count($split) == 2) {
                        $sTime = $split[0];
                        $eTime =  $split[1];
                        $query->whereBetweenTime('time', $sTime, $eTime);
                    }
                }
                if(!empty($post['username'])) {
                    $query->whereLike('admin_username',"%{$post['username']}%");
                }

                if(!empty($post['action'])) {
                    $query->whereLike('action',"%{$post['action']}%");
                }

                if(!empty($post['ip'])) {
                    $query->whereLike('ip',"%{$post['ip']}%");
                }
        }
        ],$pageSize,'*','id desc');
        //echo (new SystemLog)->getLastSql();
        $result = UtilService::getParam([
            'id',
            'ip',
            'admin_username',
            'action',
            'title',
            ['time','time','date("Y-m-d H:i:s",$1)'],
            ['ipara','ip',function($item){
                return  (new IpLocation())->getlocation($item)['country'];
            }]
        ],$data);
        return app('json')->success([
            'list'      => $result,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page']
        ]);
    }

    /**
     * 清理一个月之前的数据
     */
    public function delMonth(){
        $time = strtotime('- 1 month');
        SystemLog::where('time','<',$time)->delete();
        return app('json')->success('清理成功');
    }

}