<?php

namespace app\admin\controller;

use app\BaseController;
use app\admin\model\IpConfig as IpConfigModel;
use app\admin\validate\IpConfigValidate;
use Exception;
use think\facade\Db;
use think\facade\Cache;

class IpConfig extends BaseController
{
    /**
     * @api {post} /ip/delete IP黑名单 删除
     *
     */
    function delete()
    {

        Db::startTrans();
        try {
            $params = (new IpConfigValidate())->goCheck('id');
            $count = IpConfigModel::where('id', $params['id'])->delete();
            if ($count < 1) throw new Exception('操作失败');
            Db::commit();
        } catch (Exception $e) {
            Db::rollBack();
            return $this->error($e->getMessage());
        }
        //删除缓存
        Cache::delete('ip_blacklist');
        Cache::delete('ip_whitelist');
        return $this->success();
    }


    /**
     * @api {post} /ip/update IP 更新
     */
    function update()
    {
        Db::startTrans();
        try {

            $params = (new IpConfigValidate())->post()->goCheck('edit');
            $id = $this->request->param('id',0);
            $params['operation_id'] = $this->admin_id;
            if (!$id) {
                $ip = IpConfigModel::where('ip', $params['ip'])->find();
                if ($ip) {
                    return $this->error('IP已存在');
                }
                IpConfigModel::create($params);
            } else {
                $ip = IpConfigModel::where('id', $id)->find();
                if (!$ip) {
                    return $this->error('IP不存在');
                }
                IpConfigModel::where('id', $id)->update($params);
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollBack();
            return $this->error($e->getMessage());
        }
        //删除缓存
        Cache::delete('ip_blacklist');
        Cache::delete('ip_whitelist');
        return $this->success();
    }

    /**
     * @api {get} /ip/list IP查询
     */
    function list()
    {
        try {
            $params = $this->request->param();
            $page = $params['page'] ?? 1;
            $limit = $params['limit'] ?? 15;
            $query = IpConfigModel::alias('ip_config')->join('admin', 'admin.id = ip_config.operation_id', 'left');
            if (!empty($params['keyword'])) {
                $query = $query->where('ip_config.ip', 'like', '%'.$params['keyword'].'%');
            }
            if (!empty($params['status'])) {
                $query = $query->where('ip_config.status', $params['status']);
            }
            if (!empty($params['type'])) {
                $query = $query->where('ip_config.type', $params['type']);
            }
            $count = $query->count();
            $list = $query
                ->field(['ip_config.*', 'admin.nickname as operator_name'])
                ->order('updated_at','desc')
                ->limit($limit)
                ->page($page)
                ->select();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success(['count' => $count, 'list' => $list]);
    }
}