com.mg.common.user.service.UserServiceImpl Maven / Gradle / Ivy
package com.mg.common.user.service;
import com.alibaba.fastjson.JSONObject;
import com.mg.common.entity.QUserEntity;
import com.mg.common.entity.UserEntity;
import com.mg.common.entity.UserRuleEntity;
import com.mg.common.metadata.service.MetaDataExpressService;
import com.mg.common.metadata.service.MetaDataService;
import com.mg.common.utils.MD5;
import com.mg.framework.entity.metadata.MExpressGroupEntity;
import com.mg.framework.entity.metadata.MExpressionEntity;
import com.mg.framework.entity.metadata.MirrorPropertyEntity;
import com.mg.framework.utils.StatusEnum;
import com.mg.framework.utils.UserHolder;
import com.mysema.query.jpa.impl.JPAQuery;
import com.mysema.query.types.expr.BooleanExpression;
import com.mg.common.metadata.service.CustomFormService;
import com.mg.common.metadata.service.MetaDataQueryService;
import com.mg.common.metadata.util.MetaDataUtils;
import com.mg.common.user.dao.UserDao;
import com.mg.framework.entity.metadata.MObjectEntity;
import com.mg.framework.entity.vo.PageTableVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@Service
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {
public Logger logger = LoggerFactory.getLogger(this.getClass());
@PersistenceContext
private EntityManager entityManager;
@Autowired
private UserDao userDao;
@Autowired
private UserRuleService userRuleService;
@Autowired
MetaDataService metaDataService;
@Autowired
MetaDataQueryService metaDataQueryService;
@Autowired
CustomFormService customFormService;
@Autowired
MetaDataExpressService metaDataExpressionService;
/**
* 根据用户名,获取用户信息
* @param loginName
* @return
*/
public UserEntity getUser(String loginName) {
List names = new ArrayList<>();
names.add(loginName);
List userEntityList= getUsersByNames(names);
if(userEntityList!=null && userEntityList.size()>0){
return userEntityList.get(0);
}
return null;
}
/**
* 根据用户名和密码,获取用户信息
* @param loginName
* 用户名
* @param password
* 密码
* @return
* 返回验证通过后返回User对象,若无返回null
*/
public UserEntity getUser(String loginName, String password) {
JPAQuery query = getQuery();
QUserEntity qUserEntity = QUserEntity.userEntity;
query.from(qUserEntity);
query.where(
qUserEntity.loginName.eq(loginName),
qUserEntity.password.eq(password)
);
List users = query.list(qUserEntity);
if (users == null || users.isEmpty()) {
return null;
}
return users.get(0);
}
/**
* 根据用户id,初始化登录密码
* @param userId
* @return
*/
@Transactional
public UserEntity saveInitUserPassWord(String userId) {
UserEntity userEntity = userDao.findOne(userId);
userRuleService.initUser(userEntity, true);
userDao.save(userEntity);
return userEntity;
}
@Transactional
public void delete(String userId) {
UserEntity userEntity = userDao.findOne(userId);
userEntity.getRoles().clear();
userDao.delete(userEntity);
}
public Long findCount(PageTableVO pageTableVO) {
QUserEntity entity = QUserEntity.userEntity;
JSONObject paramObject = (JSONObject)pageTableVO.getExtendData();
UserEntity userEntity = JSONObject.toJavaObject(paramObject, UserEntity.class);
BooleanExpression ex = entity.status.eq(StatusEnum.STATUS_VALID);
if(StringUtils.isNotBlank(userEntity.getLoginName())){
ex = ex.and(entity.loginName.like("%" + userEntity.getLoginName() + "%"));
}else if(StringUtils.isNotBlank(userEntity.getName())){
ex = ex.and(entity.name.like("%"+userEntity.getName()+"%"));
}
JPAQuery query = new JPAQuery(entityManager);
Long totalNum = query.from(entity).where(
ex
).count();
return totalNum;
}
public PageTableVO findPageList(PageTableVO pageTableVO) {
QUserEntity entity = QUserEntity.userEntity;
Integer limit = pageTableVO.getPageSize();
Integer offset = pageTableVO.getOffset();
if(limit==null || limit <=0){
limit = 15;
}
JSONObject paramObject = (JSONObject)pageTableVO.getExtendData();
UserEntity userEntity = JSONObject.toJavaObject(paramObject,UserEntity.class);
BooleanExpression ex = entity.status.eq(StatusEnum.STATUS_VALID);
if(StringUtils.isNotBlank(userEntity.getLoginName())){
ex = ex.and(entity.loginName.like("%" + userEntity.getLoginName() + "%"));
}else if(StringUtils.isNotBlank(userEntity.getName())){
ex = ex.and(entity.name.like("%"+userEntity.getName()+"%"));
}
JPAQuery query = new JPAQuery(entityManager);
List list = query.from(entity)
.where(
ex
).offset(offset).limit(limit)
.list(entity);
Long totalCount = findCount(pageTableVO);
PageTableVO vo = new PageTableVO();
vo.setRowData(list);
vo.setTotalCount(totalCount);
vo.setPageNo(pageTableVO.getPageNo());
vo.setPageSize(pageTableVO.getPageSize());
return vo;
}
/**
* 插入用户信息
* @param userNames
* 用户名称组,每n个一次性提交
* @return
* n条数据倒入成功
*/
@Transactional
public List insertUsers(List userNames) {
List users = new ArrayList<>();
for (String userName : userNames) {
UserEntity user = getUser(userName);
if (user != null) { //已经在系统中
users.add(user);
continue;
}
users.add(insertUser(userName, null));
}
return users;
}
public List getUsers(List userNames) {
QUserEntity user = QUserEntity.userEntity;
JPAQuery query = new JPAQuery(entityManager);
return query.from(user)
.where(
user.name.in(userNames)
).list(user);
}
/**
* 根据用户主键获取用户对象 by huan
* @param id
* 用户ID
* @return
* 用户信息 ID==NULL 返回NULL
*/
public UserEntity getUserById(String id) {
if (StringUtils.isBlank(id)) {
return null;
}
return userDao.findOne(id);
}
/**
* 插入用户信息
* @param userName
* 单个用户名称
* @return
* 插入user实体类
*/
@Transactional
public UserEntity insertUser(String userName, String password) {
UserEntity user = getUser(userName);
if (user != null) {
return user;
}
UserEntity userEntity = new UserEntity(userName, StringUtils.isBlank(password) ? UserEntity.DEFAULT_PASSWORD : password);
entityManager.persist(userEntity);
return userEntity;
}
/**
* 获取某一用户特殊权限上的别名, 若不存在该权限, 则返回用户名称 by huan
* @param userId
* 用户ID
* @param flag
* 权限标示
* @return
* 用户特殊权限所对应别名
*/
public String getUserMarkName(String userId, int flag) {
if(StringUtils.isBlank(userId)){
return "";
}
String[] userIds = userId.split(";");
List list = getUsersByIds(Arrays.asList(userIds));
StringBuilder sb = new StringBuilder();
for (int i=0;i> getBussinessTemplate() {
return null;
}
@Override
public void insertBussinessVariables_(List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy