SysUserController.java 10.2 KB
Newer Older
zlt2000's avatar
zlt2000 已提交
1 2 3 4 5 6 7 8 9
package com.central.user.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.hutool.core.bean.BeanUtil;
10
import cn.hutool.core.util.ObjectUtil;
zlt2000's avatar
zlt2000 已提交
11 12 13 14
import com.central.common.annotation.LoginUser;
import com.central.common.constant.CommonConstant;
import com.central.common.model.*;
import com.central.common.utils.ExcelUtil;
15
import com.central.log.annotation.AuditLog;
16 17 18
import com.central.search.client.service.IQueryService;
import com.central.search.model.LogicDelDto;
import com.central.search.model.SearchDto;
zlt2000's avatar
zlt2000 已提交
19
import com.central.user.model.SysUserExcel;
zlt2000's avatar
zlt2000 已提交
20
import com.fasterxml.jackson.databind.JsonNode;
zlt2000's avatar
zlt2000 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import com.central.user.service.ISysUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
zlt2000's avatar
zlt2000 已提交
40
 * @author 作者 owen E-mail: 624191343@qq.com
zlt2000's avatar
zlt2000 已提交
41 42 43 44 45 46
 * 用户
 */
@Slf4j
@RestController
@Api(tags = "用户模块api")
public class SysUserController {
47 48
    private static final String ADMIN_CHANGE_MSG = "超级管理员不给予修改";

49 50 51 52 53
    /**
     * 全文搜索逻辑删除Dto
     */
    private static final LogicDelDto SEARCH_LOGIC_DEL_DTO = new LogicDelDto("isDel", "否");

zlt2000's avatar
zlt2000 已提交
54 55 56
    @Autowired
    private ISysUserService appUserService;

57 58 59
    @Autowired
    private IQueryService queryService;

zlt2000's avatar
zlt2000 已提交
60 61 62 63 64 65 66
    /**
     * 当前登录用户 LoginAppUser
     *
     * @return
     */
    @ApiOperation(value = "根据access_token当前登录用户")
    @GetMapping("/users/current")
67 68
    public Result<LoginAppUser> getLoginAppUser(@LoginUser(isFull = true) SysUser user) {
        return Result.succeed(appUserService.getLoginAppUser(user));
zlt2000's avatar
zlt2000 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    }

    /**
     * 查询用户实体对象SysUser
     */
    @GetMapping(value = "/users/name/{username}")
    @ApiOperation(value = "根据用户名查询用户实体")
    @Cacheable(value = "user", key = "#username")
    public SysUser selectByUsername(@PathVariable String username) {
        return appUserService.selectByUsername(username);
    }

    /**
     * 查询用户登录对象LoginAppUser
     */
    @GetMapping(value = "/users-anon/login", params = "username")
    @ApiOperation(value = "根据用户名查询用户")
    public LoginAppUser findByUsername(String username) {
        return appUserService.findByUsername(username);
    }

    /**
     * 通过手机号查询用户、角色信息
     *
     * @param mobile 手机号
     */
    @GetMapping(value = "/users-anon/mobile", params = "mobile")
    @ApiOperation(value = "根据手机号查询用户")
    public SysUser findByMobile(String mobile) {
        return appUserService.findByMobile(mobile);
    }

    /**
     * 根据OpenId查询用户信息
     *
     * @param openId openId
     */
    @GetMapping(value = "/users-anon/openId", params = "openId")
    @ApiOperation(value = "根据OpenId查询用户")
    public SysUser findByOpenId(String openId) {
        return appUserService.findByOpenId(openId);
    }

    @GetMapping("/users/{id}")
    public SysUser findUserById(@PathVariable Long id) {
        return appUserService.getById(id);
    }

    /**
     * 管理后台修改用户
     *
     * @param sysUser
     */
    @PutMapping("/users")
123 124
    @CachePut(value = "user", key = "#sysUser.username", unless="#result == null")
    //@AuditLog(operation = "'更新用户:' + #sysUser")
zlt2000's avatar
zlt2000 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    public void updateSysUser(@RequestBody SysUser sysUser) {
        appUserService.updateById(sysUser);
    }

    /**
     * 管理后台给用户分配角色
     *
     * @param id
     * @param roleIds
     */
    @PostMapping("/users/{id}/roles")
    public void setRoleToUser(@PathVariable Long id, @RequestBody Set<Long> roleIds) {
        appUserService.setRoleToUser(id, roleIds);
    }

    /**
     * 获取用户的角色
     *
     * @param
     * @return
     */
    @GetMapping("/users/{id}/roles")
    public List<SysRole> findRolesByUserId(@PathVariable Long id) {
        return appUserService.findRolesByUserId(id);
    }

