package com.ruoyi.sim.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.sim.service.impl.AddOnUserService;
import com.ruoyi.system.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sim.domain.AddOnUser;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户附加Controller
 *
 * @author tom
 * @date 2024-12-11
 */
@RestController
@RequestMapping("/sim/add-on-user")
@Api("用户附加Controller")
public class AddOnUserController extends BaseController {
    @Autowired
    private AddOnUserService addOnUserService;
    @Autowired
    private ISysRoleService roleService;

    /**
     * 查询用户附加列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AddOnUser addOnUser) {
        startPage();
        List<AddOnUser> list = addOnUserService.selectAddOnUserList(addOnUser);
        return getDataTable(list);
    }

    /**
     * 获取用户附加详细信息
     */
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId) {
        return success(addOnUserService.selectAddOnUserByUserId(userId));
    }

    /**
     * 新增用户附加
     */
    @Log(title = "用户附加", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AddOnUser addOnUser) {
        return toAjax(addOnUserService.insertAddOnUser(addOnUser));
    }

    /**
     * 修改用户附加
     */
    @Log(title = "用户附加", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AddOnUser addOnUser) {
        return toAjax(addOnUserService.updateAddOnUser(addOnUser));
    }

    /**
     * 删除用户附加
     */
    @Log(title = "用户附加", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds) {
        return toAjax(addOnUserService.deleteAddOnUserByUserIds(userIds));
    }

    /**
     * 返回当前登录账号角色info。
     */
    @GetMapping(value = "/roleInfo/")
    @ApiOperation("返回当前登录账号角色info")
    public AjaxResult getRole() {
        Long userId = SecurityUtils.getUserId();
        SysRole role = null;
        List<Long> roleList = roleService.selectRoleListByUserId(userId);
        if (roleList != null && !roleList.isEmpty()) {
            role = roleService.selectRoleById(roleList.get(0));
        }
        return success(role);
    }
}
