package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaAssetsSoftwareCertService;
import com.gxwebsoft.oa.entity.OaAssetsSoftwareCert;
import com.gxwebsoft.oa.param.OaAssetsSoftwareCertParam;
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 19:46:21
 */
@Tag(name = "计算机软件著作权登记管理")
@RestController
@RequestMapping("/api/oa/oa-assets-software-cert")
public class OaAssetsSoftwareCertController extends BaseController {
    @Resource
    private OaAssetsSoftwareCertService oaAssetsSoftwareCertService;

    @PreAuthorize("hasAuthority('oa:oaAssetsSoftwareCert:list')")
    @Operation(summary = "分页查询计算机软件著作权登记")
    @GetMapping("/page")
    public ApiResult<PageResult<OaAssetsSoftwareCert>> page(OaAssetsSoftwareCertParam param) {
        // 使用关联查询
        return success(oaAssetsSoftwareCertService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSoftwareCert:list')")
    @Operation(summary = "查询全部计算机软件著作权登记")
    @GetMapping()
    public ApiResult<List<OaAssetsSoftwareCert>> list(OaAssetsSoftwareCertParam param) {
        // 使用关联查询
        return success(oaAssetsSoftwareCertService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSoftwareCert:list')")
    @Operation(summary = "根据id查询计算机软件著作权登记")
    @GetMapping("/{id}")
    public ApiResult<OaAssetsSoftwareCert> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(oaAssetsSoftwareCertService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSoftwareCert:save')")
    @OperationLog
    @Operation(summary = "添加计算机软件著作权登记")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaAssetsSoftwareCert oaAssetsSoftwareCert) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          oaAssetsSoftwareCert.setUserId(loginUser.getUserId());
        }
        if (oaAssetsSoftwareCertService.save(oaAssetsSoftwareCert)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }
    @PreAuthorize("hasAuthority('oa:oaAssetsSoftwareCert:update')")
    @Operation(summary = "修改计算机软件著作权登记")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaAssetsSoftwareCert oaAssetsSoftwareCert) {
        if (oaAssetsSoftwareCertService.updateById(oaAssetsSoftwareCert)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSoftwareCert:remove')")
    @Operation(summary = "删除计算机软件著作权登记")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (oaAssetsSoftwareCertService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSoftwareCert:save')")
    @Operation(summary = "批量添加计算机软件著作权登记")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OaAssetsSoftwareCert> list) {
        if (oaAssetsSoftwareCertService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSoftwareCert:update')")
    @Operation(summary = "批量修改计算机软件著作权登记")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OaAssetsSoftwareCert> batchParam) {
        if (batchParam.update(oaAssetsSoftwareCertService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSoftwareCert:remove')")
    @Operation(summary = "批量删除计算机软件著作权登记")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (oaAssetsSoftwareCertService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
