package com.nanometer.smartlab.service;
|
|
import com.nanometer.smartlab.dao.BaseMetaDao;
|
import com.nanometer.smartlab.entity.BaseMeta;
|
import com.nanometer.smartlab.entity.BaseMetaGroup;
|
import com.nanometer.smartlab.exception.AlarmCode;
|
import com.nanometer.smartlab.exception.AlarmException;
|
import com.nanometer.smartlab.exception.BusinessException;
|
import com.nanometer.smartlab.exception.ExceptionEnumCode;
|
import com.nanometer.smartlab.util.IDUtils;
|
import com.nanometer.smartlab.util.MessageUtil;
|
import org.apache.commons.lang.StringUtils;
|
import org.apache.log4j.Logger;
|
import org.springframework.dao.DataAccessException;
|
import org.springframework.dao.DataIntegrityViolationException;
|
import org.springframework.dao.DuplicateKeyException;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Propagation;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* Created by johnny on 17/8/21.
|
*/
|
@Service("baseMetaService")
|
public class BaseMetaServiceImpl implements BaseMetaService {
|
|
private static Logger logger = Logger.getLogger(BaseMetaService.class);
|
|
@Resource(name = "baseMetaDao")
|
BaseMetaDao baseMetaDao;
|
@Resource
|
private BaseMetaGroupService baseMetaGroupService;
|
|
// 所有分组
|
private List<BaseMetaGroup> baseMetaGroupList;
|
// key为分组id,value为分组
|
private Map<String, BaseMetaGroup> baseMetaGroupMap;
|
// key为分组id,value为数据列表
|
private Map<String, List<BaseMeta>> baseMetaListMap;
|
// key为数据id,value为数据
|
private Map<String, BaseMeta> baseMetaMap;
|
|
public synchronized void loadData() {
|
try {
|
baseMetaGroupList = this.baseMetaGroupService.getBaseMetaGroupList();
|
baseMetaGroupMap = new HashMap<String, BaseMetaGroup>();
|
if (baseMetaGroupList == null) {
|
baseMetaGroupList = new ArrayList<BaseMetaGroup>();
|
} else {
|
for (BaseMetaGroup baseMetaGroup : baseMetaGroupList) {
|
baseMetaGroupMap.put(baseMetaGroup.getId(), baseMetaGroup);
|
}
|
}
|
|
List<BaseMeta> baseMetaList = this.getBaseMetaList(null, null, null, null);
|
baseMetaListMap = new HashMap<String, List<BaseMeta>>();
|
baseMetaMap = new HashMap<String, BaseMeta>();
|
if (baseMetaList != null) {
|
String key = null;
|
for (BaseMeta baseMeta : baseMetaList) {
|
if (!baseMetaListMap.containsKey(baseMeta.getGroupId())
|
|| baseMetaListMap.get(baseMeta.getGroupId()) == null) {
|
baseMetaListMap.put(baseMeta.getGroupId(), new ArrayList<BaseMeta>());
|
}
|
baseMetaListMap.get(baseMeta.getGroupId()).add(baseMeta);
|
|
baseMetaMap.put(baseMeta.getId(), baseMeta);
|
}
|
}
|
} catch (Exception e) {
|
logger.error("初始化数据字典数据失败。", e);
|
}
|
}
|
|
@Transactional(propagation = Propagation.REQUIRED)
|
public int getBaseMetaTotalCount(String groupId, String keyword) {
|
try {
|
Map<String, Object> params = new HashMap<String, Object>();
|
params.put("groupId", groupId);
|
if (StringUtils.isNotBlank(keyword)) {
|
params.put("keyword", "%" + keyword + "%");
|
}
|
return this.baseMetaDao.getBaseMetaTotalCount(params);
|
} catch (DataAccessException e) {
|
logger.error(e.getMessage(), e);
|
throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
|
}
|
}
|
|
@Transactional(propagation = Propagation.REQUIRED)
|
public boolean isBaseMetaExist(String groupId, String metaKey, String editId) {
|
try {
|
Map<String, Object> params = new HashMap<String, Object>();
|
params.put("groupId", groupId);
|
params.put("metaKey", metaKey);
|
if (StringUtils.isNotBlank(editId)) {
|
params.put("editId", editId);
|
}
|
int count = this.baseMetaDao.getBaseMetaTotalCount(params);
|
return count > 0;
|
} catch (DataAccessException e) {
|
logger.error(e.getMessage(), e);
|
throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
|
}
|
}
|
|
@Transactional(propagation = Propagation.REQUIRED)
|
public List<BaseMeta> getBaseMetaList(String groupId, String keyword, Integer first, Integer pageSize) {
|
try {
|
Map<String, Object> params = new HashMap<String, Object>();
|
params.put("groupId", groupId);
|
if (StringUtils.isNotBlank(keyword)) {
|
params.put("keyword", "%" + keyword + "%");
|
}
|
params.put("first", first);
|
params.put("pageSize", pageSize);
|
return this.baseMetaDao.getBaseMetaList(params);
|
} catch (DataAccessException e) {
|
logger.error(e.getMessage(), e);
|
throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
|
}
|
}
|
|
@Transactional(propagation = Propagation.REQUIRED)
|
public BaseMeta getBaseMeta(String id) {
|
try {
|
return this.baseMetaDao.getBaseMeta(id);
|
} catch (DataAccessException e) {
|
logger.error(e.getMessage(), e);
|
throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
|
}
|
}
|
|
@Transactional(propagation = Propagation.REQUIRED)
|
public BaseMeta insertBaseMeta(BaseMeta baseMeta) {
|
try {
|
if (baseMeta == null) {
|
return null;
|
}
|
|
if (baseMeta.getId() == null) {
|
baseMeta.setId(IDUtils.uuid());
|
}
|
this.baseMetaDao.insertBaseMeta(baseMeta);
|
this.loadData();
|
return baseMeta;
|
} catch (DuplicateKeyException ex) {
|
logger.warn(ex.getMessage(), ex);
|
throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
|
} catch (DataIntegrityViolationException ex) {
|
logger.warn(ex.getMessage(), ex);
|
throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
|
} catch (DataAccessException ex) {
|
logger.error(ex.getMessage(), ex);
|
throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
|
}
|
}
|
|
@Transactional(propagation = Propagation.REQUIRED)
|
public boolean updateBaseMeta(BaseMeta baseMeta) {
|
try {
|
if (baseMeta == null) {
|
return false;
|
}
|
|
int row = this.baseMetaDao.updateBaseMeta(baseMeta);
|
if (row == 0) {
|
return false;
|
}
|
this.loadData();
|
return true;
|
} catch (DuplicateKeyException ex) {
|
logger.warn(ex.getMessage(), ex);
|
throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
|
} catch (DataIntegrityViolationException ex) {
|
logger.warn(ex.getMessage(), ex);
|
throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
|
} catch (DataAccessException ex) {
|
logger.error(ex.getMessage(), ex);
|
throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
|
}
|
}
|
|
@Transactional(propagation = Propagation.REQUIRED)
|
public boolean deleteBaseMeta(List<String> ids) {
|
try {
|
if (ids == null || ids.isEmpty()) {
|
return false;
|
}
|
|
int row = this.baseMetaDao.deleteBaseMetas(ids);
|
if (row == 0) {
|
return false;
|
}
|
this.loadData();
|
return true;
|
} catch (DataIntegrityViolationException ex) {
|
logger.warn(ex.getMessage(), ex);
|
throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
|
} catch (DataAccessException ex) {
|
logger.error(ex.getMessage(), ex);
|
throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
|
}
|
}
|
|
public List<BaseMetaGroup> getBaseMetaGroupList() {
|
if (baseMetaGroupList == null) {
|
this.loadData();
|
}
|
|
return baseMetaGroupList;
|
}
|
public String getBaseMetaGroupName(String baseMetaGroupId) {
|
if (StringUtils.isBlank(baseMetaGroupId)) {
|
return "";
|
}
|
|
if (baseMetaGroupMap == null) {
|
this.loadData();
|
}
|
|
if (baseMetaGroupMap.containsKey(baseMetaGroupId)
|
&& baseMetaGroupMap.get(baseMetaGroupId) != null) {
|
return baseMetaGroupMap.get(baseMetaGroupId).getName();
|
}
|
|
return "";
|
}
|
public List<BaseMeta> getBaseMetaList(String baseMetaGroupId) {
|
if (StringUtils.isBlank(baseMetaGroupId)) {
|
return null;
|
}
|
|
if (baseMetaListMap == null) {
|
this.loadData();
|
}
|
|
return baseMetaListMap.get(baseMetaGroupId);
|
}
|
|
public String getBaseMetaValue(String id) {
|
if (StringUtils.isBlank(id)) {
|
return "";
|
}
|
|
if (baseMetaMap == null) {
|
this.loadData();
|
}
|
|
if (baseMetaMap.containsKey(id)
|
&& baseMetaMap.get(id) != null) {
|
return baseMetaMap.get(id).getMetaValue();
|
}
|
|
return "";
|
}
|
|
public String getBaseMetaKey(String id) {
|
if (StringUtils.isBlank(id)) {
|
return "";
|
}
|
|
if (baseMetaMap == null) {
|
this.loadData();
|
}
|
|
if (baseMetaMap.containsKey(id)
|
&& baseMetaMap.get(id) != null) {
|
return baseMetaMap.get(id).getMetaKey();
|
}
|
|
return "";
|
}
|
|
@Transactional(propagation = Propagation.REQUIRED)
|
public List<BaseMeta> getAllBaseMeta() {
|
return this.baseMetaDao.getAllBaseMeta();
|
}
|
}
|