use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.
the class ScheduleJobServiceImpl method changeScheduleJobStatus.
@Override
public boolean changeScheduleJobStatus(Long jobId, String jobStatus) {
boolean success = this.update(Wrappers.<ScheduleJob>lambdaUpdate().set(ScheduleJob::getJobStatus, jobStatus).eq(ScheduleJob::getId, jobId));
if (!success) {
throw new BusinessException(Status.FAIL_OPERATION, "更新状态失败!");
}
// 恢复
if (Cons.ENABLE_STATUS.A.name().equals(jobStatus)) {
// 如果已经存在job,那么直接恢复,否则添加job
if (quartzSchedulerService.existJob(jobId)) {
quartzSchedulerService.resumeJob(jobId);
} else {
ScheduleJob entity = this.getEntity(jobId);
if (V.isEmpty(entity)) {
throw new BusinessException(Status.FAIL_OPERATION, "当前任务无效!");
}
quartzSchedulerService.addJob(entity);
}
} else // 停止
if (Cons.ENABLE_STATUS.I.name().equals(jobStatus)) {
quartzSchedulerService.pauseJob(jobId);
} else {
log.warn("无效的action参数: {}", jobStatus);
}
return true;
}
use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.
the class ScheduleJobServiceImpl method updateEntity.
@Override
public boolean updateEntity(ScheduleJob entity) {
// 更新后对系统的定时任务进行操作
ScheduleJob oldJob = getEntity(entity.getId());
boolean success = super.updateEntity(entity);
if (!success) {
throw new BusinessException(Status.FAIL_OPERATION, "更新定时任务失败!");
}
// job如果存在且参数发生了变化,那么先触发删除原来的job
if (quartzSchedulerService.existJob(entity.getId()) && (V.notEquals(oldJob.getJobStatus(), entity.getJobStatus()) || V.notEquals(oldJob.getJobKey(), entity.getJobKey()) || V.notEquals(oldJob.getCron(), entity.getCron()) || V.notEquals(oldJob.getParamJson(), entity.getParamJson()) || V.notEquals(oldJob.getInitStrategy(), entity.getInitStrategy()))) {
quartzSchedulerService.deleteJob(entity.getId());
}
if (V.equals(entity.getJobStatus(), Cons.ENABLE_STATUS.A.name())) {
quartzSchedulerService.addJob(entity);
}
return true;
}
use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.
the class BaseServiceImpl method createOrUpdateN2NRelations.
@Override
@Transactional(rollbackFor = Exception.class)
public <R> boolean createOrUpdateN2NRelations(SFunction<R, ?> driverIdGetter, Object driverId, SFunction<R, ?> followerIdGetter, List<? extends Serializable> followerIdList, Consumer<QueryWrapper<R>> queryConsumer, Consumer<R> setConsumer) {
if (driverId == null) {
throw new InvalidUsageException("主动ID值不能为空!");
}
if (followerIdList == null) {
log.debug("从动对象ID集合为null,不做关联关系更新处理");
return false;
}
// 从getter中获取class和fieldName
LambdaMeta lambdaMeta = LambdaUtils.extract(driverIdGetter);
Class<R> middleTableClass = (Class<R>) lambdaMeta.getInstantiatedClass();
EntityInfoCache entityInfo = BindingCacheManager.getEntityInfoByClass(middleTableClass);
if (entityInfo == null) {
throw new InvalidUsageException("未找到 " + middleTableClass.getName() + " 的 Service 或 Mapper 定义!");
}
boolean isExistPk = entityInfo.getIdColumn() != null;
// 获取主动从动字段名
String driverFieldName = PropertyNamer.methodToProperty(lambdaMeta.getImplMethodName());
String followerFieldName = convertGetterToFieldName(followerIdGetter);
String driverColumnName = entityInfo.getColumnByField(driverFieldName);
String followerColumnName = entityInfo.getColumnByField(followerFieldName);
// 查询已有关联
QueryWrapper<R> selectOld = new QueryWrapper<R>().eq(driverColumnName, driverId);
if (queryConsumer != null) {
queryConsumer.accept(selectOld);
}
if (isExistPk) {
selectOld.select(entityInfo.getIdColumn(), followerColumnName);
} else {
selectOld.select(followerColumnName);
}
List<Map<String, Object>> oldMap;
IService<R> iService = entityInfo.getService();
BaseMapper<R> baseMapper = entityInfo.getBaseMapper();
if (iService != null) {
oldMap = iService.listMaps(selectOld);
} else {
oldMap = baseMapper.selectMaps(selectOld);
}
// 删除失效关联
List<Serializable> delIds = new ArrayList<>();
for (Map<String, Object> map : oldMap) {
if (V.notEmpty(followerIdList) && followerIdList.remove((Serializable) map.get(followerColumnName))) {
continue;
}
delIds.add((Serializable) map.get(isExistPk ? entityInfo.getIdColumn() : followerColumnName));
}
if (!delIds.isEmpty()) {
if (isExistPk) {
if (iService != null) {
iService.removeByIds(delIds);
} else {
baseMapper.deleteBatchIds(delIds);
}
} else {
QueryWrapper<R> delOld = new QueryWrapper<R>().eq(driverColumnName, driverId).in(entityInfo.getColumnByField(followerFieldName), delIds);
if (queryConsumer != null) {
queryConsumer.accept(selectOld);
}
if (iService != null) {
iService.remove(delOld);
} else if (!delIds.isEmpty()) {
baseMapper.delete(delOld);
}
}
}
// 新增关联
if (V.notEmpty(followerIdList)) {
List<R> n2nRelations = new ArrayList<>(followerIdList.size());
try {
for (Serializable followerId : followerIdList) {
R relation = middleTableClass.newInstance();
BeanUtils.setProperty(relation, driverFieldName, driverId);
BeanUtils.setProperty(relation, followerFieldName, followerId);
if (setConsumer != null) {
setConsumer.accept(relation);
}
n2nRelations.add(relation);
}
} catch (Exception e) {
throw new BusinessException(Status.FAIL_EXCEPTION, e);
}
if (iService != null) {
if (iService instanceof BaseService) {
((BaseService<R>) iService).createEntities(n2nRelations);
} else {
iService.saveBatch(n2nRelations);
}
} else {
// 新增关联,无service只能循环插入
for (R relation : n2nRelations) {
baseMapper.insert(relation);
}
}
}
return true;
}
use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.
the class BaseFileController method uploadFile.
/**
* 直接上传文件
* @param
* @return
* @throws Exception
*/
public <T> JsonResult uploadFile(MultipartFile file, Class<T> entityClass) throws Exception {
if (file == null) {
throw new BusinessException(Status.FAIL_INVALID_PARAM, "未获取待处理的文件!");
}
String originFileName = file.getOriginalFilename();
if (V.isEmpty(originFileName) || !isValidFileExt(file)) {
log.debug("非法的文件类型: " + originFileName);
throw new BusinessException(Status.FAIL_VALIDATION, "请上传合法的文件格式!");
}
// 保存文件
UploadFile uploadFile = saveFile(file, entityClass);
// 保存上传记录
createUploadFile(uploadFile);
// 返回结果
return JsonResult.OK(new HashMap(16) {
{
put("uuid", uploadFile.getUuid());
put("accessUrl", uploadFile.getAccessUrl());
put("fileName", uploadFile.getFileName());
}
});
}
use of com.diboot.core.exception.BusinessException in project diboot by dibo-software.
the class BaseController method attachMoreRelatedData.
/**
* 通用的attachMore获取数据
*
* @param attachMore 相应的attachMore
* @param parentValue 父级值
* @return labelValue集合
*/
protected List<LabelValue> attachMoreRelatedData(AttachMoreDTO attachMore, String parentValue) {
String entityClassName = S.capFirst(S.toLowerCaseCamel(attachMore.getTarget()));
Class<?> entityClass = BindingCacheManager.getEntityClassBySimpleName(entityClassName);
if (V.isEmpty(entityClass)) {
throw new BusinessException("attachMore: " + attachMore.getTarget() + " 不存在");
}
BaseService<?> baseService = ContextHelper.getBaseServiceByEntity(entityClass);
if (baseService == null) {
throw new BusinessException("attachMore: " + attachMore.getTarget() + " 的Service不存在 ");
}
PropInfo propInfoCache = BindingCacheManager.getPropInfoByClass(entityClass);
Function<String, String> field2column = field -> {
if (V.notEmpty(field)) {
String column = propInfoCache.getColumnByField(field);
if (V.notEmpty(column)) {
return column;
} else {
throw new BusinessException("attachMore: " + attachMore.getTarget() + " 无 `" + field + "` 属性");
}
}
return null;
};
String label = field2column.apply(S.defaultIfBlank(attachMore.getLabel(), "label"));
String value = S.defaultString(field2column.apply(attachMore.getValue()), propInfoCache.getIdColumn());
String ext = field2column.apply(attachMore.getExt());
// 构建查询条件
QueryWrapper<?> queryWrapper = Wrappers.query().select(V.isEmpty(ext) ? new String[] { label, value } : new String[] { label, value, ext }).like(V.notEmpty(attachMore.getKeyword()), label, attachMore.getKeyword());
// 解析排序
if (V.notEmpty(attachMore.getOrderBy())) {
String[] orderByFields = S.split(attachMore.getOrderBy(), ",");
for (String field : orderByFields) {
V.securityCheck(field);
String[] fieldAndOrder = field.split(":");
String columnName = field2column.apply(fieldAndOrder[0]);
if (fieldAndOrder.length > 1 && Cons.ORDER_DESC.equalsIgnoreCase(fieldAndOrder[1])) {
queryWrapper.orderByDesc(columnName);
} else {
queryWrapper.orderByAsc(columnName);
}
}
}
// 父级限制
String parentColumn = field2column.apply(S.defaultIfBlank(attachMore.getParent(), attachMore.isTree() ? "parentId" : null));
if (V.notEmpty(parentColumn)) {
if (V.notEmpty(parentValue)) {
queryWrapper.eq(parentColumn, parentValue);
} else {
queryWrapper.and(e -> e.isNull(parentColumn).or().eq(parentColumn, 0));
}
}
// 构建附加条件
buildAttachMoreCondition(attachMore, queryWrapper, field2column);
// 获取数据并做相应填充
List<LabelValue> labelValueList = baseService.getLabelValueList(queryWrapper);
if (V.notEmpty(parentColumn) || attachMore.getNext() != null) {
Boolean leaf = !attachMore.isTree() && attachMore.getNext() == null ? true : null;
// 第一层tree与最后一层无需返回type
String type = attachMore.isTree() || Boolean.TRUE.equals(leaf) ? null : attachMore.getTarget();
labelValueList.forEach(item -> {
item.setType(type);
item.setLeaf(leaf);
// 非异步加载
if (!attachMore.isLazy()) {
List<LabelValue> children = attachMoreRelatedData(attachMore, S.valueOf(item.getValue()), type);
item.setChildren(children.isEmpty() ? null : children);
item.setDisabled(children.isEmpty() && attachMore.getNext() != null ? true : null);
}
});
}
return labelValueList;
}
Aggregations