package com.vincent.rsf.server.system.controller; 
 | 
  
 | 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 
 | 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; 
 | 
import com.vincent.rsf.framework.common.BaseRes; 
 | 
import com.vincent.rsf.framework.common.Cools; 
 | 
import com.vincent.rsf.framework.common.R; 
 | 
import com.vincent.rsf.framework.exception.CoolException; 
 | 
import com.vincent.rsf.server.common.annotation.OperationLog; 
 | 
import com.vincent.rsf.server.common.domain.BaseParam; 
 | 
import com.vincent.rsf.server.common.domain.KeyValVo; 
 | 
import com.vincent.rsf.server.common.domain.PageParam; 
 | 
import com.vincent.rsf.server.common.domain.PageResult; 
 | 
import com.vincent.rsf.server.common.utils.ExcelUtil; 
 | 
import com.vincent.rsf.server.system.entity.Tenant; 
 | 
import com.vincent.rsf.server.system.entity.User; 
 | 
import com.vincent.rsf.server.system.entity.UserRole; 
 | 
import com.vincent.rsf.server.system.enums.StatusType; 
 | 
import com.vincent.rsf.server.system.service.TenantService; 
 | 
import com.vincent.rsf.server.system.service.UserRoleService; 
 | 
import com.vincent.rsf.server.system.service.UserService; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.security.access.prepost.PreAuthorize; 
 | 
import org.springframework.transaction.annotation.Transactional; 
 | 
import org.springframework.web.bind.annotation.*; 
 | 
  
 | 
import javax.servlet.http.HttpServletResponse; 
 | 
import java.util.*; 
 | 
  
 | 
@RestController 
 | 
public class UserController extends BaseController { 
 | 
  
 | 
    public static final String COMMON_PASSWORD = "123456"; 
 | 
  
 | 
    @Autowired 
 | 
    private UserService userService; 
 | 
    @Autowired 
 | 
    private TenantService tenantService; 
 | 
    @Autowired 
 | 
    private UserRoleService userRoleService; 
 | 
  
 | 
    @PreAuthorize("hasAuthority('system:user:list')") 
 | 
    @PostMapping("/user/page") 
 | 
    public R page(@RequestBody Map<String, Object> map) { 
 | 
        BaseParam baseParam = buildParam(map, BaseParam.class); 
 | 
        PageParam<User, BaseParam> pageParam = new PageParam<>(baseParam, User.class); 
 | 
        PageResult<User> pageResult = userService.pageRel(pageParam); 
 | 
        for (User record : pageResult.getRecords()) { 
 | 
            record.setRoles(userRoleService.listByUserId(record.getId())); 
 | 
        } 
 | 
        return R.ok().add(pageResult); 
 | 
    } 
 | 
  
 | 
    @PreAuthorize("hasAuthority('system:user:list')") 
 | 
    @PostMapping("/user/list") 
 | 
    public R list(@RequestBody Map<String, Object> map) { 
 | 
        return R.ok().add(userService.list()); 
 | 
    } 
 | 
  
 | 
    @PreAuthorize("hasAuthority('system:user:list')") 
 | 
    @PostMapping({"/user/many/{ids}", "/users/many/{ids}"}) 
 | 
    public R many(@PathVariable Long[] ids) { 
 | 
        return R.ok().add(userService.listByIds(Arrays.asList(ids))); 
 | 
    } 
 | 
  
 | 
    @PreAuthorize("hasAuthority('system:user:list')") 
 | 
    @GetMapping("/user/{id}") 
 | 
    public R get(@PathVariable("id") Long id) { 
 | 
        return R.ok().add(userService.getById(id)); 
 | 
    } 
 | 
  
