jiangping
2024-12-23 f3ce3a1f136f359ae872a223aada9e0d23afa9a0
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package com.doumee.service.business.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.AreasMapper;
import com.doumee.dao.business.model.Areas;
import com.doumee.service.business.AreasService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
 
/**
 * 省市区信息表Service实现
 * @author 江蹄蹄
 * @date 2023/02/15 08:55
 */
@Service
public class AreasServiceImpl implements AreasService {
    public static   List<Areas> ALL_AREA_LIST;
    public static   List<Areas> PROVINCE_LIST;
    public static   List<Areas> CITY_LIST;
    public static   List<Areas> AREA_LIST;
    public static   List<Areas> ALL_AREA_TREE;
 
    @Autowired
    private AreasMapper areasMapper;
 
    @Override
    public Integer create(Areas areas) {
        LoginUserInfo user = areas.getLoginUserInfo();
/*
 
        if (Objects.isNull(areas.getParentId())){
            areas.setType(Constants.ZERO);
        }else {
            Areas parentArea = areasMapper.selectById(areas.getParentId());
            if (Objects.isNull(parentArea)){
                areas.setType(Constants.ZERO);
            }else {
                areas.setType(parentArea.getType()+Constants.ONE);
            }
        }*/
        areas.setCreateDate(new Date());
        areas.setEditDate(new Date());
        areas.setCreator(user.getId());
        areas.setEditor(user.getId());
        areas.setIsdeleted(Constants.ZERO);
        areasMapper.insert(areas);
        //刷新缓存数据
        cacheData();
        return areas.getId();
    }
 
    @Override
    public void deleteById(Integer id) {
        areasMapper.deleteById(id);
        //刷新缓存数据
        cacheData();
    }
 
    @Override
    public void delete(Areas areas) {
        UpdateWrapper<Areas> deleteWrapper = new UpdateWrapper<>(areas);
        areasMapper.delete(deleteWrapper);
        //刷新缓存数据
        cacheData();
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        areasMapper.deleteBatchIds(ids);
        //刷新缓存数据
        cacheData();
    }
 
    @Override
    public void updateById(Areas areas) {
 
        UpdateWrapper<Areas> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(Areas::getId,areas.getId());
        Areas update = new Areas();
        update.setName(areas.getName());
        update.setCode(areas.getCode());
        update.setSortnum(areas.getSortnum());
        areasMapper.update(update,wrapper);
        //刷新缓存数据
        cacheData();
    }
 
    @Override
    public void updateByIdInBatch(List<Areas> areass) {
        if (CollectionUtils.isEmpty(areass)) {
            return;
        }
        for (Areas areas: areass) {
            this.updateById(areas);
        }
        //刷新缓存数据
        cacheData();
    }
 
    @Override
    public Areas findById(Integer id) {
        if(ALL_AREA_LIST!=null){
            for(Areas a : ALL_AREA_LIST){
                if(Constants.equalsInteger(a.getId(),id)){
                    return a;
                }
            }
        }
   //     return areasMapper.selectById(id);
        return  null;
    }
    @Override
    public Areas findById(Integer id,Integer type) {
        List<Areas> list = null;
        if(type == null){
            list = ALL_AREA_LIST;
        }else if(Constants.equalsInteger(type,Constants.ZERO)){
            list = PROVINCE_LIST;
        }else  if(Constants.equalsInteger(type,Constants.ONE)){
            list = CITY_LIST;
        } else if (Constants.equalsInteger(type,Constants.TWO)){
            list =  AREA_LIST;
        }
        if(list!=null){
            for(Areas a : list){
                if(Constants.equalsInteger(a.getId(),id)){
                    return a;
                }
            }
        }
        return null;
    }
 
 
    @Override
    public Areas findByName(String name,Integer type){
        List<Areas> list = null;
        if(type == null){
            list = ALL_AREA_LIST;
        }else if(Constants.equalsInteger(type,Constants.ZERO)){
            list = PROVINCE_LIST;
        }else  if(Constants.equalsInteger(type,Constants.ONE)){
            list = CITY_LIST;
        } else if (Constants.equalsInteger(type,Constants.TWO)){
            list =  AREA_LIST;
        }
        if(list!=null){
            for(Areas a : list){
                if(StringUtils.equals(name,a.getName())){
                    return a;
                }
            }
        }
        return null;
    }
    @Override
    public Areas findByNameAndParentId(String name,Integer type,Integer parentId){
        List<Areas> list = null;
        if(type == null){
            list = ALL_AREA_LIST;
        }else if(Constants.equalsInteger(type,Constants.ZERO)){
            list = PROVINCE_LIST;
        }else  if(Constants.equalsInteger(type,Constants.ONE)){
            list = CITY_LIST;
        } else if (Constants.equalsInteger(type,Constants.TWO)){
            list =  AREA_LIST;
        }
        if(list!=null){
            for(Areas a : list){
                if(StringUtils.equals(name,a.getName()) && Constants.equalsInteger(parentId,a.getParentId())){
                    return a;
                }
            }
        }
        return null;
    }
 
