SaasAccountController.java 18 KB

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