package org.dromara.zhishu.controller;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.zhishu.domain.vo.WhiteListVo;
import org.dromara.zhishu.domain.bo.WhiteListBo;
import org.dromara.zhishu.service.IWhiteListService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 白名单
 *
 * @author yxy
 * @date 2025-06-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhishu/whiteList")
public class WhiteListController extends BaseController {

    private final IWhiteListService whiteListService;

    /**
     * 查询白名单列表
     */
    @SaCheckPermission("zhishu:whiteList:list")
    @GetMapping("/list")
    public TableDataInfo<WhiteListVo> list(WhiteListBo bo, PageQuery pageQuery) {
        return whiteListService.queryPageList(bo, pageQuery);
    }

    @GetMapping("/selectAll")
    public String selectAll() {

        List<WhiteListVo> voList = whiteListService.selectAll();

        String ips = "";
        for(WhiteListVo vo : voList){
            if(StringUtils.isEmpty(ips)){
                ips = vo.getIp();
            }else{
                ips = ips + "," + vo.getIp();
            }
        }
        return ips;
    }

    /**
     * 导出白名单列表
     */
    @SaCheckPermission("zhishu:whiteList:export")
    @Log(title = "白名单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WhiteListBo bo, HttpServletResponse response) {
        List<WhiteListVo> list = whiteListService.queryList(bo);
        ExcelUtil.exportExcel(list, "白名单", WhiteListVo.class, response);
    }

    /**
     * 获取白名单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("zhishu:whiteList:query")
    @GetMapping("/{id}")
    public R<WhiteListVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(whiteListService.queryById(id));
    }

    @PostMapping("/setWhiteList")
    public R<Void> setWhiteList(String ips) {
        //清空白名单表
        whiteListService.deleteAll();


        //分割ips,创建WhiteListBo进行insert
        String[] ipArray = ips.split(",");
        for (String ip : ipArray){
            WhiteListBo bo = new WhiteListBo();
            bo.setIp(ip);
            bo.setCreateBy(1L);
            whiteListService.insertByBo(bo);
        }
        return R.ok();
    }

    /**
     * 新增白名单
     */
    @SaCheckPermission("zhishu:whiteList:add")
    @Log(title = "白名单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WhiteListBo bo) {
        return toAjax(whiteListService.insertByBo(bo));
    }

    /**
     * 修改白名单
     */
    @SaCheckPermission("zhishu:whiteList:edit")
    @Log(title = "白名单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WhiteListBo bo) {
        return toAjax(whiteListService.updateByBo(bo));
    }

    /**
     * 删除白名单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("zhishu:whiteList:remove")
    @Log(title = "白名单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(whiteListService.deleteWithValidByIds(List.of(ids), true));
    }
}
