package com.fs.city.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.city.mapper.FsCityMapper;
import com.fs.city.domain.FsCity;
import com.fs.city.service.IFsCityService;

/**
 * 城市Service业务层处理
 * 
 * @author fs
 * @date 2026-01-14
 */
@Service
public class FsCityServiceImpl implements IFsCityService 
{
    @Autowired
    private FsCityMapper fsCityMapper;

    /**
     * 查询城市
     * 
     * @param id 城市主键
     * @return 城市
     */
    @Override
    public FsCity selectFsCityById(Long id)
    {
        return fsCityMapper.selectFsCityById(id);
    }

    /**
     * 查询城市列表
     * 
     * @param fsCity 城市
     * @return 城市
     */
    @Override
    public List<FsCity> selectFsCityList(FsCity fsCity)
    {
        return fsCityMapper.selectFsCityList(fsCity);
    }

    /**
     * 新增城市
     * 
     * @param fsCity 城市
     * @return 结果
     */
    @Override
    public int insertFsCity(FsCity fsCity)
    {
        return fsCityMapper.insertFsCity(fsCity);
    }

    /**
     * 修改城市
     * 
     * @param fsCity 城市
     * @return 结果
     */
    @Override
    public int updateFsCity(FsCity fsCity)
    {
        return fsCityMapper.updateFsCity(fsCity);
    }

    /**
     * 批量删除城市
     * 
     * @param ids 需要删除的城市主键
     * @return 结果
     */
    @Override
    public int deleteFsCityByIds(Long[] ids)
    {
        return fsCityMapper.deleteFsCityByIds(ids);
    }

    /**
     * 删除城市信息
     * 
     * @param id 城市主键
     * @return 结果
     */
    @Override
    public int deleteFsCityById(Long id)
    {
        return fsCityMapper.deleteFsCityById(id);
    }

    @Override
    public List<FsCity> selectChildren(String parentId) {
        // 关键：你库里全国的 parent_id 可能是 "0" 或 ""，按实际改
        String pid = (parentId == null || parentId.trim().isEmpty()) ? "0" : parentId;
        return fsCityMapper.selectByParentId(pid);
    }

    @Override
    public List<String> selectPath(String cityId) {
        List<String> path = new ArrayList<>();
        FsCity cur = fsCityMapper.selectByCityId(cityId);
        while (cur != null) {
            path.add(cur.getCityId());
            if (cur.getParentId() == null || "0".equals(cur.getParentId()) || "".equals(cur.getParentId())) {
                break;
            }
            cur = fsCityMapper.selectByCityId(cur.getParentId());
        }
        Collections.reverse(path);
        return path;
    }
}