    /**
     * 用户查询
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "用户查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    @GetMapping("/users")
    public PageResult<SysUser> findUsers(@RequestParam Map<String, Object> params) {
        return appUserService.findUsers(params);
    }

    /**
     * 修改用户状态
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "修改用户状态")
    @GetMapping("/users/updateEnabled")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "enabled", value = "是否启用", required = true, dataType = "Boolean")
    })
    public Result updateEnabled(@RequestParam Map<String, Object> params) {
        Long id = MapUtils.getLong(params, "id");
        if (checkAdmin(id)) {
182
            return Result.failed(ADMIN_CHANGE_MSG);
zlt2000's avatar
zlt2000 已提交
183 184 185 186 187 188 189 190 191 192
        }
        return appUserService.updateEnabled(params);
    }

    /**
     * 管理后台,给用户重置密码
     *
     * @param id
     */
    @PutMapping(value = "/users/{id}/password")
193
    //@AuditLog(operation = "'重置用户密码:' + #id")
zlt2000's avatar
zlt2000 已提交
194 195
    public Result resetPassword(@PathVariable Long id) {
        if (checkAdmin(id)) {
196
            return Result.failed(ADMIN_CHANGE_MSG);
zlt2000's avatar
zlt2000 已提交
197 198 199 200 201 202 203 204 205 206 207
        }
        appUserService.updatePassword(id, null, null);
        return Result.succeed("重置成功");
    }

    /**
     * 用户自己修改密码
     */
    @PutMapping(value = "/users/password")
    public Result resetPassword(@RequestBody SysUser sysUser) {
        if (checkAdmin(sysUser.getId())) {
208
            return Result.failed(ADMIN_CHANGE_MSG);
zlt2000's avatar
zlt2000 已提交
209 210 211 212 213 214 215 216 217 218 219
        }
        appUserService.updatePassword(sysUser.getId(), sysUser.getOldPassword(), sysUser.getNewPassword());
        return Result.succeed("重置成功");
    }

    /**
     * 删除用户
     *
     * @param id
     */
    @DeleteMapping(value = "/users/{id}")
220
    //@AuditLog(operation = "'删除用户:' + #id")
zlt2000's avatar
zlt2000 已提交
221 222
    public Result delete(@PathVariable Long id) {
        if (checkAdmin(id)) {
223
            return Result.failed(ADMIN_CHANGE_MSG);
zlt2000's avatar
zlt2000 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237
        }
        appUserService.delUser(id);
        return Result.succeed("删除成功");
    }


    /**
     * 新增or更新
     *
     * @param sysUser
     * @return
     */
    @CacheEvict(value = "user", key = "#sysUser.username")
    @PostMapping("/users/saveOrUpdate")
238
    @AuditLog(operation = "'新增或更新用户:' + #sysUser.username")
239
    public Result saveOrUpdate(@RequestBody SysUser sysUser) throws Exception {
zlt2000's avatar
zlt2000 已提交
240 241 242 243 244 245 246 247
        return appUserService.saveOrUpdateUser(sysUser);
    }

    /**
     * 导出excel
     *
     * @return
     */
248
    @PostMapping("/users/export")
zlt2000's avatar
zlt2000 已提交
249 250 251
    public void exportUser(@RequestParam Map<String, Object> params, HttpServletResponse response) throws IOException {
        List<SysUserExcel> result = appUserService.findAllUsers(params);
        //导出操作
252
        ExcelUtil.exportExcel(result, null, "用户", SysUserExcel.class, "user", response);
zlt2000's avatar
zlt2000 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    }

    @PostMapping(value = "/users/import")
    public Result importExcl(@RequestParam("file") MultipartFile excl) throws Exception {
        int rowNum = 0;
        if(!excl.isEmpty()) {
            List<SysUserExcel> list = ExcelUtil.importExcel(excl, 0, 1, SysUserExcel.class);
            rowNum = list.size();
            if (rowNum > 0) {
                List<SysUser> users = new ArrayList<>(rowNum);
                list.forEach(u -> {
                    SysUser user = new SysUser();
                    BeanUtil.copyProperties(u, user);
                    user.setPassword(CommonConstant.DEF_USER_PASSWORD);
                    user.setType(UserType.BACKEND.name());
                    users.add(user);
                });
270
                appUserService.saveBatch(users);
zlt2000's avatar
zlt2000 已提交
271 272 273 274 275
            }
        }
        return Result.succeed("导入数据成功,一共【"+rowNum+"】行");
    }

276 277 278 279 280 281 282
    @ApiOperation(value = "用户全文搜索列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "queryStr", value = "搜索关键字", dataType = "String")
    })
    @GetMapping("/users/search")
zlt2000's avatar
zlt2000 已提交
283
    public PageResult<JsonNode> search(SearchDto searchDto) {
284 285 286 287 288
        searchDto.setIsHighlighter(true);
        searchDto.setSortCol("createTime");
        return queryService.strQuery("sys_user", searchDto, SEARCH_LOGIC_DEL_DTO);
    }

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    /**
     * 获取用户并返回角色列表
     * @param username
     * @return
     */
    @GetMapping(value = "/users/roleUser/{username}")
    @ApiOperation(value = "查询用户-带角色信息")
    @Cacheable(value = "userRoles", key = "#username")
    public SysUser selectRoleUser(@PathVariable("username") String username){
        SysUser sysUser = selectByUsername(username);
        if(ObjectUtil.isNotNull(sysUser)){
            List<SysRole> roleList = findRolesByUserId(sysUser.getId());
            sysUser.setRoles(roleList);
        }
        return sysUser;
    }

zlt2000's avatar
zlt2000 已提交
306 307 308 309 310 311 312
    /**
     * 是否超级管理员
     */
    private boolean checkAdmin(long id) {
        return id == 1L;
    }
}