package com.gxwebsoft.project.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.project.service.ProjectUrlService;
import com.gxwebsoft.project.entity.ProjectUrl;
import com.gxwebsoft.project.param.ProjectUrlParam;
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 com.gxwebsoft.common.system.entity.User;
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 2025-03-14 16:21:11
 */
@Tag(name = "项目域名管理")
@RestController
@RequestMapping("/api/project/project-url")
public class ProjectUrlController extends BaseController {
    @Resource
    private ProjectUrlService projectUrlService;

    @PreAuthorize("hasAuthority('project:projectUrl:list')")
    @Operation(summary = "分页查询项目域名")
    @GetMapping("/page")
    public ApiResult<PageResult<ProjectUrl>> page(ProjectUrlParam param) {
        // 使用关联查询
        return success(projectUrlService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('project:projectUrl:list')")
    @Operation(summary = "查询全部项目域名")
    @GetMapping()
    public ApiResult<List<ProjectUrl>> list(ProjectUrlParam param) {
        // 使用关联查询
        return success(projectUrlService.listRel(param));
    }

    @PreAuthorize("hasAuthority('project:projectUrl:list')")
    @Operation(summary = "根据id查询项目域名")
    @GetMapping("/{id}")
    public ApiResult<ProjectUrl> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(projectUrlService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('project:projectUrl:save')")
    @OperationLog
    @Operation(summary = "添加项目域名")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ProjectUrl projectUrl) {
        if (projectUrlService.save(projectUrl)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('project:projectUrl:update')")
    @OperationLog
    @Operation(summary = "修改项目域名")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ProjectUrl projectUrl) {
        if (projectUrlService.updateById(projectUrl)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

    @PreAuthorize("hasAuthority('project:projectUrl:save')")
    @OperationLog
    @Operation(summary = "批量添加项目域名")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ProjectUrl> list) {
        if (projectUrlService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('project:projectUrl:update')")
    @OperationLog
    @Operation(summary = "批量修改项目域名")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ProjectUrl> batchParam) {
        if (batchParam.update(projectUrlService, "app_url_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
