package com.bonus.material.user.controller;

import com.bonus.common.biz.domain.SysDept;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.common.security.annotation.InnerAuth;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.common.security.annotation.RequiresPermissionsOrInnerAuth;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.user.entity.UserDto;
import com.bonus.material.user.service.UserService;
import com.bonus.system.api.domain.SysRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author：liang.chao
 * @Date：2024/12/19 - 14:38
 */
@RestController
@Api(value = "个人管理", tags = {"个人管理"})
@RequestMapping("/userManage")
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "个人信息")
    @GetMapping("/userInfo")
    public AjaxResult getUserInfo() {
        return AjaxResult.success(userService.getUserById(SecurityUtils.getLoginUser().getUserid()));
    }

    @ApiOperation(value = "修改个人信息")
    @PostMapping("/editUser")
    public AjaxResult editUser(@RequestBody UserDto userDto) {
       Integer i = userService.editUser(userDto);
       if (i > 0){
           return success("修改成功");
       }else {
           return error("修改失败");
       }
    }

    /**
     * 获取部门树列表
     */
    @GetMapping("/getDeptTree")
    public AjaxResult deptTree(SysDept dept) {
        try {
            return success(userService.selectDeptTreeList(dept));
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return error("系统异常,请联系管理员");
    }

    @GetMapping("/roleList")
    @SysLog(title = "角色管理", businessType = OperaType.QUERY,logType = 0,module = "系统管理->角色管理",details = "查询角色列表")
    public TableDataInfo list(SysRole role) {
        try{
            startPage();
            List<SysRole> list = userService.selectRoleList(role);
            return getDataTable(list);
        }catch (Exception e){
            logger.error(e.toString(),e);
        }
        return  getDataTableError(new ArrayList<>());
    }

    @GetMapping("/userList")
    @SysLog(title = "用户列表", businessType = OperaType.QUERY,logType = 0,module = "系统管理->用户管理",details = "查询用户列表")
    public TableDataInfo userList(com.bonus.common.biz.domain.SysUser user) {
        try{
            List<com.bonus.common.biz.domain.SysUser> list = userService.selectUserList(user);
            return getDataTable(list);
        }catch (Exception e){
            logger.error(e.toString(),e);
        }
        return getDataTableError(new ArrayList<>());
    }

}
