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;
|
}
|
}
|