package com.gxwebsoft.common.system.controller;

import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.WhiteDomainService;
import com.gxwebsoft.common.system.entity.WhiteDomain;
import com.gxwebsoft.common.system.param.WhiteDomainParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务器白名单控制器
 *
 * @author 科技小王子
 * @since 2024-03-26 00:22:21
 */
@Tag(name = "安全")
@RestController
@RequestMapping("/api/system/white-domain")
public class WhiteDomainController extends BaseController {
    @Resource
    private WhiteDomainService whiteDomainService;
    @Resource
    private RedisUtil redisUtil;

    @PreAuthorize("hasAuthority('sys:whiteDomain:list')")
    @Operation(summary = "分页查询服务器白名单")
    @GetMapping("/page")
    public ApiResult<PageResult<WhiteDomain>> page(WhiteDomainParam param) {
        // 使用关联查询
        return success(whiteDomainService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('sys:whiteDomain:list')")
    @Operation(summary = "查询全部服务器白名单")
    @GetMapping()
    public ApiResult<List<WhiteDomain>> list(WhiteDomainParam param) {
        // 使用关联查询
        return success(whiteDomainService.listRel(param));
    }

    @PreAuthorize("hasAuthority('sys:whiteDomain:list')")
    @Operation(summary = "根据id查询服务器白名单")
    @GetMapping("/{id}")
    public ApiResult<WhiteDomain> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(whiteDomainService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('sys:whiteDomain:list')")
    @OperationLog
    @Operation(summary = "添加服务器白名单")
    @PostMapping()
    public ApiResult<?> save(@RequestBody WhiteDomain whiteDomain) {
        String key = "WhiteDomain:";
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          key = key + loginUser.getTenantId();
          whiteDomain.setUserId(loginUser.getUserId());
        }
        if (whiteDomainService.save(whiteDomain)) {
          // 重写缓存
          final List<WhiteDomain> list = whiteDomainService.list();
          final List<String> collect = list.stream().map(WhiteDomain::getDomain).collect(Collectors.toList());
          redisUtil.set(key,collect);
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:whiteDomain:list')")
    @OperationLog
    @Operation(summary = "修改服务器白名单")
    @PutMapping()
    public ApiResult<?> update(@RequestBody WhiteDomain whiteDomain) {
        if (whiteDomainService.updateById(whiteDomain)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:whiteDomain:list')")
    @OperationLog
    @Operation(summary = "删除服务器白名单")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (whiteDomainService.removeById(id)) {
            // 重写缓存
            String key = "WhiteDomain:";
            User loginUser = getLoginUser();
            if (loginUser != null) {
              key = key + loginUser.getTenantId();
            }
            final List<WhiteDomain> list = whiteDomainService.list();
            final List<String> collect = list.stream().map(WhiteDomain::getDomain).collect(Collectors.toList());
            redisUtil.set(key,collect);
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:whiteDomain:list')")
    @OperationLog
    @Operation(summary = "批量添加服务器白名单")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<WhiteDomain> list) {
        if (whiteDomainService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:whiteDomain:list')")
    @OperationLog
    @Operation(summary = "批量修改服务器白名单")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<WhiteDomain> batchParam) {
        if (batchParam.update(whiteDomainService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:whiteDomain:list')")
    @OperationLog
    @Operation(summary = "批量删除服务器白名单")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (whiteDomainService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
