package com.ruoyi.web.controller.system; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.ruoyi.common.annotation.Anonymous; import com.ruoyi.common.core.domain.entity.sysUserInfo; import com.ruoyi.domain.*; import com.ruoyi.service.*; import com.ruoyi.system.domain.SysUserRole; import com.ruoyi.system.mapper.SysRoleMapper; import com.ruoyi.system.mapper.SysUserRoleMapper; import org.apache.commons.lang3.ArrayUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.entity.SysDept; import com.ruoyi.common.core.domain.entity.SysRole; import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.system.service.ISysDeptService; import com.ruoyi.system.service.ISysPostService; import com.ruoyi.system.service.ISysRoleService; import com.ruoyi.system.service.ISysUserService; /** * 用户信息 * * @author ruoyi */ @RestController @RequestMapping("/system/user") public class SysUserController extends BaseController { @Autowired private ISysUserService userService; @Autowired private ISysRoleService roleService; @Autowired private ISysDeptService deptService; @Autowired private ISysPostService postService; @Resource private ZInfoUserService zInfoUserService; @Resource private ZfFamilyService zfFamilyService; @Resource private ZfAncestorService zfAncestorService; @Resource private SysUserRoleMapper sysUserRoleMapper; @Resource MarrySelfService marrySelfService; @Resource MarryUserService marryUserService; /** * 获取用户列表 */ @PreAuthorize("@ss.hasPermi('system:user:list')") @GetMapping("/list") public TableDataInfo list(SysUser user) { startPage(); System.out.println(user.getClanId()); System.out.println("----------------++++++++++++==0------------"); List list = userService.selectUserList(user); return getDataTable(list); } @GetMapping("/listCommonUser") public TableDataInfo listCommonUser(SysUser user) { startPage(); // System.out.println(user.getClanId()); System.out.println("----------------++++++++++++==0------------"); List list = userService.selectCommonUserList(user); return getDataTable(list); } @Log(title = "用户管理", businessType = BusinessType.EXPORT) @PreAuthorize("@ss.hasPermi('system:user:export')") @PostMapping("/exportCommonUser") public void exportCommonUser(HttpServletResponse response, SysUser user) { List list = userService.selectCommonUserList(user); ExcelUtil util = new ExcelUtil(SysUser.class); util.exportExcel(response, list, "用户数据"); } @Log(title = "用户管理", businessType = BusinessType.EXPORT) @PreAuthorize("@ss.hasPermi('system:user:export')") @PostMapping("/export") public void export(HttpServletResponse response, SysUser user) { List list = userService.selectUserList(user); ExcelUtil util = new ExcelUtil(SysUser.class); util.exportExcel(response, list, "用户数据"); } @Log(title = "用户管理", businessType = BusinessType.IMPORT) @PreAuthorize("@ss.hasPermi('system:user:import')") @PostMapping("/importData") public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception { ExcelUtil util = new ExcelUtil(SysUser.class); List userList = util.importExcel(file.getInputStream()); String operName = getUsername(); String message = userService.importUser(userList, updateSupport, operName); return success(message); } @PostMapping("/importTemplate") public void importTemplate(HttpServletResponse response) { ExcelUtil util = new ExcelUtil(SysUser.class); util.importTemplateExcel(response, "用户数据"); } /** * 根据用户编号获取详细信息 */ @PreAuthorize("@ss.hasPermi('system:user:query')") @GetMapping(value = { "/", "/{userId}" }) public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId) { // userService.checkUserDataScope(userId); AjaxResult ajax = AjaxResult.success(); List roles = roleService.selectRoleAll(); //如果是admin则直接返回所有的角色,否则就返回不是管理员的角色信息 ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList())); ajax.put("posts", postService.selectPostAll()); if (StringUtils.isNotNull(userId)) { SysUser sysUser = userService.selectUserById(userId); ajax.put(AjaxResult.DATA_TAG, sysUser); ajax.put("postIds", postService.selectPostListByUserId(userId)); // ajax.put("roleIds", sysUser.getRoles().stream().map(SysRole::getRoleId).collect(Collectors.toList())); } return ajax; } /** * 修改用户 * */ @PreAuthorize("@ss.hasPermi('system:user:edit')") @Log(title = "用户管理", businessType = BusinessType.UPDATE) @PostMapping("/updateUserInfo") public AjaxResult editUser(@Validated @RequestBody sysUserAndUserInfo user) { SysUser us = new SysUser(); us.setUserId(user.getSysId()); us.setUserName(user.getUserName()); us.setNickName(user.getNickName()); us.setRoleId(user.getRoleId()); us.setSex(user.getSex()); us.setStatus(user.getStatus()); us.setRemark(user.getRemark()); userService.updateUser(us); // System.out.println(user.getFamilyId()); // System.out.println("[[[[]]]]]]"); Long userId = user.getSysId(); // SysUserRole sysUserRole=sysUserRoleMapper.selectUserById(userId); //System.out.println(sysUserRole.getRoleId()); System.out.println("你好呀"); System.out.println(user.getRoleId()); if (user.getRoleId() != null && user.getRoleId().equals(102L)){ ZInfoUser infoUser = new ZInfoUser(); infoUser.setUserId(user.getUserId()); infoUser.setOldName(user.getUserName()); infoUser.setNickName(user.getNickName()); infoUser.setIsMyFamily(user.getIsMyFamily()); infoUser.setMomId(user.getMid()); infoUser.setFatherId(user.getFid()); if (user.getSex() != null)infoUser.setSex(Integer.valueOf(user.getSex())); // infoUser.setFamilyId(user.getFamilyId()); infoUser.setSysId(user.getSysId()); if(user.getSpouseId()!=null) { infoUser.setSpouseId(user.getSpouseId()); } // ZfFamily zfFamily = zfFamilyService.getByFamId(user.getFamilyId()); // if(zfFamily!=null&&zfFamily.getFid()!=null) // infoUser.setFatherId(zfFamily.getFid()); // if(zfFamily!=null&&zfFamily.getMid()!=null) // infoUser.setMomId(zfFamily.getMid()); Long myFamily = zInfoUserService.getById(user).getFamilyId(); if (myFamily != null) infoUser.setFamilyId(myFamily); if (user.getFid() != null){ //跟随父亲的家族和家庭信息 // if (!zInfoUserService.getById(user.getUserId()).getFatherId().equals(user.getFid())){ ZInfoUser father = zInfoUserService.getById(user.getFid()); System.out.println("11111111111111111111111111111111/"); // infoUser.setFamilyId(father.getFamilyId()); infoUser.setClanId(father.getClanId()); // } } if(user.getMid()!=null) { //跟随母亲的家族和家庭信息 // if (!zInfoUserService.getById(user.getUserId()).getFatherId().equals(user.getFid())){ ZInfoUser mother = zInfoUserService.getById(user.getMid()); System.out.println("11111111111111111111111111111111/"); // infoUser.setFamilyId(mother.getFamilyId()); infoUser.setClanId(mother.getClanId()); } // else if(user.getMid() != null){ // ZInfoUser mother = zInfoUserService.getById(user.getMid()); // if (mother.getIsMyFamily() == 1) // System.out.println("2222222222222222222222222222222222/"); // infoUser.setFamilyId(mother.getFamilyId()); // } if (user.getSpouseId() != null){ //跟随配偶的家族和家庭信息 // if ( !zInfoUserService.getById(user.getUserId()).getSpouseId().equals(user.getSpouseId())){ //是本家,一种是普通用户更新为家庭用户, if (zInfoUserService.getById(user.getUserId())==null||zInfoUserService.getById(user.getUserId()).getIsMyFamily() == 0) { ZInfoUser spouse = zInfoUserService.getById(user.getSpouseId()); System.out.println("33333333333333333333333333333333333333333/"); infoUser.setFamilyId(spouse.getFamilyId()); if(spouse.getFatherId() !=null || spouse.getMomId() !=null) { infoUser.setFatherId(spouse.getFatherId()); infoUser.setMomId(spouse.getMomId()); } infoUser.setClanId(spouse.getClanId()); // } } } if(infoUser.getFamilyId() != null) zInfoUserService.saveOrUpdate(infoUser); else { System.out.println("家庭成员不能修改为普通用户"); return AjaxResult.error("家庭成员不能修改为普通用户"); } //更新配偶信息 if(user.getSpouseId()!=null) { ZInfoUser zu = new ZInfoUser(); zu.setUserId(user.getSpouseId()); zu.setSpouseId(infoUser.getUserId()); // Long spouseMoM = zInfoUserService.getById(user.getSpouseId()).getMomId(); // Long spouseDad = zInfoUserService.getById(user.getSpouseId()).getFatherId(); // zu.setMomId(spouseMoM); // zu.setFatherId(spouseDad); // zInfoUserService.updateById(zu); //更新MarrySelf和MarryUser ZInfoUser zfi = zInfoUserService.getById(user.getUserId()); MarrySelf marrySelf = new MarrySelf(); marrySelf.setUid(user.getSpouseId()); marrySelf.setName(zfi.getOldName()); marrySelf.setSpouseSex(zfi.getSex()==1?"男":"女"); //这个可能还要修改 一下 marrySelf.setMarryStatus(0); marrySelf.setSpouseName(infoUser.getOldName()); marrySelf.setSpouseSex(infoUser.getSex()==1?"男":"女"); marrySelf.setSpouseMarryStatus(0); marrySelfService.saveOrUpdate(marrySelf); //增加MarryUser //增加marryUser MarryUser marryUser = new MarryUser(); marryUser.setUid(user.getSpouseId()); marryUser.setName(infoUser.getOldName()); marryUser.setSex(infoUser.getSex()==1?"男":"女"); marryUser.setMarryStatus(0); marryUserService.addData(marryUser); } return AjaxResult.success(); } else { System.out.println("daniuni"); return AjaxResult.error("家庭成员不能修改为普通用户"); } // zInfoUserService.update(infoUser); // .saveOrUpdate(infoUser); } /** * 新增用户 */ @PreAuthorize("@ss.hasPermi('system:user:add')") @Log(title = "用户管理", businessType = BusinessType.INSERT) @PostMapping public AjaxResult add(@Validated @RequestBody sysUserAndUserInfo userAll) { SysUser user = new SysUser(); // user.setUserId(userAll.getUserId()); user.setAvatar(userAll.getAvatar()); user.setPassword("123456"); user.setNickName(userAll.getNickName()); user.setUserName(userAll.getUserName()); user.setPhonenumber(userAll.getPhonenumber()); user.setRoleId(userAll.getRoleId()); user.setRoleIds(userAll.getRoleIds()); user.setStatus(userAll.getStatus()); user.setSex(userAll.getSex()); user.setRemark(userAll.getRemark()); user.setUpdateBy(userAll.getUpdateBy()); user.setClanId(userAll.getClanId()); // user.setSex(userAll.getSex()=="男"?"1":"0"); // user.setPostIds(userAll.getPostIds()); // user.set if (!userService.checkNickNameUnique(user)) { return error("新增用户'" + user.getNickName() + "'失败,登录账号已存在"); } else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) { return error("新增用户'" + user.getUserName() + "'失败,手机号码已存在"); } else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user)) { return error("新增用户'" + user.getUserName() + "'失败,邮箱账号已存在"); } user.setCreateBy(getUsername()); user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); userService.insertUser(user); //写到用户与角色对应的表 SysUserRole sysUserRole = new SysUserRole(); sysUserRole.setUserId(user.getUserId()); sysUserRole.setRoleId(user.getRoleId()); List sysUserRoles = new ArrayList<>(); sysUserRoles.add(sysUserRole); sysUserRoleMapper.batchUserRole(sysUserRoles); //写入到 新建立的信息表里面 // ZfFamily zfFamily = zfFamilyService.getByFamId(userAll.getFamilyId()); if (userAll.getRoleId() == 102){ ZInfoUser infoUser = new ZInfoUser(); infoUser.setSysId(user.getUserId()); // infoUser.setUserId(user.getUserId()); infoUser.setNickName(userAll.getNickName()); infoUser.setOldName(userAll.getUserName()); infoUser.setPhoneNumber(userAll.getPhonenumber()); System.out.println(userAll.getSex()); System.out.println("性别的原因"); infoUser.setSex(userAll.getSex().equals("0")?0:1); System.out.println(infoUser.getSex()); // infoUser.setFamilyId(userAll.getFid()); infoUser.setSpouseId(userAll.getSpouseId()); infoUser.setFatherId(userAll.getFid()); infoUser.setMomId(userAll.getMid()); // infoUser.setFamilyId(userAll.getFamilyId()); // if(zfFamily!=null&&zfFamily.getFid()!=null) { // infoUser.setFatherId(zfFamily.getFid()); // // infoUser.setMomId(zfFamily.getMid()); // } // if(zfFamily!=null&&zfFamily.getMid()!=null) { // infoUser.setFatherId(zfFamily.getMid()); // // infoUser.setMomId(zfFamily.getMid()); // } // System.out.println("-----"); // System.out.println(userAll.getIsMyFamily()); infoUser.setIsMyFamily(userAll.getIsMyFamily()); if(userAll.getIsMyFamily()==1) { ZfFamily zfFamily = new ZfFamily(); zfFamily.setName(userAll.getNickName()+"的家庭"); zfFamilyService.addFamily(zfFamily); infoUser.setFamilyId(zfFamily.getId()); } // if(userAll.getSpouseId()==0) // infoUser.setSpouseId(null); // else { // infoUser.setSpouseId(userAll.getSpouseId()); // // } if (userAll.getFid() != null && userAll.getMid() != null){ //证明是本家的一个,增加进来的 ZInfoUser father = zInfoUserService.getById(userAll.getFid()); ZInfoUser mother = zInfoUserService.getById(userAll.getMid()); //如果是本家,就简历一个家庭号 if (father.getIsMyFamily() == 1){ // infoUser.setFamilyId(father.getFamilyId()); infoUser.setClanId(father.getClanId()); } else if (mother.getIsMyFamily() == 1){ // infoUser.setFamilyId(mother.getFamilyId()); infoUser.setClanId(mother.getClanId()); } MarrySelf marrySelf = new MarrySelf(); marrySelf.setName(infoUser.getOldName()); marrySelf.setSex(infoUser.getSex()==1?"男":"女"); marrySelf.setMarryStatus(0); marrySelfService.save(marrySelf); } else { ZInfoUser spouse = zInfoUserService.getById(userAll.getSpouseId()); infoUser.setFamilyId(spouse.getFamilyId()); infoUser.setClanId(spouse.getClanId()); //通过配偶的信息,更新marrySelf和增加marryUser表 MarrySelf marrySelf = new MarrySelf(); // marrySelf.setUid(userAll.getSpouseId()); marrySelf.setSpouseName(infoUser.getOldName()); marrySelf.setSpouseSex(infoUser.getSex()==1?"男":"女"); marrySelf.setMarryStatus(1); //修改条件 QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("uid", userAll.getSpouseId()); marrySelfService.update(marrySelf, queryWrapper); //增加marryUser MarryUser marryUser = new MarryUser(); marryUser.setUid(userAll.getSpouseId()); marryUser.setName(infoUser.getOldName()); marryUser.setSex(infoUser.getSex()==1?"男":"女"); marryUser.setMarryStatus(0); marryUserService.addData(marryUser); } //前端保证了角色不为空 // if(user.getRoleIds()[0]==102) // infoUser.setRoleName(user.g); // if(userAll.getFamilyId()!=-1) { if(userAll.getRoleId().equals(102L)) { infoUser.setRoleId(3L); zInfoUserService.save(infoUser); if(userAll.getSpouseId()!=null) { //更新配偶信息 ZInfoUser zu = new ZInfoUser(); zu.setUserId(userAll.getSpouseId()); zu.setSpouseId(infoUser.getUserId()); Long spouseMoM = zInfoUserService.getById(userAll.getSpouseId()).getMomId(); Long spouseDad = zInfoUserService.getById(userAll.getSpouseId()).getFatherId(); zu.setMomId(spouseMoM); zu.setFatherId(spouseDad); zInfoUserService.updateById(zu); } } } // zInfoUserService.saveOrUpdate(infoUser); // LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); // lqw.eq(ZInfoUser::getSpouseId, userAll.getSpouseId()); // List lis = zInfoUserService.list(lqw); // if(lis.size()>0) { //// ZInfoUser zi = new ZInfoUser(); //// zi.setUserId(userAll.getSpouseId()); //// // UpdateWrapper wrapper = new UpdateWrapper(); //// zi.setSpouseId(lis.get(0).getUserId()); //// System.out.println(userAll.getSpouseId()); //// System.out.println(lis.get(0).getUserId()); // zInfoUserService.lambdaUpdate().eq(ZInfoUser::getUserId, userAll.getSpouseId()) // .set(ZInfoUser::getSpouseId, lis.get(0).getUserId()) // .update(); // } // } return AjaxResult.success(); } /** * 修改用户 */ @PreAuthorize("@ss.hasPermi('system:user:edit')") @Log(title = "用户管理", businessType = BusinessType.UPDATE) @PutMapping public AjaxResult edit(@Validated @RequestBody SysUser user) { userService.checkUserAllowed(user); userService.checkUserDataScope(user.getUserId()); if (!userService.checkNickNameUnique(user)) { return error("修改用户'" + user.getNickName() + "'失败,登录账号已存在"); } else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) { return error("修改用户'" + user.getUserName() + "'失败,手机号码已存在"); } else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user)) { return error("修改用户'" + user.getUserName() + "'失败,邮箱账号已存在"); } user.setUpdateBy(getUsername()); return toAjax(userService.updateUser(user)); } /** * 删除用户 */ @PreAuthorize("@ss.hasPermi('system:user:remove')") @Log(title = "用户管理", businessType = BusinessType.DELETE) @DeleteMapping("/{userIds}") public AjaxResult remove(@PathVariable Long[] userIds) { if (ArrayUtils.contains(userIds, getUserId())) { return error("当前用户不能删除"); } // for(Long uid: userIds) { // zInfoUserService.getInfoBysysId(); // } // zInfoUserService.removeByUserName( userService.findUserNames(userIds)); return toAjax(userService.deleteUserByIds(userIds)); } /** * 重置密码 */ @PreAuthorize("@ss.hasPermi('system:user:resetPwd')") @Log(title = "用户管理", businessType = BusinessType.UPDATE) @PutMapping("/resetPwd") public AjaxResult resetPwd(@RequestBody SysUser user) { // userService.checkUserAllowed(user); // userService.checkUserDataScope(user.getUserId()); user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); user.setUpdateBy(getUsername()); return toAjax(userService.resetPwd(user)); } /** * 状态修改 */ @PreAuthorize("@ss.hasPermi('system:user:edit')") @Log(title = "用户管理", businessType = BusinessType.UPDATE) @PutMapping("/changeStatus") public AjaxResult changeStatus(@RequestBody SysUser user) { userService.checkUserAllowed(user); userService.checkUserDataScope(user.getUserId()); user.setUpdateBy(getUsername()); return toAjax(userService.updateUserStatus(user)); } /** * 根据用户编号获取授权角色 */ @PreAuthorize("@ss.hasPermi('system:user:query')") @GetMapping("/authRole/{userId}") public AjaxResult authRole(@PathVariable("userId") Long userId) { AjaxResult ajax = AjaxResult.success(); SysUser user = userService.selectUserById(userId); List roles = roleService.selectRolesByUserId(userId); ajax.put("user", user); ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList())); return ajax; } /** * 用户授权角色 */ @PreAuthorize("@ss.hasPermi('system:user:edit')") @Log(title = "用户管理", businessType = BusinessType.GRANT) @PutMapping("/authRole") public AjaxResult insertAuthRole(Long userId, Long[] roleIds) { userService.checkUserDataScope(userId); userService.insertUserAuth(userId, roleIds); return success(); } /** * 获取部门树列表 */ @PreAuthorize("@ss.hasPermi('system:user:list')") @GetMapping("/deptTree") public AjaxResult deptTree(SysDept dept) { return success(deptService.selectDeptTreeList(dept)); } }