package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.entity.OaAssetsServer;
import com.gxwebsoft.oa.param.OaAssetsServerParam;
import com.gxwebsoft.oa.service.OaAssetsServerService;
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-10-21 19:15:26
 */
@Tag(name = "服务管理")
@RestController
@RequestMapping("/api/oa/oa-assets-server")
public class OaAssetsServerController extends BaseController {
    @Resource
    private OaAssetsServerService oaAssetsServerService;

    @Operation(summary = "分页查询服务")
    @GetMapping("/page")
    public ApiResult<PageResult<OaAssetsServer>> page(OaAssetsServerParam param) {
        // 使用关联查询
        return success(oaAssetsServerService.pageRel(param));
    }

    @Operation(summary = "查询全部服务")
    @GetMapping()
    public ApiResult<List<OaAssetsServer>> list(OaAssetsServerParam param) {
        // 使用关联查询
        return success(oaAssetsServerService.listRel(param));
    }

    @Operation(summary = "根据id查询服务")
    @GetMapping("/{id}")
    public ApiResult<OaAssetsServer> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(oaAssetsServerService.getByIdRel(id));
    }
    @PreAuthorize("hasAuthority('oa:oaAssetsServer:save')")
    @Operation(summary = "添加服务")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaAssetsServer oaAssetsServer) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          oaAssetsServer.setUserId(loginUser.getUserId());
          oaAssetsServer.setTenantId(loginUser.getTenantId());
        }
        if (oaAssetsServerService.save(oaAssetsServer)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsServer:update')")
    @Operation(summary = "修改服务")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaAssetsServer oaAssetsServer) {
        if (oaAssetsServerService.updateById(oaAssetsServer)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsServer:remove')")
    @Operation(summary = "删除服务")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (oaAssetsServerService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsServer:save')")
    @Operation(summary = "批量添加服务")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OaAssetsServer> list) {
        if (oaAssetsServerService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsServer:update')")
    @Operation(summary = "批量修改服务")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OaAssetsServer> batchParam) {
        if (batchParam.update(oaAssetsServerService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsServer:remove')")
    @Operation(summary = "批量删除服务")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (oaAssetsServerService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
