package com.CUST.brain.web.controller.system;

import com.CUST.brain.common.api.CommonResult;
import com.CUST.brain.common.dto.system.AssignRoleRequestDTO;
import com.CUST.brain.dao.domain.system.SysUser;
import com.CUST.brain.dao.domain.system.SysPermission;
import com.CUST.brain.service.SysUserService;
import com.CUST.brain.service.SysPermissionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 系统用户管理
 */
@RestController
@RequestMapping("/system/user")
@Api(tags = "系统用户管理")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysPermissionService permissionService;

    @GetMapping("/page")
    @ApiOperation("分页查询用户列表")
    @PreAuthorize("hasAuthority('system:user')")
    public CommonResult<Page<SysUser>> page(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("用户名") @RequestParam(required = false) String username,
            @ApiParam("真实姓名") @RequestParam(required = false) String realName,
            @ApiParam("状态") @RequestParam(required = false) Integer status) {
        
        Page<SysUser> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(username != null, SysUser::getUsername, username)
                .like(realName != null, SysUser::getRealName, realName)
                .eq(status != null, SysUser::getStatus, status)
                .eq(SysUser::getFlag, 0)
                .orderByDesc(SysUser::getCreateTime);
        
        return CommonResult.success(sysUserService.page(page, wrapper));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取用户详情")
    @PreAuthorize("hasAuthority('system:user')")
    public CommonResult<SysUser> getById(@PathVariable String id) {
        return CommonResult.success(sysUserService.getById(id));
    }

    @PostMapping("/add")
    @ApiOperation("新增用户")
    @PreAuthorize("hasAuthority('system:user')")
    public CommonResult<Boolean> add(@Valid @RequestBody SysUser user) {
        return CommonResult.success(sysUserService.addUser(user));
    }

    @PutMapping("/update")
    @ApiOperation("修改用户")
    @PreAuthorize("hasAuthority('system:user')")
    public CommonResult<Boolean> update(@Valid @RequestBody SysUser user) {
        return CommonResult.success(sysUserService.updateUser(user));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除用户")
    @PreAuthorize("hasAuthority('system:user')")
    public CommonResult<Boolean> delete(@PathVariable String id) {
        return CommonResult.success(sysUserService.deleteUser(id));
    }

    @PutMapping("/status")
    @ApiOperation("修改用户状态")
    @PreAuthorize("hasAuthority('system:user')")
    public CommonResult<Boolean> updateStatus(
            @ApiParam("用户ID") @RequestParam String id,
            @ApiParam("状态：0-禁用，1-启用") @RequestParam Integer status) {
        return CommonResult.success(sysUserService.updateStatus(id, status));
    }

    @PutMapping("/password")
    @ApiOperation("修改密码")
    public CommonResult<Boolean> updatePassword(
            @ApiParam("用户ID") @RequestParam String id,
            @ApiParam("旧密码") @RequestParam String oldPassword,
            @ApiParam("新密码") @RequestParam String newPassword) {
        return CommonResult.success(sysUserService.updatePassword(id, oldPassword, newPassword));
    }

    @PutMapping("/reset-password/{id}")
    @ApiOperation("重置密码")
    @PreAuthorize("hasAuthority('system:user')")
    public CommonResult<String> resetPassword(@PathVariable String id) {
        return CommonResult.success(sysUserService.resetPassword(id));
    }

    @PostMapping("/assignRoles")
    @ApiOperation("分配用户角色")
    @PreAuthorize("hasAuthority('system:user')")
    public CommonResult<?> assignRoles(@RequestBody AssignRoleRequestDTO request) {
        sysUserService.assignRoles(request.getUserId(), request.getRoleIds());
        return CommonResult.success(null, "分配成功");
    }
} 