package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.service.UserOauthService;
import com.gxwebsoft.shop.entity.UserOauth;
import com.gxwebsoft.shop.param.UserOauthParam;
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.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 第三方用户信息表控制器
 *
 * @author 科技小王子
 * @since 2022-12-15 08:33:13
 */
@Api(tags = "第三方用户信息表管理")
@RestController
@RequestMapping("/api/shop/user-oauth")
public class UserOauthController extends BaseController {
    @Resource
    private UserOauthService userOauthService;

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

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

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

    @PreAuthorize("hasAuthority('shop:userOauth:save')")
    @OperationLog
    @ApiOperation("添加第三方用户信息表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody UserOauth userOauth) {
        if (userOauthService.save(userOauth)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

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

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

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

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

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

}