    @Override
    public List<Areas> findByParentId(Integer type, Integer parentId) {
 
        List<Areas> list = null;
        if(type == null){
            list = ALL_AREA_LIST;
        }else if(Constants.equalsInteger(type,Constants.ZERO)){
            list = PROVINCE_LIST;
        }else  if(Constants.equalsInteger(type,Constants.ONE)){
            list = CITY_LIST;
        } else if (Constants.equalsInteger(type,Constants.TWO)){
            list =  AREA_LIST;
        }
        if(list!=null){
            return list.stream().filter(s->Constants.equalsInteger(s.getParentId(),parentId)).collect(Collectors.toList());
        }
        return null;
    }
 
    @Override
    public List<Areas> findChildByParentId(Integer id,List<Areas> list){
        List<Areas> result =null;
        if(list == null){
            list = ALL_AREA_LIST;
        }
        if(list!=null && list.size()>0){
            for(Areas model :list){
                if(Constants.equalsInteger(id,model.getParentId())){
                    if(result == null){
                        result = new ArrayList<>();
                    }
                    result.add(model );
                }
            }
        }
        return result;
    }
 
    @Override
    public boolean isAreaValid(String proName,String cityName,String areaName){
        Areas pro = findByName(proName,Constants.ZERO);
        if(pro == null){
            return  false;
        }
        Areas city = findByName(proName,Constants.ONE);
        if(city == null && !Constants.equalsInteger(city.getParentId(),pro.getId())){
            return  false;
        }
        Areas area = findByName(proName,Constants.TWO);
        if(area == null && !Constants.equalsInteger(area.getParentId(),city.getId())){
            return  false;
        }
        return false;
    }
    @Override
    public Areas findOne(Areas areas) {
        QueryWrapper<Areas> wrapper = new QueryWrapper<>(areas);
        return areasMapper.selectOne(wrapper);
    }
 
    @Override
    public List<Areas> findList(Areas areas) {
        List<Areas> list = null;
        Integer type =areas.getType();
        if(type == null){
            list = ALL_AREA_LIST;
        }else if(Constants.equalsInteger(type,Constants.ZERO)){
            list = PROVINCE_LIST;
        }else  if(Constants.equalsInteger(type,Constants.ONE)){
            list = CITY_LIST;
        } else if (Constants.equalsInteger(type,Constants.TWO)){
            list =  AREA_LIST;
        }
        List<Areas> result = null;
        if(StringUtils.isNotBlank(areas.getName())){
            for(Areas a : list){
                if(StringUtils.contains(a.getName(),areas.getName())){
                    if(result == null){
                        result = new ArrayList<>();
                    }
                    result.add(a);
                }
            }
            return  result;
        }
        return list;
    }
 
