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

namespace app\system\controller;

use app\BaseController;
use app\model\system\AdminLog;
use app\model\api\UserLogin;
use app\model\api\User as UserModel;
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 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']}%");
                }
            },
            ], $pageSize, '*', 'id desc');
        $result = UtilService::getParam([
            'id',
            'ip',
            'admin_username',
            'action',
            'controller',
            'title',
            'post',
            'get',
            ['time', 'time', 'date("Y-m-d H:i:s",$1)'],
            ['ipara', 'ip', function ($item) {
                return "";
//                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');
        AdminLog::where('time', '<', $time)->delete();
        return app('json')->success('清理成功');
    }
    
    
    
    public function listLoginLog()
    {
        $pageSize = 50;
        $post     = UtilService::getMore([
            ['page', 1],
            ['data', ''],
            ['uid', ''],
            ['mobile', ''],
        ]);
        $where = [];
        if(!empty($post["uid"])){
            $where["l.uid"] = $post["uid"];
        }

        if(!empty($post["mobile"])){
            $udata = (new UserModel)->where("mobile",$post["mobile"])->find();
            if(!empty($udata)){
                $where["l.uid"] = $udata["uid"];
            }
        }
        $data = (new UserLogin)
                ->alias("l")
                ->field("l.*,u.mobile,u.nickname")
                ->leftJoin("user u","u.uid = l.uid")
                ->where($where)
                ->when(!empty($post["data"]), function ($query) use ($post) {
                    if ($post['data'] == "lately7") {
                        $sTime = date('Y-m-d', strtotime('-7 day'));
                        $eTime = date('Y-m-d');
                        $query->whereBetweenTime('l.time', $sTime, $eTime);
                    } else if ($post['data'] == "lately30") {
                        $sTime = date('Y-m-d', strtotime('-30 day'));
                        $eTime = date('Y-m-d');
                        $query->whereBetweenTime('l.time', $sTime, $eTime);
                    } else if ($post['data'] == "today") {
                        $query->whereTime('l.time', 'today');
                    } else if ($post['data'] == "yesterday") {
                        $query->whereTime('l.time', 'yesterday');
                    } else if ($post['data'] == "month") {
                        $query->whereMonth('l.time');
                    } else if ($post['data'] == "year") {
                        $query->whereYear('l.time');
                    } else {
                        $split = explode('-', $post['data']);
                        if (count($split) == 2) {
                            $sTime = $split[0];
                            $eTime = $split[1];
                            $query->whereBetweenTime('l.time', $sTime, $eTime);
                        }
                    }
                })
                ->page((int)$post["page"], $pageSize)
                ->order("l.id desc")
                ->select()
                ->toArray();
        $pageCount = (new UserLogin)
                ->alias("l")
                ->where($where)
                ->when(!empty($post["data"]), function ($query) use ($post) {
                    if ($post['data'] == "lately7") {
                        $sTime = date('Y-m-d', strtotime('-7 day'));
                        $eTime = date('Y-m-d');
                        $query->whereBetweenTime('l.time', $sTime, $eTime);
                    } else if ($post['data'] == "lately30") {
                        $sTime = date('Y-m-d', strtotime('-30 day'));
                        $eTime = date('Y-m-d');
                        $query->whereBetweenTime('l.time', $sTime, $eTime);
                    } else if ($post['data'] == "today") {
                        $query->whereTime('l.time', 'today');
                    } else if ($post['data'] == "yesterday") {
                        $query->whereTime('l.time', 'yesterday');
                    } else if ($post['data'] == "month") {
                        $query->whereMonth('l.time');
                    } else if ($post['data'] == "year") {
                        $query->whereYear('l.time');
                    } else {
                        $split = explode('-', $post['data']);
                        if (count($split) == 2) {
                            $sTime = $split[0];
                            $eTime = $split[1];
                            $query->whereBetweenTime('l.time', $sTime, $eTime);
                        }
                    }
                })
                ->order("l.id desc")
                ->count();
                
        foreach($data as $k=>$v){
            $data[$k]["time"] = date("Y-m-d H:i:s",$v["time"]);
            $data[$k]["title"] = "{$v['nickname']}进行了登录";
        }

        return app('json')->success([
            'list'      => $data,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page'],
        ]);
    }

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

}