增加多租户的功能

This commit is contained in:
季圣华
2019-06-27 18:01:08 +08:00
parent a6223e57db
commit 97111a9c82
16 changed files with 1632 additions and 45 deletions

View File

@@ -24,6 +24,10 @@ import java.lang.annotation.*;
* depotItem-90
* accountHead-95
* accountItem-100
* serialNumber-105
* organization-110
* orgaUserRel-115
* tenant-120
*/
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)

View File

@@ -0,0 +1,71 @@
package com.jsh.erp.service.tenant;
import com.jsh.erp.service.ICommonQuery;
import com.jsh.erp.service.user.UserResource;
import com.jsh.erp.service.user.UserService;
import com.jsh.erp.utils.Constants;
import com.jsh.erp.utils.QueryUtils;
import com.jsh.erp.utils.StringUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
@Service(value = "tenant_component")
@UserResource
public class TenantComponent implements ICommonQuery {
@Resource
private TenantService tenantService;
@Override
public Object selectOne(Long id) throws Exception {
return tenantService.getTenant(id);
}
@Override
public List<?> select(Map<String, String> map)throws Exception {
return getTenantList(map);
}
private List<?> getTenantList(Map<String, String> map)throws Exception {
String search = map.get(Constants.SEARCH);
String loginName = StringUtil.getInfo(search, "loginName");
return tenantService.select(loginName, QueryUtils.offset(map), QueryUtils.rows(map));
}
@Override
public Long counts(Map<String, String> map)throws Exception {
String search = map.get(Constants.SEARCH);
String loginName = StringUtil.getInfo(search, "loginName");
return tenantService.countTenant(loginName);
}
@Override
public int insert(String beanJson, HttpServletRequest request)throws Exception {
return tenantService.insertTenant(beanJson, request);
}
@Override
public int update(String beanJson, Long id)throws Exception {
return tenantService.updateTenant(beanJson, id);
}
@Override
public int delete(Long id)throws Exception {
return tenantService.deleteTenant(id);
}
@Override
public int batchDelete(String ids)throws Exception {
return tenantService.batchDeleteTenant(ids);
}
@Override
public int checkIsNameExist(Long id, String name)throws Exception {
return tenantService.checkIsNameExist(id, name);
}
}

View File

@@ -0,0 +1,15 @@
package com.jsh.erp.service.tenant;
import com.jsh.erp.service.ResourceInfo;
import java.lang.annotation.*;
/**
* @author jishenghua qq752718920 2019-6-27 22:56:56
*/
@ResourceInfo(value = "tenant", type = 120)
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TenantResource {
}

View File

