admin
2021-12-04 c79b1ebed5a42a4cbb2f824232da2a51ff22a9a1
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.yeshi.location.app.service.impl.sos;
 
import com.yeshi.location.app.dao.sos.SOSTargetInfoDao;
import com.yeshi.location.app.dao.sos.SOSTargetInfoDao.DaoQuery;
import com.yeshi.location.app.entity.sos.SOSRecordList;
import com.yeshi.location.app.entity.sos.SOSTargetInfo;
import com.yeshi.location.app.service.inter.sos.SOSRecordListService;
import com.yeshi.location.app.service.inter.sos.SOSTargetInfoService;
import com.yeshi.location.app.service.query.sos.SOSTargetInfoQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.yeshi.utils.bean.BeanUtil;
 
import javax.annotation.Resource;
import java.util.*;
 
@Service
public class SOSTargetInfoServiceImpl implements SOSTargetInfoService {
 
    @Resource
    private SOSTargetInfoDao sOSTargetInfoDao;
 
    @Resource
    private SOSRecordListService sosRecordListService;
 
    @Override
    public List<SOSTargetInfo> list(SOSTargetInfoQuery sOSTargetInfoQuery, int page, int pageSize) {
        DaoQuery daoQuery = new DaoQuery();
        try {
            BeanUtil.copyProperties(sOSTargetInfoQuery, daoQuery);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        daoQuery.start = (page - 1) * pageSize;
        daoQuery.count = pageSize;
        return sOSTargetInfoDao.list(daoQuery);
    }
 
    @Override
    public long count(SOSTargetInfoQuery sOSTargetInfoQuery) {
        DaoQuery daoQuery = new DaoQuery();
        try {
            BeanUtil.copyProperties(sOSTargetInfoQuery, daoQuery);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return sOSTargetInfoDao.count(daoQuery);
    }
 
    @Override
    public SOSTargetInfo get(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        return sOSTargetInfoDao.findOne(query);
    }
 
    @Override
    public void add(SOSTargetInfo sOSTargetInfo) throws Exception {
 
        if (sOSTargetInfo == null || sOSTargetInfo.getTargetUid() == null || sOSTargetInfo.getSosId() == null) {
            throw new Exception("参数不完整");
        }
 
        if (sOSTargetInfo.getId() == null) {
            sOSTargetInfo.setId(SOSTargetInfo.createId(sOSTargetInfo.getSosId(), sOSTargetInfo.getTargetUid()));
        }
        //查询主键ID是否存在
        if (sOSTargetInfoDao.get(sOSTargetInfo.getId()) != null) {
            throw new Exception("已存在");
        }
 
        if (sOSTargetInfo.getStatus() == null) {
            sOSTargetInfo.setStatus(SOSTargetInfo.STATUS_UNREAD);
            sOSTargetInfo.setStatusDesc("未读");
        }
 
        if (sOSTargetInfo.getCreateTime() == null) {
            sOSTargetInfo.setCreateTime(new Date());
        }
 
 
        //保存
        sOSTargetInfoDao.save(sOSTargetInfo);
 
        SOSRecordList recordList = new SOSRecordList();
        recordList.setSourceId(sOSTargetInfo.getId());
        recordList.setType(SOSRecordList.SOSRecordType.passive);
        recordList.setUid(sOSTargetInfo.getTargetUid());
        sosRecordListService.add(recordList);
    }
 
    @Override
    public void update(SOSTargetInfo sOSTargetInfo) {
        if (sOSTargetInfo.getUpdateTime() == null) {
            sOSTargetInfo.setUpdateTime(new Date());
        }
        //更新
        sOSTargetInfoDao.updateSelective(sOSTargetInfo);
    }
 
    @Override
    public void delete(List<String> idList) {
        for (String id : idList) {
            sOSTargetInfoDao.delete(id);
        }
    }
 
    @Override
    public List<SOSTargetInfo> listByIds(List<String> ids) {
        if(ids==null||ids.size()==0) {
            return new ArrayList<>();
        }
 
        List<Criteria> orList = new ArrayList<>();
        for (String id : ids) {
            orList.add(Criteria.where("_id").is(id));
        }
        Criteria[] ors = new Criteria[orList.size()];
        orList.toArray(ors);
 
        Query query = new Query();
        query.addCriteria(new Criteria().orOperator(ors));
        List<SOSTargetInfo> list = sOSTargetInfoDao.findList(query);
        Comparator<SOSTargetInfo> cm = (SOSTargetInfo o1, SOSTargetInfo o2) -> {
            return ids.indexOf(o1.getId()) - ids.indexOf(o2.getId());
        };
        Collections.sort(list, cm);
        return list;
    }
 
 
}