package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaAssetsService;
import com.gxwebsoft.oa.entity.OaAssets;
import com.gxwebsoft.oa.param.OaAssetsParam;
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-10-18 18:34:15
 */
@Tag(name = "云服务器管理")
@RestController
@RequestMapping("/api/oa/oa-assets")
public class OaAssetsController extends BaseController {
    @Resource
    private OaAssetsService oaAssetsService;

    @PreAuthorize("hasAuthority('oa:oaAssets:list')")
    @Operation(summary = "分页查询云服务器")
    @GetMapping("/page")
    public ApiResult<PageResult<OaAssets>> page(OaAssetsParam param) {
        // 使用关联查询
        return success(oaAssetsService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssets:list')")
    @Operation(summary = "查询全部云服务器")
    @GetMapping()
    public ApiResult<List<OaAssets>> list(OaAssetsParam param) {
        // 使用关联查询
        return success(oaAssetsService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssets:list')")
    @Operation(summary = "根据id查询云服务器")
    @GetMapping("/{id}")
    public ApiResult<OaAssets> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(oaAssetsService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('oa:oaAssets:save')")
    @OperationLog
    @Operation(summary = "添加云服务器")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaAssets oaAssets) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          oaAssets.setUserId(loginUser.getUserId());
        }
        if (oaAssetsService.save(oaAssets)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }
    @PreAuthorize("hasAuthority('oa:oaAssets:update')")
    @Operation(summary = "修改云服务器")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaAssets oaAssets) {
        if (oaAssetsService.updateById(oaAssets)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
