You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
6.4 KiB
172 lines
6.4 KiB
package com.community.pocket.repository.android;
|
|
|
|
import com.community.pocket.entity.po.android.MyInfo;
|
|
import com.community.pocket.entity.vo.android.*;
|
|
import com.community.pocket.repository.BaseDao;
|
|
import com.mongodb.client.result.UpdateResult;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.data.domain.Sort;
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
import org.springframework.data.mongodb.core.query.Update;
|
|
import org.springframework.stereotype.Repository;
|
|
import org.springframework.util.DigestUtils;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.regex.Pattern;
|
|
|
|
//用户数据接口
|
|
@Repository
|
|
public class UserDao extends BaseDao<MyInfo> {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(UserDao.class);
|
|
|
|
@Value("${register.credit-score}")
|
|
private int creditScore;
|
|
|
|
//检查用户名是否已注册
|
|
public boolean hasUser(String username){
|
|
if(!StringUtils.isEmpty(username)){
|
|
return mongoTemplate.exists(new Query().addCriteria(Criteria.where("username").is(username)),entityClass());
|
|
}else{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//检查手机是否已绑定
|
|
public boolean hasMobie(String mobie) {
|
|
if (!StringUtils.isEmpty(mobie)) {
|
|
return mongoTemplate.exists(new Query().addCriteria(Criteria.where("mobie").is(mobie)),entityClass());
|
|
}else{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//检查邮箱是否已绑定
|
|
public boolean hasEmail(String email){
|
|
if(!StringUtils.isEmpty(email)){
|
|
return mongoTemplate.exists(new Query().addCriteria(Criteria.where("email").is(email)),entityClass());
|
|
}else{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//检查用户和邮箱是否对应
|
|
public boolean hasUseWithEmail(String username, String email){
|
|
if(!StringUtils.isEmpty(username)&&!StringUtils.isEmpty(email)){
|
|
return mongoTemplate.exists(new Query().addCriteria(Criteria.where("username").is(username).and("email").is(email)),entityClass());
|
|
}else{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
//重置密码
|
|
public boolean resetPwd(String username,String password){
|
|
if(!StringUtils.isEmpty(username)&&!StringUtils.isEmpty(password)){
|
|
String md5Password= DigestUtils.md5DigestAsHex(password.getBytes());
|
|
UpdateResult result=mongoTemplate.updateFirst(new Query().addCriteria(Criteria.where("username").is(username)), Update.update("password",md5Password),entityClass());
|
|
return result.wasAcknowledged();
|
|
}else{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//登陆用户
|
|
public boolean login(String username, String password) {
|
|
if(!StringUtils.isEmpty(username)&&!StringUtils.isEmpty(password)){
|
|
String md5Password= DigestUtils.md5DigestAsHex(password.getBytes());
|
|
return mongoTemplate.exists(new Query(Criteria.where("username").is(username).and("password").is(md5Password)),entityClass());
|
|
}else{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
//注册用户
|
|
public void save(UserRegister userRegister){
|
|
MyInfo myInfo=new MyInfo();
|
|
myInfo.setUsername(userRegister.getUsername());
|
|
myInfo.setPassword(DigestUtils.md5DigestAsHex(userRegister.getPassword().getBytes()));
|
|
myInfo.setMobie(userRegister.getMobie());
|
|
myInfo.setEmail(userRegister.getEmail());
|
|
myInfo.setCreditScore(creditScore);
|
|
myInfo.setRecentPosts(0);
|
|
myInfo.setRecentVisitors(0);
|
|
myInfo.setScoreHistory(new ArrayList<>());
|
|
save(myInfo);
|
|
}
|
|
|
|
//获取活跃用户
|
|
public List<UserHot> rankUser(int num){
|
|
List<UserHot> userHots=new ArrayList<>();
|
|
List<MyInfo> myInfos=mongoTemplate.find(new Query().with(Sort.by("creditScore").descending()).limit(num),entityClass());
|
|
for(MyInfo info:myInfos){
|
|
UserHot userHot=new UserHot();
|
|
userHot.setUserName(info.getUsername());
|
|
userHot.setCreditScore(info.getCreditScore());
|
|
userHots.add(userHot);
|
|
}
|
|
return userHots;
|
|
}
|
|
|
|
//根据关键字匹配用户
|
|
public List<String> queryUser(QueryUserForm userForm){
|
|
List<String> userNames=new ArrayList<>();
|
|
List<MyInfo> myInfos=mongoTemplate.find(new Query(new Criteria().andOperator(
|
|
Criteria.where("username").regex(Pattern.compile(".*"+userForm.getName()+".*")),
|
|
Criteria.where("username").ne(userForm.getUsername())))
|
|
,entityClass());
|
|
for(MyInfo info:myInfos){
|
|
userNames.add(info.getUsername());
|
|
}
|
|
return userNames;
|
|
}
|
|
|
|
//获取个人信息
|
|
public Info queryUser(String username) {
|
|
MyInfo myInfo = mongoTemplate.findOne(new Query(Criteria.where("username").is(username)), entityClass());
|
|
if (myInfo != null) {
|
|
Info info = new Info();
|
|
info.setUsername(myInfo.getUsername());
|
|
info.setCreditScore(myInfo.getCreditScore());
|
|
info.setEmail(myInfo.getEmail());
|
|
info.setHeadImg(myInfo.getHeadImg());
|
|
info.setMobie(myInfo.getMobie());
|
|
info.setRecentPosts(myInfo.getRecentPosts());
|
|
info.setRecentVisitors(myInfo.getRecentVisitors());
|
|
info.setScoreHistory(myInfo.getScoreHistory());
|
|
return info;
|
|
} else {
|
|
return null;
|
|
}
|
|
|
|
}
|
|
|
|
//修改密码
|
|
public boolean modifyPwd(ModifyPwdForm modifyPwdForm) {
|
|
String md5Password= DigestUtils.md5DigestAsHex(modifyPwdForm.getOldPassword().getBytes());
|
|
String newMd5password=DigestUtils.md5DigestAsHex(modifyPwdForm.getNewPassword().getBytes());
|
|
return mongoTemplate.updateFirst(new Query(Criteria.where("username").is(modifyPwdForm.getUsername()).and("password").is(md5Password)),Update.update("password",newMd5password),entityClass()).wasAcknowledged();
|
|
}
|
|
|
|
@Override
|
|
public MyInfo save(MyInfo myInfo) {
|
|
try {
|
|
mongoTemplate.save(myInfo);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
LOG.error(e.toString());
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public Class<MyInfo> entityClass() {
|
|
return MyInfo.class;
|
|
}
|
|
}
|
|
|