admin
2021-03-20 ad3ac53da1c3a11a96ae62d790aa61a81b9eab91
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.yeshi.buwan.dao.recommend;
 
import com.yeshi.buwan.domain.recommend.HomeRecommendSpecial;
import com.yeshi.buwan.dao.base.MongodbBaseDao;
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 java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@Repository
public class HomeRecommendSpecialDao extends MongodbBaseDao<HomeRecommendSpecial> {
 
    public List<HomeRecommendSpecial> list(List<String> ids) {
        Query query = new Query();
        List<Criteria> orList = new ArrayList<>();
 
        if (ids != null) {
            for (String id : ids) {
                orList.add(Criteria.where("id").is(id));
            }
        }
 
        Criteria criteria = new Criteria();
        if (orList.size() > 0) {
            Criteria[] ors = new Criteria[orList.size()];
            orList.toArray(ors);
            criteria = criteria.orOperator(ors);
        }
        query.addCriteria(criteria);
        return findList(query);
    }
 
    private Query getQuery(DaoQuery daoQuery) {
        Query query = new Query();
        List<Criteria> andList = new ArrayList<>();
 
        if (daoQuery.systemId != null) {
            andList.add(Criteria.where("systemId").is(daoQuery.systemId));
        }
 
        if (daoQuery.key != null) {
            andList.add(Criteria.where("name").regex(daoQuery.key));
        }
 
        Criteria criteria = new Criteria();
        if (andList.size() > 0) {
            Criteria[] ands = new Criteria[andList.size()];
            andList.toArray(ands);
            criteria = criteria.andOperator(ands);
        }
 
        query.addCriteria(criteria);
        return query;
    }
 
    public List<HomeRecommendSpecial> list(DaoQuery daoQuery) {
        Query query = getQuery(daoQuery);
        query.skip(daoQuery.start);
        query.limit(daoQuery.count);
        query.with(new Sort(new Sort.Order(Sort.Direction.DESC, "weight")));
        return findList(query);
    }
 
    public HomeRecommendSpecial selectByDataKey(String dataKey) {
        Query query = new Query();
        query.addCriteria(Criteria.where("dataKey").is(dataKey));
        return findOne(query);
    }
 
 
    public long count(DaoQuery daoQuery) {
        Query query = getQuery(daoQuery);
        return count(query);
    }
 
    public void updateSelective(HomeRecommendSpecial bean) {
        Query query = new Query();
        Update update = new Update();
        query.addCriteria(Criteria.where("id").is(bean.getId()));
        if (bean.getName() != null) {
            update.set("name", bean.getName());
        }
        if (bean.getSystemId() != null) {
            update.set("systemId", bean.getSystemId());
        }
        if (bean.getBannerSizeRate() != null) {
            update.set("bannerSizeRate", bean.getBannerSizeRate());
        }
        if (bean.getWeight() != null) {
            update.set("weight", bean.getWeight());
        }
        if (bean.getCreateTime() != null) {
            update.set("createTime", bean.getCreateTime());
        }
        update.set("updateTime", new Date());
        update(query, update);
    }
 
 
    public static class DaoQuery {
        public String systemId;
        public int start;
        public int count;
        public String key;
    }
 
 
}