package com.gxwebsoft.common.system.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.DomainService;
import com.gxwebsoft.common.system.entity.Domain;
import com.gxwebsoft.common.system.param.DomainParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
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;

/**
 * 授权域名控制器
 *
 * @author 科技小王子
 * @since 2024-09-19 23:56:33
 */
@Tag(name = "授权域名管理")
@RestController
@RequestMapping("/api/system/domain")
public class DomainController extends BaseController {
    @Resource
    private DomainService domainService;

    @Operation(summary = "分页查询授权域名")
    @GetMapping("/page")
    public ApiResult<PageResult<Domain>> page(DomainParam param) {
        // 使用关联查询
        return success(domainService.pageRel(param));
    }

    @Operation(summary = "查询全部授权域名")
    @GetMapping()
    public ApiResult<List<Domain>> list(DomainParam param) {
        // 使用关联查询
        return success(domainService.listRel(param));
    }

    @PreAuthorize("hasAuthority('sys:domain:list')")
    @OperationLog
    @Operation(summary = "根据id查询授权域名")
    @GetMapping("/{id}")
    public ApiResult<Domain> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(domainService.getByIdRel(id));
    }

    @Operation(summary = "根据domain查询授权域名")
    @GetMapping("/getByDomain/{domain}")
    public ApiResult<Domain> getByDomain(@PathVariable("domain") String domain) {
      // 使用关联查询
      return success(domainService.getByDomainRel(domain));
    }

    @PreAuthorize("hasAuthority('sys:domain:save')")
    @OperationLog
    @Operation(summary = "添加授权域名")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Domain domain) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          domain.setUserId(loginUser.getUserId());
        }
        if (domainService.save(domain)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:domain:update')")
    @OperationLog
    @Operation(summary = "修改授权域名")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Domain domain) {
        if (domainService.updateById(domain)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:domain:remove')")
    @OperationLog
    @Operation(summary = "删除授权域名")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (domainService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:domain:save')")
    @OperationLog
    @Operation(summary = "批量添加授权域名")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Domain> list) {
        if (domainService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:domain:update')")
    @OperationLog
    @Operation(summary = "批量修改授权域名")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Domain> batchParam) {
        if (batchParam.update(domainService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:domain:remove')")
    @OperationLog
    @Operation(summary = "批量删除授权域名")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (domainService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
