package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaAssetsSiteService;
import com.gxwebsoft.oa.entity.OaAssetsSite;
import com.gxwebsoft.oa.param.OaAssetsSiteParam;
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-site")
public class OaAssetsSiteController extends BaseController {
    @Resource
    private OaAssetsSiteService oaAssetsSiteService;

    @PreAuthorize("hasAuthority('oa:oaAssetsSite:list')")
    @Operation(summary = "分页查询网站信息记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<OaAssetsSite>> page(OaAssetsSiteParam param) {
        // 使用关联查询
        return success(oaAssetsSiteService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSite:list')")
    @Operation(summary = "查询全部网站信息记录表")
    @GetMapping()
    public ApiResult<List<OaAssetsSite>> list(OaAssetsSiteParam param) {
        // 使用关联查询
        return success(oaAssetsSiteService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSite:list')")
    @Operation(summary = "根据id查询网站信息记录表")
    @GetMapping("/{id}")
    public ApiResult<OaAssetsSite> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(oaAssetsSiteService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSite:save')")
    @OperationLog
    @Operation(summary = "添加网站信息记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaAssetsSite oaAssetsSite) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          oaAssetsSite.setUserId(loginUser.getUserId());
        }
        if (oaAssetsSiteService.save(oaAssetsSite)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSite:update')")
    @Operation(summary = "修改网站信息记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaAssetsSite oaAssetsSite) {
        if (oaAssetsSiteService.updateById(oaAssetsSite)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSite:remove')")
    @Operation(summary = "删除网站信息记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (oaAssetsSiteService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSite:save')")
    @Operation(summary = "批量添加网站信息记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OaAssetsSite> list) {
        if (oaAssetsSiteService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSite:update')")
    @Operation(summary = "批量修改网站信息记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OaAssetsSite> batchParam) {
        if (batchParam.update(oaAssetsSiteService, "website_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSite:remove')")
    @Operation(summary = "批量删除网站信息记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (oaAssetsSiteService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
