All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.base4j.mvc.sys.controller.SysUserController Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
package com.base4j.mvc.sys.controller;

import com.base4j.mvc.base.controller.BaseController;
import com.base4j.mvc.sys.entity.SysRole;
import com.base4j.mvc.sys.entity.SysUser;
import com.base4j.mvc.sys.entity.SysUserRole;
import com.base4j.mvc.sys.service.SysRoleService;
import com.base4j.mvc.sys.service.SysUserRoleService;
import com.base4j.mvc.sys.service.SysUserService;
import com.base4j.mvc.util.Res;
import com.base4j.mybatis.base.QueryParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping("/sys/user")
public class SysUserController extends BaseController {
    static public Logger logger = LoggerFactory.getLogger(SysUserController.class);

    private static final String SALT = "seed";

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Override
    public Res insert(SysUser sysUser) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encodePassword = passwordEncoder.encode("000000");
        sysUser.setPassword(encodePassword);
        return super.insert(sysUser);
    }

    /**
     * 查询用户角色信息
     * @param userId 用户id
     * @return
     */
    @RequestMapping("selectRoleInfo/{userId}")
    public Res selectRoleInfo(@PathVariable("userId") long userId) {
        //查询所有角色信息
        QueryParams allroleQueryParams = new QueryParams(SysRole.class);
        allroleQueryParams.selectProperties("id", "name");
        List allRoles = sysRoleService.selectListByParams(allroleQueryParams);
        //查询已有角色
        QueryParams userRoleQueryParams = new QueryParams(SysUserRole.class);
        userRoleQueryParams.selectProperties("id", "name");
        QueryParams.Criteria userRoleCriteria = userRoleQueryParams.createCriteria();
        userRoleCriteria.andEqualTo("sysUserId", userId);
        List userRoles = sysUserRoleService.selectListByParams(userRoleQueryParams);
        Map resMap = new HashMap<>();
        resMap.put("allRoles", allRoles);
        resMap.put("userRoles", userRoles);
        logger.info("测试日志");
        return Res.ok(resMap);
    }

    /**
     * 保存角色
     * @param userId 用户id
     * @param roleIds 要保存的角色id数组
     * @return
     */
    @RequestMapping("/saveUserRoles")
    public Res saveUserRoles(Long userId, Long[] roleIds) {
        sysUserRoleService.saveUserRoles(userId,roleIds);
        return Res.ok();
    }

    /**
     * 删除角色
     * @param id 主键
     * @return
     */
    @RequestMapping("/deleteByPrimary/{id}")
    public Res deleteByPrimary(@PathVariable("id") long id) {
        sysUserService.deleteByPrimaryKey(id);
        return Res.ok();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy