优化账户和租户两个模块的接口

This commit is contained in:
jishenghua
2025-02-21 01:28:44 +08:00
parent 20b100b5e2
commit 53004b1fa5
19 changed files with 263 additions and 392 deletions

View File

@@ -1,78 +0,0 @@
package com.jsh.erp.service.tenant;
import com.alibaba.fastjson.JSONObject;
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")
@TenantResource
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");
String type = StringUtil.getInfo(search, "type");
String enabled = StringUtil.getInfo(search, "enabled");
String remark = StringUtil.getInfo(search, "remark");
return tenantService.select(loginName, type, enabled, remark, 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");
String type = StringUtil.getInfo(search, "type");
String enabled = StringUtil.getInfo(search, "enabled");
String remark = StringUtil.getInfo(search, "remark");
return tenantService.countTenant(loginName, type, enabled, remark);
}
@Override
public int insert(JSONObject obj, HttpServletRequest request)throws Exception {
return tenantService.insertTenant(obj, request);
}
@Override
public int update(JSONObject obj, HttpServletRequest request)throws Exception {
return tenantService.updateTenant(obj, request);
}
@Override
public int delete(Long id, HttpServletRequest request)throws Exception {
return tenantService.deleteTenant(id, request);
}
@Override
public int deleteBatch(String ids, HttpServletRequest request)throws Exception {
return tenantService.batchDeleteTenant(ids, request);
}
@Override
public int checkIsNameExist(Long id, String name)throws Exception {
return tenantService.checkIsNameExist(id, name);
}
}

View File

@@ -1,15 +0,0 @@
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")
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TenantResource {
}

View File

