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.UserOauthService;
import com.gxwebsoft.common.system.entity.UserOauth;
import com.gxwebsoft.common.system.param.UserOauthParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
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 2023-10-07 22:39:46
 */
@Tag(name = "第三方用户")
@RestController
@RequestMapping("/api/system/user-oauth")
public class UserOauthController extends BaseController {
    @Resource
    private UserOauthService userOauthService;

    @PreAuthorize("hasAuthority('sys:userOauth:list')")
    @OperationLog
    @Operation(summary = "分页查询第三方用户信息表")
    @GetMapping("/page")
    public ApiResult<PageResult<UserOauth>> page(UserOauthParam param) {
        // 使用关联查询
        return success(userOauthService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('sys:userOauth:list')")
    @OperationLog
    @Operation(summary = "查询全部第三方用户信息表")
    @GetMapping()
    public ApiResult<List<UserOauth>> list(UserOauthParam param) {
        // 使用关联查询
        return success(userOauthService.listRel(param));
    }

    @PreAuthorize("hasAuthority('sys:userOauth:list')")
    @OperationLog
    @Operation(summary = "根据id查询第三方用户信息表")
    @GetMapping("/{id}")
    public ApiResult<UserOauth> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(userOauthService.getByIdRel(id));
    }

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

    @PreAuthorize("hasAuthority('sys:userOauth:update')")
    @OperationLog
    @Operation(summary = "修改第三方用户信息表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody UserOauth userOauth) {
        if (userOauthService.updateById(userOauth)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:userOauth:remove')")
    @OperationLog
    @Operation(summary = "删除第三方用户信息表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (userOauthService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:userOauth:save')")
    @OperationLog
    @Operation(summary = "批量添加第三方用户信息表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<UserOauth> list) {
        if (userOauthService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:userOauth:update')")
    @OperationLog
    @Operation(summary = "批量修改第三方用户信息表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<UserOauth> batchParam) {
        if (batchParam.update(userOauthService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:userOauth:remove')")
    @OperationLog
    @Operation(summary = "批量删除第三方用户信息表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (userOauthService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
