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.AppUserRoleVo;
import com.fuyuanshen.app.domain.bo.AppUserRoleBo;
import com.fuyuanshen.app.service.IAppUserRoleService;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;

/**
 * 用户和角色关联
 *
 * @author Lion Li
 * @date 2025-06-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/userRole")
public class AppUserRoleController extends BaseController {

    private final IAppUserRoleService appUserRoleService;

    /**
     * 查询用户和角色关联列表
     */
    @SaCheckPermission("app:userRole:list")
    @GetMapping("/list")
    public TableDataInfo<AppUserRoleVo> list(AppUserRoleBo bo, PageQuery pageQuery) {
        return appUserRoleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户和角色关联列表
     */
    @SaCheckPermission("app:userRole:export")
    @Log(title = "用户和角色关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppUserRoleBo bo, HttpServletResponse response) {
        List<AppUserRoleVo> list = appUserRoleService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户和角色关联", AppUserRoleVo.class, response);
    }

    /**
     * 获取用户和角色关联详细信息
     *
     * @param userId 主键
     */
    @SaCheckPermission("app:userRole:query")
    @GetMapping("/{userId}")
    public R<AppUserRoleVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long userId) {
        return R.ok(appUserRoleService.queryById(userId));
    }

    /**
     * 新增用户和角色关联
     */
    @SaCheckPermission("app:userRole:add")
    @Log(title = "用户和角色关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppUserRoleBo bo) {
        return toAjax(appUserRoleService.insertByBo(bo));
    }

    /**
     * 修改用户和角色关联
     */
    @SaCheckPermission("app:userRole:edit")
    @Log(title = "用户和角色关联", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppUserRoleBo bo) {
        return toAjax(appUserRoleService.updateByBo(bo));
    }

    /**
     * 删除用户和角色关联
     *
     * @param userIds 主键串
     */
    @SaCheckPermission("app:userRole:remove")
    @Log(title = "用户和角色关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] userIds) {
        return toAjax(appUserRoleService.deleteWithValidByIds(List.of(userIds), true));
    }
}
