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

namespace app\admin\controller\v1;


use app\BaseController;
use app\model\admin\Admin;
use app\model\admin\AdminLog;
use library\services\UtilService;
use library\utils\IpLocation;
use app\Request;

class Log extends  BaseController
{

    public function list(Request $request)
    {
        $pageSize = 50;
        $post = UtilService::getMore([
            ['page',1],
            ['data',''],
            ['action',''],
            ['username',''],
            ['ip',''],
            ['sassid',$request->site['sassid']]
        ]);
        list($pageCount,$data) = (new AdminLog())
            ->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']}%");
                }
                $query->where('sassid',$post['sassid']);
        }
        ],$pageSize,'*','id desc');
        $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(Request $request){
        $time = strtotime('- 1 month');
        AdminLog::where([['time','<',$time],['sassid','=',$request->site['sassid']]])->delete();
        return app('json')->success('清理成功');
    }

}