admin
2022-04-16 04f09e52ffd4681bdfd85e51acd3da0d1280c3d3
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
package com.yeshi.buwan.service.imp.video;
 
import com.yeshi.buwan.dao.video.VideoInfoV2Dao;
import com.yeshi.buwan.domain.video.VideoInfoV2;
import com.yeshi.buwan.service.inter.video.VideoInfoV2Service;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
/**
 * @author hxh
 * @title: VideoInfoV2ServiceImpl
 * @description: TODO
 * @date 2022/3/17 16:02
 */
@Service
public class VideoInfoV2ServiceImpl implements VideoInfoV2Service {
 
    @Resource
    private VideoInfoV2Dao videoInfoV2Dao;
 
 
    @Override
    public void save(VideoInfoV2 bean) {
        if (videoInfoV2Dao.get(bean.getId()) == null) {
            if (bean.getCreateTime() == null) {
                bean.setCreateTime(new Date());
            }
            videoInfoV2Dao.save(bean);
        } else {
            videoInfoV2Dao.updateSelective(bean);
        }
    }
 
    @Override
    public VideoInfoV2 selectById(String id) {
        List<VideoInfoV2> list = listByIds(Arrays.asList(new String[]{id}));
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }
 
    @Override
    public VideoInfoV2 selectDetailById(String id) {
        List<VideoInfoV2> list = listDetailByIds(Arrays.asList(new String[]{id}));
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }
 
 
    @Override
    public List<VideoInfoV2> listByIds(List<String> ids) {
        Criteria[] ors = new Criteria[ids.size()];
        for (int i = 0; i < ors.length; i++) {
            ors[i] = Criteria.where("_id").is(ids.get(i));
        }
        Query query = new Query();
        query.addCriteria(new Criteria().orOperator(ors));
        return videoInfoV2Dao.findList(query);
    }
 
    @Override
    public List<VideoInfoV2> listDetailByIds(List<String> ids) {
        Criteria[] ors = new Criteria[ids.size()];
        for (int i = 0; i < ors.length; i++) {
            ors[i] = Criteria.where("_id").is(ids.get(i));
        }
        List<AggregationOperation> list = new ArrayList<>();
        list.add(Aggregation.match(new Criteria().orOperator(ors)));
        list.add(Aggregation.lookup("videoInfoExtraV2", "_id", "_id", "extra"));
        list.add(Aggregation.unwind("extra", true));
        AggregationResults<VideoInfoV2> results = videoInfoV2Dao.aggregate(list, VideoInfoV2.class);
        return results.getMappedResults();
    }
}