zhou zhou
9 小时以前 25f0001a7e76d0565fa9de0651f1177b9f61472f
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package com.vincent.rsf.server.manager.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.vincent.rsf.server.manager.partition.AsnLogPartitionSupport;
import com.vincent.rsf.server.manager.mapper.AsnOrderItemLogMapper;
import com.vincent.rsf.server.manager.entity.AsnOrderItemLog;
import com.vincent.rsf.server.manager.service.AsnOrderItemLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
@Service("asnOrderItemLogService")
public class AsnOrderItemLogServiceImpl extends ServiceImpl<AsnOrderItemLogMapper, AsnOrderItemLog> implements AsnOrderItemLogService {
 
    @Autowired
    private AsnLogPartitionSupport partitionSupport;
 
    @Override
    public boolean save(AsnOrderItemLog entity) {
        if (entity == null) {
            return false;
        }
        String tableName = partitionSupport.resolveOrderItemLogTable(entity.getCreateTime());
        partitionSupport.ensureTableExists(AsnLogPartitionSupport.ORDER_ITEM_LOG_TABLE, tableName);
        return partitionSupport.executeOnTable(AsnLogPartitionSupport.ORDER_ITEM_LOG_TABLE, tableName, () -> super.save(entity));
    }
 
    @Override
    public boolean saveBatch(Collection<AsnOrderItemLog> entityList) {
        return saveBatch(entityList, DEFAULT_BATCH_SIZE);
    }
 
    @Override
    public boolean saveBatch(Collection<AsnOrderItemLog> entityList, int batchSize) {
        if (entityList == null || entityList.isEmpty()) {
            return false;
        }
        Map<String, List<AsnOrderItemLog>> grouped = new LinkedHashMap<>();
        for (AsnOrderItemLog entity : entityList) {
            String tableName = partitionSupport.resolveOrderItemLogTable(entity == null ? null : entity.getCreateTime());
            grouped.computeIfAbsent(tableName, key -> new ArrayList<>()).add(entity);
        }
        boolean success = true;
        for (Map.Entry<String, List<AsnOrderItemLog>> entry : grouped.entrySet()) {
            partitionSupport.ensureTableExists(AsnLogPartitionSupport.ORDER_ITEM_LOG_TABLE, entry.getKey());
            Boolean saved = partitionSupport.executeOnTable(
                    AsnLogPartitionSupport.ORDER_ITEM_LOG_TABLE,
                    entry.getKey(),
                    () -> super.saveBatch(entry.getValue(), batchSize)
            );
            success = success && Boolean.TRUE.equals(saved);
        }
        return success;
    }
 
    @Override
    public boolean saveBatchToDate(Collection<AsnOrderItemLog> entityList, Date partitionDate) {
        if (entityList == null || entityList.isEmpty()) {
            return false;
        }
        String tableName = partitionSupport.resolveOrderItemLogTable(partitionDate);
        partitionSupport.ensureTableExists(AsnLogPartitionSupport.ORDER_ITEM_LOG_TABLE, tableName);
        return partitionSupport.executeOnTable(
                AsnLogPartitionSupport.ORDER_ITEM_LOG_TABLE,
                tableName,
                () -> super.saveBatch(entityList, DEFAULT_BATCH_SIZE)
        );
    }
 
    @Override
    public AsnOrderItemLog getById(Serializable id) {
        if (id == null) {
            return null;
        }
        for (String tableName : partitionSupport.listOrderItemLogTables()) {
            AsnOrderItemLog record = partitionSupport.executeOnTable(
                    AsnLogPartitionSupport.ORDER_ITEM_LOG_TABLE,
                    tableName,
                    () -> baseMapper.selectById(id)
            );
            if (record != null) {
                return record;
            }
        }
        return null;
    }
 
    @Override
    public List<AsnOrderItemLog> listByIds(Collection<? extends Serializable> idList) {
        if (idList == null || idList.isEmpty()) {
            return Collections.emptyList();
        }
        Map<Long, AsnOrderItemLog> merged = new LinkedHashMap<>();
        for (String tableName : partitionSupport.listOrderItemLogTables()) {
            List<AsnOrderItemLog> part = partitionSupport.executeOnTable(
                    AsnLogPartitionSupport.ORDER_ITEM_LOG_TABLE,
                    tableName,
                    () -> baseMapper.selectBatchIds(idList)
            );
            mergeRecords(merged, part);
        }
        return sortRecords(new ArrayList<>(merged.values()));
    }
 
    @Override
    public List<AsnOrderItemLog> list() {
        return list((Wrapper<AsnOrderItemLog>) null);
    }
 
