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

146 lines
6.9 KiB
Java

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;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
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;
/**
* 新增全局配置
*/
@Override
public String insertGlobalConfiguration(GlobalConfigurationEntity globalConfiguration) {
final String belongGroup = LocalNullUtils.formatNullValue(globalConfiguration.getBelongGroup());
final String key = LocalNullUtils.formatNullValue(globalConfiguration.getKey());
final String value = LocalNullUtils.formatNullValue(globalConfiguration.getValue());
final String remark = LocalNullUtils.formatNullValue(globalConfiguration.getRemark());
if (TextUtils.isEmpty(belongGroup)) {
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "belongGroup不可为空", locale);
}
if (TextUtils.isEmpty(key)) {
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "key不可为空", locale);
}
if (TextUtils.isEmpty(value)) {
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "value不可为空", locale);
}
GlobalConfigurationEntity globalConfigurationEntity = new GlobalConfigurationEntity();
globalConfigurationEntity.setBelongGroup(belongGroup);
globalConfigurationEntity.setKey(key);
globalConfigurationEntity.setValue(value);
globalConfigurationEntity.setRemark(remark);
globalConfigurationDao.save(globalConfigurationEntity);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "新增成功", locale);
}
/**
* 删除全局配置
*/
@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()) {
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "id不存在", locale);
}
globalConfigurationDao.deleteById(id);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "删除成功", locale);
}
/**
* 修改全局配置
*/
@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 belongGroup = LocalNullUtils.formatNullValue(globalConfiguration.getBelongGroup());
final String key = LocalNullUtils.formatNullValue(globalConfiguration.getKey());
final String value = LocalNullUtils.formatNullValue(globalConfiguration.getValue());
final String remark = LocalNullUtils.formatNullValue(globalConfiguration.getRemark());
if (TextUtils.isEmpty(belongGroup)) {
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "belongGroup不可为空", locale);
}
if (TextUtils.isEmpty(key)) {
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "key不可为空", locale);
}
if (TextUtils.isEmpty(value)) {
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "value不可为空", locale);
}
GlobalConfigurationEntity targetEntity = targetEntityOptional.get();
targetEntity.setBelongGroup(belongGroup);
targetEntity.setKey(key);
targetEntity.setValue(value);
if (!TextUtils.isEmpty(remark)) {
targetEntity.setRemark(remark);
}
globalConfigurationDao.save(targetEntity);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功", locale);
}
/**
* 查询全局配置
*/
@Override
public String queryGlobalConfiguration(GlobalConfigurationEntity globalConfiguration) {
final Long id = globalConfiguration.getId();
final String belongGroup = globalConfiguration.getBelongGroup();
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(belongGroup)) {
predicateList.add(criteriaBuilder.like(root.get("belongGroup").as(String.class), "%" + belongGroup + "%"));
}
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);
}
}