package com.qxueyou.scc.user.service.impl;
|
|
import java.util.List;
|
import java.util.UUID;
|
|
import com.qxueyou.scc.base.dao.CommonDAO;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import com.qxueyou.scc.admin.classes.model.ClsClass;
|
import com.qxueyou.scc.base.model.Result;
|
import com.qxueyou.scc.base.model.UserInfoWrapper;
|
import com.qxueyou.scc.base.service.impl.CommonAppService;
|
import com.qxueyou.scc.base.util.ClientUtils;
|
import com.qxueyou.scc.base.util.CollectionUtils;
|
import com.qxueyou.scc.base.util.TraceUtils;
|
import com.qxueyou.scc.org.model.Organization;
|
import com.qxueyou.scc.org.service.IOrganizationService;
|
import com.qxueyou.scc.sys.model.CacheParamters;
|
import com.qxueyou.scc.teach.student.model.StuStudent;
|
import com.qxueyou.scc.user.model.User;
|
import com.qxueyou.scc.user.model.UserReRoleUser;
|
import com.qxueyou.scc.user.service.IUserService;
|
import org.springframework.util.DigestUtils;
|
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpSession;
|
|
/**
|
* 用户管理服务
|
*
|
* @author 德虎
|
*/
|
@Service
|
public class UserService extends CommonAppService implements IUserService {
|
|
@Autowired
|
IOrganizationService organizationService;
|
|
@Autowired
|
private CommonDAO commonDAO;
|
|
/**
|
* 初始化用户wrapper
|
*/
|
@Override
|
public UserInfoWrapper initUserInfoWrapper(CacheParamters param, boolean needMenus) {
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
HttpSession session = request.getSession();
|
UserInfoWrapper wrapper = new UserInfoWrapper();
|
User user = read(User.class, param.getUserId());
|
// System.out.println("用户信息"+user);
|
// TODO:这里需要对应的获取组织信息
|
Organization org = read(Organization.class, user.getOrganizationId());
|
// System.out.println("组织信息"+user.getOrganizationId());
|
|
//TODO 设置user,添加UUNUMBER
|
user.setEmail(param.getCustomRoleValue());
|
user.setAge(1);
|
//TODO 设置user,添加UURENUMBER
|
user.setImei(param.getCustomOrgId());
|
//查询班级信息
|
// System.out.println(user.getUserId());
|
// System.out.println(user.getOrganizationId());
|
// System.out.println(StuStudent.STATUS_ACTIVE);
|
System.out.println(user.getOrganizationId());
|
System.out.println(user.getUserId());
|
|
ClsClass objClsClass = this.findUnique("select c from StuStudent t,"
|
+ " ClsClass c where c.classId = t.classId "
|
+ " and c.orgId = ? and t.userId = ? and t.status = ? order by t.createTime desc", CollectionUtils.newList(user.getOrganizationId(), user.getUserId(), StuStudent.STATUS_ACTIVE), ClsClass.class);
|
wrapper.setUser(user);
|
wrapper.setOrg(org);
|
wrapper.setClass(objClsClass);
|
wrapper.setLoginInfo(param.getCacheIpFlag(), param.getIp(), param.getPlatForm());
|
session.setAttribute(UserInfoWrapper.INF_USER_ID,user.getUserId());
|
session.setAttribute(UserInfoWrapper.INF_USER_NAME,user.getName());
|
if(objClsClass!=null){
|
session.setAttribute(UserInfoWrapper.INF_CLASS_ID,objClsClass.getClassId());
|
session.setAttribute(UserInfoWrapper.INF_CLASS_NAME,objClsClass.getName());
|
}
|
return wrapper;
|
}
|
|
@Override
|
public User insertUser(String name, String account, String mobilePhone, String password, Boolean sex, String orgId) {
|
return newUser(name, account, mobilePhone, password, sex, orgId);
|
}
|
|
private User newUser(String name, String account, String mobilePhone, String password, Boolean sex, String orgId) {
|
//随机字符串
|
String salt= UUID.randomUUID().toString();
|
User user = new User();
|
TraceUtils.setCreateTrace(user);
|
user.setAccount(account);
|
user.setName(name);
|
//盐值
|
user.setSalt(salt);
|
//对密码进行加密处理
|
String newPassword= DigestUtils.md5DigestAsHex((salt+password).getBytes());
|
user.setPassword(newPassword);
|
user.setMobilePhone(mobilePhone);
|
user.setSex(sex);
|
user.setOrganizationId(orgId);
|
save(user);
|
|
return user;
|
}
|
|
@Override
|
public User getUserByAccount(String account, String orgId) {
|
String hql = "from User where account=? and deleteFlag is false and organizationId = ?";
|
return findUnique(hql, CollectionUtils.newList(account, orgId), User.class);
|
}
|
|
@Override
|
public Result addRole(String userId, String roleId) {
|
|
String hql = "from UserReRoleUser where userId=? and roleId=? and deleteFlag is false";
|
UserReRoleUser re = findUnique(hql, CollectionUtils.newList(userId, roleId), UserReRoleUser.class);
|
|
if (re == null) {
|
re = new UserReRoleUser();
|
re.setDeleteFlag(false);
|
re.setRoleId(roleId);
|
re.setUserId(userId);
|
save(re);
|
}
|
|
return new Result(true);
|
}
|
|
@Override
|
public Result updatePassword(String userId, String usedPass, String pass) {
|
String hql = null;
|
|
List<Object> params = null;
|
|
User user = read(User.class, userId);
|
//随机字符串
|
String salt= UUID.randomUUID().toString();
|
|
String salts = user.getSalt();
|
//盐值
|
user.setSalt(salt);
|
//对密码进行加密处理
|
String newPassword= DigestUtils.md5DigestAsHex((salt+pass).getBytes());
|
String newPasswords= DigestUtils.md5DigestAsHex((salts+usedPass).getBytes());
|
if (StringUtils.isNotBlank(usedPass) && StringUtils.isNotBlank(pass)) {
|
if (!user.getPassword().equals(newPasswords)) {
|
return new Result(false, "输入的旧密码错误");
|
}
|
user.setPassword(newPassword);
|
}
|
TraceUtils.setUpdateTrace(user);
|
save(user);
|
|
return new Result(true);
|
}
|
|
/**
|
* 用户设置头像
|
*
|
* @param headPath
|
* @return
|
*/
|
public Result updateUserHead(String userId, String headPath) {
|
if(StringUtils.isEmpty(headPath)) {
|
return new Result(false, "头像地址为空");
|
}
|
User user = read(User.class, userId);
|
|
user.setImgPath(headPath);
|
TraceUtils.setUpdateTrace(user);
|
save(user);
|
|
return new Result(true);
|
}
|
|
/**
|
* 用户设置手机号
|
*
|
* @param headPath
|
* @return
|
*/
|
public Result updateUserInfo(String userId, String mobilePhone) {
|
|
User user = read(User.class, userId);
|
|
user.setMobilePhone(mobilePhone);
|
TraceUtils.setUpdateTrace(user);
|
save(user);
|
|
return new Result(true);
|
}
|
|
@Override
|
public Result updatePassword(String userId, String password) {
|
User user = read(User.class, userId);
|
|
//随机字符串
|
String salt= UUID.randomUUID().toString();
|
//盐值
|
user.setSalt(salt);
|
//对密码进行加密处理
|
String newPassword= DigestUtils.md5DigestAsHex((salt+password).getBytes());
|
TraceUtils.setUpdateTrace(user);
|
user.setPassword(newPassword);
|
|
save(user);
|
|
return new Result(true);
|
}
|
|
@Override
|
public boolean isAdmin() {
|
String hqlRole = "select min(u.type) from UserRole u , UserReRoleUser r where u.deleteFlag is false and r.deleteFlag is false and r.userId =:userId and u.roleId =r.roleId";
|
String maxRoleTypeStr = findUniqueByHql(hqlRole,
|
CollectionUtils.newObjectMap("userId", ClientUtils.getUserId()));
|
Integer type = Integer.valueOf(StringUtils.isNotBlank(maxRoleTypeStr) ? maxRoleTypeStr : "0");
|
if (type == 1) {
|
return true;
|
}
|
return false;
|
}
|
|
@Override
|
public boolean isTeacher() {
|
String hqlRole = "select min(u.type) from UserRole u , UserReRoleUser r where u.deleteFlag is false and r.deleteFlag is false and r.userId =:userId and u.roleId =r.roleId";
|
String maxRoleTypeStr = findUniqueByHql(hqlRole,
|
CollectionUtils.newObjectMap("userId", ClientUtils.getUserId()));
|
Integer type = Integer.valueOf(StringUtils.isNotBlank(maxRoleTypeStr) ? maxRoleTypeStr : "0");
|
if (type == 2) {
|
return true;
|
}
|
return false;
|
}
|
|
@Override
|
public boolean doBatchUpdateUser(List<User> lstUser) {
|
return this.saveOrUpdateAll(lstUser).isSuccess();
|
}
|
|
@Override
|
public Result updateUser(String userId, String name, String account, String mobilePhone, String password,
|
Boolean sex) {
|
User dbUser = read(User.class, userId);
|
String salt = dbUser.getSalt();
|
if(salt == null){
|
salt= UUID.randomUUID().toString();
|
dbUser.setSalt(salt);
|
salt = dbUser.getSalt();
|
}
|
String newPassword= DigestUtils.md5DigestAsHex((salt+password).getBytes());
|
if (dbUser.getAccount().equals(account)) {
|
dbUser.setName(name);
|
dbUser.setMobilePhone(mobilePhone);
|
dbUser.setPassword(newPassword);
|
TraceUtils.setCreateTrace(dbUser);
|
save(dbUser);
|
return new Result(true, "success");
|
}
|
|
User user = getUserByAccount(account, ClientUtils.getOrgId());
|
if (null != user) {
|
return new Result(false, "用户账号已经存在");
|
}
|
dbUser.setAccount(account);
|
dbUser.setName(name);
|
dbUser.setMobilePhone(mobilePhone);
|
dbUser.setPassword(newPassword);
|
TraceUtils.setCreateTrace(dbUser);
|
save(dbUser);
|
return new Result(true, "success");
|
}
|
|
@Override
|
public boolean exists(String account) {
|
return findCount("from User where account = ?", CollectionUtils.newList(account)) > 0;
|
}
|
|
@Override
|
public Result countUser() {
|
String hql = "from count User where sex=? and deleteFlag is false";
|
return new Result(true,findCount(hql,CollectionUtils.newList(1)));
|
}
|
|
}
|