lrj
2 天以前 c61d4fe27c97d2ecc907756aa571a4ef14a7b9b6
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
package com.rongyichuang.region.service;
 
import com.rongyichuang.region.entity.Region;
import com.rongyichuang.region.repository.RegionRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
 
@Service
@Transactional
public class RegionService {
 
    private static final Logger logger = LoggerFactory.getLogger(RegionService.class);
    
    @Autowired
    private RegionRepository regionRepository;
    
    /**
     * 分页查询区域
     */
    public Page<Region> findRegions(String name, Integer state, int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        return regionRepository.findByNameAndState(name, state, pageable);
    }
    
    /**
     * 查询所有区域
     */
    public List<Region> findAllRegions() {
        return regionRepository.findAll();
    }
    
    /**
     * 根据ID查找区域
     */
    public Optional<Region> findById(Long id) {
        return regionRepository.findById(id);
    }
    
    /**
     * 保存区域
     */
    public Region saveRegion(Region region) {
        if (region.getId() == null) {
            // 新增
            region.setCreateTime(LocalDateTime.now());
            region.setUpdateTime(LocalDateTime.now());
            region.setCreateUserId(1L); // 默认用户ID
            region.setUpdateUserId(1L);
            region.setVersion(0L);
        } else {
            // 更新
            region.setUpdateTime(LocalDateTime.now());
            region.setUpdateUserId(1L);
        }
        return regionRepository.save(region);
    }
    
    /**
     * 删除区域
     */
    public void deleteRegion(Long id) {
        regionRepository.deleteById(id);
    }
    
    /**
     * 切换区域状态
     */
    public Region toggleRegionState(Long id) {
        Optional<Region> regionOpt = regionRepository.findById(id);
        if (regionOpt.isPresent()) {
            Region region = regionOpt.get();
            region.setState(region.getState() == 1 ? 0 : 1);
            region.setUpdateTime(LocalDateTime.now());
            region.setUpdateUserId(1L);
            return regionRepository.save(region);
        }
        throw new RuntimeException("区域不存在");
    }
    
    /**
     * 获取省级区域列表
     */
    public List<Region> getProvinces() {
        try {
            return regionRepository.findAllProvinces();
        } catch (Exception e) {
            logger.error("!!!!!! ERROR fetching provinces:", e);
            return Collections.emptyList();
        }
    }
    
    /**
     * 获取指定省份下的城市列表
     */
    public List<Region> getCitiesByProvinceId(Long provinceId) {
        try {
            return regionRepository.findCitiesByProvinceId(provinceId);
        } catch (Exception e) {
            logger.error("!!!!!! ERROR fetching cities for province {}:", provinceId, e);
            return Collections.emptyList();
        }
    }
    
    /**
     * 获取指定城市下的区县列表
     */
    public List<Region> getDistrictsByCityId(Long cityId) {
        try {
            return regionRepository.findDistrictsByCityId(cityId);
        } catch (Exception e) {
            logger.error("!!!!!! ERROR fetching districts for city {}:", cityId, e);
            return Collections.emptyList();
        }
    }
    
    /**
     * 获取子区域列表
     */
    public List<Region> getChildren(Long parentId) {
        return regionRepository.findChildren(parentId);
    }
    
    /**
     * 构建树形结构
     */
    public List<Region> buildRegionTree() {
        List<Region> provinces = getProvinces();
        for (Region province : provinces) {
            List<Region> cities = getCitiesByProvinceId(province.getId());
            // 这里可以扩展为完整的树形结构,暂时返回省级列表
        }
        return provinces;
    }
}