package com.ks.app.service.impl.config; import com.ks.app.dao.config.SystemConfigDao; import com.ks.app.dao.config.SystemConfigDao.DaoQuery; import com.ks.app.entity.SystemEnum; import com.ks.app.entity.config.SystemConfig; import com.ks.app.entity.config.SystemConfigKey; import com.ks.app.entity.config.SystemConfigType; import com.ks.app.service.inter.config.SystemConfigService; import com.ks.app.service.query.config.SystemConfigQuery; import org.springframework.cache.annotation.Cacheable; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Service; import org.yeshi.utils.StringUtil; import org.yeshi.utils.bean.BeanUtil; import javax.annotation.Resource; import java.util.ArrayList; import java.util.Date; import java.util.List; @Service public class SystemConfigServiceImpl implements SystemConfigService { @Resource private SystemConfigDao systemConfigDao; @Cacheable(key = "configKey", value = "'getValueCache-'+#system+'-'+#key") @Override public String getValueCache(SystemEnum system, SystemConfigKey key) { DaoQuery daoQuery = new DaoQuery(); daoQuery.system = system; daoQuery.key = key; daoQuery.count = 1; List list = systemConfigDao.list(daoQuery); if (list == null || list.size() == 0) { return null; } return list.get(0).getValue(); } @Override public SystemConfig getByKey(SystemEnum system, SystemConfigKey key) { DaoQuery daoQuery = new DaoQuery(); daoQuery.system = system; daoQuery.key = key; daoQuery.count = 1; List list = systemConfigDao.list(daoQuery); if (list == null || list.size() == 0) { return null; } return list.get(0); } @Override public List list(SystemConfigQuery systemConfigQuery, int page, int pageSize) { DaoQuery daoQuery = new DaoQuery(); try { BeanUtil.copyProperties(systemConfigQuery, daoQuery); } catch (IllegalAccessException e) { e.printStackTrace(); } daoQuery.start = (page - 1) * pageSize; daoQuery.count = pageSize; return systemConfigDao.list(daoQuery); } @Override public long count(SystemConfigQuery systemConfigQuery) { DaoQuery daoQuery = new DaoQuery(); try { BeanUtil.copyProperties(systemConfigQuery, daoQuery); } catch (IllegalAccessException e) { e.printStackTrace(); } return systemConfigDao.count(daoQuery); } @Override public List listByType(SystemConfigType type) { List keyList = new ArrayList<>(); for (SystemConfigKey key : SystemConfigKey.values()) { if (key.getType() == type) { keyList.add(key); } } DaoQuery daoQuery = new DaoQuery(); daoQuery.keyList = keyList; daoQuery.start = 0; daoQuery.count = keyList.size(); return systemConfigDao.list(daoQuery); } @Override public SystemConfig get(String id) { Query query = new Query(); query.addCriteria(Criteria.where("_id").is(id)); return systemConfigDao.findOne(query); } @Override public void add(SystemConfig systemConfig) throws Exception { if (systemConfig.getId() == null) { systemConfig.setId(systemConfig.getSystem().name() + "-" + systemConfig.getKey().name()); } if (StringUtil.isNullOrEmpty(systemConfig.getName())) { systemConfig.setName(systemConfig.getKey().getDesc()); } //查询主键ID是否存在 if (systemConfigDao.get(systemConfig.getId()) != null) { throw new Exception("已存在"); } if (systemConfig.getCreateTime() == null) { systemConfig.setCreateTime(new Date()); } //保存 systemConfigDao.save(systemConfig); } @Override public void save(SystemConfig config) { if (config.getId() == null) { config.setId(config.getSystem().name() + "-" + config.getKey().name()); } if (systemConfigDao.get(config.getId()) != null) { update(config); } else { if (config.getCreateTime() == null) { config.setCreateTime(new Date()); } systemConfigDao.save(config); } } @Override public void update(SystemConfig systemConfig) { if (systemConfig.getUpdateTime() == null) { systemConfig.setUpdateTime(new Date()); } //更新 systemConfigDao.updateSelective(systemConfig); } @Override public void delete(List idList) { for (String id : idList) { systemConfigDao.delete(id); } } }