package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.service.OaAssetsSslService;
import com.gxwebsoft.oa.entity.OaAssetsSsl;
import com.gxwebsoft.oa.param.OaAssetsSslParam;
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.time.LocalDateTime;
import java.util.List;

/**
 * ssl证书控制器
 *
 * @author 科技小王子
 * @since 2024-10-18 19:25:40
 */
@Tag(name = "ssl证书管理")
@RestController
@RequestMapping("/api/oa/oa-assets-ssl")
public class OaAssetsSslController extends BaseController {
    @Resource
    private OaAssetsSslService oaAssetsSslService;

    @PreAuthorize("hasAuthority('oa:oaAssetsSsl:list')")
    @Operation(summary = "分页查询ssl证书")
    @GetMapping("/page")
    public ApiResult<PageResult<OaAssetsSsl>> page(OaAssetsSslParam param) {
        // 使用关联查询
        return success(oaAssetsSslService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSsl:list')")
    @Operation(summary = "查询全部ssl证书")
    @GetMapping()
    public ApiResult<List<OaAssetsSsl>> list(OaAssetsSslParam param) {
        // 使用关联查询
        return success(oaAssetsSslService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSsl:list')")
    @OperationLog
    @Operation(summary = "根据id查询ssl证书")
    @GetMapping("/{id}")
    public ApiResult<OaAssetsSsl> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(oaAssetsSslService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSsl:save')")
    @Operation(summary = "添加ssl证书")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OaAssetsSsl oaAssetsSsl) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          oaAssetsSsl.setUserId(loginUser.getUserId());
          oaAssetsSsl.setTenantId(loginUser.getTenantId());
        }
        if (oaAssetsSslService.save(oaAssetsSsl)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }
    @PreAuthorize("hasAuthority('oa:oaAssetsSsl:update')")
    @Operation(summary = "修改ssl证书")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OaAssetsSsl oaAssetsSsl) {
        if (oaAssetsSslService.updateById(oaAssetsSsl)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

    @PreAuthorize("hasAuthority('oa:oaAssetsSsl:save')")
    @Operation(summary = "批量添加ssl证书")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OaAssetsSsl> list) {
        if (oaAssetsSslService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:oaAssetsSsl:update')")
    @Operation(summary = "批量修改ssl证书")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OaAssetsSsl> batchParam) {
        if (batchParam.update(oaAssetsSslService, "ssl_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
