package com.xkrs.service.impl; import com.xkrs.common.encapsulation.PromptMessageEnum; import com.xkrs.dao.GlobalConfigDao; import com.xkrs.dao.GlobalConfigDictDao; import com.xkrs.model.entity.GlobalConfigDictEntity; import com.xkrs.model.entity.GlobalConfigEntity; import com.xkrs.model.qo.GlobalConfigDictQo; import com.xkrs.service.GlobalConfigService; 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.*; import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject; @Service public class GlobalConfigServiceImpl implements GlobalConfigService { private final Locale locale = LocaleContextHolder.getLocale(); @Resource private GlobalConfigDao globalConfigDao; @Resource private GlobalConfigDictDao globalConfigDictDao; @Override public String selectGlobalConfigDict() { List globalConfigDictList = globalConfigDictDao.findAll(Sort.by(Sort.Direction.ASC, "id")); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, globalConfigDictList, locale); } @Override public Long selectGlobalConfigValue(Long code) { try { Optional existsGlobalConfigOptional = globalConfigDao.findByCode(code); if (existsGlobalConfigOptional.isEmpty()) { return 0L; } return existsGlobalConfigOptional.get().getValue(); } catch (Exception e) { e.printStackTrace(); } return 0L; } @Override public String selectGlobalConfig(Long code) { Specification specification = (root, criteriaQuery, criteriaBuilder) -> { //查询条件集合 List predicateList = new ArrayList<>(); if (code != null) { predicateList.add(criteriaBuilder.equal(root.get("code").as(Long.class), code)); } Predicate[] predicateArray = new Predicate[predicateList.size()]; return criteriaBuilder.and(predicateList.toArray(predicateArray)); }; List globalConfigList = globalConfigDao.findAll(specification, Sort.by(Sort.Direction.ASC, "id")); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, globalConfigList, locale); } @Override public String globalConfig(Map globalConfigMap) { if (globalConfigMap == null || globalConfigMap.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.PARAM_NULL, "配置失败", locale); } for (Map.Entry entry : globalConfigMap.entrySet()) { Long code = entry.getKey(); Long value = entry.getValue(); if (code == null || value == null) { continue; } Optional existsGlobalConfigOptional = globalConfigDao.findByCode(code); if (existsGlobalConfigOptional.isEmpty()) { GlobalConfigEntity globalConfigEntity = new GlobalConfigEntity(); globalConfigEntity.setCode(code); globalConfigEntity.setValue(value); globalConfigDao.save(globalConfigEntity); } else { GlobalConfigEntity existsGlobalConfig = existsGlobalConfigOptional.get(); existsGlobalConfig.setCode(code); existsGlobalConfig.setValue(value); globalConfigDao.save(existsGlobalConfig); } } return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "配置成功", locale); } @Override public String globalConfigDict(GlobalConfigDictQo globalConfigDictQo) { Long code = globalConfigDictQo.getCode(); String name = globalConfigDictQo.getName(); String remark = globalConfigDictQo.getRemark(); Optional existsGlobalConfigDictOptional = globalConfigDictDao.findByCode(code); if (existsGlobalConfigDictOptional.isPresent()) { return outputEncapsulationObject(PromptMessageEnum.PARAM_ILLEGAL, "配置失败", locale); } GlobalConfigDictEntity globalConfigDictEntity = new GlobalConfigDictEntity(); globalConfigDictEntity.setCode(code); globalConfigDictEntity.setName(name); globalConfigDictEntity.setRemark(remark); globalConfigDictDao.save(globalConfigDictEntity); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "配置成功", locale); } }