DataLogDaoImpl.java
8.9 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.myproject.dao.mongo.impl;
import com.myproject.bean.json.ChartItem;
import com.myproject.bean.json.InventoryItem;
import com.myproject.bean.update.DataLog;
import com.myproject.dao.mongo.AbstractMongoDao;
import com.myproject.dao.mongo.IDataLogDao;
import com.myproject.util.DateUtil;
import com.myproject.util.StorageConstants;
import org.apache.logging.log4j.util.Strings;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
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.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Repository
public class DataLogDaoImpl extends AbstractMongoDao implements IDataLogDao {
@Override
public Class getEntityClass() {
return DataLog.class;
}
@Override
public List<DataLog> latestLogs(int count){
Query query = new Query();
query.with(new Sort(Sort.Direction.DESC,"updateDate"));
query.limit(count);
return getMongoTemplate().find(query,getEntityClass());
}
@Override
public DataLog findLatestOutTask(String barcode){
Criteria c = new Criteria();
c.and("barcode").is(barcode).and("type").is(StorageConstants.OP.CHECKOUT);
Query query = new Query(c);
query.with(new Sort(Sort.Direction.DESC,"updateDate"));
DataLog task = findOne(query);
return task;
}
@Override
public List<DataLog> storageHistory(String cid, Date fromDate){
Criteria c = new Criteria();
c.and("cid").is(cid).and("updateDate").gte(fromDate);
Query query = new Query(c);
query.with(new Sort(Sort.Direction.DESC,"updateDate"));
return getMongoTemplate().find(query,getEntityClass());
}
@Override
public List<ChartItem> putInChart(Date fromTime, Date toTime,String partNumber){
return chart(fromTime, toTime, StorageConstants.OP.PUT_IN,partNumber);
}
@Override
public List<ChartItem> checkOutChart(Date fromTime, Date toTime,String partNumber){
return chart(fromTime, toTime, StorageConstants.OP.CHECKOUT,partNumber);
}
private List<ChartItem> chart(Date fromTime, Date toTime, int type, String partNumber){
Date addOneDayToTime = DateUtil.addOneDayNoTime(toTime);
Criteria c = new Criteria().andOperator(Criteria.where("updateDate").gte(fromTime),
Criteria.where("updateDate").lt(addOneDayToTime)).and("type").is(type);
c.and("status").in(StorageConstants.OP_STATUS.END.name(),StorageConstants.OP_STATUS.FINISHED.name());
if(Strings.isNotBlank(partNumber)){
c.and("partNumber").is(partNumber);
}
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(c),
Aggregation.project().and("updateDate").extractDayOfYear().as("label"),
Aggregation.group("label").count().as("value"),
Aggregation.project("value").and("label").previousOperation()
);
AggregationResults<ChartItem> results = getMongoTemplate().aggregate(agg, getEntityClass(), ChartItem.class);
return results.getMappedResults();
}
/**
* 获取最后一次出库记录
*/
@Override
public DataLog findLastOut(String areaId, String barcode){
Query query = Query.query(Criteria.where("barcode").is(barcode)//二维码
.and("areaId").is(areaId)//区域
.and("type").is(StorageConstants.OP.CHECKOUT)//出库
.and("status").is(StorageConstants.OP_STATUS.FINISHED.name()));//已完成
query.with(new Sort(Sort.Direction.DESC, "updateDate"));
return findOne(query);
}
private List<DataLog> findUnExecuteTasks(String executingHSerial) {
Criteria c = Criteria.where("appendInfo.hSerial").ne(executingHSerial).and("status").in(StorageConstants.OP_STATUS.WAIT.name(),StorageConstants.OP_STATUS.EXECUTING.name(),StorageConstants.OP_STATUS.PAUSE.name());
Query query = Query.query(c);
List<DataLog> feederTasks = findByQuery(query);
if(feederTasks == null){
feederTasks = new ArrayList<>();
}
return feederTasks;
}
@Override
public List<DataLog> findUnFinishedTasks(String executingHSerial){
//Criteria c = Criteria.where("appendInfo.hSerial").is(executingHSerial).and("lessSendReel").is(false).and("status").nin(StorageConstants.OP_STATUS.FINISHED.name(),StorageConstants.OP_STATUS.CANCEL.name());
Criteria c = Criteria.where("status").nin(StorageConstants.OP_STATUS.FINISHED.name(),StorageConstants.OP_STATUS.CANCEL.name()).and("stopSendToQisda").is(false);
//只查找近5个小时未完成的任务
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY,-5);
c.and("createDate").gte(calendar.getTime());
Query query = Query.query(c);
List<DataLog> unFinishedTasks = findByQuery(query);
if(unFinishedTasks == null){
unFinishedTasks = new ArrayList<>();
}
// List<DataLog> unExecuteTasks = findUnExecuteTasks(executingHSerial);
// for (DataLog unExecuteTask : unExecuteTasks) {
// unFinishedTasks.add(unExecuteTask);
// }
return unFinishedTasks;
}
@Override
public List<InventoryItem> getStorageLockCount(String storageId){
//被锁定的仓位
Criteria c = Criteria.where("sourceType").is(StorageConstants.TASK_SOURCE.FEEDER.name()).and("storageId").is(storageId)
.and("status").in(StorageConstants.OP_STATUS.WAIT.name(),StorageConstants.OP_STATUS.EXECUTING.name(),StorageConstants.OP_STATUS.PAUSE.name());
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(c),
Aggregation.group("partNumber").count().as("lockReel"),
Aggregation.project("lockReel").and("partNumber").previousOperation()
);
AggregationResults<InventoryItem> results = getMongoTemplate().aggregate(agg, getEntityClass(), InventoryItem.class);
return results.getMappedResults();
}
@Override
public int unExecuteFeederTaskCount(String feederId){
Query query = Query.query(Criteria.where("sourceId").is(feederId)
.and("sourceType").is(StorageConstants.TASK_SOURCE.FEEDER.name())
.and("status").in(StorageConstants.OP_STATUS.WAIT.name(),StorageConstants.OP_STATUS.EXECUTING.name(),StorageConstants.OP_STATUS.PAUSE.name()));
return super.countByQuery(query);
}
@Override
public void changeUnExeCuteTaskStatus(StorageConstants.TASK_SOURCE taskSource, String sourceId, StorageConstants.OP_STATUS op_status){
Query query = Query.query(Criteria.where("sourceId").is(sourceId)
.and("sourceType").is(taskSource.name())
.and("status").in(StorageConstants.OP_STATUS.WAIT.name(),StorageConstants.OP_STATUS.EXECUTING.name(),StorageConstants.OP_STATUS.PAUSE.name()));
Update update = Update.update("status", op_status.name());
updateMulti(query,update);
}
@Override
public DataLog findTask(String cid, String posName) {
Query query = Query.query(Criteria.where("cid").is(cid).and("posName").is(posName));
query.with(new Sort(Sort.Direction.DESC,"updateDate"));
return findOne(query);
}
@Override
public List<DataLog> findByFeedPosId(String feederPosId) {
Query query = Query.query(Criteria.where("subSourceId").is(feederPosId)
.and("sourceType").is(StorageConstants.TASK_SOURCE.FEEDER.name()));
List<DataLog> feederTasks = findByQuery(query);
if(feederTasks == null){
feederTasks = new ArrayList<>();
}
return feederTasks;
}
@Override
public List<DataLog> findHistory(String barcode, Date startDate, Date endDate) {
Criteria c = new Criteria();
if(barcode != null && !barcode.isEmpty()){
c.and("barcode").is(barcode);
}
if(startDate != null){
c.and("createDate").gte(startDate);
}
if(endDate != null){
c.and("updateDate").lte(endDate);
}
return findByQuery(Query.query(c));
}
@Override
public List<DataLog> findHistory(String barcode, int num) {
Criteria c = new Criteria();
if(barcode != null && !barcode.isEmpty()){
c.and("barcode").is(barcode);
}
//if(startDate != null){
// c.and("createDate").gte(startDate);
//}
// if(endDate != null){
// c.and("updateDate").lte(endDate);
// }
Query q = Query.query(c);
q.limit(num);
q.with(new Sort(Sort.Direction.DESC, "updateDate"));
return findByQuery(q);
}
}