package com.ruoyi.project.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.redis.RedisCache;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysIp;
import com.ruoyi.project.system.service.ISysIpService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 授权登录IPController
 *
 * @author ruoyi
 * @date 2023-10-16
 */
@RestController
@RequestMapping("/system/ip")
public class SysIpController extends BaseController {
    @Autowired
    private ISysIpService sysIpService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询授权登录IP列表
     */
    @PreAuthorize("@ss.hasPermi('system:ip:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysIp sysIp) {
        startPage();
        startOrderBy();
        List<SysIp> list = sysIpService.selectSysIpList(sysIp);
        return getDataTable(list);
    }

    /**
     * 导出授权登录IP列表
     */
    @PreAuthorize("@ss.hasPermi('system:ip:export')")
    @Log(title = "授权登录IP", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysIp sysIp) {
        List<SysIp> list = sysIpService.selectSysIpList(sysIp);
        ExcelUtil<SysIp> util = new ExcelUtil<SysIp>(SysIp.class);
        util.exportExcel(response, list, "授权登录IP数据");
    }

    /**
     * 获取授权登录IP详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:ip:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysIpService.selectSysIpById(id));
    }

    /**
     * 新增授权登录IP
     */
    @PreAuthorize("@ss.hasPermi('system:ip:add')")
    @Log(title = "授权登录IP", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysIp sysIp) {
        sysIp.setCreateBy(getUsername());
        sysIp.setUpdateBy(getUsername());
        redisCache.deleteObject(CacheConstants.IP_WHITE_LIST_KEY + SecurityUtils.slot);
        return toAjax(sysIpService.insertSysIp(sysIp));
    }

    /**
     * 修改授权登录IP
     */
    @PreAuthorize("@ss.hasPermi('system:ip:edit')")
    @Log(title = "授权登录IP", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysIp sysIp) {
        sysIp.setUpdateBy(getUsername());
        redisCache.deleteObject(CacheConstants.IP_WHITE_LIST_KEY + SecurityUtils.slot);
        return toAjax(sysIpService.updateSysIp(sysIp));
    }

    /**
     * 删除授权登录IP
     */
    @PreAuthorize("@ss.hasPermi('system:ip:remove')")
    @Log(title = "授权登录IP", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        redisCache.deleteObject(CacheConstants.IP_WHITE_LIST_KEY + SecurityUtils.slot);
        return toAjax(sysIpService.deleteSysIpByIds(ids));
    }
}
