package com.ruoyi.web.controller.system;

import com.mybatisflex.core.paginate.Page;
import com.ruoyi.common.constants.UserConstant;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ExcelUtil;
import com.ruoyi.common.web.controller.BaseController;
import com.ruoyi.common.web.domain.PageData;
import com.ruoyi.common.web.domain.entity.SysRole;
import com.ruoyi.common.web.domain.entity.SysUser;
import com.ruoyi.framework.annotation.RyDataScope;
import com.ruoyi.framework.annotation.RyLog;
import com.ruoyi.system.service.ISysUserService;
import org.noear.solon.annotation.*;
import org.noear.solon.auth.annotation.AuthPermissions;
import org.noear.solon.core.handle.Result;
import org.noear.solon.data.annotation.Transaction;
import org.noear.solon.validation.annotation.Validated;

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

/**
 * 系统用户 操作处理
 * @author ruoyi
 */
@Controller
@Mapping("system/user")
public class SysUserController extends BaseController {

    @Inject
    ISysUserService sysUserService;

    /**
     * 查询用户列表
     */
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT, userAlias = UserConstant.DATA_SCOPE_ALIAS_USER)
    @AuthPermissions("system:user:list")
    @Get
    @Mapping("list")
    public PageData<SysUser> list(Page<SysUser> page, SysUser sysUser) {
        startPage(page);
        List<SysUser> list = sysUserService.selectUserList(sysUser);
        return getPageData(list);
    }

    /**
     * 导出用户列表
     */
    @RyLog(title = "系统用户", businessType = BusinessType.EXPORT)
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT, userAlias = UserConstant.DATA_SCOPE_ALIAS_USER)
    @AuthPermissions("system:user:export")
    @Post
    @Mapping("export")
    public void export(SysUser sysUser) {
        List<SysUser> list = sysUserService.selectUserList(sysUser);
        ExcelUtil.export(list);
    }

    /**
     * 获取用户详情
     */
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT, userAlias = UserConstant.DATA_SCOPE_ALIAS_USER)
    @AuthPermissions("system:user:query")
    @Get
    @Mapping("{userId}")
    public Map<String, Object> info(@Path Long userId, SysUser sysUser, SysRole sysRole) {
        return sysUserService.info(userId, sysUser, sysRole);
    }

    /**
     * 获取用户详情
     */
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT, userAlias = UserConstant.DATA_SCOPE_ALIAS_USER)
    @AuthPermissions("system:user:query")
    @Get
    @Mapping
    public Map<String, Object> info(SysUser sysUser, SysRole sysRole) {
        return sysUserService.info(null, sysUser, sysRole);
    }

    /**
     * 新增用户
     */
    @RyLog(title = "系统用户", businessType = BusinessType.INSERT)
    @AuthPermissions("system:user:add")
    @Transaction
    @Post
    @Mapping
    public Result<Void> add(@Body @Validated SysUser sysUser) {
        return toResult(sysUserService.add(sysUser));
    }

    /**
     * 修改用户
     */
    @RyLog(title = "系统用户", businessType = BusinessType.UPDATE)
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT, userAlias = UserConstant.DATA_SCOPE_ALIAS_USER)
    @AuthPermissions("system:user:edit")
    @Transaction
    @Put
    @Mapping
    public Result<Void> edit(@Body @Validated SysUser sysUser) {
        return toResult(sysUserService.edit(sysUser));
    }

    /**
     * 删除用户
     */
    @RyLog(title = "系统用户", businessType = BusinessType.DELETE)
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT, userAlias = UserConstant.DATA_SCOPE_ALIAS_USER)
    @AuthPermissions("system:user:remove")
    @Transaction
    @Delete
    @Mapping("{userIds}")
    public Result<Void> delete(@Path Long[] userIds, SysUser sysUser) {
        return toResult(sysUserService.delete(userIds, sysUser));
    }

    /**
     * 重置密码
     */
    @RyLog(title = "系统用户", businessType = BusinessType.UPDATE, isSaveRequestData = false)
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT, userAlias = UserConstant.DATA_SCOPE_ALIAS_USER)
    @AuthPermissions("system:user:edit")
    @Put
    @Mapping("resetPwd")
    public Result<Void> resetPwd(@Body SysUser sysUser) {
        return toResult(sysUserService.resetPwd(sysUser));
    }

    /**
     * 状态修改
     */
    @RyLog(title = "系统用户", businessType = BusinessType.UPDATE)
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT, userAlias = UserConstant.DATA_SCOPE_ALIAS_USER)
    @AuthPermissions("system:user:edit")
    @Put
    @Mapping("changeStatus")
    public Result<Void> changeStatus(@Body SysUser sysUser) {
        return toResult(sysUserService.changeStatus(sysUser));
    }

    /**
     * 根据用户编号获取授权角色
     */
    @RyLog(title = "系统用户", businessType = BusinessType.GRANT)
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT, userAlias = UserConstant.DATA_SCOPE_ALIAS_USER)
    @AuthPermissions("system:user:query")
    @Get
    @Mapping("authRole/{userId}")
    public Map<String, Object> authRole(@Path Long userId, SysRole sysRole) {
        return sysUserService.authRole(userId, sysRole);
    }

    /**
     * 用户授权角色
     */
    @RyLog(title = "系统用户", businessType = BusinessType.GRANT)
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT, userAlias = UserConstant.DATA_SCOPE_ALIAS_USER)
    @AuthPermissions("system:user:edit")
    @Put
    @Mapping("authRole")
    public Result<Void> insertAuthRole(Long userId, Long[] roleIds, SysUser sysUser) {
        sysUserService.insertAuthRole(userId, roleIds, sysUser);
        return Result.succeed();
    }

    /**
     * 查询角色已授权用户列表
     */
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT, userAlias = UserConstant.DATA_SCOPE_ALIAS_USER)
    @AuthPermissions("system:role:list")
    @Get
    @Mapping("authUser/allocatedList")
    public PageData<SysUser> allocatedList(Page<SysUser> page, SysUser sysUser) {
        startPage(page);
        List<SysUser> list = sysUserService.selectAllocatedList(sysUser);
        return getPageData(list);
    }

    /**
     * 查询角色未授权用户列表
     */
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT, userAlias = UserConstant.DATA_SCOPE_ALIAS_USER)
    @AuthPermissions("system:role:list")
    @Get
    @Mapping("authUser/unallocatedList")
    public PageData<SysUser> unallocatedList(Page<SysUser> page, SysUser sysUser) {
        startPage(page);
        List<SysUser> list = sysUserService.selectUnallocatedList(sysUser);
        return getPageData(list);
    }

    /**
     * 个人中心 - 查询个人信息
     */
    @Get
    @Mapping("profile")
    public Map<String, Object> profile() {
        return sysUserService.profile();
    }

    /**
     * 个人中心 - 修改用户
     */
    @RyLog(title = "个人中心", businessType = BusinessType.UPDATE)
    @Put
    @Mapping("profile")
    public Result<Void> updateProfile(@Body SysUser sysUser) {
        return toResult(sysUserService.updateProfile(sysUser));
    }

    /**
     * 个人中心 - 修改密码
     */
    @RyLog(title = "个人中心", businessType = BusinessType.UPDATE, isSaveRequestData = false)
    @Put
    @Mapping("profile/updatePwd")
    public Result<Void> updateProfilePwd(String oldPassword, String newPassword) {
        return toResult(sysUserService.updateProfilePwd(oldPassword, newPassword));
    }

    /**
     * 个人中心 - 修改头像
     */
    @RyLog(title = "个人中心", businessType = BusinessType.UPDATE)
    @Put
    @Mapping("profile/avatar")
    public Result<Void> updateProfileAvatar(String avatar) {
        return toResult(sysUserService.updateProfileAvatar(avatar));
    }

}
