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 findRegions(String name, Integer state, int page, int size) { Pageable pageable = PageRequest.of(page, size); return regionRepository.findByNameAndState(name, state, pageable); } /** * 查询所有区域 */ public List findAllRegions() { return regionRepository.findAll(); } /** * 根据ID查找区域 */ public Optional 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 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 getProvinces() { try { return regionRepository.findAllProvinces(); } catch (Exception e) { logger.error("!!!!!! ERROR fetching provinces:", e); return Collections.emptyList(); } } /** * 获取指定省份下的城市列表 */ public List 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 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 getChildren(Long parentId) { return regionRepository.findChildren(parentId); } /** * 构建树形结构 */ public List buildRegionTree() { List provinces = getProvinces(); for (Region province : provinces) { List cities = getCitiesByProvinceId(province.getId()); // 这里可以扩展为完整的树形结构,暂时返回省级列表 } return provinces; } }