package com.persagy.account.controller; import java.util.List; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.persagy.account.config.ApplicationProperties; import com.persagy.account.manage.SaasAccountHandler; import com.persagy.account.pojo.dto.SaasAccount; import com.persagy.account.pojo.dto.SaasRole; import com.persagy.account.pojo.vo.account.SaasAccountCreateVO; import com.persagy.account.pojo.vo.account.SaasAccountDeleteVO; import com.persagy.account.pojo.vo.account.SaasAccountGroupVO; import com.persagy.account.pojo.vo.account.SaasAccountPageVO; import com.persagy.account.pojo.vo.account.SaasAccountQueryByNameVO; import com.persagy.account.pojo.vo.account.SaasAccountQueryVO; import com.persagy.account.pojo.vo.thirty.SaasCodeVO; import com.persagy.account.service.ISaasAccountService; import com.persagy.account.service.ISaasRoleService; import com.persagy.common.constant.SaasCommonConstant; import com.persagy.common.enums.ResponseCode; import com.persagy.common.model.BaseGroupModel; import com.persagy.common.utils.ResponseResult; import com.persagy.common.utils.ResponseResultUtil; import com.persagy.common.utils.StringUtil; import com.persagy.security.util.BouncycastleCipher; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.BooleanUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; /** * 账号信息 * * @version 1.0.0 * @company persagy * @author zhangqiankun * @date 2021-03-13 15:29:50 */ @RestController @Api(tags = "账号信息") @SuppressWarnings("unchecked") @RequestMapping(value = "/account", method = RequestMethod.POST) public class SaasAccountController { @Autowired private ApplicationProperties properties; @Autowired private ISaasRoleService saasRoleService; @Autowired private BouncycastleCipher bouncycastleCipher; @Autowired private SaasAccountHandler saasAccountHandler; @Autowired private ISaasAccountService saasAccountService; @Autowired private RedisTemplate redisTemplate; /** * 账号信息查询 */ @ApiOperation(value = "账号信息查询") @RequestMapping(value = "querySaasAccountList") public ResponseResult querySaasAccountList(@RequestBody @Validated SaasAccountQueryVO queryVO) { if (CollectionUtil.isNotEmpty(queryVO.getAccountIds()) && queryVO.getAccountIds().size() > 100) { return ResponseResultUtil.errorResult(ResponseCode.A0400.getCode(), "账号ID集合数量过多,请分批查询"); } SaasAccount saasAccount = new SaasAccount(); BeanUtils.copyProperties(queryVO, saasAccount); saasAccount.setId(queryVO.getAccountId()); LambdaQueryWrapper queryWrapper = this.saasAccountService.getQueryWrapper(saasAccount); if (BooleanUtil.isTrue(queryVO.getGroupIsNull())) { queryWrapper.apply("(GROUP_CODE IS NULL OR TRIM(GROUP_CODE) = '')"); } queryWrapper.orderByDesc(SaasAccount::getUpdateTime, SaasAccount::getId); // 默认更新时间降序 List list = this.saasAccountService.list(queryWrapper); if (CollectionUtil.isEmpty(list)) { return ResponseResultUtil.successResult(Lists.newArrayList(), 0L); } return ResponseResultUtil.successResult(list, (long)list.size()); } /** * 账号分页信息查询-普通列表 */ @ApiOperation(value = "账号分页信息查询") @RequestMapping(value = "queryAccountPageList") public ResponseResult queryAccountPageList(@RequestBody @Validated SaasAccountPageVO pageVO) { if (CollectionUtil.isNotEmpty(pageVO.getAccountIds()) && pageVO.getAccountIds().size() > 100) { return ResponseResultUtil.errorResult(ResponseCode.A0400.getCode(), "账号ID集合数量过多,请分批查询"); } SaasAccount saasAccount = new SaasAccount(); BeanUtils.copyProperties(pageVO, saasAccount); saasAccount.setId(pageVO.getAccountId()); Page pageList = null; if (BooleanUtil.isTrue(pageVO.getShowGroup())) { pageList = this.saasAccountService.queryAccountPageList(saasAccount, pageVO.getPage(), pageVO.getSize()); } else { Page page = new Page(pageVO.getPage(), pageVO.getSize()); LambdaQueryWrapper queryWrapper = this.saasAccountService.getQueryWrapper(saasAccount); queryWrapper.orderByDesc(SaasAccount::getUpdateTime, SaasAccount::getId); // 默认更新时间降序 pageList = this.saasAccountService.page(page, queryWrapper); } if (pageList == null || CollectionUtil.isEmpty(pageList.getRecords())) { return ResponseResultUtil.successResult(Lists.newArrayList(), 0L); } // 额外信息补充 List records = pageList.getRecords(); if (BooleanUtil.isTrue(pageVO.getShowRoles())) { // 获取角色集合 for (SaasAccount account : records) { List roleList = this.saasRoleService.querySaasRoleList(account.getId()); account.setRoles(roleList); } } return ResponseResultUtil.successResult(records, pageList.getTotal()); } /** * 根据角色ID,查询出此角色下所有的账号信息 */ @ApiOperation(value = "查询角色账号信息") @RequestMapping(value = "queryRoleAccountList") public ResponseResult queryRoleAccountList(@RequestBody @Validated SaasAccountQueryVO queryVO) { if (StringUtil.isBlank(queryVO.getRoleId())) { return ResponseResultUtil.errorResult(ResponseCode.A0400.getCode(), "角色ID,不可为空"); } SaasAccount saasAccount = new SaasAccount(); BeanUtils.copyProperties(queryVO, saasAccount); List accounts = this.saasAccountService.queryRoleAccountList(saasAccount, queryVO.getRoleId()); if (CollectionUtil.isEmpty(accounts)) { return ResponseResultUtil.successResult(Lists.newArrayList(), 0L); } return ResponseResultUtil.successResult(accounts, (long) accounts.size()); } /** * 根据角色ID,查询出此角色下所有的账号信息 */ @ApiOperation(value = "查询角色账号信息") @RequestMapping(value = "queryRoleAccountPage") public ResponseResult queryRoleAccountPage(@RequestBody @Validated SaasAccountPageVO queryVO) { if (StringUtil.isBlank(queryVO.getRoleId()) && CollectionUtil.isEmpty(queryVO.getRoleIds())) { return ResponseResultUtil.errorResult(ResponseCode.A0400.getCode(), "角色ID,不可为空"); } if (CollectionUtil.isNotEmpty(queryVO.getRoleIds()) && queryVO.getRoleIds().size() > 1000) { return ResponseResultUtil.errorResult(ResponseCode.A0400.getCode(), "角色ID数量过多,请分批查询"); } SaasAccount saasAccount = new SaasAccount(); BeanUtils.copyProperties(queryVO, saasAccount); Page accounts = this.saasAccountService.queryRoleAccountPage(saasAccount, queryVO.getRoleIds(), queryVO.getPage(), queryVO.getSize()); return ResponseResultUtil.successResult(accounts.getRecords(), accounts.getTotal()); } /** * 根据账号名称获取账号信息 */ @ApiOperation(value = "根据账号名称获取账号信息") @RequestMapping(value = "querySaasAccountByUsername") public ResponseResult querySaasAccountByUsername(@RequestBody @Validated SaasAccountQueryByNameVO queryVO) { SaasAccount saasAccount = new SaasAccount(); BeanUtils.copyProperties(queryVO, saasAccount); LambdaQueryWrapper queryWrapper = this.saasAccountService.getQueryWrapperByUsername(saasAccount); List list = this.saasAccountService.list(queryWrapper); if (CollectionUtil.isEmpty(list)) { return ResponseResultUtil.successResult(Lists.newArrayList(), 0L); } return ResponseResultUtil.successResult(list, (long)list.size()); } /** * 获取账号信息 */ @ApiOperation(value = "获取账号信息") @RequestMapping(value = "getSaasAccount") public ResponseResult getSaasAccount(@RequestBody @Validated SaasCodeVO model) { String accountId = (String) this.redisTemplate.opsForHash().get(model.getClientId(), SaasCommonConstant.ACCOUNT_ID_REDIS_HASH_KEY); if (StringUtil.isBlank(accountId)) { return ResponseResultUtil.errorResult(ResponseCode.A0201.getCode(), ResponseCode.A0201.getDesc()); } SaasAccount saasAccount = this.saasAccountService.getOne(accountId, null, null); if (saasAccount == null) { return ResponseResultUtil.errorResult(ResponseCode.A0201.getCode(), ResponseCode.A0201.getDesc()); } return ResponseResultUtil.successResult(saasAccount); } /** * 新增,登录用户名,全局唯一 */ @ApiOperation(value = "保存") @RequestMapping(value = "createSaasAccount") public ResponseResult createSaasAccount(@RequestBody @Validated SaasAccountCreateVO createVO) { if (createVO.getValid() == null) { createVO.setValid(SaasCommonConstant.STATUS_1); } if (SaasCommonConstant.STR_STATUS_0.equals(createVO.getValidLast())) { if (!(createVO.getValidStartTime() != null && createVO.getValidEndTime() != null)) { return ResponseResultUtil.errorResult(ResponseCode.C0341.getCode(), "当validLast为0时,账号有效期时间字段不可为空"); } } if (createVO.isVerify() && SaasCommonConstant.STR_STATUS_1.equals(createVO.getAccountBelong()) && StringUtil.isBlank(createVO.getGroupCode())) { return ResponseResultUtil.errorResult(ResponseCode.C0341.getCode(), "业务系统账号,请先关联集团"); } boolean exists = this.saasAccountService.validAccountName(createVO.getUsername(), createVO.getMail(), createVO.getPhoneNum(), null, false); if (!exists) { return ResponseResultUtil.errorResult(ResponseCode.C0341.getCode(), "账号用户名、邮箱、手机号,存在重复"); } SaasAccount saasAccount = new SaasAccount(); BeanUtils.copyProperties(createVO, saasAccount); saasAccount.setUpdateUser(createVO.getAccountId()); saasAccount.setTerminal(Sets.newHashSet(createVO.getAppId())); saasAccount.setId(null); if (StringUtil.isBlank(createVO.getPassword())) { saasAccount.setPassword(this.properties.getDefaultPwd()); } exists = this.saasAccountHandler.createSaasAccount(saasAccount, createVO.getCasType(), createVO.getRoles()); return exists ? ResponseResultUtil.successResult("保存成功", saasAccount.getId()) : ResponseResultUtil.errorResult("保存失败"); } /** * 更新 */ @ApiOperation(value = "更新") @RequestMapping(value = "updateSaasAccount") public ResponseResult updateSaasAccount(@RequestBody @Validated SaasAccountCreateVO createVO) { createVO.setPassword(null); //目前不允许更新时,更新密码 if (StringUtil.isBlank(createVO.getAccountId())) { return ResponseResultUtil.errorResult(ResponseCode.A0400.getCode(), "主键ID,不可为空"); } if (createVO.getValid() == null) { return ResponseResultUtil.errorResult(ResponseCode.A0400.getCode(), "账号状态不可为空"); } if (SaasCommonConstant.STR_STATUS_0.equals(createVO.getValidLast())) { if (!(createVO.getValidStartTime() != null && createVO.getValidEndTime() != null)) { return ResponseResultUtil.errorResult(ResponseCode.C0341.getCode(), "当validLast为0时,账号有效期时间字段不可为空"); } } if (createVO.isVerify() && SaasCommonConstant.STR_STATUS_1.equals(createVO.getAccountBelong()) && StringUtil.isBlank(createVO.getGroupCode())) { return ResponseResultUtil.errorResult(ResponseCode.C0341.getCode(), "业务系统账号,集团编码不可为空"); } boolean exists = this.saasAccountService.validAccountName(createVO.getUsername(), createVO.getMail(), createVO.getPhoneNum(), createVO.getAccountId(), true); if (!exists) { return ResponseResultUtil.errorResult(ResponseCode.C0341.getCode(), "账号用户名、邮箱、手机号,存在重复"); } SaasAccount saasAccount = new SaasAccount(); BeanUtils.copyProperties(createVO, saasAccount); saasAccount.setId(createVO.getAccountId()); saasAccount.setTerminal(Sets.newHashSet(createVO.getAppId())); saasAccount.setUpdateUser(createVO.getAccountId()); exists = this.saasAccountHandler.updateSaasAccount(saasAccount, createVO.getCasType(), createVO.getRoles()); return exists ? ResponseResultUtil.successResult("更新成功") : ResponseResultUtil.errorResult("更新失败"); } /** * 业务超管账号关联集团 */ @ApiOperation(value = "集团关联业务超管账号") @RequestMapping(value = "addSaasAccountGroup") public ResponseResult addSaasAccountGroup(@RequestBody @Validated BaseGroupModel model) { boolean result = this.saasAccountHandler.addSaasAccountGroup(model); return result ? ResponseResultUtil.successResult("关联成功") : ResponseResultUtil.errorResult("关联失败"); } /** * 逻辑删除 */ @ApiOperation(value = "禁用") @RequestMapping(value = "deleteSaasAccount") public ResponseResult deleteSaasAccount(@RequestBody @Validated BaseGroupModel deleteModel) { // 验证数据是否存在 SaasAccount account = this.saasAccountService.getOne(deleteModel.getAccountId(), deleteModel.getGroupCode(), null); if (account == null) { return ResponseResultUtil.errorResult(ResponseCode.C0320.getCode(), ResponseCode.C0320.getDesc()); } // 逻辑删除 boolean result = this.saasAccountHandler.deleteSaasAccount(deleteModel); if (result) { // 移除token this.bouncycastleCipher.remove(deleteModel.getAccountId()); } return result ? ResponseResultUtil.successResult("禁用成功") : ResponseResultUtil.errorResult("禁用失败"); } /** * 启用 */ @ApiOperation(value = "启用") @RequestMapping(value = "enableSaasAccount") public ResponseResult enableSaasAccount(@RequestBody @Validated BaseGroupModel model) { LambdaUpdateWrapper updateWrapper = new SaasAccount.Builder().createUpdateWrapper() .idEq(model.getAccountId()).groupCodeEq(model.getGroupCode()) .validEq(SaasCommonConstant.STATUS_0).builderUpdateWrapper(); updateWrapper.set(SaasAccount::getUpdateUser, model.getAccountId()); updateWrapper.set(SaasAccount::getValid, SaasCommonConstant.STATUS_1); boolean result = this.saasAccountService.update(updateWrapper); return result ? ResponseResultUtil.successResult("启用成功") : ResponseResultUtil.errorResult("启用失败"); } /** * 修改密码 */ @ApiOperation(value = "修改密码") @RequestMapping(value = "updateAccountPwd") public ResponseResult updateAccountPwd(@RequestBody @Validated SaasAccountGroupVO model) { if (StringUtil.isBlank(model.getOldPassword())) { return ResponseResultUtil.errorResult(ResponseCode.A0400.getCode(), "账号旧密码,不可为空"); } if (StringUtil.isBlank(model.getNewPassword())) { return ResponseResultUtil.errorResult(ResponseCode.A0400.getCode(), "账号新密码,不可为空"); } // 验证数据是否存在 SaasAccount account = this.saasAccountService.getOne(model.getAccountId(), model.getGroupCode(), null); if (account == null) { return ResponseResultUtil.errorResult(ResponseCode.C0320.getCode(), ResponseCode.C0320.getDesc()); } // 修改密码 LambdaUpdateWrapper updateWrapper = new SaasAccount.Builder().createUpdateWrapper().idEq(model.getAccountId()) .groupCodeEq(model.getGroupCode()).validEq(SaasCommonConstant.STATUS_1).passwordEq(model.getOldPassword()).builderUpdateWrapper(); updateWrapper.set(SaasAccount::getUpdateUser, model.getAccountId()); updateWrapper.set(SaasAccount::getPassword, model.getNewPassword()); boolean result = this.saasAccountService.update(updateWrapper); return result ? ResponseResultUtil.successResult("密码修改成功") : ResponseResultUtil.errorResult("密码修改失败"); } /** * 重置密码 */ @ApiOperation(value = "重置密码") @RequestMapping(value = "resetAccountPwd") public ResponseResult resetAccountPwd(@RequestBody @Validated SaasAccountGroupVO model) { // 验证数据是否存在 SaasAccount account = this.saasAccountService.getById(model.getAccountId()); if (account == null) { return ResponseResultUtil.errorResult(ResponseCode.C0320.getCode(), ResponseCode.C0320.getDesc()); } // 重置密码 LambdaUpdateWrapper updateWrapper = new SaasAccount.Builder().createUpdateWrapper().idEq(model.getAccountId()) .groupCodeEq(model.getGroupCode()).validEq(SaasCommonConstant.STATUS_1).builderUpdateWrapper(); updateWrapper.set(SaasAccount::getUpdateUser, model.getAccountId()); updateWrapper.set(SaasAccount::getPassword, this.properties.getDefaultPwd()); boolean result = this.saasAccountService.update(updateWrapper); return result ? ResponseResultUtil.successResult("密码重置成功") : ResponseResultUtil.errorResult("密码重置失败"); } /** * 根据id集合批量删除 ---物理删除 */ @ApiOperation(value = "根据id集合批量删除") @RequestMapping(value = "deleteSaasAccountListByIds") public ResponseResult deleteSaasAccountListByIds(@RequestBody @Validated SaasAccountDeleteVO deleteModel) { boolean result = this.saasAccountHandler.deleteSaasAccountListByIds(deleteModel); return result ? ResponseResultUtil.successResult("删除成功") : ResponseResultUtil.errorResult("删除失败"); } }