fire_point/src/main/java/com/xkrs/service/impl/GlobalConfigurationServiceImpl.java

140 lines
6.6 KiB
Java
Raw Normal View History

2022-08-31 08:55:48 +08:00
package com.xkrs.service.impl;
import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.dao.GlobalConfigurationDao;
import com.xkrs.model.entity.GlobalConfigurationEntity;
import com.xkrs.service.GlobalConfigurationService;
import com.xkrs.utils.LocalNullUtils;
import org.apache.hc.core5.util.TextUtils;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
2022-08-31 08:55:48 +08:00
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
2022-08-31 08:55:48 +08:00
import java.util.Locale;
import java.util.Optional;
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
@Service
public class GlobalConfigurationServiceImpl implements GlobalConfigurationService {
private final Locale locale = LocaleContextHolder.getLocale();
@Resource
private GlobalConfigurationDao globalConfigurationDao;
/**
* 新增全局配置
*/
2022-08-31 08:55:48 +08:00
@Override
public String insertGlobalConfiguration(GlobalConfigurationEntity globalConfiguration) {
final String key = LocalNullUtils.formatNullValue(globalConfiguration.getKey());
final String value = LocalNullUtils.formatNullValue(globalConfiguration.getValue());
final String remark = LocalNullUtils.formatNullValue(globalConfiguration.getRemark());
if (TextUtils.isEmpty(key)) {
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "key不可为空", locale);
}
if (TextUtils.isEmpty(value)) {
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "value不可为空", locale);
}
List<GlobalConfigurationEntity> duplicatedEntityList = globalConfigurationDao.findByKey(key);
if (duplicatedEntityList != null && duplicatedEntityList.size() > 0) {
2022-08-31 08:55:48 +08:00
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "key已存在", locale);
}
GlobalConfigurationEntity globalConfigurationEntity = new GlobalConfigurationEntity();
globalConfigurationEntity.setKey(key);
globalConfigurationEntity.setValue(value);
globalConfigurationEntity.setRemark(remark);
globalConfigurationDao.save(globalConfigurationEntity);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "新增成功", locale);
}
/**
* 删除全局配置
*/
2022-08-31 08:55:48 +08:00
@Override
public String deleteGlobalConfiguration(GlobalConfigurationEntity globalConfiguration) {
Long id = globalConfiguration.getId();
if (null == id) {
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "id不可为空", locale);
}
Optional<GlobalConfigurationEntity> targetEntityOptional = globalConfigurationDao.findById(id);
if (targetEntityOptional.isEmpty()) {
2022-08-31 08:55:48 +08:00
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "id不存在", locale);
}
globalConfigurationDao.deleteById(id);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "删除成功", locale);
}
/**
* 修改全局配置
*/
2022-08-31 08:55:48 +08:00
@Override
public String updateGlobalConfiguration(GlobalConfigurationEntity globalConfiguration) {
Long id = globalConfiguration.getId();
if (null == id) {
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "id不可为空", locale);
}
Optional<GlobalConfigurationEntity> targetEntityOptional = globalConfigurationDao.findById(id);
if (targetEntityOptional.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "id不存在", locale);
}
final String key = LocalNullUtils.formatNullValue(globalConfiguration.getKey());
final String value = LocalNullUtils.formatNullValue(globalConfiguration.getValue());
final String remark = LocalNullUtils.formatNullValue(globalConfiguration.getRemark());
if (TextUtils.isEmpty(key)) {
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "key不可为空", locale);
}
if (TextUtils.isEmpty(value)) {
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "value不可为空", locale);
}
List<GlobalConfigurationEntity> duplicatedEntityList = globalConfigurationDao.findByKey(key);
if (duplicatedEntityList != null && duplicatedEntityList.size() > 0) {
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "key已存在", locale);
}
GlobalConfigurationEntity targetEntity = targetEntityOptional.get();
targetEntity.setKey(key);
targetEntity.setValue(value);
if (!TextUtils.isEmpty(remark)) {
targetEntity.setRemark(remark);
}
globalConfigurationDao.save(targetEntity);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功", locale);
2022-08-31 08:55:48 +08:00
}
/**
* 查询全局配置
*/
2022-08-31 08:55:48 +08:00
@Override
public String queryGlobalConfiguration(GlobalConfigurationEntity globalConfiguration) {
final Long id = globalConfiguration.getId();
final String key = globalConfiguration.getKey();
final String value = globalConfiguration.getValue();
final String remark = globalConfiguration.getRemark();
Specification<GlobalConfigurationEntity> specification = (root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> predicateList = new ArrayList<>();
if (null != id) {
predicateList.add(criteriaBuilder.equal(root.get("id").as(Long.class), id));
}
if (!TextUtils.isEmpty(key)) {
predicateList.add(criteriaBuilder.like(root.get("key").as(String.class), "%" + key + "%"));
}
if (!TextUtils.isEmpty(value)) {
predicateList.add(criteriaBuilder.like(root.get("value").as(String.class), "%" + value + "%"));
}
if (!TextUtils.isEmpty(remark)) {
predicateList.add(criteriaBuilder.like(root.get("remark").as(String.class), "%" + remark + "%"));
}
Predicate[] predicateArray = new Predicate[predicateList.size()];
return criteriaBuilder.and(predicateList.toArray(predicateArray));
};
List<GlobalConfigurationEntity> globalConfigurationEntityList = globalConfigurationDao.findAll(specification, Sort.by(Sort.Direction.ASC, "id"));
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, globalConfigurationEntityList, locale);
2022-08-31 08:55:48 +08:00
}
}