SaasAccountController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package com.persagy.person.controller;
  2. import java.util.List;
  3. import com.persagy.person.pojo.vo.account.*;
  4. import com.persagy.security.util.BouncycastleCipher;
  5. import org.springframework.beans.BeanUtils;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.validation.annotation.Validated;
  8. import org.springframework.web.bind.annotation.RequestBody;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. import org.springframework.web.bind.annotation.RestController;
  12. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  13. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  14. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  15. import com.google.common.collect.Lists;
  16. import com.persagy.common.constant.SaasCommonConstant;
  17. import com.persagy.common.enums.ResponseCode;
  18. import com.persagy.common.model.BaseGroupModel;
  19. import com.persagy.common.utils.ResponseResult;
  20. import com.persagy.common.utils.ResponseResultUtil;
  21. import com.persagy.common.utils.StringUtil;
  22. import com.persagy.person.config.ApplicationProperties;
  23. import com.persagy.person.manage.SaasAccountHandler;
  24. import com.persagy.person.pojo.dto.SaasAccount;
  25. import com.persagy.person.pojo.dto.SaasRole;
  26. import com.persagy.person.service.ISaasAccountService;
  27. import com.persagy.person.service.ISaasRoleService;
  28. import cn.hutool.core.collection.CollectionUtil;
  29. import cn.hutool.core.util.BooleanUtil;
  30. import io.swagger.annotations.Api;
  31. import io.swagger.annotations.ApiOperation;
  32. /**
  33. * 账号信息
  34. *
  35. * @version 1.0.0
  36. * @company persagy
  37. * @author zhangqiankun
  38. * @date 2021-03-13 15:29:50
  39. */
  40. @Api(tags = "账号信息")
  41. @RestController
  42. @RequestMapping(value = "/account", method = RequestMethod.POST)
  43. public class SaasAccountController {
  44. @Autowired
  45. private ApplicationProperties properties;
  46. @Autowired
  47. private ISaasRoleService saasRoleService;
  48. @Autowired
  49. private BouncycastleCipher bouncycastleCipher;
  50. @Autowired
  51. private SaasAccountHandler saasAccountHandler;
  52. @Autowired
  53. private ISaasAccountService saasAccountService;
  54. /**
  55. * 账号信息查询
  56. */
  57. @ApiOperation(value = "账号信息查询")
  58. @RequestMapping(value = "querySaasAccountList")
  59. public ResponseResult querySaasAccountList(@RequestBody @Validated SaasAccountQueryVO queryVO) {
  60. SaasAccount saasAccount = new SaasAccount();
  61. BeanUtils.copyProperties(queryVO, saasAccount);
  62. saasAccount.setId(queryVO.getAccountId());
  63. LambdaQueryWrapper<SaasAccount> queryWrapper = this.saasAccountService.getQueryWrapper(saasAccount);
  64. if (BooleanUtil.isTrue(queryVO.getGroupIsNull())) {
  65. queryWrapper.apply("(GROUP_CODE IS NULL OR TRIM(GROUP_CODE) = '')");
  66. }
  67. queryWrapper.orderByDesc(SaasAccount::getUpdateTime); //更新时间降序
  68. List<SaasAccount> list = this.saasAccountService.list(queryWrapper);
  69. if (CollectionUtil.isEmpty(list)) {
  70. return ResponseResultUtil.successResult(Lists.newArrayList(), 0L);
  71. }
  72. return ResponseResultUtil.successResult(list, (long)list.size());
  73. }
  74. /**
  75. * 账号分页信息查询-普通列表
  76. */
  77. @ApiOperation(value = "账号分页信息查询")
  78. @RequestMapping(value = "queryAccountPageList")
  79. public ResponseResult queryAccountPageList(@RequestBody @Validated SaasAccountPageVO pageVO) {
  80. SaasAccount saasAccount = new SaasAccount();
  81. BeanUtils.copyProperties(pageVO, saasAccount);
  82. saasAccount.setId(pageVO.getAccountId());
  83. Page<SaasAccount> pageList = null;
  84. if (BooleanUtil.isTrue(pageVO.getShowGroup())) {
  85. pageList = this.saasAccountService.queryAccountPageList(saasAccount, pageVO.getPage(), pageVO.getSize());
  86. } else {
  87. Page<SaasAccount> page = new Page<SaasAccount>(pageVO.getPage(), pageVO.getSize());
  88. LambdaQueryWrapper<SaasAccount> queryWrapper = this.saasAccountService.getQueryWrapper(saasAccount);
  89. queryWrapper.orderByDesc(SaasAccount::getUpdateTime); //更新时间降序
  90. pageList = this.saasAccountService.page(page, queryWrapper);
  91. }
  92. if (pageList == null || CollectionUtil.isEmpty(pageList.getRecords())) {
  93. return ResponseResultUtil.successResult(Lists.newArrayList(), 0L);
  94. }
  95. // 额外信息补充
  96. List<SaasAccount> records = pageList.getRecords();
  97. if (BooleanUtil.isTrue(pageVO.getShowRoles())) {
  98. // 获取角色集合
  99. for (SaasAccount account : records) {
  100. List<SaasRole> roleList = this.saasRoleService.querySaasRoleList(account.getId());
  101. account.setRoles(roleList);
  102. }
  103. }
  104. return ResponseResultUtil.successResult(records, pageList.getTotal());
  105. }
  106. /**
  107. * 新增,登录用户名,全局唯一
  108. */
  109. @ApiOperation(value = "保存")
  110. @RequestMapping(value = "createSaasAccount")
  111. public ResponseResult createSaasAccount(@RequestBody @Validated SaasAccountCreateVO createVO) {
  112. if (createVO.getValid() == null) {
  113. createVO.setValid(SaasCommonConstant.STATUS_1);
  114. }
  115. if (SaasCommonConstant.STR_STATUS_0.equals(createVO.getValidLast())) {
  116. if (!(createVO.getValidStartTime() != null && createVO.getValidEndTime() != null)) {
  117. return ResponseResultUtil.errorResult(ResponseCode.C0341.getCode(), "当validLast为0时,账号有效期时间字段不可为空");
  118. }
  119. }
  120. if (createVO.isVerify() && SaasCommonConstant.STR_STATUS_1.equals(createVO.getAccountBelong()) && StringUtil.isBlank(createVO.getGroupCode())) {
  121. return ResponseResultUtil.errorResult(ResponseCode.C0341.getCode(), "业务系统账号,请先关联集团");
  122. }
  123. boolean exists = this.saasAccountService.validAccountName(createVO.getUsername(), createVO.getMail(), createVO.getPhoneNum(), null, false);
  124. if (!exists) {
  125. return ResponseResultUtil.errorResult(ResponseCode.C0341.getCode(), "账号用户名、邮箱、手机号,存在重复");
  126. }
  127. SaasAccount saasAccount = new SaasAccount();
  128. BeanUtils.copyProperties(createVO, saasAccount);
  129. saasAccount.setUpdateUser(createVO.getAccountId());
  130. saasAccount.setId(null);
  131. if (StringUtil.isBlank(createVO.getPassword())) {
  132. saasAccount.setPassword(this.properties.getDefaultPwd());
  133. }
  134. exists = this.saasAccountHandler.createSaasAccount(saasAccount, createVO.getCasType(), createVO.getRoles());
  135. return exists ? ResponseResultUtil.successResult("保存成功", saasAccount.getId()) : ResponseResultUtil.errorResult("保存失败");
  136. }
  137. /**
  138. * 更新
  139. */
  140. @ApiOperation(value = "更新")
  141. @RequestMapping(value = "updateSaasAccount")
  142. public ResponseResult updateSaasAccount(@RequestBody @Validated SaasAccountCreateVO createVO) {
  143. createVO.setPassword(null); //目前不允许更新时,更新密码
  144. if (StringUtil.isBlank(createVO.getAccountId())) {
  145. return ResponseResultUtil.errorResult(ResponseCode.A0400.getCode(), "主键ID,不可为空");
  146. }
  147. if (createVO.getValid() == null) {
  148. return ResponseResultUtil.errorResult(ResponseCode.A0400.getCode(), "账号状态不可为空");
  149. }
  150. if (SaasCommonConstant.STR_STATUS_0.equals(createVO.getValidLast())) {
  151. if (!(createVO.getValidStartTime() != null && createVO.getValidEndTime() != null)) {
  152. return ResponseResultUtil.errorResult(ResponseCode.C0341.getCode(), "当validLast为0时,账号有效期时间字段不可为空");
  153. }
  154. }
  155. if (createVO.isVerify() && SaasCommonConstant.STR_STATUS_1.equals(createVO.getAccountBelong()) && StringUtil.isBlank(createVO.getGroupCode())) {
  156. return ResponseResultUtil.errorResult(ResponseCode.C0341.getCode(), "业务系统账号,集团编码不可为空");
  157. }
  158. boolean exists = this.saasAccountService.validAccountName(createVO.getUsername(), createVO.getMail(), createVO.getPhoneNum(), createVO.getAccountId(), true);
  159. if (!exists) {
  160. return ResponseResultUtil.errorResult(ResponseCode.C0341.getCode(), "账号用户名、邮箱、手机号,存在重复");
  161. }
  162. SaasAccount saasAccount = new SaasAccount();
  163. BeanUtils.copyProperties(createVO, saasAccount);
  164. saasAccount.setId(createVO.getAccountId());
  165. saasAccount.setUpdateUser(createVO.getAccountId());
  166. exists = this.saasAccountHandler.updateSaasAccount(saasAccount, createVO.getCasType(), createVO.getRoles());
  167. return exists ? ResponseResultUtil.successResult("更新成功") : ResponseResultUtil.errorResult("更新失败");
  168. }
  169. /**
  170. * 业务超管账号关联集团
  171. */
  172. @ApiOperation(value = "集团关联业务超管账号")
  173. @RequestMapping(value = "addSaasAccountGroup")
  174. public ResponseResult addSaasAccountGroup(@RequestBody @Validated BaseGroupModel model) {
  175. boolean result = this.saasAccountHandler.addSaasAccountGroup(model);
  176. return result ? ResponseResultUtil.successResult("关联成功") : ResponseResultUtil.errorResult("关联失败");
  177. }
  178. /**
  179. * 逻辑删除
  180. */
  181. @ApiOperation(value = "禁用")
  182. @RequestMapping(value = "deleteSaasAccount")
  183. public ResponseResult deleteSaasAccount(@RequestBody @Validated BaseGroupModel deleteModel) {
  184. // 验证数据是否存在
  185. SaasAccount account = this.saasAccountService.getOne(deleteModel.getAccountId(), deleteModel.getGroupCode(), null);
  186. if (account == null) {
  187. return ResponseResultUtil.errorResult(ResponseCode.C0320.getCode(), ResponseCode.C0320.getDesc());
  188. }
  189. // 逻辑删除
  190. boolean result = this.saasAccountHandler.deleteSaasAccount(deleteModel);
  191. if (result) {
  192. // 移除token
  193. this.bouncycastleCipher.remove(deleteModel.getAccountId());
  194. }
  195. return result ? ResponseResultUtil.successResult("禁用成功") : ResponseResultUtil.errorResult("禁用失败");
  196. }
  197. /**
  198. * 启用
  199. */
  200. @ApiOperation(value = "启用")
  201. @RequestMapping(value = "enableSaasAccount")
  202. public ResponseResult enableSaasAccount(@RequestBody @Validated BaseGroupModel model) {
  203. LambdaUpdateWrapper<SaasAccount> updateWrapper = new SaasAccount.Builder().createUpdateWrapper()
  204. .idEq(model.getAccountId()).groupCodeEq(model.getGroupCode())
  205. .validEq(SaasCommonConstant.STATUS_0).builderUpdateWrapper();
  206. updateWrapper.set(SaasAccount::getUpdateUser, model.getAccountId());
  207. updateWrapper.set(SaasAccount::getValid, SaasCommonConstant.STATUS_1);
  208. boolean result = this.saasAccountService.update(updateWrapper);
  209. return result ? ResponseResultUtil.successResult("启用成功") : ResponseResultUtil.errorResult("启用失败");
  210. }
  211. /**
  212. * 修改密码
  213. */
  214. @ApiOperation(value = "修改密码")
  215. @RequestMapping(value = "updateAccountPwd")
  216. public ResponseResult updateAccountPwd(@RequestBody @Validated SaasAccountGroupVO model) {
  217. if (StringUtil.isBlank(model.getOldPassword())) {
  218. return ResponseResultUtil.errorResult(ResponseCode.A0400.getCode(), "账号旧密码,不可为空");
  219. }
  220. if (StringUtil.isBlank(model.getNewPassword())) {
  221. return ResponseResultUtil.errorResult(ResponseCode.A0400.getCode(), "账号新密码,不可为空");
  222. }
  223. // 验证数据是否存在
  224. SaasAccount account = this.saasAccountService.getOne(model.getAccountId(), model.getGroupCode(), null);
  225. if (account == null) {
  226. return ResponseResultUtil.errorResult(ResponseCode.C0320.getCode(), ResponseCode.C0320.getDesc());
  227. }
  228. // 修改密码
  229. LambdaUpdateWrapper<SaasAccount> updateWrapper = new SaasAccount.Builder().createUpdateWrapper().idEq(model.getAccountId())
  230. .groupCodeEq(model.getGroupCode()).validEq(SaasCommonConstant.STATUS_1).passwordEq(model.getOldPassword()).builderUpdateWrapper();
  231. updateWrapper.set(SaasAccount::getUpdateUser, model.getAccountId());
  232. updateWrapper.set(SaasAccount::getPassword, model.getNewPassword());
  233. boolean result = this.saasAccountService.update(updateWrapper);
  234. return result ? ResponseResultUtil.successResult("密码修改成功") : ResponseResultUtil.errorResult("密码修改失败");
  235. }
  236. /**
  237. * 重置密码
  238. */
  239. @ApiOperation(value = "重置密码")
  240. @RequestMapping(value = "resetAccountPwd")
  241. public ResponseResult resetAccountPwd(@RequestBody @Validated SaasAccountGroupVO model) {
  242. // 验证数据是否存在
  243. SaasAccount account = this.saasAccountService.getById(model.getAccountId());
  244. if (account == null) {
  245. return ResponseResultUtil.errorResult(ResponseCode.C0320.getCode(), ResponseCode.C0320.getDesc());
  246. }
  247. // 重置密码
  248. LambdaUpdateWrapper<SaasAccount> updateWrapper = new SaasAccount.Builder().createUpdateWrapper().idEq(model.getAccountId())
  249. .groupCodeEq(model.getGroupCode()).validEq(SaasCommonConstant.STATUS_1).builderUpdateWrapper();
  250. updateWrapper.set(SaasAccount::getUpdateUser, model.getAccountId());
  251. updateWrapper.set(SaasAccount::getPassword, this.properties.getDefaultPwd());
  252. boolean result = this.saasAccountService.update(updateWrapper);
  253. return result ? ResponseResultUtil.successResult("密码重置成功") : ResponseResultUtil.errorResult("密码重置失败");
  254. }
  255. /**
  256. * 根据账号名称获取账号信息
  257. */
  258. @ApiOperation(value = "根据账号名称获取账号信息")
  259. @RequestMapping(value = "querySaasAccountByUsername")
  260. public ResponseResult querySaasAccountByUsername(@RequestBody @Validated SaasAccountQueryByNameVO queryVO) {
  261. SaasAccount saasAccount = new SaasAccount();
  262. BeanUtils.copyProperties(queryVO, saasAccount);
  263. LambdaQueryWrapper<SaasAccount> queryWrapper = this.saasAccountService.getQueryWrapperByUsername(saasAccount);
  264. queryWrapper.orderByDesc(SaasAccount::getUpdateTime); //更新时间降序
  265. List<SaasAccount> list = this.saasAccountService.list(queryWrapper);
  266. if (CollectionUtil.isEmpty(list)) {
  267. return ResponseResultUtil.successResult(Lists.newArrayList(), 0L);
  268. }
  269. return ResponseResultUtil.successResult(list, (long)list.size());
  270. }
  271. }