UserController.java 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. package com.xjrsoft.module.organization.controller;
  2. import cn.dev33.satoken.secure.BCrypt;
  3. import cn.dev33.satoken.session.SaSession;
  4. import cn.dev33.satoken.stp.StpUtil;
  5. import cn.hutool.core.bean.BeanUtil;
  6. import cn.hutool.core.util.ObjectUtil;
  7. import cn.hutool.core.util.StrUtil;
  8. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  9. import com.baomidou.mybatisplus.core.metadata.IPage;
  10. import com.baomidou.mybatisplus.core.toolkit.StringPool;
  11. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  12. import com.fasterxml.jackson.core.type.TypeReference;
  13. import com.github.yulichang.toolkit.MPJWrappers;
  14. import com.github.yulichang.wrapper.MPJLambdaWrapper;
  15. import com.xjrsoft.common.constant.GlobalConstant;
  16. import com.xjrsoft.common.enums.DeleteMark;
  17. import com.xjrsoft.common.enums.EnabledMark;
  18. import com.xjrsoft.common.enums.GenderDictionaryEnum;
  19. import com.xjrsoft.common.enums.RoleEnum;
  20. import com.xjrsoft.common.enums.WorkflowIsRecycleType;
  21. import com.xjrsoft.common.exception.MyException;
  22. import com.xjrsoft.common.model.result.R;
  23. import com.xjrsoft.common.model.result.RT;
  24. import com.xjrsoft.common.page.ConventPage;
  25. import com.xjrsoft.common.page.PageOutput;
  26. import com.xjrsoft.common.sms.SmsCtcc;
  27. import com.xjrsoft.common.utils.RedisUtil;
  28. import com.xjrsoft.common.utils.TreeUtil;
  29. import com.xjrsoft.common.utils.VoToColumnUtil;
  30. import com.xjrsoft.config.CommonPropertiesConfig;
  31. import com.xjrsoft.module.base.entity.BaseClass;
  32. import com.xjrsoft.module.base.entity.BaseGrade;
  33. import com.xjrsoft.module.base.service.IBaseClassService;
  34. import com.xjrsoft.module.base.service.IBaseGradeService;
  35. import com.xjrsoft.module.organization.dto.AddUserDto;
  36. import com.xjrsoft.module.organization.dto.BindOpenidDto;
  37. import com.xjrsoft.module.organization.dto.LoginResetPasswordDto;
  38. import com.xjrsoft.module.organization.dto.RegisterDto;
  39. import com.xjrsoft.module.organization.dto.ResetPasswordDto;
  40. import com.xjrsoft.module.organization.dto.UpdateInfoDto;
  41. import com.xjrsoft.module.organization.dto.UpdatePasswordDto;
  42. import com.xjrsoft.module.organization.dto.UpdateUserDto;
  43. import com.xjrsoft.module.organization.dto.UploadSignDto;
  44. import com.xjrsoft.module.organization.dto.UserPageDto;
  45. import com.xjrsoft.module.organization.dto.UserStudentAddDto;
  46. import com.xjrsoft.module.organization.dto.UserStudentBindDto;
  47. import com.xjrsoft.module.organization.dto.UserStudentDeleteDto;
  48. import com.xjrsoft.module.organization.entity.Department;
  49. import com.xjrsoft.module.organization.entity.Post;
  50. import com.xjrsoft.module.organization.entity.Role;
  51. import com.xjrsoft.module.organization.entity.User;
  52. import com.xjrsoft.module.organization.entity.UserDeptRelation;
  53. import com.xjrsoft.module.organization.entity.UserPostRelation;
  54. import com.xjrsoft.module.organization.entity.UserRoleRelation;
  55. import com.xjrsoft.module.organization.entity.UserStudent;
  56. import com.xjrsoft.module.organization.service.IDepartmentService;
  57. import com.xjrsoft.module.organization.service.IPostService;
  58. import com.xjrsoft.module.organization.service.IRoleService;
  59. import com.xjrsoft.module.organization.service.IUserDeptRelationService;
  60. import com.xjrsoft.module.organization.service.IUserPostRelationService;
  61. import com.xjrsoft.module.organization.service.IUserRoleRelationService;
  62. import com.xjrsoft.module.organization.service.IUserService;
  63. import com.xjrsoft.module.organization.service.IUserStudentService;
  64. import com.xjrsoft.module.organization.utils.OrganizationUtil;
  65. import com.xjrsoft.module.organization.vo.PendingCountDto;
  66. import com.xjrsoft.module.organization.vo.ResetUserPageVo;
  67. import com.xjrsoft.module.organization.vo.ResetUserRoleVo;
  68. import com.xjrsoft.module.organization.vo.UserDeptVo;
  69. import com.xjrsoft.module.organization.vo.UserDetailVo;
  70. import com.xjrsoft.module.organization.vo.UserInfoVo;
  71. import com.xjrsoft.module.organization.vo.UserListVo;
  72. import com.xjrsoft.module.organization.vo.UserPageVo;
  73. import com.xjrsoft.module.organization.vo.UserPostVo;
  74. import com.xjrsoft.module.organization.vo.UserRoleVo;
  75. import com.xjrsoft.module.organization.vo.UserStudentInfoVo;
  76. import com.xjrsoft.module.organization.vo.UserStudentVo;
  77. import com.xjrsoft.module.organization.vo.UserVo;
  78. import com.xjrsoft.module.oss.factory.OssFactory;
  79. import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
  80. import com.xjrsoft.module.student.entity.BaseStudentUser;
  81. import com.xjrsoft.module.student.service.IBaseStudentSchoolRollService;
  82. import com.xjrsoft.module.system.entity.DictionaryDetail;
  83. import com.xjrsoft.module.system.entity.File;
  84. import com.xjrsoft.module.system.service.IFileService;
  85. import com.xjrsoft.module.teacher.entity.BaseTeacher;
  86. import com.xjrsoft.module.workflow.constant.WorkflowConstant;
  87. import com.xjrsoft.module.workflow.mapper.WorkflowExtraMapper;
  88. import com.xjrsoft.module.workflow.service.IWorkflowExecuteService;
  89. import io.swagger.annotations.Api;
  90. import io.swagger.annotations.ApiOperation;
  91. import lombok.AllArgsConstructor;
  92. import org.camunda.bpm.engine.HistoryService;
  93. import org.camunda.bpm.engine.history.HistoricProcessInstance;
  94. import org.camunda.bpm.engine.history.HistoricProcessInstanceQuery;
  95. import org.springframework.web.bind.annotation.DeleteMapping;
  96. import org.springframework.web.bind.annotation.GetMapping;
  97. import org.springframework.web.bind.annotation.PostMapping;
  98. import org.springframework.web.bind.annotation.PutMapping;
  99. import org.springframework.web.bind.annotation.RequestBody;
  100. import org.springframework.web.bind.annotation.RequestMapping;
  101. import org.springframework.web.bind.annotation.RequestParam;
  102. import org.springframework.web.bind.annotation.RestController;
  103. import org.springframework.web.multipart.MultipartFile;
  104. import javax.validation.Valid;
  105. import java.util.ArrayList;
  106. import java.util.List;
  107. import java.util.Objects;
  108. import java.util.concurrent.CompletableFuture;
  109. import java.util.stream.Collectors;
  110. /**
  111. * <p>
  112. * 用户 前端控制器
  113. * </p>
  114. *
  115. * @author tzx
  116. * @since 2022-03-02
  117. */
  118. @RestController
  119. @RequestMapping(GlobalConstant.ORGANIZATION_MODULE_PREFIX + "/user")
  120. @Api(value = GlobalConstant.ORGANIZATION_MODULE_PREFIX + "/user", tags = "用户")
  121. @AllArgsConstructor
  122. public class UserController {
  123. private final IUserService userService;
  124. private final IDepartmentService departmentService;
  125. private final IPostService postService;
  126. private final IRoleService roleService;
  127. private final IUserRoleRelationService userRoleRelationService;
  128. private final IUserDeptRelationService userDeptRelationService;
  129. private final IUserPostRelationService userPostRelationService;
  130. private final RedisUtil redisUtil;
  131. private final IFileService fileService;
  132. private final SmsCtcc smsCtcc;
  133. private final IBaseGradeService baseGradeService;
  134. private final IUserStudentService userStudentService;
  135. private final IBaseClassService baseClassService;
  136. private final IWorkflowExecuteService workflowExecuteService;
  137. private final HistoryService historyService;
  138. private final IBaseStudentSchoolRollService baseStudentSchoolRollService;
  139. @GetMapping(value = "/list")
  140. @ApiOperation(value = "用户列表(不分页)")
  141. public R list(String keyword) {
  142. List<User> list = userService.list(Wrappers.lambdaQuery(User.class)
  143. .like(StrUtil.isNotBlank(keyword), User::getUserName, keyword)
  144. .like(StrUtil.isNotBlank(keyword), User::getCode, keyword)
  145. .like(StrUtil.isNotBlank(keyword), User::getName, keyword)
  146. .like(StrUtil.isNotBlank(keyword), User::getMobile, keyword)
  147. .select(User.class, x -> VoToColumnUtil.fieldsToColumns(UserListVo.class).contains(x.getProperty())));
  148. List<UserListVo> userListVos = BeanUtil.copyToList(list, UserListVo.class);
  149. return R.ok(userListVos);
  150. }
  151. @GetMapping(value = "/page")
  152. @ApiOperation(value = "用户列表(分页)")
  153. public R page(UserPageDto dto) {
  154. List<Long> deptIds = new ArrayList<>();
  155. if (ObjectUtil.isNotNull(dto.getDepartmentId())) {
  156. List<Department> list = redisUtil.get(GlobalConstant.DEP_CACHE_KEY, new TypeReference<List<Department>>() {
  157. });
  158. //当前部门的层级
  159. String hierarchy = list.stream().filter(x -> x.getId().equals(dto.getDepartmentId())).findFirst().orElse(new Department()).getHierarchy();
  160. if (StrUtil.isNotBlank(hierarchy)) {
  161. //层级里面包含当前部门层级的就是它的子集,如1-1,下面包含了1-1、1-1-2这种
  162. deptIds = list.stream().filter(x -> StrUtil.isNotBlank(x.getHierarchy()) && x.getHierarchy().contains(hierarchy)).map(Department::getId).collect(Collectors.toList());
  163. } else {
  164. //如果不存在层级就查询自己的数据
  165. deptIds.add(dto.getDepartmentId());
  166. }
  167. }
  168. //因为多表关联 会有多个表都使用了id字段, 所以必须专门指定主表的Id
  169. if (ObjectUtil.isNotNull(dto.getDepartmentId())) {//不为空联合查询
  170. MPJLambdaWrapper<User> queryUser = new MPJLambdaWrapper<>();
  171. queryUser
  172. .disableSubLogicDel()
  173. .distinct()
  174. .like(StrUtil.isNotBlank(dto.getKeyword()), User::getName, dto.getKeyword())
  175. .or(StrUtil.isNotBlank(dto.getKeyword()), x -> x.like(StrUtil.isNotBlank(dto.getKeyword()), User::getCode, dto.getKeyword()))
  176. .in(ObjectUtil.isNotNull(dto.getDepartmentId()), UserDeptRelation::getDeptId, deptIds)
  177. .like(StrUtil.isNotBlank(dto.getUserName()), User::getUserName, dto.getUserName())
  178. .like(StrUtil.isNotBlank(dto.getCode()), User::getCode, dto.getCode())
  179. .like(StrUtil.isNotBlank(dto.getName()), User::getName, dto.getName())
  180. .like(StrUtil.isNotBlank(dto.getMobile()), User::getMobile, dto.getMobile())
  181. .eq((ObjectUtil.isNotNull(dto.getTreeType()) && dto.getTreeType() == 1), Role::getId, dto.getTreeId())
  182. .eq((ObjectUtil.isNotNull(dto.getTreeType()) && dto.getTreeType() == 2), UserDeptRelation::getDeptId, dto.getTreeId())
  183. .eq((ObjectUtil.isNotNull(dto.getTreeType()) && dto.getTreeType() == 3), BaseStudentSchoolRoll::getGradeId, dto.getTreeId())
  184. .eq((ObjectUtil.isNotNull(dto.getTreeType()) && dto.getTreeType() == 4), BaseStudentSchoolRoll::getClassId, dto.getTreeId())
  185. .eq(ObjectUtil.isNotNull(dto.getUserType()), Role::getId, dto.getUserType())
  186. .eq(ObjectUtil.isNotNull(dto.getEmployType()), BaseTeacher::getEmployType, dto.getEmployType())
  187. .eq(ObjectUtil.isNotNull(dto.getEmployWay()), BaseTeacher::getEmployWay, dto.getEmployWay())
  188. .orderByDesc(User::getId)
  189. .select(User::getId)
  190. .select("d1.name", UserPageVo::getEmployWay)
  191. .select("d2.name", UserPageVo::getEmployType)
  192. .select(User.class, x -> VoToColumnUtil.fieldsToColumns(UserPageVo.class).contains(x.getProperty()))
  193. .leftJoin(UserDeptRelation.class, UserDeptRelation::getUserId, User::getId)
  194. .leftJoin(UserRoleRelation.class, UserRoleRelation::getUserId, User::getId)
  195. .leftJoin(BaseTeacher.class, BaseTeacher::getUserId, User::getId)
  196. .leftJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId, User::getId)
  197. .leftJoin(DictionaryDetail.class, "d1", DictionaryDetail::getCode, BaseTeacher::getEmployWay)
  198. .leftJoin(DictionaryDetail.class, "d2", DictionaryDetail::getCode, BaseTeacher::getEmployType)
  199. .leftJoin(Role.class, Role::getId, UserRoleRelation::getRoleId);
  200. IPage<UserPageVo> page = userService.selectJoinListPage(ConventPage.getPage(dto), UserPageVo.class, queryUser);
  201. for (UserPageVo record : page.getRecords()) {
  202. record.setGenderCn(GenderDictionaryEnum.getValue(record.getGender()));
  203. }
  204. PageOutput<UserPageVo> pageOutput = ConventPage.getPageOutput(page, UserPageVo.class);
  205. return R.ok(pageOutput);
  206. } else {
  207. // LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
  208. // queryWrapper.like(StrUtil.isNotBlank(dto.getKeyword()), User::getName, dto.getKeyword())
  209. // .or(StrUtil.isNotBlank(dto.getKeyword()), x -> x.like(StrUtil.isNotBlank(dto.getKeyword()), User::getCode, dto.getKeyword()))
  210. // .like(StrUtil.isNotBlank(dto.getUserName()), User::getUserName, dto.getUserName())
  211. // .like(StrUtil.isNotBlank(dto.getCode()), User::getCode, dto.getCode())
  212. // .like(StrUtil.isNotBlank(dto.getName()), User::getName, dto.getName())
  213. // .like(StrUtil.isNotBlank(dto.getMobile()), User::getMobile, dto.getMobile())
  214. // .orderByDesc(User::getCreateDate)
  215. // .select(User.class, x -> VoToColumnUtil.fieldsToColumns(UserPageVo.class).contains(x.getProperty()));
  216. //
  217. // IPage<User> page = userService.page(ConventPage.getPage(dto),queryWrapper);
  218. MPJLambdaWrapper<User> queryUser = new MPJLambdaWrapper<>();
  219. queryUser
  220. .disableSubLogicDel()
  221. .distinct()
  222. .like(StrUtil.isNotBlank(dto.getKeyword()), User::getName, dto.getKeyword())
  223. .or(StrUtil.isNotBlank(dto.getKeyword()), x -> x.like(StrUtil.isNotBlank(dto.getKeyword()), User::getCode, dto.getKeyword()))
  224. .like(StrUtil.isNotBlank(dto.getUserName()), User::getUserName, dto.getUserName())
  225. .like(StrUtil.isNotBlank(dto.getCode()), User::getCode, dto.getCode())
  226. .like(StrUtil.isNotBlank(dto.getName()), User::getName, dto.getName())
  227. .like(StrUtil.isNotBlank(dto.getMobile()), User::getMobile, dto.getMobile())
  228. .eq(ObjectUtil.isNotNull(dto.getUserType()), Role::getId, dto.getUserType())
  229. .eq((ObjectUtil.isNotNull(dto.getTreeType()) && dto.getTreeType() == 1), Role::getId, dto.getTreeId())
  230. .eq((ObjectUtil.isNotNull(dto.getTreeType()) && dto.getTreeType() == 2), UserDeptRelation::getDeptId, dto.getTreeId())
  231. .eq((ObjectUtil.isNotNull(dto.getTreeType()) && dto.getTreeType() == 3), BaseStudentSchoolRoll::getGradeId, dto.getTreeId())
  232. .eq((ObjectUtil.isNotNull(dto.getTreeType()) && dto.getTreeType() == 4), BaseStudentSchoolRoll::getClassId, dto.getTreeId())
  233. .eq(ObjectUtil.isNotNull(dto.getEmployType()), BaseTeacher::getEmployType, dto.getEmployType())
  234. .eq(ObjectUtil.isNotNull(dto.getEmployWay()), BaseTeacher::getEmployWay, dto.getEmployWay())
  235. .orderByDesc(User::getId)
  236. .select(User::getId)
  237. .select("d1.name", UserPageVo::getEmployWay)
  238. .select("d2.name", UserPageVo::getEmployType)
  239. .select(User.class, x -> VoToColumnUtil.fieldsToColumns(UserPageVo.class).contains(x.getProperty()))
  240. .leftJoin(UserRoleRelation.class, UserRoleRelation::getUserId, User::getId)
  241. .leftJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId, User::getId)
  242. .leftJoin(UserDeptRelation.class, UserDeptRelation::getUserId, User::getId)
  243. .leftJoin(BaseTeacher.class, BaseTeacher::getUserId, User::getId)
  244. .leftJoin(DictionaryDetail.class, "d1", DictionaryDetail::getCode, BaseTeacher::getEmployWay)
  245. .leftJoin(DictionaryDetail.class, "d2", DictionaryDetail::getCode, BaseTeacher::getEmployType)
  246. .leftJoin(Role.class, Role::getId, UserRoleRelation::getRoleId);
  247. IPage<UserPageVo> page = userService.selectJoinListPage(ConventPage.getPage(dto), UserPageVo.class,queryUser);
  248. for (UserPageVo record : page.getRecords()) {
  249. record.setGenderCn(GenderDictionaryEnum.getValue(record.getGender()));
  250. }
  251. PageOutput<UserPageVo> pageOutput = ConventPage.getPageOutput(page, UserPageVo.class);
  252. return R.ok(pageOutput);
  253. }
  254. }
  255. @GetMapping(value = "/info")
  256. @ApiOperation(value = "根据id查询用户信息")
  257. public R info(@RequestParam Long id) {
  258. User user = userService.getById(id);
  259. if (user == null) {
  260. R.error("找不到此用户!");
  261. }
  262. UserVo userVo = BeanUtil.toBean(user, UserVo.class);
  263. userVo.setGenderCn(GenderDictionaryEnum.getValue(userVo.getGender()));
  264. List<Long> deptIds = userDeptRelationService.list(Wrappers.lambdaQuery(UserDeptRelation.class)
  265. .eq(UserDeptRelation::getUserId, user.getId()))
  266. .stream().map(UserDeptRelation::getDeptId).collect(Collectors.toList());
  267. String allDeptIdStr = StrUtil.join(StringPool.COMMA, deptIds);
  268. userVo.setDepartmentIds(allDeptIdStr);
  269. List<Long> roleIds = userRoleRelationService.list(
  270. Wrappers.lambdaQuery(UserRoleRelation.class)
  271. .eq(UserRoleRelation::getUserId, user.getId())
  272. ).stream().map(UserRoleRelation::getRoleId).collect(Collectors.toList());
  273. userVo.setRoleIds(roleIds);
  274. return R.ok(userVo);
  275. }
  276. @GetMapping(value = "/detail")
  277. @ApiOperation(value = "根据id查询用户信息")
  278. public RT<UserDetailVo> detail(@RequestParam Long id) {
  279. User user = userService.getById(id);
  280. if (user == null) {
  281. R.error("找不到此用户!");
  282. }
  283. UserDetailVo userVo = BeanUtil.toBean(user, UserDetailVo.class);
  284. List<Long> deptIds = userDeptRelationService.list(Wrappers.lambdaQuery(UserDeptRelation.class)
  285. .eq(UserDeptRelation::getUserId, user.getId()))
  286. .stream().map(UserDeptRelation::getDeptId).collect(Collectors.toList());
  287. String allDeptIdStr = StrUtil.join(StringPool.COMMA, deptIds);
  288. userVo.setDepartmentIds(allDeptIdStr);
  289. return RT.ok(userVo);
  290. }
  291. @PostMapping
  292. @ApiOperation(value = "新增用户")
  293. public R add(@Valid @RequestBody AddUserDto dto) {
  294. if (!OrganizationUtil.validatePassword(dto.getPassword())) {
  295. return R.error("密码必须包含大写字母、小写字母、数字和特殊字符,长度8~16位");
  296. }
  297. return R.ok(userService.add(dto));
  298. }
  299. @PutMapping
  300. @ApiOperation(value = "修改用户 不能修改用户名")
  301. public R update(@Valid @RequestBody UpdateUserDto dto) {
  302. return R.ok(userService.update(dto));
  303. }
  304. @GetMapping(value = "/current/info")
  305. @ApiOperation(value = "当前登录用户信息")
  306. public R info() {
  307. SaSession tokenSession = StpUtil.getTokenSession();
  308. User user = tokenSession.get(GlobalConstant.LOGIN_USER_INFO_KEY, new User());
  309. List<Long> roleIds = userRoleRelationService.list(Wrappers.lambdaQuery(UserRoleRelation.class)
  310. .eq(UserRoleRelation::getUserId, user.getId()))
  311. .stream().map(UserRoleRelation::getRoleId).collect(Collectors.toList());
  312. List<Long> deptIds = userDeptRelationService.list(Wrappers.lambdaQuery(UserDeptRelation.class)
  313. .eq(UserDeptRelation::getUserId, user.getId()))
  314. .stream().map(UserDeptRelation::getDeptId).collect(Collectors.toList());
  315. List<Long> postIds = userPostRelationService.list(Wrappers.lambdaQuery(UserPostRelation.class)
  316. .eq(UserPostRelation::getUserId, user.getId()))
  317. .stream().map(UserPostRelation::getPostId).collect(Collectors.toList());
  318. UserInfoVo vo = BeanUtil.toBean(user, UserInfoVo.class);
  319. vo.setGenderCn(GenderDictionaryEnum.getValue(vo.getGender()));
  320. if (roleIds.size() > 0) {
  321. List<Role> list = roleService.list(Wrappers.lambdaQuery(Role.class).in(Role::getId, roleIds));
  322. List<UserRoleVo> userRoleVoList = BeanUtil.copyToList(list, UserRoleVo.class);
  323. vo.setRoles(userRoleVoList);
  324. }
  325. if (deptIds.size() > 0) {
  326. List<Department> list = departmentService.list(Wrappers.lambdaQuery(Department.class).in(Department::getId, deptIds));
  327. List<UserDeptVo> userDeptVoList = BeanUtil.copyToList(list, UserDeptVo.class);
  328. vo.setDepartments(userDeptVoList);
  329. }
  330. if (postIds.size() > 0) {
  331. List<Post> list = postService.list(Wrappers.lambdaQuery(Post.class).in(Post::getId, postIds));
  332. List<UserPostVo> userPostVoList = BeanUtil.copyToList(list, UserPostVo.class);
  333. vo.setPosts(userPostVoList);
  334. }
  335. // 家长
  336. List<UserStudentVo> list = baseClassService.getStudents(user.getId());
  337. if (list != null) {
  338. vo.setStudents(list);
  339. }
  340. // 学生
  341. UserStudentVo userStudentVo = baseClassService.getClassInfo(user.getId());
  342. if (userStudentVo != null) {
  343. vo.setClassId(userStudentVo.getClassId());
  344. vo.setClassName(userStudentVo.getClassName());
  345. }
  346. List<File> fileList = fileService.list(Wrappers.lambdaQuery(File.class).eq(File::getFolderId, user.getSignFolderId()));
  347. if(!fileList.isEmpty()){
  348. vo.setSignUrl(fileList.get(0).getFileUrl());
  349. }
  350. return R.ok(vo);
  351. }
  352. @GetMapping(value = "/pending/count")
  353. @ApiOperation(value = "当前待办数量统计")
  354. public RT<PendingCountDto> pendingCount() {
  355. PendingCountDto pendingCountDto = new PendingCountDto();
  356. pendingCountDto.setWfPendingCount(workflowExecuteService.pendingCount());
  357. HistoricProcessInstanceQuery historicProcessInstanceQuery = historyService.createHistoricProcessInstanceQuery()
  358. .variableValueEquals(WorkflowConstant.PROCESS_START_USER_ID_KEY, StpUtil.getLoginIdAsLong())
  359. .variableValueEquals(WorkflowConstant.PROCESS_ISRECYCLE_FLAG_KEY, WorkflowIsRecycleType.NO.getCode());
  360. List<HistoricProcessInstance> historicProcessInstances = historicProcessInstanceQuery.orderByProcessInstanceStartTime().desc().list();
  361. //获取到所有流程id
  362. List<String> processIds = historicProcessInstances.stream().map(HistoricProcessInstance::getId).collect(Collectors.toList());
  363. int myApplicationCount = 0;
  364. for (HistoricProcessInstance historicProcessInstance : historicProcessInstances) {
  365. Long countByProcessId = workflowExecuteService.getCountByProcessId(historicProcessInstance.getId());
  366. if(countByProcessId == 0L){
  367. continue;
  368. }
  369. if (!ObjectUtil.isNotNull(historicProcessInstance.getEndTime())) {
  370. myApplicationCount ++;
  371. }
  372. }
  373. pendingCountDto.setMyApplicationCount(myApplicationCount);
  374. return RT.ok(pendingCountDto);
  375. }
  376. @GetMapping(value = "/reset-user-page")
  377. @ApiOperation(value = "移动端用户列表")
  378. public RT<PageOutput<ResetUserPageVo>> resetUserPage(UserPageDto dto) {
  379. PageOutput<ResetUserPageVo> userPage = userService.getResetUserPage(dto);
  380. return RT.ok(userPage);
  381. }
  382. @GetMapping(value = "/reset-page-role-list")
  383. @ApiOperation(value = "移动端用户列表分类")
  384. public RT<List<ResetUserRoleVo>> resetUserTree() {
  385. List<ResetUserRoleVo> roleList = new ArrayList<>();
  386. roleList.add(new ResetUserRoleVo(){{
  387. setId(null);
  388. setName("全部");
  389. }});
  390. roleList.add(new ResetUserRoleVo(){{
  391. setId(2L);
  392. setName("教师");
  393. }});
  394. roleList.add(new ResetUserRoleVo(){{
  395. setId(3L);
  396. setName("学生");
  397. }});
  398. roleList.add(new ResetUserRoleVo(){{
  399. setId(4L);
  400. setName("家长");
  401. }});
  402. return RT.ok(roleList);
  403. }
  404. @GetMapping(value = "/pc-role-tree")
  405. @ApiOperation(value = "pc端用户列表分类")
  406. public RT<List<ResetUserRoleVo>> pcRoleTree() {
  407. List<Department> departmentList = departmentService.list(
  408. new QueryWrapper<Department>().lambda()
  409. .eq(Department::getDeleteMark, DeleteMark.NODELETE.getCode())
  410. );
  411. List<ResetUserRoleVo> voList = new ArrayList<>();
  412. for (Department department : departmentList) {
  413. ResetUserRoleVo roleVo = BeanUtil.toBean(department, ResetUserRoleVo.class);
  414. roleVo.setType(2);
  415. voList.add(roleVo);
  416. }
  417. List<ResetUserRoleVo> treeVoList = TreeUtil.build(voList);
  418. List<ResetUserRoleVo> roleList = new ArrayList<>();
  419. ResetUserRoleVo roleVo = new ResetUserRoleVo() {{
  420. setId(2L);
  421. setType(1);
  422. setName("教师");
  423. setChildren(treeVoList);
  424. }};
  425. roleList.add(roleVo);
  426. List<ResetUserRoleVo> gradeClassList = new ArrayList<>();
  427. baseClassService.list(
  428. new QueryWrapper<BaseClass>().lambda().eq(BaseClass::getDeleteMark, DeleteMark.NODELETE.getCode())
  429. ).forEach(e->{
  430. ResetUserRoleVo gradeClass = new ResetUserRoleVo();
  431. gradeClass.setType(4);
  432. gradeClass.setName(e.getName());
  433. gradeClass.setId(e.getId());
  434. gradeClass.setParentId(e.getGradeId());
  435. gradeClassList.add(gradeClass);
  436. });
  437. baseGradeService.list(
  438. new QueryWrapper<BaseGrade>().lambda().eq(BaseGrade::getDeleteMark, DeleteMark.NODELETE.getCode())
  439. ).forEach(e->{
  440. ResetUserRoleVo gradeClass = new ResetUserRoleVo();
  441. gradeClass.setType(3);
  442. gradeClass.setName(e.getName());
  443. gradeClass.setId(e.getId());
  444. gradeClass.setParentId(3L);
  445. gradeClassList.add(gradeClass);
  446. });
  447. List<ResetUserRoleVo> gradeClassTreeVoList = TreeUtil.build(gradeClassList);
  448. roleList.add(new ResetUserRoleVo(){{
  449. setId(3L);
  450. setType(1);
  451. setName("学生");
  452. setChildren(gradeClassTreeVoList);
  453. }});
  454. roleList.add(new ResetUserRoleVo(){{
  455. setId(4L);
  456. setType(1);
  457. setName("家长");
  458. }});
  459. return RT.ok(roleList);
  460. }
  461. @PutMapping("/update/info")
  462. @ApiOperation(value = "登陆人修改自己得用户信息")
  463. public R updateInfo(@RequestBody @Valid UpdateInfoDto dto) {
  464. User updateUserInfo = BeanUtil.toBean(dto, User.class);
  465. updateUserInfo.setId(StpUtil.getLoginIdAsLong());
  466. CompletableFuture.runAsync(() -> {
  467. List<User> list = userService.list();
  468. redisUtil.set(GlobalConstant.USER_CACHE_KEY, list);
  469. });
  470. return R.ok(userService.updateById(updateUserInfo));
  471. }
  472. @PutMapping("/update/password")
  473. @ApiOperation(value = "当前登录用户修改本人密码")
  474. public RT<Boolean> updatePassword(@RequestBody @Valid UpdatePasswordDto dto) {
  475. if (!OrganizationUtil.validatePassword(dto.getNewPassword())) {
  476. return RT.error("密码必须包含大写字母、小写字母、数字和特殊字符,长度8~16位");
  477. }
  478. User user = userService.getById(StpUtil.getLoginIdAsLong());
  479. if (!BCrypt.checkpw(dto.getOldPassword(), user.getPassword())) {
  480. return RT.error("当前密码填写错误!");
  481. }
  482. if (!StrUtil.equals(dto.getNewPassword(), dto.getConfirmPassword())) {
  483. return RT.error("2次密码输入不一致!");
  484. }
  485. return RT.ok(userService.updatePassword(dto));
  486. }
  487. @PutMapping("/login/reset-password")
  488. @ApiOperation(value = "登录后修改密码")
  489. public RT<Boolean> loginResetPassword(@RequestBody @Valid LoginResetPasswordDto dto) {
  490. if (!OrganizationUtil.validatePassword(dto.getNewPassword())) {
  491. return RT.error("密码必须包含大写字母、小写字母、数字和特殊字符,长度8~16位");
  492. }
  493. if (!StrUtil.equals(dto.getNewPassword(), dto.getConfirmPassword())) {
  494. return RT.error("2次密码输入不一致!");
  495. }
  496. UpdatePasswordDto pd = BeanUtil.toBean(dto, UpdatePasswordDto.class);
  497. return RT.ok(userService.updatePassword(pd));
  498. }
  499. @PostMapping("/update/avatar")
  500. @ApiOperation(value = "当前登录用户修改头像")
  501. public R uploadAvatar(@RequestParam("file") MultipartFile file) throws Exception {
  502. if (file.isEmpty()) {
  503. throw new MyException("上传文件不能为空");
  504. }
  505. //上传文件
  506. String suffix = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(StringPool.DOT));
  507. String url = Objects.requireNonNull(OssFactory.build()).uploadSuffix(file.getBytes(), suffix);
  508. User updateUser = new User();
  509. updateUser.setId(StpUtil.getLoginIdAsLong());
  510. updateUser.setAvatar(url);
  511. userService.updateById(updateUser);
  512. SaSession tokenSession = StpUtil.getTokenSession();
  513. User user = tokenSession.get(GlobalConstant.LOGIN_USER_INFO_KEY, new User());
  514. user.setAvatar(url);
  515. tokenSession.set(GlobalConstant.LOGIN_USER_INFO_KEY, user);
  516. return R.ok(url);
  517. }
  518. @DeleteMapping
  519. @ApiOperation(value = "删除用户(可批量)")
  520. public R delete(@RequestBody List<Long> ids) {
  521. if (ids.contains(GlobalConstant.SUPER_ADMIN_USER_ID)) {
  522. R.error("管理员账户不能删除!");
  523. }
  524. if (ids.contains(StpUtil.getLoginIdAsLong())) {
  525. R.error("当前登录账户不能删除!");
  526. }
  527. //删除时需要同时删除用户部门关联表和用户角色关联表和用户岗位关系表数据。
  528. return R.ok(userService.deleteBatch(ids));
  529. }
  530. @GetMapping("/info/multi")
  531. @ApiOperation(value = "批量获取用户信息")
  532. public R usersInfo(@RequestParam String ids) {
  533. return R.ok(userService.getUsersInfo(ids));
  534. }
  535. @GetMapping("/enabled")
  536. @ApiOperation(value = "启用/禁用用户")
  537. public R enabled(@RequestParam Long id) {
  538. User user = userService.getOne(Wrappers.<User>query().lambda().select(User::getEnabledMark).eq(User::getId, id), false);
  539. if (user != null) {
  540. User updateUser = new User();
  541. updateUser.setId(id);
  542. updateUser.setEnabledMark(user.getEnabledMark() == EnabledMark.ENABLED.getCode() ? EnabledMark.DISABLED.getCode() : EnabledMark.ENABLED.getCode());
  543. return R.ok(userService.updateById(updateUser));
  544. }
  545. CompletableFuture.runAsync(() -> {
  546. List<User> list = userService.list();
  547. redisUtil.set(GlobalConstant.USER_CACHE_KEY, list);
  548. });
  549. return R.error("该用户不存在!");
  550. }
  551. @PutMapping("/reset-password")
  552. @ApiOperation(value = "重置密码")
  553. public RT<Boolean> resetPassword(@RequestBody ResetPasswordDto dto) {
  554. return RT.ok(userService.resetPassword(dto));
  555. }
  556. @PutMapping("/batch-reset-password")
  557. @ApiOperation(value = "批量重置密码")
  558. public RT<Boolean> batchResetPassword(@RequestBody List<Long> ids) {
  559. return RT.ok(userService.batchResetPassword(ids));
  560. }
  561. @PostMapping("/bind-unionid")
  562. @ApiOperation(value = "绑定微信 UnionId")
  563. public R bindUnionId(@RequestBody BindOpenidDto dto) {
  564. User user = userService.getOne(Wrappers.<User>query().lambda().select(User::getId, User::getOpenId, User::getUnionId).eq(User::getId, dto.getId()), false);
  565. if (user != null) {
  566. if (StrUtil.isNotBlank(user.getUnionId())) {
  567. return R.error("该用户已经绑定微信!");
  568. }
  569. long count = userService.count(Wrappers.<User>query().lambda().eq(User::getUnionId, dto.getOpenid()));
  570. if (count > 0) {
  571. return R.error("该用户已经绑定微信!");
  572. }
  573. User updateUser = new User();
  574. updateUser.setId(dto.getId());
  575. // updateUser.setOpenId(dto.getOpenid());
  576. updateUser.setUnionId(dto.getOpenid());
  577. return R.ok(userService.updateById(updateUser));
  578. } else {
  579. long count = userService.count(Wrappers.<User>query().lambda().eq(User::getOpenId, dto.getOpenid()));
  580. if (count > 0) {
  581. return R.error("该用户已经绑定微信!");
  582. }
  583. }
  584. return R.error("该用户不存在!");
  585. }
  586. @PostMapping("/bind-openid")
  587. @ApiOperation(value = "绑定微信 Openid")
  588. public R bindOpenid(@RequestBody BindOpenidDto dto) {
  589. return R.ok(userService.bindOpenid(dto));
  590. }
  591. @GetMapping("/unbind-openid")
  592. @ApiOperation(value = "取消绑定微信 UnionId")
  593. public R unbindOpenid(@RequestParam Long id) {
  594. return R.ok(userService.unbindOpenid(id));
  595. }
  596. @PostMapping("/register")
  597. @ApiOperation(value = "家长注册")
  598. public R register(@Valid @RequestBody RegisterDto dto) {
  599. // 验证验证码
  600. if (!smsCtcc.captchaVerify(dto.getMobile(), dto.getSmsCode())) {
  601. return R.error("验证码不正确!");
  602. }
  603. // 赋值家长角色
  604. List<Long> roleIds = new ArrayList<>();
  605. roleIds.add(RoleEnum.PARENT.getCode());
  606. dto.setRoleIds(roleIds);
  607. return R.ok(userService.add(dto));
  608. }
  609. @PostMapping("/bind-student")
  610. @ApiOperation(value = "绑定学生")
  611. public R bindStudent(@Valid @RequestBody UserStudentBindDto dto) {
  612. User user = userService.getOne(Wrappers.<User>query().lambda()
  613. .eq(User::getName, dto.getName())
  614. .eq(User::getCredentialNumber, dto.getIdCard()));
  615. UserStudentAddDto userStudentAddDto = new UserStudentAddDto();
  616. if (user == null) {
  617. return R.error("学生不存在!");
  618. }
  619. userStudentAddDto.setUserId(dto.getUserId());
  620. userStudentAddDto.setStudentId(user.getId());
  621. return R.ok(userStudentService.add(userStudentAddDto));
  622. }
  623. @GetMapping(value = "/validate-student")
  624. public RT<UserStudentInfoVo> getValidateStudent(UserStudentBindDto dto) {
  625. UserStudentInfoVo userStudentInfoVo = baseStudentSchoolRollService.selectJoinOne(UserStudentInfoVo.class,
  626. MPJWrappers.<BaseStudentSchoolRoll>lambdaJoin()
  627. .innerJoin(User.class, User::getId, BaseStudentSchoolRoll::getUserId)
  628. .leftJoin(BaseClass.class,BaseClass::getId,BaseStudentSchoolRoll::getClassId)
  629. .eq(User::getName, dto.getName())
  630. .eq(User::getCredentialNumber, dto.getIdCard())
  631. .select(User::getId, User::getName, User::getMobile, User::getCredentialNumber)
  632. .select(BaseClass::getTeacherId)
  633. );
  634. if (userStudentInfoVo == null) {
  635. return RT.error("学生不存在!");
  636. }
  637. return RT.ok(userStudentInfoVo);
  638. }
  639. @DeleteMapping("/unbind-student")
  640. @ApiOperation(value = "解绑学生")
  641. public R unBindStudent(@Valid @RequestBody UserStudentDeleteDto dto) {
  642. return R.ok(userStudentService.delete(dto));
  643. }
  644. @GetMapping("/student")
  645. @ApiOperation(value = "获取绑定学生")
  646. public R unBindStudent(@Valid @RequestParam Long id) {
  647. return R.ok(baseClassService.getStudents(id));
  648. }
  649. @GetMapping("/user-student")
  650. @ApiOperation(value = "获取绑定学生")
  651. public RT<List<UserStudentVo>> getBindStudent(@Valid @RequestParam Long id) {
  652. List<UserStudentVo> userStudentVoList = userStudentService.selectJoinList(UserStudentVo.class,
  653. MPJWrappers.<UserStudent>lambdaJoin()
  654. .innerJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId, UserStudent::getStudentId)
  655. .innerJoin(BaseStudentUser.class, BaseStudentUser::getId, UserStudent::getStudentId)
  656. .leftJoin(BaseClass.class, BaseClass::getId, BaseStudentSchoolRoll::getClassId)
  657. .select(BaseStudentSchoolRoll::getClassId)
  658. .select(UserStudent::getStudentId,UserStudent::getStatus)
  659. .selectAs(BaseClass::getName, UserStudentVo::getClassName)
  660. .selectAs(BaseStudentUser::getName, UserStudentVo::getStudentName)
  661. .selectAs(BaseStudentUser::getAvatar, UserStudentVo::getAvatar)
  662. .eq(UserStudent::getUserId, id)
  663. );
  664. return RT.ok(userStudentVoList);
  665. }
  666. @PostMapping("/upload-sign")
  667. @ApiOperation(value = "上传签名")
  668. public RT<Boolean> uploadSign(@Valid @RequestBody UploadSignDto dto) {
  669. long loginIdAsLong = StpUtil.getLoginIdAsLong();
  670. User user = userService.getById(loginIdAsLong);
  671. user.setSignFolderId(dto.getFolderId());
  672. user.setSignPassword(BCrypt.hashpw(dto.getPassword(), BCrypt.gensalt()));
  673. return RT.ok(userService.updateById(user));
  674. }
  675. @GetMapping("/getSign")
  676. @ApiOperation(value = "获取登录者的签名url")
  677. public RT<String> getSign() {
  678. long loginIdAsLong = StpUtil.getLoginIdAsLong();
  679. User user = userService.getById(loginIdAsLong);
  680. if(user.getSignFolderId() == null){
  681. return RT.error("未上传签名");
  682. }
  683. List<File> list = fileService.list(Wrappers.lambdaQuery(File.class).eq(File::getFolderId, user.getSignFolderId()));
  684. if(list.isEmpty()){
  685. return RT.error("签名文件丢失");
  686. }
  687. return RT.ok(list.get(0).getFileUrl());
  688. }
  689. @GetMapping("/check-password")
  690. @ApiOperation(value = "验证登录者的密码")
  691. public RT<Boolean> checkSignpassword(@RequestParam String password) {
  692. long loginIdAsLong = StpUtil.getLoginIdAsLong();
  693. User user = userService.getById(loginIdAsLong);
  694. if (!BCrypt.checkpw(password, user.getPassword())) {
  695. return RT.error("密码填写错误!");
  696. }
  697. return RT.ok(true);
  698. }
  699. }