    @Override
    public List<AsnOrderItemLog> list(Wrapper<AsnOrderItemLog> queryWrapper) {
        Map<Long, AsnOrderItemLog> merged = new LinkedHashMap<>();
        for (String tableName : partitionSupport.listOrderItemLogTables()) {
            List<AsnOrderItemLog> part = partitionSupport.executeOnTable(
                    AsnLogPartitionSupport.ORDER_ITEM_LOG_TABLE,
                    tableName,
                    () -> baseMapper.selectList(queryWrapper)
            );
            mergeRecords(merged, part);
        }
        return sortRecords(new ArrayList<>(merged.values()));
    }
 
    @Override
    public AsnOrderItemLog getOne(Wrapper<AsnOrderItemLog> queryWrapper) {
        List<AsnOrderItemLog> records = list(queryWrapper);
        return records.isEmpty() ? null : records.get(0);
    }
 
    @Override
    public <E extends IPage<AsnOrderItemLog>> E page(E page, Wrapper<AsnOrderItemLog> queryWrapper) {
        List<AsnOrderItemLog> records = list(queryWrapper);
        long current = page.getCurrent() <= 0 ? 1L : page.getCurrent();
        long size = page.getSize() <= 0 ? records.size() : page.getSize();
        int fromIndex = (int) Math.min((current - 1) * size, records.size());
        int toIndex = (int) Math.min(fromIndex + size, records.size());
        page.setTotal(records.size());
        page.setRecords(fromIndex >= records.size() ? Collections.emptyList() : new ArrayList<>(records.subList(fromIndex, toIndex)));
        return page;
    }
 
    @Override
    public boolean updateById(AsnOrderItemLog entity) {
        if (entity == null || entity.getId() == null) {
            return false;
        }
        String tableName = locateTableById(entity.getId());
        if (tableName == null) {
            return false;
        }
        return partitionSupport.executeOnTable(
                AsnLogPartitionSupport.ORDER_ITEM_LOG_TABLE,
                tableName,
                () -> super.updateById(entity)
        );
    }
 
    @Override
    public boolean removeById(Serializable id) {
        if (id == null) {
            return false;
        }
        String tableName = locateTableById(id);
        if (tableName == null) {
            return false;
        }
        return partitionSupport.executeOnTable(
                AsnLogPartitionSupport.ORDER_ITEM_LOG_TABLE,
                tableName,
                () -> super.removeById(id)
        );
    }
 
    @Override
    public boolean removeByIds(Collection<?> list) {
        if (list == null || list.isEmpty()) {
            return false;
        }
        Map<String, List<Serializable>> groupedIds = new LinkedHashMap<>();
        for (Object idObj : list) {
            if (!(idObj instanceof Serializable)) {
                continue;
            }
            Serializable id = (Serializable) idObj;
            String tableName = locateTableById(id);
            if (tableName != null) {
                groupedIds.computeIfAbsent(tableName, key -> new ArrayList<>()).add(id);
            }
        }
        boolean removed = false;
        for (Map.Entry<String, List<Serializable>> entry : groupedIds.entrySet()) {
            Boolean partRemoved = partitionSupport.executeOnTable(
                    AsnLogPartitionSupport.ORDER_ITEM_LOG_TABLE,
                    entry.getKey(),
                    () -> super.removeByIds(entry.getValue())
            );
            removed = removed || Boolean.TRUE.equals(partRemoved);
        }
        return removed;
    }
 
    @Override
    public boolean remove(Wrapper<AsnOrderItemLog> queryWrapper) {
        int affected = 0;
        for (String tableName : partitionSupport.listOrderItemLogTables()) {
            Integer count = partitionSupport.executeOnTable(
                    AsnLogPartitionSupport.ORDER_ITEM_LOG_TABLE,
                    tableName,
                    () -> baseMapper.delete(queryWrapper)
            );
            affected += count == null ? 0 : count;
        }
        return SqlHelper.retBool(affected);
    }
 
    private String locateTableById(Serializable id) {
        for (String tableName : partitionSupport.listOrderItemLogTables()) {
            AsnOrderItemLog record = partitionSupport.executeOnTable(
                    AsnLogPartitionSupport.ORDER_ITEM_LOG_TABLE,
                    tableName,
                    () -> baseMapper.selectById(id)
            );
            if (record != null) {
                return tableName;
            }
        }
        return null;
    }
 
    private void mergeRecords(Map<Long, AsnOrderItemLog> merged, List<AsnOrderItemLog> records) {
        if (records == null || records.isEmpty()) {
            return;
        }
        for (AsnOrderItemLog record : records) {
            if (record == null || record.getId() == null) {
                continue;
            }
            merged.putIfAbsent(record.getId(), record);
        }
    }
 
    private List<AsnOrderItemLog> sortRecords(List<AsnOrderItemLog> records) {
        records.sort(Comparator.comparing(AsnOrderItemLog::getId, Comparator.nullsLast(Long::compareTo)).reversed());
        return records;
    }
}