@@ -0,0 +1,171 @@
package com.jsh.erp.service.tenant;
import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.constants.ExceptionConstants;
import com.jsh.erp.datasource.entities.*;
import com.jsh.erp.datasource.mappers.TenantMapper;
import com.jsh.erp.datasource.mappers.TenantMapperEx;
import com.jsh.erp.exception.BusinessRunTimeException;
import com.jsh.erp.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
@Service
public class TenantService {
private Logger logger = LoggerFactory.getLogger(TenantService.class);
@Resource
private TenantMapper tenantMapper;
@Resource
private TenantMapperEx tenantMapperEx;
public Tenant getTenant(long id)throws Exception {
Tenant result=null;
try{
result=tenantMapper.selectByPrimaryKey(id);
}catch(Exception e){
logger.error("异常码[{}],异常提示[{}],异常[{}]",
ExceptionConstants.DATA_READ_FAIL_CODE,ExceptionConstants.DATA_READ_FAIL_MSG,e);
throw new BusinessRunTimeException(ExceptionConstants.DATA_READ_FAIL_CODE,
ExceptionConstants.DATA_READ_FAIL_MSG);
}
return result;
}
public List<Tenant> getTenant()throws Exception {
TenantExample example = new TenantExample();
List<Tenant> list=null;
try{
list=tenantMapper.selectByExample(example);
}catch(Exception e){
logger.error("异常码[{}],异常提示[{}],异常[{}]",
ExceptionConstants.DATA_READ_FAIL_CODE,ExceptionConstants.DATA_READ_FAIL_MSG,e);
throw new BusinessRunTimeException(ExceptionConstants.DATA_READ_FAIL_CODE,
ExceptionConstants.DATA_READ_FAIL_MSG);
}
return list;
}
public List<Tenant> select(String loginName, int offset, int rows)throws Exception {
List<Tenant> list=null;
try{
list=tenantMapperEx.selectByConditionTenant(loginName, offset, rows);
}catch(Exception e){
logger.error("异常码[{}],异常提示[{}],异常[{}]",
ExceptionConstants.DATA_READ_FAIL_CODE,ExceptionConstants.DATA_READ_FAIL_MSG,e);
throw new BusinessRunTimeException(ExceptionConstants.DATA_READ_FAIL_CODE,
ExceptionConstants.DATA_READ_FAIL_MSG);
}
return list;
}
public Long countTenant(String loginName)throws Exception {
Long result=null;
try{
result=tenantMapperEx.countsByTenant(loginName);
}catch(Exception e){
logger.error("异常码[{}],异常提示[{}],异常[{}]",
ExceptionConstants.DATA_READ_FAIL_CODE,ExceptionConstants.DATA_READ_FAIL_MSG,e);
throw new BusinessRunTimeException(ExceptionConstants.DATA_READ_FAIL_CODE,
ExceptionConstants.DATA_READ_FAIL_MSG);
}
return result;
}
@Transactional(value = "transactionManager", rollbackFor = Exception.class)
public int insertTenant(String beanJson, HttpServletRequest request)throws Exception {
Tenant tenant = JSONObject.parseObject(beanJson, Tenant.class);
int result=0;
try{
tenant.setUserNumLimit(2); //默认用户限制数量
tenant.setBillsNumLimit(200); //默认单据限制数量
tenant.setCreateTime(new Date());
result=tenantMapper.insertSelective(tenant);
}catch(Exception e){
logger.error("异常码[{}],异常提示[{}],异常[{}]",
ExceptionConstants.DATA_WRITE_FAIL_CODE,ExceptionConstants.DATA_WRITE_FAIL_MSG,e);
throw new BusinessRunTimeException(ExceptionConstants.DATA_WRITE_FAIL_CODE,
ExceptionConstants.DATA_WRITE_FAIL_MSG);
}
return result;
}
@Transactional(value = "transactionManager", rollbackFor = Exception.class)
public int updateTenant(String beanJson, Long id)throws Exception {
Tenant tenant = JSONObject.parseObject(beanJson, Tenant.class);
int result=0;
try{
tenant.setId(id);
result=tenantMapper.updateByPrimaryKeySelective(tenant);
}catch(Exception e){
logger.error("异常码[{}],异常提示[{}],异常[{}]",
ExceptionConstants.DATA_WRITE_FAIL_CODE,ExceptionConstants.DATA_WRITE_FAIL_MSG,e);
throw new BusinessRunTimeException(ExceptionConstants.DATA_WRITE_FAIL_CODE,
ExceptionConstants.DATA_WRITE_FAIL_MSG);
}
return result;
}
@Transactional(value = "transactionManager", rollbackFor = Exception.class)
public int deleteTenant(Long id)throws Exception {
int result=0;
try{
result= tenantMapper.deleteByPrimaryKey(id);
}catch(Exception e){
logger.error("异常码[{}],异常提示[{}],异常[{}]",
ExceptionConstants.DATA_WRITE_FAIL_CODE,ExceptionConstants.DATA_WRITE_FAIL_MSG,e);
throw new BusinessRunTimeException(ExceptionConstants.DATA_WRITE_FAIL_CODE,
ExceptionConstants.DATA_WRITE_FAIL_MSG);
}
return result;
}
@Transactional(value = "transactionManager", rollbackFor = Exception.class)
public int batchDeleteTenant(String ids)throws Exception {
List<Long> idList = StringUtil.strToLongList(ids);
TenantExample example = new TenantExample();
example.createCriteria().andIdIn(idList);
int result=0;
try{
result= tenantMapper.deleteByExample(example);
}catch(Exception e){
logger.error("异常码[{}],异常提示[{}],异常[{}]",
ExceptionConstants.DATA_WRITE_FAIL_CODE,ExceptionConstants.DATA_WRITE_FAIL_MSG,e);
throw new BusinessRunTimeException(ExceptionConstants.DATA_WRITE_FAIL_CODE,
ExceptionConstants.DATA_WRITE_FAIL_MSG);
}
return result;
}
public int checkIsNameExist(Long id, String name)throws Exception {
TenantExample example = new TenantExample();
example.createCriteria().andIdEqualTo(id);
List<Tenant> list=null;
try{
list= tenantMapper.selectByExample(example);
}catch(Exception e){
logger.error("异常码[{}],异常提示[{}],异常[{}]",
ExceptionConstants.DATA_READ_FAIL_CODE,ExceptionConstants.DATA_READ_FAIL_MSG,e);
throw new BusinessRunTimeException(ExceptionConstants.DATA_READ_FAIL_CODE,
ExceptionConstants.DATA_READ_FAIL_MSG);
}
return list==null?0:list.size();
}
public Tenant getTenantByTenantId(long tenantId) {
Tenant tenant = new Tenant();
TenantExample example = new TenantExample();
example.createCriteria().andTenantIdEqualTo(tenantId);
List<Tenant> list = tenantMapper.selectByExample(example);
if(list.size()>0) {
tenant = list.get(0);
}
return tenant;
}
}

View File

@@ -15,6 +15,7 @@ import com.jsh.erp.datasource.vo.TreeNodeEx;
import com.jsh.erp.exception.BusinessRunTimeException;
import com.jsh.erp.service.log.LogService;
import com.jsh.erp.service.orgaUserRel.OrgaUserRelService;
import com.jsh.erp.service.tenant.TenantService;
import com.jsh.erp.service.userBusiness.UserBusinessService;
import com.jsh.erp.utils.ExceptionCodeConstants;
import com.jsh.erp.utils.JshException;
@@ -49,7 +50,8 @@ public class UserService {
private LogService logService;
@Resource
private UserService userService;
@Resource
private TenantService tenantService;
@Resource
private UserBusinessService userBusinessService;
@@ -416,7 +418,7 @@ public class UserService {
}
@Transactional(value = "transactionManager", rollbackFor = Exception.class)
public UserEx registerUser(UserEx ue, Integer manageRoleId) throws Exception{
public UserEx registerUser(UserEx ue, Integer manageRoleId, HttpServletRequest request) throws Exception{
/**
* create by: qiankunpingtai
* create time: 2019/4/9 18:00
@@ -456,6 +458,13 @@ public class UserService {
ubArr.add(manageRoleId);
ubObj.put("value", ubArr.toString());
userBusinessService.insertUserBusiness(ubObj.toString(), ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
//创建租户信息
JSONObject tenantObj = new JSONObject();
tenantObj.put("tenantId", ue.getId());
tenantObj.put("loginName",ue.getLoginame());
String param = tenantObj.toJSONString();
tenantService.insertTenant(param, request);
logger.info("===============创建租户信息完成===============");
if (result > 0) {
return ue;
}