package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaAssetsVhostService;
import com.gxwebsoft.oa.entity.OaAssetsVhost;
import com.gxwebsoft.oa.param.OaAssetsVhostParam;
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:27:02
 */
@Tag(name = "虚拟主机记录表管理")
@RestController
@RequestMapping("/api/oa/oa-assets-vhost")
public class OaAssetsVhostController extends BaseController {
    @Resource
    private OaAssetsVhostService oaAssetsVhostService;

    @PreAuthorize("hasAuthority('oa:oaAssetsVhost:list')")
    @Operation(summary = "分页查询虚拟主机记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<OaAssetsVhost>> page(OaAssetsVhostParam param) {
        // 使用关联查询
        return success(oaAssetsVhostService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsVhost:list')")
    @Operation(summary = "查询全部虚拟主机记录表")
    @GetMapping()
    public ApiResult<List<OaAssetsVhost>> list(OaAssetsVhostParam param) {
        // 使用关联查询
        return success(oaAssetsVhostService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsVhost:list')")
    @Operation(summary = "根据id查询虚拟主机记录表")
    @GetMapping("/{id}")
    public ApiResult<OaAssetsVhost> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(oaAssetsVhostService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsVhost:save')")
    @OperationLog
    @Operation(summary = "添加虚拟主机记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaAssetsVhost oaAssetsVhost) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          oaAssetsVhost.setUserId(loginUser.getUserId());
        }
        if (oaAssetsVhostService.save(oaAssetsVhost)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsVhost:update')")
    @Operation(summary = "修改虚拟主机记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaAssetsVhost oaAssetsVhost) {
        if (oaAssetsVhostService.updateById(oaAssetsVhost)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsVhost:remove')")
    @Operation(summary = "删除虚拟主机记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (oaAssetsVhostService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsVhost:save')")
    @Operation(summary = "批量添加虚拟主机记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OaAssetsVhost> list) {
        if (oaAssetsVhostService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsVhost:update')")
    @Operation(summary = "批量修改虚拟主机记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OaAssetsVhost> batchParam) {
        if (batchParam.update(oaAssetsVhostService, "vhost_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsVhost:remove')")
    @Operation(summary = "批量删除虚拟主机记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (oaAssetsVhostService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
