package com.gxwebsoft.common.system.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.service.UserGroupService;
import com.gxwebsoft.common.system.entity.UserGroup;
import com.gxwebsoft.common.system.param.UserGroupParam;
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 2023-10-28 15:16:39
 */
@Tag(name = "用户")
@RestController
@RequestMapping("/api/system/user-group")
public class UserGroupController extends BaseController {
    @Resource
    private UserGroupService userGroupService;

    @PreAuthorize("hasAuthority('sys:userGroup:list')")
    @OperationLog
    @Operation(summary = "分页查询用户分组管理表")
    @GetMapping("/page")
    public ApiResult<PageResult<UserGroup>> page(UserGroupParam param) {
        PageParam<UserGroup, UserGroupParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(userGroupService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(userGroupService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('sys:userGroup:list')")
    @OperationLog
    @Operation(summary = "查询全部用户分组管理表")
    @GetMapping()
    public ApiResult<List<UserGroup>> list(UserGroupParam param) {
        PageParam<UserGroup, UserGroupParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(userGroupService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(userGroupService.listRel(param));
    }

    @PreAuthorize("hasAuthority('sys:userGroup:list')")
    @OperationLog
    @Operation(summary = "根据id查询用户分组管理表")
    @GetMapping("/{id}")
    public ApiResult<UserGroup> get(@PathVariable("id") Integer id) {
        return success(userGroupService.getById(id));
        // 使用关联查询
        //return success(userGroupService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('sys:userGroup:save')")
    @OperationLog
    @Operation(summary = "添加用户分组管理表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody UserGroup userGroup) {
        if (userGroupService.save(userGroup)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:userGroup:update')")
    @OperationLog
    @Operation(summary = "修改用户分组管理表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody UserGroup userGroup) {
        if (userGroupService.updateById(userGroup)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:userGroup:remove')")
    @OperationLog
    @Operation(summary = "删除用户分组管理表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (userGroupService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:userGroup:save')")
    @OperationLog
    @Operation(summary = "批量添加用户分组管理表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<UserGroup> list) {
        if (userGroupService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:userGroup:update')")
    @OperationLog
    @Operation(summary = "批量修改用户分组管理表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<UserGroup> batchParam) {
        if (batchParam.update(userGroupService, "group_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:userGroup:remove')")
    @OperationLog
    @Operation(summary = "批量删除用户分组管理表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (userGroupService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
