package com.nanometer.smartlab.service;
|
|
import com.nanometer.smartlab.dao.BaseRoleDao;
|
import com.nanometer.smartlab.entity.BaseRole;
|
import com.nanometer.smartlab.entity.BaseRolePage;
|
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/11/18.
|
*/
|
@Service("baseRoleService")
|
public class BaseRoleServiceImpl implements BaseRoleService {
|
|
private static Logger logger = Logger.getLogger(BaseRoleService.class);
|
|
@Resource(name = "baseRoleDao")
|
BaseRoleDao baseRoleDao;
|
|
@Resource
|
private BaseRolePageService baseRolePageService;
|
|
@Transactional(propagation = Propagation.REQUIRED)
|
public List<BaseRole> getBaseRoleList(String name, String memo, Integer first, Integer pageSize) {
|
try {
|
Map<String, Object> params = new HashMap<String, Object>();
|
if (StringUtils.isNotBlank(name)) {
|
params.put("name", "%" + name + "%");
|
}
|
if (StringUtils.isNotBlank(memo)) {
|
params.put("memo", "%" + memo + "%");
|
}
|
params.put("first", first);
|
params.put("pageSize", pageSize);
|
return this.baseRoleDao.getBaseRoleList(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 List<BaseRole> getBaseRoleList() {
|
return this.getBaseRoleList(null, null, null, null);
|
}
|
|
@Transactional(propagation = Propagation.REQUIRED)
|
public int getBaseRoleTotalCount(String name, String memo) {
|
try {
|
Map<String, Object> params = new HashMap<String, Object>();
|
if (StringUtils.isNotBlank(name)) {
|
params.put("name", "%" + name + "%");
|
}
|
if (StringUtils.isNotBlank(memo)) {
|
params.put("memo", "%" + memo + "%");
|
}
|
return this.baseRoleDao.getBaseRoleTotalCount(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 BaseRole getBaseRole(String id) {
|
try {
|
BaseRole baseRole = this.baseRoleDao.getBaseRole(id);
|
List<BaseRolePage> baseRolePageList = this.baseRolePageService.getBaseRolePageList(id, null);
|
if (baseRolePageList != null && baseRolePageList.size() > 0) {
|
List<String> pageIdList = new ArrayList<String>();
|
for (BaseRolePage baseRolePage : baseRolePageList) {
|
pageIdList.add(baseRolePage.getPageId());
|
}
|
baseRole.setPageIdList(pageIdList);
|
}
|
|
return baseRole;
|
} 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 isBaseRoleExist(String name, String editId) {
|
try {
|
Map<String, Object> params = new HashMap<String, Object>();
|
if (StringUtils.isNotBlank(name)) {
|
params.put("name", name);
|
}
|
if (StringUtils.isNotBlank(editId)) {
|
params.put("editId", editId);
|
}
|
int count = this.baseRoleDao.getBaseRoleTotalCount(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 BaseRole insertBaseRole(BaseRole baseRole) {
|
try {
|
if (baseRole.getId() == null) {
|
baseRole.setId(IDUtils.uuid());
|
}
|
this.baseRoleDao.insertBaseRole(baseRole);
|
if (baseRole.getPageIdList() != null) {
|
BaseRolePage baseRolePage = null;
|
for (String pageId : baseRole.getPageIdList()) {
|
baseRolePage = new BaseRolePage();
|
baseRolePage.setId(IDUtils.uuid());
|
baseRolePage.setRoleId(baseRole.getId());
|
baseRolePage.setPageId(pageId);
|
this.baseRolePageService.insertBaseRolePage(baseRolePage);
|
}
|
}
|
|
return baseRole;
|
} 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 updateBaseRole(BaseRole baseRole) {
|
try {
|
int row = this.baseRoleDao.updateBaseRole(baseRole);
|
this.baseRolePageService.deleteBaseRolePage(baseRole.getId());
|
if (baseRole.getPageIdList() != null) {
|
BaseRolePage baseRolePage = null;
|
for (String pageId : baseRole.getPageIdList()) {
|
baseRolePage = new BaseRolePage();
|
baseRolePage.setId(IDUtils.uuid());
|
baseRolePage.setRoleId(baseRole.getId());
|
baseRolePage.setPageId(pageId);
|
this.baseRolePageService.insertBaseRolePage(baseRolePage);
|
}
|
}
|
|
if (row == 0) {
|
return false;
|
}
|
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 deleteBaseRole(List<BaseRole> baseRoleList) {
|
try {
|
if (baseRoleList == null || baseRoleList.size() == 0) {
|
return false;
|
}
|
|
List<String> ids = new ArrayList<String>();
|
for (BaseRole baseRole : baseRoleList) {
|
ids.add(baseRole.getId());
|
}
|
|
int row = this.baseRoleDao.deleteBaseRoles(ids);
|
if (row == 0) {
|
return false;
|
}
|
|
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);
|
}
|
}
|
|
@Override
|
public void updateUserMngPageBtn(BaseRole baseRole) {
|
|
if (baseRole.getBtnPermissions() != null && baseRole.getBtnPermissions().size() > 0) {
|
List<String> btnPermissions = baseRole.getBtnPermissions();
|
for (String btnPermission : btnPermissions) {
|
if (btnPermissions.indexOf(btnPermission) == 0) {
|
baseRole.setBtnPermission(btnPermission);
|
} else {
|
baseRole.setBtnPermission(baseRole.getBtnPermission() + "," + btnPermission);
|
}
|
}
|
}
|
|
//更新操作可见
|
baseRoleDao.updateBaseRoleInfo(baseRole);
|
|
|
}
|
|
@Override
|
public void updateUserMngPageUser(BaseRole baseRole) {
|
//更新人员可见
|
baseRoleDao.updateBaseRoleInfo(baseRole);
|
}
|
|
@Override
|
public BaseRole getRoleByUserId(String id) {
|
return baseRoleDao.getBaseRoleByUser(id);
|
}
|
}
|