package com.fuyuanshen.app.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.fuyuanshen.common.idempotent.annotation.RepeatSubmit;
import com.fuyuanshen.common.log.annotation.Log;
import com.fuyuanshen.common.web.core.BaseController;
import com.fuyuanshen.common.mybatis.core.page.PageQuery;
import com.fuyuanshen.common.core.domain.R;
import com.fuyuanshen.common.core.validate.AddGroup;
import com.fuyuanshen.common.core.validate.EditGroup;
import com.fuyuanshen.common.log.enums.BusinessType;
import com.fuyuanshen.common.excel.utils.ExcelUtil;
import com.fuyuanshen.app.domain.vo.AppRoleVo;
import com.fuyuanshen.app.domain.bo.AppRoleBo;
import com.fuyuanshen.app.service.IAppRoleService;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;

/**
 * 角色信息
 *
 * @author Lion Li
 * @date 2025-06-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/role")
public class AppRoleController extends BaseController {

    private final IAppRoleService appRoleService;

    /**
     * 查询角色信息列表
     */
    @SaCheckPermission("app:role:list")
    @GetMapping("/list")
    public TableDataInfo<AppRoleVo> list(AppRoleBo bo, PageQuery pageQuery) {
        return appRoleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出角色信息列表
     */
    @SaCheckPermission("app:role:export")
    @Log(title = "角色信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppRoleBo bo, HttpServletResponse response) {
        List<AppRoleVo> list = appRoleService.queryList(bo);
        ExcelUtil.exportExcel(list, "角色信息", AppRoleVo.class, response);
    }

    /**
     * 获取角色信息详细信息
     *
     * @param roleId 主键
     */
    @SaCheckPermission("app:role:query")
    @GetMapping("/{roleId}")
    public R<AppRoleVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long roleId) {
        return R.ok(appRoleService.queryById(roleId));
    }

    /**
     * 新增角色信息
     */
    @SaCheckPermission("app:role:add")
    @Log(title = "角色信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppRoleBo bo) {
        return toAjax(appRoleService.insertByBo(bo));
    }

    /**
     * 修改角色信息
     */
    @SaCheckPermission("app:role:edit")
    @Log(title = "角色信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppRoleBo bo) {
        return toAjax(appRoleService.updateByBo(bo));
    }

    /**
     * 删除角色信息
     *
     * @param roleIds 主键串
     */
    @SaCheckPermission("app:role:remove")
    @Log(title = "角色信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] roleIds) {
        return toAjax(appRoleService.deleteWithValidByIds(List.of(roleIds), true));
    }
}