    @Override
    public  List<Areas> listByParentId(Areas areas) {
        List<Areas> list = null;
        Integer type =areas.getType();
        if(type == null){
            list = ALL_AREA_LIST;
        }else if(Constants.equalsInteger(type,Constants.ZERO)){
            list = PROVINCE_LIST;
        }else  if(Constants.equalsInteger(type,Constants.ONE)){
            list = CITY_LIST;
        } else if (Constants.equalsInteger(type,Constants.TWO)){
            list =  AREA_LIST;
        }
        List<Areas> result = null;
        if(list!=null){
            for(Areas a : list){
                if(result == null){
                    result = new ArrayList<>();
                }
                if( areas.getParentId() == null
                        ||(areas.getParentId() !=null) && Constants.equalsInteger(a.getParentId(),areas.getParentId())){
                    Areas t = new Areas();
                    BeanUtils.copyProperties(a,t);
                    t.setChildList(null);
                    result.add(t);
                }
            }
        }
        return result;
    }
    @Override
    @PostConstruct
    public  void cacheData() {
       Areas a = new Areas();
       a.setIsdeleted(Constants.ZERO);
       ALL_AREA_LIST = null;
       PROVINCE_LIST =null;
       CITY_LIST=null;
       AREA_LIST = null;
       ALL_AREA_TREE = null;
       ALL_AREA_LIST =  areasMapper.selectList(new QueryWrapper<>(a).lambda().orderByAsc(Areas::getCode));
       if(ALL_AREA_LIST!=null){
           for(Areas model : ALL_AREA_LIST){
                if(Constants.equalsInteger(model.getType(),Constants.ZERO)){
                    if(PROVINCE_LIST == null){
                        PROVINCE_LIST = new ArrayList<>();
                    }
                    PROVINCE_LIST.add(model);
                }else if(Constants.equalsInteger(model.getType(),Constants.ONE)){
                    if(CITY_LIST == null){
                        CITY_LIST = new ArrayList<>();
                    }
                    CITY_LIST.add(model);
                    if(model.getParentId() != null){
                        Areas p = findById(model.getParentId());
                        if(p!=null  ){
                            model.setProvinceId(p.getId());
                            model.setProvinceName(p.getName());
                        }
                    }
                }else if(Constants.equalsInteger(model.getType(),Constants.TWO)){
                    if(AREA_LIST == null){
                        AREA_LIST = new ArrayList<>();
                    }
                    AREA_LIST.add(model);
                    Areas city = findById(model.getParentId());
                    if(city!=null  ){
                        model.setCityId(city.getId());
                        model.setCityName(city.getName());
                        if(city!=null && city.getParentId()!=null){
                            Areas p = findById(city.getParentId());
                            if(p!=null  ){
                                model.setProvinceId(p.getId());
                                model.setProvinceName(p.getName());
                            }
                        }
                    }
                }
           }
       }
       if(CITY_LIST!=null){
           for(Areas aa : CITY_LIST){
               aa.setChildList(findChildByParentId(aa.getId(), AREA_LIST));
           }
       }
       if(PROVINCE_LIST!=null){
           for(Areas aa : PROVINCE_LIST){
               aa.setChildList(findChildByParentId(aa.getId(),CITY_LIST));
           }
       }
       System.out.println("=================");
    }
 
    @Override
    public PageData<Areas> findPage(PageWrap<Areas> pageWrap) {
        IPage<Areas> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<Areas> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        if (pageWrap.getModel().getId() != null) {
            queryWrapper.lambda().eq(Areas::getId, pageWrap.getModel().getId());
        }
 
        if (pageWrap.getModel().getCreator() != null) {
            queryWrapper.lambda().eq(Areas::getCreator, pageWrap.getModel().getCreator());
        }
        if (pageWrap.getModel().getCreateDate() != null) {
            queryWrapper.lambda().ge(Areas::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate()));
            queryWrapper.lambda().le(Areas::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate()));
        }
        if (pageWrap.getModel().getEditor() != null) {
            queryWrapper.lambda().eq(Areas::getEditor, pageWrap.getModel().getEditor());
        }
        if (pageWrap.getModel().getEditDate() != null) {
            queryWrapper.lambda().ge(Areas::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate()));
            queryWrapper.lambda().le(Areas::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate()));
        }
        if (pageWrap.getModel().getIsdeleted() != null) {
            queryWrapper.lambda().eq(Areas::getIsdeleted, pageWrap.getModel().getIsdeleted());
        }
        if (pageWrap.getModel().getName() != null) {
            queryWrapper.lambda().eq(Areas::getName, pageWrap.getModel().getName());
        }
        if (pageWrap.getModel().getInfo() != null) {
            queryWrapper.lambda().eq(Areas::getInfo, pageWrap.getModel().getInfo());
        }
        if (pageWrap.getModel().getCode() != null) {
            queryWrapper.lambda().eq(Areas::getCode, pageWrap.getModel().getCode());
        }
        if (pageWrap.getModel().getParentId() != null) {
            queryWrapper.lambda().eq(Areas::getParentId, pageWrap.getModel().getParentId());
        }
        if (pageWrap.getModel().getType() != null) {
            queryWrapper.lambda().eq(Areas::getType, pageWrap.getModel().getType());
        }
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(areasMapper.selectPage(page, queryWrapper));
    }
 
    @Override
    public long count(Areas areas) {
        QueryWrapper<Areas> wrapper = new QueryWrapper<>(areas);
        return areasMapper.selectCount(wrapper);
    }
 
    @Override
    public Areas findByCityAndArea(String cityName, String areasName) {
        Areas city = findByName(cityName,Constants.ONE);
        if(city !=null){
           return  findByNameAndParentId(areasName,Constants.TWO,city.getId());
        }
        return null;
    }
 
    @Override
    public String getAddress(Integer cityId,Integer areaId){
        Areas cityAreas = findById(cityId, Constants.ONE);
        Areas areas = findById(areaId, Constants.TWO);
 
        String cityName = Optional.ofNullable(cityAreas)
                .map(s -> s.getProvinceName() + s.getName())
                .orElseThrow(() -> new BusinessException(ResponseStatus.BAD_REQUEST));
        String areaName = Optional.ofNullable(areas).map(s -> s.getName()).orElse("");
 
        return cityName+areaName;
    }
}