package com.gxwebsoft.common.system.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.AuthorizeCodeService;
import com.gxwebsoft.common.system.entity.AuthorizeCode;
import com.gxwebsoft.common.system.param.AuthorizeCodeParam;
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 2025-08-05 01:17:27
 */
@Tag(name = "授权码管理")
@RestController
@RequestMapping("/api/system/authorize-code")
public class AuthorizeCodeController extends BaseController {
    @Resource
    private AuthorizeCodeService authorizeCodeService;

    @PreAuthorize("hasAuthority('sys:authorizeCode:list')")
    @OperationLog
    @Operation(summary = "分页查询授权码")
    @GetMapping("/page")
    public ApiResult<PageResult<AuthorizeCode>> page(AuthorizeCodeParam param) {
        // 使用关联查询
        return success(authorizeCodeService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('sys:authorizeCode:list')")
    @OperationLog
    @Operation(summary = "查询全部授权码")
    @GetMapping()
    public ApiResult<List<AuthorizeCode>> list(AuthorizeCodeParam param) {
        // 使用关联查询
        return success(authorizeCodeService.listRel(param));
    }

    @PreAuthorize("hasAuthority('sys:authorizeCode:list')")
    @OperationLog
    @Operation(summary = "根据id查询授权码")
    @GetMapping("/{id}")
    public ApiResult<AuthorizeCode> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(authorizeCodeService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('sys:authorizeCode:save')")
    @OperationLog
    @Operation(summary = "添加授权码")
    @PostMapping()
    public ApiResult<?> save(@RequestBody AuthorizeCode authorizeCode) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          authorizeCode.setUserId(loginUser.getUserId());
        }
        if (authorizeCodeService.save(authorizeCode)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:authorizeCode:update')")
    @OperationLog
    @Operation(summary = "修改授权码")
    @PutMapping()
    public ApiResult<?> update(@RequestBody AuthorizeCode authorizeCode) {
        if (authorizeCodeService.updateById(authorizeCode)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:authorizeCode:remove')")
    @OperationLog
    @Operation(summary = "删除授权码")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (authorizeCodeService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:authorizeCode:save')")
    @OperationLog
    @Operation(summary = "批量添加授权码")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<AuthorizeCode> list) {
        if (authorizeCodeService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:authorizeCode:update')")
    @OperationLog
    @Operation(summary = "批量修改授权码")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<AuthorizeCode> batchParam) {
        if (batchParam.update(authorizeCodeService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:authorizeCode:remove')")
    @OperationLog
    @Operation(summary = "批量删除授权码")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (authorizeCodeService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
