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 baseMetaGroupList; // key为分组id,value为分组 private Map baseMetaGroupMap; // key为分组id,value为数据列表 private Map> baseMetaListMap; // key为数据id,value为数据 private Map baseMetaMap; public synchronized void loadData() { try { baseMetaGroupList = this.baseMetaGroupService.getBaseMetaGroupList(); baseMetaGroupMap = new HashMap(); if (baseMetaGroupList == null) { baseMetaGroupList = new ArrayList(); } else { for (BaseMetaGroup baseMetaGroup : baseMetaGroupList) { baseMetaGroupMap.put(baseMetaGroup.getCode(), baseMetaGroup); } } List baseMetaList = this.getBaseMetaList((String) null, null, null, null); baseMetaListMap = new HashMap>(); baseMetaMap = new HashMap(); if (baseMetaList != null) { String key = null; for (BaseMeta baseMeta : baseMetaList) { if (!baseMetaListMap.containsKey(baseMeta.getGroupId()) || baseMetaListMap.get(baseMeta.getGroupId()) == null) { baseMetaListMap.put(baseMeta.getGroupCode(), new ArrayList()); } 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 groupCode, String keyword) { try { Map params = new HashMap(); params.put("groupCode", groupCode); 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 groupCode, String metaKey, Long editId) { try { Map params = new HashMap(); params.put("groupCode", groupCode); params.put("metaKey", metaKey); if (editId!=null) { 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 getBaseMetaList(String groupCode, String keyword, Integer first, Integer pageSize) { try { Map params = new HashMap(); params.put("groupCode", groupCode); 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(Long 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; } 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 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 getBaseMetaGroupList() { if (baseMetaGroupList == null) { baseMetaGroupList = this.baseMetaGroupService.getBaseMetaGroupList(); } return baseMetaGroupList; } public String getBaseMetaGroupName(String baseMetaGroupCode) { if (StringUtils.isBlank(baseMetaGroupCode)) { return ""; } BaseMetaGroup baseMetaGroup= this.baseMetaGroupService.getBaseMetaGroupByCode(baseMetaGroupCode); if(baseMetaGroup!=null){ return baseMetaGroup.getName(); } return ""; } public List getBaseMetaList(String baseMetaGroupCode) { if (StringUtils.isBlank(baseMetaGroupCode)) { return null; } Map params=new HashMap(); params.put("groupCode",baseMetaGroupCode); return baseMetaDao.getBaseMetaList(params); } public String getBaseMetaValue(Long id) { if (id==null) { return ""; } BaseMeta baseMeta=this.baseMetaDao.getBaseMeta(id); if(baseMeta!=null){ return baseMeta.getMetaValue(); } return ""; } public String getBaseMetaKey(Long id) { if (id==null) { return ""; } BaseMeta baseMeta=this.baseMetaDao.getBaseMeta(id); if(baseMeta!=null){ return baseMeta.getMetaKey(); } return ""; } @Transactional(propagation = Propagation.REQUIRED) public List getAllBaseMeta() { return this.baseMetaDao.getAllBaseMeta(); } @Override public BaseMeta getBaseMeta(String groupCode, String key,String value) { return baseMetaDao.selectBaseMeta(groupCode,key,value); } }