package
com.dao
import java
.util.List
import java
.util.Set
import org
.springframework.beans.factory.annotation.Autowired
import org
.springframework.data.mongodb.core.MongoTemplate
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.data.redis.core.ListOperations
import org
.springframework.data.redis.core.RedisTemplate
import org
.springframework.data.redis.core.ZSetOperations
import org
.springframework.data.redis.core.ZSetOperations.TypedTuple
import org
.springframework.scheduling.annotation.Scheduled
import org
.springframework.stereotype.Component
import
com.dto.Lesson
import
com.dto.PageUtils
@Component
public class LessonDao {
@Autowired
private MongoTemplate mongo
@Autowired
private RedisTemplate<String, String> redis
public List<String> toAdd() {
// TODO Auto-generated method stub
ListOperations<String, String> opsForList = redis
.opsForList()
// opsForList
.leftPush(
"lessontype",
"摄影")
// opsForList
.leftPush(
"lessontype",
"书法")
// opsForList
.leftPush(
"lessontype",
"棋牌")
// opsForList
.leftPush(
"lessontype",
"传统文化")
List<String> range = opsForList
.range(
"lessontype",
0, -
1)
return range
}
public void
add(Lesson lesson) {
// TODO Auto-generated method stub
mongo
.insert(lesson)
}
public List<Lesson> findList(PageUtils pu, String mohu) {
// TODO Auto-generated method stub
List<Lesson> find = mongo
.find(new Query(Criteria
.where(
"lessonname")
.regex(mohu))
.skip(pu
.getStartIndex())
.limit(pu
.getTotalPage()), Lesson
.class)
return find
}
public Lesson getObj(String id) {
// TODO Auto-generated method stub
Lesson lesson = mongo
.findOne(new Query(Criteria
.where(
"id")
.is(id)), Lesson
.class)
return lesson
}
public void update(Lesson lesson) {
// TODO Auto-generated method stub
Update update = new Update()
update
.set(
"lessontype", lesson
.getLessontype())
update
.set(
"lessonname", lesson
.getLessonname())
update
.set(
"teacher", lesson
.getTeacher())
update
.set(
"content", lesson
.getContent())
update
.set(
"starttime", lesson
.getStarttime())
update
.set(
"endtime", lesson
.getEndtime())
mongo
.updateFirst(new Query(Criteria
.where(
"id")
.is(lesson
.getId())), update, Lesson
.class)
}
public void del(String id) {
// TODO Auto-generated method stub
mongo
.remove(new Query(Criteria
.where(
"id")
.is(id)), Lesson
.class)
}
public void score(String id) {
// TODO Auto-generated method stub
ZSetOperations<String, String> opsForZSet = redis
.opsForZSet()
opsForZSet
.incrementScore(
"lesson", id,
1)
}
@Scheduled(cron=
"0/5 * * * * ?")
public void tt(){
ZSetOperations<String, String> opsForZSet = redis
.opsForZSet()
Set<TypedTuple<String>> rangeWithScores = opsForZSet
.rangeWithScores(
"lesson",
0, -
1)
for (TypedTuple<String> typedTuple : rangeWithScores) {
String value = typedTuple
.getValue()
Double score = typedTuple
.getScore()
Lesson lesson = mongo
.findOne(new Query(Criteria
.where(
"id")
.is(value)), Lesson
.class)
if(lesson!=null){
Update update = new Update()
update
.set(
"hot", score)
mongo
.updateFirst(new Query(Criteria
.where(
"id")
.is(value)), update, Lesson
.class)
System
.out.print(lesson
.getLessonname()+
" "+lesson
.getHot())
}else{
opsForZSet
.remove(
"lesson", Lesson
.class)
}
}
System
.out.println(
"Running ...")
}
public Integer getCount() {
// TODO Auto-generated method stub
long count = mongo
.count(new Query(), Lesson
.class)
return (int) count
}
}
茫茫人海,如果你不够特别,很容易就会被遗忘呢。 ——黑心苹果《重生之爷太重口了》