@@ -1,223 +0,0 @@
package com.jsh.erp.service.tenant;
import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.constants.BusinessConstants;
import com.jsh.erp.datasource.entities.Tenant;
import com.jsh.erp.datasource.entities.TenantEx;
import com.jsh.erp.datasource.entities.TenantExample;
import com.jsh.erp.datasource.entities.UserEx;
import com.jsh.erp.datasource.mappers.TenantMapper;
import com.jsh.erp.datasource.mappers.TenantMapperEx;
import com.jsh.erp.datasource.mappers.UserBusinessMapperEx;
import com.jsh.erp.datasource.mappers.UserMapperEx;
import com.jsh.erp.exception.JshException;
import com.jsh.erp.service.log.LogService;
import com.jsh.erp.service.user.UserService;
import com.jsh.erp.utils.StringUtil;
import com.jsh.erp.utils.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
@Service
public class TenantService {
private Logger logger = LoggerFactory.getLogger(TenantService.class);
@Resource
private TenantMapper tenantMapper;
@Resource
private TenantMapperEx tenantMapperEx;
@Resource
private UserMapperEx userMapperEx;
@Resource
private UserBusinessMapperEx userBusinessMapperEx;
@Resource
private UserService userService;
@Resource
private LogService logService;
@Value("${manage.roleId}")
private Integer manageRoleId;
public Tenant getTenant(long id)throws Exception {
Tenant result=null;
try{
result=tenantMapper.selectByPrimaryKey(id);
}catch(Exception e){
JshException.readFail(logger, e);
}
return result;
}
public List<Tenant> getTenant()throws Exception {
TenantExample example = new TenantExample();
example.createCriteria().andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
List<Tenant> list=null;
try{
list=tenantMapper.selectByExample(example);
}catch(Exception e){
JshException.readFail(logger, e);
}
return list;
}
public List<TenantEx> select(String loginName, String type, String enabled, String remark, int offset, int rows)throws Exception {
List<TenantEx> list= new ArrayList<>();
try{
if(BusinessConstants.DEFAULT_MANAGER.equals(userService.getCurrentUser().getLoginName())) {
list = tenantMapperEx.selectByConditionTenant(loginName, type, enabled, remark, offset, rows);
if (null != list) {
for (TenantEx tenantEx : list) {
tenantEx.setCreateTimeStr(Tools.getCenternTime(tenantEx.getCreateTime()));
tenantEx.setExpireTimeStr(Tools.getCenternTime(tenantEx.getExpireTime()));
}
}
}
}catch(Exception e){
JshException.readFail(logger, e);
}
return list;
}
public Long countTenant(String loginName, String type, String enabled, String remark)throws Exception {
Long result=null;
try{
if(BusinessConstants.DEFAULT_MANAGER.equals(userService.getCurrentUser().getLoginName())) {
result = tenantMapperEx.countsByTenant(loginName, type, enabled, remark);
}
}catch(Exception e){
JshException.readFail(logger, e);
}
return result;
}
@Transactional(value = "transactionManager", rollbackFor = Exception.class)
public int insertTenant(JSONObject obj, HttpServletRequest request)throws Exception {
UserEx ue = JSONObject.parseObject(obj.toJSONString(), UserEx.class);
int result = 0;
try{
ue.setUsername(ue.getLoginName());
userService.checkLoginName(ue); //检查登录名
userService.registerUser(ue,manageRoleId,request);
result = 1;
} catch(Exception e){
JshException.writeFail(logger, e);
}
return result;
}
@Transactional(value = "transactionManager", rollbackFor = Exception.class)
public int updateTenant(JSONObject obj, HttpServletRequest request)throws Exception {
Tenant tenant = JSONObject.parseObject(obj.toJSONString(), Tenant.class);
int result=0;
try{
if(BusinessConstants.DEFAULT_MANAGER.equals(userService.getCurrentUser().getLoginName())) {
//如果租户下的用户限制数量为1则将该租户之外的用户全部禁用
if (1 == tenant.getUserNumLimit()) {
userMapperEx.disableUserByLimit(tenant.getTenantId());
}
result = tenantMapper.updateByPrimaryKeySelective(tenant);
//更新租户对应的角色
if(obj.get("roleId")!=null) {
String ubValue = "[" + obj.getString("roleId") + "]";
userBusinessMapperEx.updateValueByTypeAndKeyId("UserRole", tenant.getTenantId().toString(), ubValue);
}
}
}catch(Exception e){
JshException.writeFail(logger, e);
}
return result;
}
@Transactional(value = "transactionManager", rollbackFor = Exception.class)
public int deleteTenant(Long id, HttpServletRequest request)throws Exception {
int result=0;
try{
if(BusinessConstants.DEFAULT_MANAGER.equals(userService.getCurrentUser().getLoginName())) {
result = tenantMapper.deleteByPrimaryKey(id);
}
}catch(Exception e){
JshException.writeFail(logger, e);
}
return result;
}
@Transactional(value = "transactionManager", rollbackFor = Exception.class)
public int batchDeleteTenant(String ids, HttpServletRequest request)throws Exception {
List<Long> idList = StringUtil.strToLongList(ids);
TenantExample example = new TenantExample();
example.createCriteria().andIdIn(idList);
int result=0;
try{
if(BusinessConstants.DEFAULT_MANAGER.equals(userService.getCurrentUser().getLoginName())) {
result = tenantMapper.deleteByExample(example);
}
}catch(Exception e){
JshException.writeFail(logger, e);
}
return result;
}
public int checkIsNameExist(Long id, String name)throws Exception {
TenantExample example = new TenantExample();
example.createCriteria().andIdNotEqualTo(id).andLoginNameEqualTo(name).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
List<Tenant> list=null;
try{
list= tenantMapper.selectByExample(example);
}catch(Exception e){
JshException.readFail(logger, e);
}
return list==null?0:list.size();
}
public Tenant getTenantByTenantId(long tenantId) {
Tenant tenant = new Tenant();
TenantExample example = new TenantExample();
example.createCriteria().andTenantIdEqualTo(tenantId).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
List<Tenant> list = tenantMapper.selectByExample(example);
if(list.size()>0) {
tenant = list.get(0);
}
return tenant;
}
public int batchSetStatus(Boolean status, String ids)throws Exception {
int result=0;
try{
if(BusinessConstants.DEFAULT_MANAGER.equals(userService.getCurrentUser().getLoginName())) {
String statusStr = "";
if (status) {
statusStr = "批量启用";
} else {
statusStr = "批量禁用";
}
logService.insertLog("用户",
new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(ids).append("-").append(statusStr).toString(),
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
List<Long> idList = StringUtil.strToLongList(ids);
Tenant tenant = new Tenant();
tenant.setEnabled(status);
TenantExample example = new TenantExample();
example.createCriteria().andIdIn(idList);
result = tenantMapper.updateByExampleSelective(tenant, example);
}
}catch(Exception e){
JshException.writeFail(logger, e);
}
return result;
}
}