package com.vci.ubcs.system.service.impl;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.vci.ubcs.common.constant.CommonConstant;
|
import com.vci.ubcs.system.mapper.StrategyMapper;
|
import com.vci.ubcs.system.service.IStrategyService;
|
import org.springblade.core.cache.utils.CacheUtil;
|
import org.springblade.core.log.exception.ServiceException;
|
import org.springblade.core.mp.support.Query;
|
import org.springblade.core.tool.utils.Func;
|
import com.vci.ubcs.system.entity.Strategy;
|
import org.springframework.data.domain.PageImpl;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.util.Date;
|
import java.util.List;
|
|
import static org.springblade.core.cache.constant.CacheConstant.SYS_CACHE;
|
|
/**
|
* 密码策略(Strategy)表服务实现类
|
*
|
* @author makejava
|
* @since 2023-03-20 15:25:05
|
*/
|
@Service
|
public class StrategyServiceImpl extends ServiceImpl<StrategyMapper, Strategy> implements IStrategyService {
|
|
@Resource
|
private StrategyMapper strategyMapper;
|
|
/**
|
* 通过ID查询单条数据
|
*
|
* @param id 主键
|
* @return 实例对象
|
*/
|
@Override
|
public Strategy queryById(String id) {
|
return this.getById(id);
|
}
|
|
/**
|
* 查询默认密码策略
|
* @return
|
*/
|
@Override
|
public Strategy queryByIsDefault() {
|
return this.strategyMapper.queryByIsDefault();
|
}
|
|
/**
|
* 分页查询
|
*
|
* @param query 分页对象
|
* @return 查询结果
|
*/
|
@Override
|
public PageImpl<Strategy> queryAllByPage(Query query) {
|
Page<Strategy> strategyPage = new Page<>(query.getCurrent(), query.getSize());
|
return new PageImpl<>(this.strategyMapper.queryAllByPage(strategyPage));
|
}
|
|
/**
|
* 新增数据或者修改数据
|
*
|
* @param strategy 实例对象
|
* @return 实例对象
|
*/
|
@Override
|
public boolean submit(Strategy strategy) {
|
//判断是否携带id
|
if(Func.isEmpty(strategy.getId())){
|
//执行新增
|
Strategy dbstrategy = this.getOne(Wrappers.<Strategy>query().lambda()
|
.eq(Strategy::getStrategyName, strategy.getStrategyName()));
|
//如果数据库中存在这条组合名称的记录直接返回
|
if(!Func.isEmpty(dbstrategy)){
|
throw new ServiceException("该密码策略已存在!");
|
}
|
//如果当前新增设置为默认密码策略,需要将已存在默认密码策略修改为非默认
|
if(strategy.getIsDefault().equals("1") || strategy.getIsDefault() == 1){
|
this.update(Wrappers.<Strategy>update().lambda()
|
.set(Strategy::getIsDefault, CommonConstant.NOT_SEALED_ID)
|
.eq(Strategy::getIsDefault, CommonConstant.DATA_SCOPE_CATEGORY));
|
}
|
if(strategy.getRequiredType() > 0){
|
throw new ServiceException("必填种类不能小于等于0!");
|
}
|
if(strategy.getRequiredType() > strategy.getCombinationIds().split(",").length){
|
throw new ServiceException("必填种类不能大于所选择的密码组合方式的个数!");
|
}
|
if(strategy.getMaxPwdLen() > strategy.getMinPwdLen()){
|
throw new ServiceException("密码最大长度不能小于最小长度!");
|
}
|
if(strategy.getMinPwdLen() < strategy.getCombinationIds().split(",").length || strategy.getMaxPwdLen() < strategy.getCombinationIds().split(",").length){
|
throw new ServiceException("密码最小长度不能小于秘密策略的值!");
|
}
|
if(Func.isEmpty(strategy.getCreateTime())){
|
strategy.setCreateTime(new Date());
|
}
|
if(Func.isEmpty(strategy.getUpdateTime())){
|
strategy.setUpdateTime(new Date());
|
}
|
boolean temp = super.saveOrUpdate(strategy);
|
return temp;
|
}else {
|
//如果当前修改设置为默认密码策略,需要将已存在默认密码策略修改为非默认
|
if(strategy.getIsDefault().equals("1") || strategy.getIsDefault() == 1){
|
this.update(Wrappers.<Strategy>update().lambda()
|
.set(Strategy::getIsDefault,CommonConstant.NOT_SEALED_ID)
|
.eq(Strategy::getIsDefault,CommonConstant.DATA_SCOPE_CATEGORY));
|
}else {
|
if(Func.isEmpty(queryByIsDefault())){
|
throw new ServiceException("默认密码策略必须有且仅有一条!");
|
}
|
}
|
if(Func.isEmpty(strategy.getUpdateTime())){
|
strategy.setUpdateTime(new Date());
|
}
|
CacheUtil.clear(SYS_CACHE, Boolean.FALSE);
|
return super.saveOrUpdate(strategy);
|
}
|
}
|
|
/**
|
* 通过主键删除数据
|
*
|
* @param ids 主键
|
* @return 是否成功
|
*/
|
@Override
|
public boolean deleteByIds(List<String> ids) {
|
Strategy strategy = this.getOne(Wrappers.<Strategy>query().lambda()
|
.in(Strategy::getId,ids)
|
.eq(Strategy::getIsDefault, CommonConstant.DATA_SCOPE_CATEGORY));
|
//如果存在默认策略的id,就不能直接删除给出提示
|
if(!Func.isEmpty(strategy)){
|
throw new ServiceException("不能删除默认密码策略!");
|
}
|
boolean tenantTemp = this.removeBatchByIds(ids);
|
return tenantTemp;
|
}
|
|
/**
|
* 通过租户id以及用户名查询密码策略
|
* @param tenantId
|
* @param name
|
* @return
|
*/
|
@Override
|
public Strategy queryByNameAndTenantId(String tenantId, String name) {
|
Strategy strategy = this.strategyMapper.queryByNameAndTenantId(tenantId,name);
|
if(!Func.isEmpty(strategy)){
|
return strategy;
|
}
|
return queryByIsDefault();
|
}
|
|
/**
|
* 根据用户id查询密码策略
|
* @param userId
|
* @return
|
*/
|
@Override
|
public Strategy queryByUserId(Long userId) {
|
Strategy strategy = this.strategyMapper.queryByUserId(userId);
|
if(!Func.isEmpty(strategy)){
|
return strategy;
|
}
|
return queryByIsDefault();
|
}
|
|
}
|