 | 
    @PreAuthorize("hasAuthority('system:user:save')") 
 | 
    @OperationLog("Save User") 
 | 
    @PostMapping("/user/save") 
 | 
    @Transactional 
 | 
    public R save(@RequestBody User user) { 
 | 
        if (!Cools.isEmpty(user.getUsername()) 
 | 
                && userService.count(new LambdaQueryWrapper<User>().eq(User::getUsername, user.getUsername())) > 0) { 
 | 
            return R.error("the username already exist"); 
 | 
        } 
 | 
        if (!Cools.isEmpty(user.getNickname()) 
 | 
                && userService.count(new LambdaQueryWrapper<User>().eq(User::getNickname, user.getNickname())) > 0) { 
 | 
            return R.error("the nickname already exist"); 
 | 
        } 
 | 
        if (!Cools.isEmpty(user.getPhone()) 
 | 
                && userService.count(new LambdaQueryWrapper<User>().eq(User::getPhone, user.getPhone())) > 0) { 
 | 
            return R.error("the phone already exist"); 
 | 
        } 
 | 
        if (!Cools.isEmpty(user.getEmail()) 
 | 
                && userService.count(new LambdaQueryWrapper<User>().eq(User::getEmail, user.getEmail())) > 0) { 
 | 
            return R.error("the email already exist"); 
 | 
        } 
 | 
  
 | 
        if (Cools.isEmpty(user.getPassword())) { 
 | 
            user.setPassword(userService.encodePassword(COMMON_PASSWORD)); 
 | 
        } 
 | 
  
 | 
        Date now = new Date(); 
 | 
        user.setCreateBy(getLoginUserId()); 
 | 
        user.setCreateTime(now); 
 | 
        user.setUpdateBy(getLoginUserId()); 
 | 
        user.setUpdateTime(now); 
 | 
        user.setStatus(StatusType.ENABLE.val); 
 | 
        if (!userService.save(user)) { 
 | 
            throw new CoolException("Internal Server Error"); 
 | 
        } 
 | 
  
 | 
        userRoleService.remove(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, user.getId())); 
 | 
        if (!Cools.isEmpty((Object) user.getRoleIds())) { 
 | 
            for (Long roleId : user.getRoleIds()) { 
 | 
                if (!userRoleService.save(new UserRole(user.getId(), roleId))) { 
 | 
                    throw new CoolException("Internal Server Error"); 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
  
 | 
        return R.ok("Save Success").add(user); 
 | 
    } 
 | 
  
 | 
    @PreAuthorize("hasAuthority('system:user:update')") 
 | 
    @OperationLog("Update User") 
 | 
    @PostMapping("/user/update") 
 | 
    @Transactional 
 | 
    public R update(@RequestBody User user) { 
 | 
        Tenant tenant = tenantService.getById(getTenantId()); 
 | 
        if (!user.getId().equals(getLoginUserId()) && tenant.getRoot().equals(user.getId())) { 
 | 
            return R.parse(BaseRes.LIMIT); 
 | 
        } 
 | 
  
 | 
        if (user.getPassword() != null && user.getPassword().equals("")) { 
 | 
            user.setPassword(null); 
 | 
        } 
 | 
        if (!Cools.isEmpty(user.getUsername()) && userService.count(new LambdaQueryWrapper<User>() 
 | 
                .eq(User::getUsername, user.getUsername()) 
 | 
                .ne(User::getId, user.getId())) > 0) { 
 | 
            return R.error("the username already exist"); 
 | 
        } 
 | 
        if (!Cools.isEmpty(user.getNickname()) && userService.count(new LambdaQueryWrapper<User>() 
 | 
                .eq(User::getNickname, user.getNickname()) 
 | 
                .ne(User::getId, user.getId())) > 0) { 
 | 
            return R.error("the nickname already exist"); 
 | 
        } 
 | 
        if (!Cools.isEmpty(user.getPhone()) && userService.count(new LambdaQueryWrapper<User>() 
 | 
                .eq(User::getPhone, user.getPhone()) 
 | 
                .ne(User::getId, user.getId())) > 0) { 
 | 
            return R.error("the phone already exist"); 
 | 
        } 
 | 
        if (!Cools.isEmpty(user.getEmail()) && userService.count(new LambdaQueryWrapper<User>() 
 | 
                .eq(User::getEmail, user.getEmail()) 
 | 
                .ne(User::getId, user.getId())) > 0) { 
 | 
            return R.error("the email already exist"); 
 | 
        } 
 | 
  
 | 
        if (!Cools.isEmpty(user.getPassword())) { 
 | 
            user.setPassword(userService.encodePassword(user.getPassword())); 
 | 
        } 
 | 
  
 | 
        user.setUpdateBy(getLoginUserId()); 
 | 
        user.setUpdateTime(new Date()); 
 | 
  
 | 
        if (!userService.updateById(user)) { 
 | 
            throw new CoolException("Internal Server Error"); 
 | 
        } 
 | 
  
 | 
        if (user.getRoleIds() != null && user.getRoleIds().length > 0) { 
 | 
            userRoleService.remove(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, user.getId())); 
 | 
            for (Long roleId : user.getRoleIds()) { 
 | 
                if (!userRoleService.save(new UserRole(user.getId(), roleId))) { 
 | 
                    throw new CoolException("Internal Server Error"); 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
  
 | 
        return R.ok("Update Success").add(user); 
 | 
    } 
 | 
  
 | 
    @PreAuthorize("hasAuthority('system:user:remove')") 
 | 
    @OperationLog("Remove User") 
 | 
    @PostMapping("/user/remove/{ids}") 
 | 
    @Transactional 
 | 
    public R remove(@PathVariable Long[] ids) { 
 | 
        Tenant tenant = tenantService.getById(getTenantId()); 
 | 
        Long loginUserId = getLoginUserId(); 
 | 
        for (Long userId : ids) { 
 | 
            if (!userId.equals(loginUserId) && tenant.getRoot().equals(userId)) { 
 | 
                return R.parse(BaseRes.LIMIT); 
 | 
            } 
 | 
            if (!userService.removeById(userId)) { 
 | 
                throw new CoolException("Internal Server Error"); 
 | 
            } 
 | 
            userRoleService.remove(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, userId)); 
 | 
        } 
 | 
        return R.ok("Delete Success").add(ids); 
 | 
    } 
 | 
  
 | 
    @PreAuthorize("hasAuthority('system:user:list')") 
 | 
    @PostMapping("/user/query") 
 | 
    public R query(@RequestParam(required = false) String condition) { 
 | 
        List<KeyValVo> vos = new ArrayList<>(); 
 | 
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>(); 
 | 
        if (!Cools.isEmpty(condition)) { 
 | 
            wrapper.like(User::getNickname, condition); 
 | 
        } 
 | 
        userService.page(new Page<>(1, 30), wrapper).getRecords().forEach( 
 | 
                item -> vos.add(new KeyValVo(item.getId(), item.getNickname())) 
 | 
        ); 
 | 
        return R.ok().add(vos); 
 | 
    } 
 | 
  
 | 
    @PreAuthorize("hasAuthority('system:user:list')") 
 | 
    @PostMapping("/user/export") 
 | 
    public void export(@RequestBody Map<String, Object> map, HttpServletResponse response) throws Exception { 
 | 
        ExcelUtil.build(ExcelUtil.create(userService.list(), User.class), response); 
 | 
    } 
 | 
  
 | 
} 
 |