package com.rj.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rj.entity.sys.Role;
import com.rj.service.sys.IRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author 系统生成
 * @since 2025-08-07
 */
@RestController
@RequestMapping("/api/sys/role")
@Tag(name = "角色管理", description = "角色管理相关接口")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     * 新增角色
     */
    @PostMapping("/add")
    @Operation(summary = "新增角色", description = "添加新的角色信息")
    public ResponseEntity<Map<String, Object>> addRole(
            @Parameter(description = "角色信息", required = true)
            @RequestBody Role role) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = roleService.save(role);
            if (success) {
                result.put("success", true);
                result.put("message", "角色添加成功");
                result.put("data", role);
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "角色添加失败");
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "角色添加异常：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 根据ID查询角色
     */
    @GetMapping("/get/{id}")
    @Operation(summary = "根据ID查询角色", description = "根据角色ID获取角色详细信息")
    public ResponseEntity<Map<String, Object>> getRoleById(
            @Parameter(description = "角色ID", required = true)
            @PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        try {
            Role role = roleService.getById(id);
            if (role != null) {
                result.put("success", true);
                result.put("message", "查询成功");
                result.put("data", role);
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "角色不存在");
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询异常：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 根据角色名查询角色
     */
    @GetMapping("/getByRoleName")
    @Operation(summary = "根据角色名查询角色", description = "根据角色名查询角色信息")
    public ResponseEntity<Map<String, Object>> getRoleByRoleName(
            @Parameter(description = "角色名", required = true)
            @RequestParam String roleName) {
        Map<String, Object> result = new HashMap<>();
        try {
            LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Role::getRoleName, roleName);
            List<Role> roleList = roleService.list(queryWrapper);
            result.put("success", true);
            result.put("message", "查询成功");
            result.put("data", roleList);
            result.put("count", roleList.size());
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询异常：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 分页查询角色列表
     */
    @GetMapping("/list")
    @Operation(summary = "分页查询角色列表", description = "分页查询角色信息列表")
    public ResponseEntity<Map<String, Object>> getRoleList(
            @Parameter(description = "页码", example = "1")
            @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页大小", example = "10")
            @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "角色名（模糊查询）")
            @RequestParam(required = false) String roleName) {
        Map<String, Object> result = new HashMap<>();
        try {
            Page<Role> page = new Page<>(current, size);
            LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
            
            // 添加查询条件
            if (roleName != null && !roleName.trim().isEmpty()) {
                queryWrapper.like(Role::getRoleName, roleName);
            }
            
            // 按角色ID倒序排列
            queryWrapper.orderByDesc(Role::getRoleId);
            
            Page<Role> rolePage = roleService.page(page, queryWrapper);
            
            result.put("success", true);
            result.put("message", "查询成功");
            result.put("data", rolePage.getRecords());
            result.put("total", rolePage.getTotal());
            result.put("current", rolePage.getCurrent());
            result.put("size", rolePage.getSize());
            result.put("pages", rolePage.getPages());
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询异常：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 更新角色信息
     */
    @PutMapping("/update")
    @Operation(summary = "更新角色信息", description = "更新角色详细信息")
    public ResponseEntity<Map<String, Object>> updateRole(
            @Parameter(description = "角色信息", required = true)
            @RequestBody Role role) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (role.getRoleId() == null) {
                result.put("success", false);
                result.put("message", "角色ID不能为空");
                return ResponseEntity.badRequest().body(result);
            }
            
            boolean success = roleService.updateById(role);
            
            if (success) {
                result.put("success", true);
                result.put("message", "角色信息更新成功");
                result.put("data", role);
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "角色信息更新失败");
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新异常：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 根据ID删除角色
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除角色", description = "根据角色ID删除角色信息")
    public ResponseEntity<Map<String, Object>> deleteRole(
            @Parameter(description = "角色ID", required = true)
            @PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = roleService.removeById(id);
            if (success) {
                result.put("success", true);
                result.put("message", "角色删除成功");
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "角色删除失败");
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除异常：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 批量删除角色
     */
    @DeleteMapping("/batchDelete")
    @Operation(summary = "批量删除角色", description = "根据角色ID列表批量删除角色信息")
    public ResponseEntity<Map<String, Object>> batchDeleteRoles(
            @Parameter(description = "角色ID列表", required = true)
            @RequestBody List<Integer> ids) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (ids == null || ids.isEmpty()) {
                result.put("success", false);
                result.put("message", "角色ID列表不能为空");
                return ResponseEntity.badRequest().body(result);
            }
            
            boolean success = roleService.removeByIds(ids);
            if (success) {
                result.put("success", true);
                result.put("message", "批量删除成功，共删除 " + ids.size() + " 条记录");
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "批量删除失败");
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "批量删除异常：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 获取角色统计信息
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取角色统计信息", description = "获取角色总数等统计信息")
    public ResponseEntity<Map<String, Object>> getRoleStatistics() {
        Map<String, Object> result = new HashMap<>();
        try {
            long totalCount = roleService.count();
            
            Map<String, Object> statistics = new HashMap<>();
            statistics.put("totalRoles", totalCount);
            
            result.put("success", true);
            result.put("message", "统计信息获取成功");
            result.put("data", statistics);
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "统计信息获取异常：" + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }
}























































































































